blob: df0f6516edd635f4830a2cdc443ac40c55e57df1 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Colin Crossf8387882012-05-24 17:18:41 -070029#define _LARGEFILE64_SOURCE
30
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070031#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <errno.h>
33#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070034#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070035#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070036#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070037#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080045
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070046#include <functional>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes749ae2d2016-06-28 14:48:45 -070050#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080051#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080052#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070053#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080054#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070055#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070056#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Elliott Hughes253c18d2015-03-18 22:47:09 -070058#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080059#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070061#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080062#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070063#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080064#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070065#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066
Colin Crossf8387882012-05-24 17:18:41 -070067#ifndef O_BINARY
68#define O_BINARY 0
69#endif
70
Wink Savilleb98762f2011-04-04 17:54:59 -070071char cur_product[FB_RESPONSE_SZ + 1];
72
David Pursell2ec418a2016-01-20 08:32:08 -080073static const char* serial = nullptr;
74static const char* product = nullptr;
75static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070077static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070078static int64_t sparse_limit = -1;
79static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
Elliott Hughesfc797672015-04-07 20:12:50 -070081static unsigned page_size = 2048;
82static unsigned base_addr = 0x10000000;
83static unsigned kernel_offset = 0x00008000;
84static unsigned ramdisk_offset = 0x01000000;
85static unsigned second_offset = 0x00f00000;
86static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080087
Paul Crowley8f7f56e2015-11-27 09:29:37 +000088static const std::string convert_fbe_marker_filename("convert_fbe");
89
Rom Lemarchand622810c2013-06-28 09:54:59 -070090enum fb_buffer_type {
91 FB_BUFFER,
92 FB_BUFFER_SPARSE,
93};
94
95struct fastboot_buffer {
96 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070097 void* data;
98 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070099};
100
101static struct {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700102 char img_name[17];
103 char sig_name[17];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700104 char part_name[9];
105 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700106 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700107} images[] = {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700108 {"boot.img", "boot.sig", "boot", false, false},
109 {"boot_other.img", "boot.sig", "boot", true, true},
110 {"recovery.img", "recovery.sig", "recovery", true, false},
111 {"system.img", "system.sig", "system", false, false},
112 {"system_other.img", "system.sig", "system", true, true},
113 {"vendor.img", "vendor.sig", "vendor", true, false},
114 {"vendor_other.img", "vendor.sig", "vendor", true, true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700115};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700116
Alex Lightbb9b8a52016-06-29 09:26:44 -0700117static std::string find_item_given_name(const char* img_name, const char* product) {
118 if(product) {
119 std::string path = get_my_path();
120 path.erase(path.find_last_of('/'));
121 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
122 path.c_str(), product, img_name);
123 }
124
125 char *dir = getenv("ANDROID_PRODUCT_OUT");
126 if (dir == nullptr || dir[0] == '\0') {
127 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
128 }
129
130 return android::base::StringPrintf("%s/%s", dir, img_name);
131}
132
133std::string find_item(const char* item, const char* product) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700134 const char *fn;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700135
Elliott Hughes2810d002016-04-25 14:31:18 -0700136 if (!strcmp(item,"boot")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 fn = "boot.img";
138 } else if(!strcmp(item,"recovery")) {
139 fn = "recovery.img";
140 } else if(!strcmp(item,"system")) {
141 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700142 } else if(!strcmp(item,"vendor")) {
143 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 } else if(!strcmp(item,"userdata")) {
145 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700146 } else if(!strcmp(item,"cache")) {
147 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 } else if(!strcmp(item,"info")) {
149 fn = "android-info.txt";
150 } else {
151 fprintf(stderr,"unknown partition '%s'\n", item);
Elliott Hughes2810d002016-04-25 14:31:18 -0700152 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 }
154
Alex Lightbb9b8a52016-06-29 09:26:44 -0700155 return find_item_given_name(fn, product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156}
157
Elliott Hughesfc797672015-04-07 20:12:50 -0700158static int64_t get_file_size(int fd) {
159 struct stat sb;
160 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700161}
162
Elliott Hughesfc797672015-04-07 20:12:50 -0700163static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800164 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700165 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166
Elliott Hughesfc797672015-04-07 20:12:50 -0700167 *sz = get_file_size(fd);
168 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700169 goto oops;
170 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171
Elliott Hughesfc797672015-04-07 20:12:50 -0700172 data = (char*) malloc(*sz);
173 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174
Elliott Hughesfc797672015-04-07 20:12:50 -0700175 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 close(fd);
177
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 return data;
179
180oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800181 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 close(fd);
183 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800184 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 return 0;
186}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187
Elliott Hughes2810d002016-04-25 14:31:18 -0700188static void* load_file(const std::string& path, int64_t* sz) {
189 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700190 if (fd == -1) return nullptr;
191 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700192}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193
Elliott Hughesfc797672015-04-07 20:12:50 -0700194static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700195 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700196 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700197 return -1;
198 }
199
200 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
201 return -1;
202 }
203
Scott Anderson13081c62012-04-06 12:39:30 -0700204 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700206 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
207 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 return 0;
209}
210
Elliott Hughesfc797672015-04-07 20:12:50 -0700211static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800212 return match_fastboot_with_serial(info, serial);
213}
214
Elliott Hughesfc797672015-04-07 20:12:50 -0700215static int list_devices_callback(usb_ifc_info* info) {
216 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800217 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700218 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800219 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700220 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 if (!serial[0]) {
222 serial = "????????????";
223 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700224 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700225 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800226 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700227 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800228 printf("%-22s fastboot", serial.c_str());
229 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700230 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800231 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 }
233
234 return -1;
235}
236
David Pursell2ec418a2016-01-20 08:32:08 -0800237// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
238// a specific device, otherwise the first USB device found will be used.
239//
240// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
241// Otherwise it blocks until the target is available.
242//
243// The returned Transport is a singleton, so multiple calls to this function will return the same
244// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700245static Transport* open_device() {
246 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800247 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248
David Pursell2ec418a2016-01-20 08:32:08 -0800249 if (transport != nullptr) {
250 return transport;
251 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800252
David Pursell4601c972016-02-05 15:35:09 -0800253 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800254 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800255 int port = 0;
256 if (serial != nullptr) {
257 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800258
David Pursell4601c972016-02-05 15:35:09 -0800259 if (android::base::StartsWith(serial, "tcp:")) {
260 protocol = Socket::Protocol::kTcp;
261 port = tcp::kDefaultPort;
262 net_address = serial + strlen("tcp:");
263 } else if (android::base::StartsWith(serial, "udp:")) {
264 protocol = Socket::Protocol::kUdp;
265 port = udp::kDefaultPort;
266 net_address = serial + strlen("udp:");
267 }
268
269 if (net_address != nullptr) {
270 std::string error;
271 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
272 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
273 error.c_str());
274 return nullptr;
275 }
David Pursell2ec418a2016-01-20 08:32:08 -0800276 }
277 }
278
279 while (true) {
280 if (!host.empty()) {
281 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800282 if (protocol == Socket::Protocol::kTcp) {
283 transport = tcp::Connect(host, port, &error).release();
284 } else if (protocol == Socket::Protocol::kUdp) {
285 transport = udp::Connect(host, port, &error).release();
286 }
287
David Pursell2ec418a2016-01-20 08:32:08 -0800288 if (transport == nullptr && announce) {
289 fprintf(stderr, "error: %s\n", error.c_str());
290 }
291 } else {
292 transport = usb_open(match_fastboot);
293 }
294
295 if (transport != nullptr) {
296 return transport;
297 }
298
David Pursell0b156632015-10-30 11:22:01 -0700299 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800300 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700301 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700303 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 }
305}
306
Elliott Hughesfc797672015-04-07 20:12:50 -0700307static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 // We don't actually open a USB device here,
309 // just getting our callback called so we can
310 // list all the connected devices.
311 usb_open(list_devices_callback);
312}
313
Elliott Hughesfc797672015-04-07 20:12:50 -0700314static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 fprintf(stderr,
316/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
317 "usage: fastboot [ <option> ] <command>\n"
318 "\n"
319 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700320 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700321 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700322 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700323 " if found -- recovery. If the device\n"
324 " supports slots, the slot that has\n"
325 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700326 " Secondary images may be flashed to\n"
327 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700328 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
329 " flashing lock Locks the device. Prevents flashing.\n"
330 " flashing unlock Unlocks the device. Allows flashing\n"
331 " any partition except\n"
332 " bootloader-related partitions.\n"
333 " flashing lock_critical Prevents flashing bootloader-related\n"
334 " partitions.\n"
335 " flashing unlock_critical Enables flashing bootloader-related\n"
336 " partitions.\n"
337 " flashing get_unlock_ability Queries bootloader to see if the\n"
338 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700339 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700340 " unlock nonce.\n"
341 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700342 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700343 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700344 " erase <partition> Erase a flash partition.\n"
345 " format[:[<fs type>][:[<size>]] <partition>\n"
346 " Format a flash partition. Can\n"
347 " override the fs type and/or size\n"
348 " the bootloader reports.\n"
349 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700350 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800351 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700352 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
353 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
354 " Create bootimage and flash it.\n"
355 " devices [-l] List all connected devices [with\n"
356 " device paths].\n"
357 " continue Continue with autoboot.\n"
358 " reboot [bootloader] Reboot device [into bootloader].\n"
359 " reboot-bootloader Reboot device into bootloader.\n"
360 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 "\n"
362 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700363 " -w Erase userdata and cache (and format\n"
364 " if supported by partition type).\n"
365 " -u Do not erase partition before\n"
366 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800367 " -s <specific device> Specify a device. For USB, provide either\n"
368 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700369 " For ethernet, provide an address in the\n"
370 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800371 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700372 " -p <product> Specify product name.\n"
373 " -c <cmdline> Override kernel commandline.\n"
374 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800375 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700376 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800377 " --kernel-offset Specify a custom kernel offset.\n"
378 " (default: 0x00008000)\n"
379 " --ramdisk-offset Specify a custom ramdisk offset.\n"
380 " (default: 0x01000000)\n"
381 " --tags-offset Specify a custom tags offset.\n"
382 " (default: 0x00000100)\n"
383 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700384 " (default: 2048).\n"
385 " -S <size>[K|M|G] Automatically sparse files greater\n"
386 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700387 " --slot <slot> Specify slot name to be used if the\n"
388 " device supports slots. All operations\n"
389 " on partitions that support slots will\n"
390 " be done on the slot specified.\n"
391 " 'all' can be given to refer to all slots.\n"
392 " 'other' can be given to refer to a\n"
393 " non-current slot. If this flag is not\n"
394 " used, slotted partitions will default\n"
395 " to the current active slot.\n"
396 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800397 " provided, this will default to the value\n"
398 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800399 " supported, this does nothing. This will\n"
400 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700401 " --skip-secondary Will not flash secondary slots when\n"
402 " performing a flashall or update. This\n"
403 " will preserve data on other slots.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000404#if !defined(_WIN32)
405 " --wipe-and-use-fbe On devices which support it,\n"
406 " erase userdata and cache, and\n"
407 " enable file-based encryption\n"
408#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800409 " --unbuffered Do not buffer input or output.\n"
410 " --version Display version.\n"
411 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000414
Elliott Hughesfc797672015-04-07 20:12:50 -0700415static void* load_bootable_image(const char* kernel, const char* ramdisk,
416 const char* secondstage, int64_t* sz,
417 const char* cmdline) {
418 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419 fprintf(stderr, "no image specified\n");
420 return 0;
421 }
422
Elliott Hughesfc797672015-04-07 20:12:50 -0700423 int64_t ksize;
424 void* kdata = load_file(kernel, &ksize);
425 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800426 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427 return 0;
428 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800429
Elliott Hughesfc797672015-04-07 20:12:50 -0700430 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700432 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800433
Elliott Hughesfc797672015-04-07 20:12:50 -0700434 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
436 return 0;
437 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800438
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 *sz = ksize;
440 return kdata;
441 }
442
Elliott Hughesfc797672015-04-07 20:12:50 -0700443 void* rdata = nullptr;
444 int64_t rsize = 0;
445 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700447 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800448 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 return 0;
450 }
451 }
452
Elliott Hughesfc797672015-04-07 20:12:50 -0700453 void* sdata = nullptr;
454 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200455 if (secondstage) {
456 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700457 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200458 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
459 return 0;
460 }
461 }
462
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700464 int64_t bsize = 0;
465 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800466 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200467 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800468 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700469 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 fprintf(stderr,"failed to create boot.img\n");
471 return 0;
472 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700473 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
474 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800476
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 return bdata;
478}
479
Elliott Hughesfc797672015-04-07 20:12:50 -0700480static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481{
Yusuke Sato07447542015-06-25 14:39:19 -0700482 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700483 ZipEntry zip_entry;
484 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700485 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000486 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 }
488
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700489 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700491 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700492 if (data == nullptr) {
493 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000494 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 }
496
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700497 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
498 if (error != 0) {
499 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000501 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000503
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 return data;
505}
506
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700507#if defined(_WIN32)
508
509// TODO: move this to somewhere it can be shared.
510
511#include <windows.h>
512
513// Windows' tmpfile(3) requires administrator rights because
514// it creates temporary files in the root directory.
515static FILE* win32_tmpfile() {
516 char temp_path[PATH_MAX];
517 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
518 if (nchars == 0 || nchars >= sizeof(temp_path)) {
519 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
520 return nullptr;
521 }
522
523 char filename[PATH_MAX];
524 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
525 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
526 return nullptr;
527 }
528
529 return fopen(filename, "w+bTD");
530}
531
532#define tmpfile win32_tmpfile
533
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000534static std::string make_temporary_directory() {
535 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
536 return "";
537}
538
539#else
540
541static std::string make_temporary_directory() {
542 const char *tmpdir = getenv("TMPDIR");
543 if (tmpdir == nullptr) {
544 tmpdir = P_tmpdir;
545 }
546 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
547 if (mkdtemp(&result[0]) == NULL) {
548 fprintf(stderr, "Unable to create temporary directory: %s\n",
549 strerror(errno));
550 return "";
551 }
552 return result;
553}
554
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700555#endif
556
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000557static std::string create_fbemarker_tmpdir() {
558 std::string dir = make_temporary_directory();
559 if (dir.empty()) {
560 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
561 return "";
562 }
563 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
564 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
565 if (fd == -1) {
566 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
567 marker_file.c_str(), errno, strerror(errno));
568 return "";
569 }
570 close(fd);
571 return dir;
572}
573
574static void delete_fbemarker_tmpdir(const std::string& dir) {
575 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
576 if (unlink(marker_file.c_str()) == -1) {
577 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
578 marker_file.c_str(), errno, strerror(errno));
579 return;
580 }
581 if (rmdir(dir.c_str()) == -1) {
582 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
583 dir.c_str(), errno, strerror(errno));
584 return;
585 }
586}
587
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700588static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
589 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700590 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700591 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
592 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700593 return -1;
594 }
595
Yusuke Sato07447542015-06-25 14:39:19 -0700596 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700597 ZipEntry zip_entry;
598 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
599 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700600 fclose(fp);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000601 return -1;
602 }
603
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700604 int fd = fileno(fp);
605 int error = ExtractEntryToFile(zip, &zip_entry, fd);
606 if (error != 0) {
607 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700608 fclose(fp);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700609 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700610 }
611
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000612 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700613 // TODO: We're leaking 'fp' here.
Rom Lemarchand622810c2013-06-28 09:54:59 -0700614 return fd;
615}
616
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800617static char *strip(char *s)
618{
619 int n;
620 while(*s && isspace(*s)) s++;
621 n = strlen(s);
622 while(n-- > 0) {
623 if(!isspace(s[n])) break;
624 s[n] = 0;
625 }
626 return s;
627}
628
629#define MAX_OPTIONS 32
630static int setup_requirement_line(char *name)
631{
632 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700633 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634 unsigned n, count;
635 char *x;
636 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800637
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638 if (!strncmp(name, "reject ", 7)) {
639 name += 7;
640 invert = 1;
641 } else if (!strncmp(name, "require ", 8)) {
642 name += 8;
643 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700644 } else if (!strncmp(name, "require-for-product:", 20)) {
645 // Get the product and point name past it
646 prod = name + 20;
647 name = strchr(name, ' ');
648 if (!name) return -1;
649 *name = 0;
650 name += 1;
651 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800652 }
653
654 x = strchr(name, '=');
655 if (x == 0) return 0;
656 *x = 0;
657 val[0] = x + 1;
658
659 for(count = 1; count < MAX_OPTIONS; count++) {
660 x = strchr(val[count - 1],'|');
661 if (x == 0) break;
662 *x = 0;
663 val[count] = x + 1;
664 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800665
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666 name = strip(name);
667 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800668
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800669 name = strip(name);
670 if (name == 0) return -1;
671
Elliott Hughes253c18d2015-03-18 22:47:09 -0700672 const char* var = name;
673 // Work around an unfortunate name mismatch.
674 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675
Elliott Hughes253c18d2015-03-18 22:47:09 -0700676 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677 if (out == 0) return -1;
678
679 for(n = 0; n < count; n++) {
680 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700681 if (out[n] == 0) {
682 for(size_t i = 0; i < n; ++i) {
683 free((char*) out[i]);
684 }
685 free(out);
686 return -1;
687 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688 }
689
Elliott Hughes253c18d2015-03-18 22:47:09 -0700690 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 return 0;
692}
693
Elliott Hughesfc797672015-04-07 20:12:50 -0700694static void setup_requirements(char* data, int64_t sz) {
695 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800696 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700697 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698 *s++ = 0;
699 if (setup_requirement_line(data)) {
700 die("out of memory");
701 }
702 data = s;
703 } else {
704 s++;
705 }
706 }
707}
708
Elliott Hughesfc797672015-04-07 20:12:50 -0700709static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800710 fb_queue_notice("--------------------------------------------");
711 fb_queue_display("version-bootloader", "Bootloader Version...");
712 fb_queue_display("version-baseband", "Baseband Version.....");
713 fb_queue_display("serialno", "Serial Number........");
714 fb_queue_notice("--------------------------------------------");
715}
716
Rom Lemarchand622810c2013-06-28 09:54:59 -0700717static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700718{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700719 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700720 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700721 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700722 }
723
Elliott Hughesfc797672015-04-07 20:12:50 -0700724 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700725 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700726 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700727 }
728
Elliott Hughes253c18d2015-03-18 22:47:09 -0700729 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700730 if (!out_s) {
731 die("Failed to allocate sparse file array\n");
732 }
733
734 files = sparse_file_resparse(s, max_size, out_s, files);
735 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700736 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700737 }
738
739 return out_s;
740}
741
David Pursell0b156632015-10-30 11:22:01 -0700742static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700743 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700744 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
745 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800746 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700747 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700748 }
749
Elliott Hughes77c0e662015-11-02 15:51:12 -0800750 // Some bootloaders (angler, for example) send spurious whitespace too.
751 max_download_size = android::base::Trim(max_download_size);
752
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700753 uint64_t limit;
754 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800755 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700756 return 0;
757 }
758 if (limit > 0) {
759 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
760 }
Colin Crossf8387882012-05-24 17:18:41 -0700761 return limit;
762}
763
David Pursell0b156632015-10-30 11:22:01 -0700764static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700765 int64_t limit;
766
767 if (sparse_limit == 0) {
768 return 0;
769 } else if (sparse_limit > 0) {
770 limit = sparse_limit;
771 } else {
772 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700773 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700774 }
775 if (target_sparse_limit > 0) {
776 limit = target_sparse_limit;
777 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700778 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700779 }
780 }
781
782 if (size > limit) {
783 return limit;
784 }
785
786 return 0;
787}
788
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700789// Until we get lazy inode table init working in make_ext4fs, we need to
790// erase partitions of type ext4 before flashing a filesystem so no stale
791// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700792static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800793 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700794 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800795 return false;
796 }
797 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700798}
799
Elliott Hughes53ec4952016-05-11 12:39:27 -0700800static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700801 int64_t sz = get_file_size(fd);
802 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700803 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700804 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700805
Elliott Hughesfc797672015-04-07 20:12:50 -0700806 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700807 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700808 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700809 sparse_file** s = load_sparse_files(fd, limit);
810 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700811 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700812 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700813 buf->type = FB_BUFFER_SPARSE;
814 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700815 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700816 void* data = load_fd(fd, &sz);
817 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700818 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700819 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000820 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700821 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700822
Elliott Hughes53ec4952016-05-11 12:39:27 -0700823 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700824}
825
Elliott Hughes53ec4952016-05-11 12:39:27 -0700826static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
827 int fd = open(fname, O_RDONLY | O_BINARY);
828 if (fd == -1) {
829 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700830 }
David Pursell0b156632015-10-30 11:22:01 -0700831 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700832}
833
834static void flash_buf(const char *pname, struct fastboot_buffer *buf)
835{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700836 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700837
838 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800839 case FB_BUFFER_SPARSE: {
840 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700841 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700842 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700843 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800844 sparse_files.emplace_back(*s, sz);
845 ++s;
846 }
847
848 for (size_t i = 0; i < sparse_files.size(); ++i) {
849 const auto& pair = sparse_files[i];
850 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700851 }
852 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800853 }
854
Rom Lemarchand622810c2013-06-28 09:54:59 -0700855 case FB_BUFFER:
856 fb_queue_flash(pname, buf->data, buf->sz);
857 break;
858 default:
859 die("unknown buffer type: %d", buf->type);
860 }
861}
862
Daniel Rosenberg80919472016-06-30 19:25:31 -0700863static std::string get_current_slot(Transport* transport)
864{
865 std::string current_slot;
866 if (fb_getvar(transport, "current-slot", &current_slot)) {
867 if (current_slot == "_a") return "a"; // Legacy support
868 if (current_slot == "_b") return "b"; // Legacy support
869 return current_slot;
870 }
871 return "";
872}
873
874// Legacy support
875static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700876 std::vector<std::string> suffixes;
877 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700878 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700879 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700880 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700881 suffixes = android::base::Split(suffix_list, ",");
882 // Unfortunately some devices will return an error message in the
883 // guise of a valid value. If we only see only one suffix, it's probably
884 // not real.
885 if (suffixes.size() == 1) {
886 suffixes.clear();
887 }
888 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700889}
890
Daniel Rosenberg80919472016-06-30 19:25:31 -0700891// Legacy support
892static bool supports_AB_obsolete(Transport* transport) {
893 return !get_suffixes_obsolete(transport).empty();
894}
895
896static int get_slot_count(Transport* transport) {
897 std::string var;
898 int count;
899 if (!fb_getvar(transport, "slot-count", &var)) {
900 if (supports_AB_obsolete(transport)) return 2; // Legacy support
901 }
902 if (!android::base::ParseInt(var.c_str(), &count)) return 0;
903 return count;
904}
905
Alex Lightbb9b8a52016-06-29 09:26:44 -0700906static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700907 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700908}
909
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700910// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700911static std::string get_other_slot(const std::string& current_slot, int count) {
912 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700913
Daniel Rosenberg80919472016-06-30 19:25:31 -0700914 char next = (current_slot[0] - 'a' + 1)%count + 'a';
915 return std::string(1, next);
916}
917
918static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
919 return get_other_slot(current_slot, get_slot_count(transport));
920}
921
922static std::string get_other_slot(Transport* transport, int count) {
923 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700924}
925
Alex Lightbb9b8a52016-06-29 09:26:44 -0700926static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700927 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700928}
929
Daniel Rosenberg80919472016-06-30 19:25:31 -0700930static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
931 std::string slot = slot_name;
932 if (slot == "_a") slot = "a"; // Legacy support
933 if (slot == "_b") slot = "b"; // Legacy support
934 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800935 if (allow_all) {
936 return "all";
937 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700938 int count = get_slot_count(transport);
939 if (count > 0) {
940 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800941 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800942 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800943 }
944 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700945 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800946
Daniel Rosenberg80919472016-06-30 19:25:31 -0700947 int count = get_slot_count(transport);
948 if (count == 0) die("Device does not support slots.\n");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800949
Daniel Rosenberg80919472016-06-30 19:25:31 -0700950 if (slot == "other") {
951 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700952 if (other == "") {
953 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800954 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700955 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800956 }
957
Daniel Rosenberg80919472016-06-30 19:25:31 -0700958 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
959
960 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
961 for (int i=0; i<count; i++) {
962 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700963 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700964
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700965 exit(1);
966}
967
Daniel Rosenberg80919472016-06-30 19:25:31 -0700968static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800969 return verify_slot(transport, slot, true);
970}
971
Daniel Rosenberg80919472016-06-30 19:25:31 -0700972static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700973 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800974 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700975 std::string current_slot;
976
Daniel Rosenberg80919472016-06-30 19:25:31 -0700977 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800978 /* If has-slot is not supported, the answer is no. */
979 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700980 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800981 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700982 if (slot == "") {
983 current_slot = get_current_slot(transport);
984 if (current_slot == "") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700985 die("Failed to identify current slot.\n");
986 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700987 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700988 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700989 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700990 }
991 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700992 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700993 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700994 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700995 }
996 func(part);
997 }
998}
999
David Pursell0b156632015-10-30 11:22:01 -07001000/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1001 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1002 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1003 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001004 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001005static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001006 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001007 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001008
Daniel Rosenberg80919472016-06-30 19:25:31 -07001009 if (slot == "all") {
1010 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
1011 die("Could not check if partition %s has slot.", part.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001012 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001013 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001014 for (int i=0; i < get_slot_count(transport); i++) {
1015 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001016 }
1017 } else {
David Pursell0b156632015-10-30 11:22:01 -07001018 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001019 }
1020 } else {
David Pursell0b156632015-10-30 11:22:01 -07001021 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001022 }
1023}
1024
David Pursell0b156632015-10-30 11:22:01 -07001025static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001026 struct fastboot_buffer buf;
1027
Elliott Hughes53ec4952016-05-11 12:39:27 -07001028 if (!load_buf(transport, fname, &buf)) {
1029 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001030 }
1031 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001032}
1033
Elliott Hughesfc797672015-04-07 20:12:50 -07001034static void do_update_signature(ZipArchiveHandle zip, char* fn) {
1035 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001036 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001037 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001038 fb_queue_download("signature", data, sz);
1039 fb_queue_command("signature", "installing signature");
1040}
1041
Daniel Rosenberg13454092016-06-27 19:43:11 -07001042// Sets slot_override as the active slot. If slot_override is blank,
1043// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001044static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001045 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001046 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001047 if (supports_AB_obsolete(transport)) {
1048 separator = "_"; // Legacy support
1049 } else {
1050 return;
1051 }
1052 }
1053 if (slot_override != "") {
1054 fb_set_active((separator + slot_override).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001055 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001056 std::string current_slot = get_current_slot(transport);
1057 if (current_slot != "") {
1058 fb_set_active((separator + current_slot).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001059 }
1060 }
1061}
1062
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001063static 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 -08001064 queue_info_dump();
1065
Wink Savilleb98762f2011-04-04 17:54:59 -07001066 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1067
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001068 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001069 int error = OpenArchive(filename, &zip);
1070 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001071 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001072 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001073 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001074
Elliott Hughesfc797672015-04-07 20:12:50 -07001075 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001076 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001077 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001078 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001079 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001080 }
1081
Elliott Hughes253c18d2015-03-18 22:47:09 -07001082 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001083
Alex Lightbb9b8a52016-06-29 09:26:44 -07001084 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001085 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001086 if (slot_override != "") {
1087 secondary = get_other_slot(transport, slot_override);
1088 } else {
1089 secondary = get_other_slot(transport);
1090 }
1091 if (secondary == "") {
1092 if (supports_AB(transport)) {
1093 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1094 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001095 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001096 }
1097 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001098 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001099 const char* slot = slot_override.c_str();
1100 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001101 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001102 slot = secondary.c_str();
1103 } else {
1104 continue;
1105 }
1106 }
1107
Elliott Hughes253c18d2015-03-18 22:47:09 -07001108 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001109 if (fd == -1) {
1110 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001111 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001112 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001113 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001114 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001115 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001116 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001117 if (!load_buf_fd(transport, fd, &buf)) {
1118 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1119 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001120
1121 auto update = [&](const std::string &partition) {
1122 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001123 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001124 fb_queue_erase(partition.c_str());
1125 }
1126 flash_buf(partition.c_str(), &buf);
1127 /* not closing the fd here since the sparse code keeps the fd around
1128 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1129 * program exits.
1130 */
1131 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001132 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001133 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001134
1135 CloseArchive(zip);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001136 if (slot_override == "all") {
1137 set_active(transport, "a");
1138 } else {
1139 set_active(transport, slot_override);
1140 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001141}
1142
Alex Lightbb9b8a52016-06-29 09:26:44 -07001143static void do_send_signature(const std::string& fn) {
1144 std::size_t extension_loc = fn.find(".img");
1145 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001146
Alex Lightbb9b8a52016-06-29 09:26:44 -07001147 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001148
1149 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001150 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001151 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001152
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001153 fb_queue_download("signature", data, sz);
1154 fb_queue_command("signature", "installing signature");
1155}
1156
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001157static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001158 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001159 queue_info_dump();
1160
Wink Savilleb98762f2011-04-04 17:54:59 -07001161 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1162
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001163 fname = find_item("info", product);
Elliott Hughes2810d002016-04-25 14:31:18 -07001164 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001165
Elliott Hughesfc797672015-04-07 20:12:50 -07001166 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001167 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001168 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001169
1170 setup_requirements(reinterpret_cast<char*>(data), sz);
1171
Alex Lightbb9b8a52016-06-29 09:26:44 -07001172 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001173 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001174 if (slot_override != "") {
1175 secondary = get_other_slot(transport, slot_override);
1176 } else {
1177 secondary = get_other_slot(transport);
1178 }
1179 if (secondary == "") {
1180 if (supports_AB(transport)) {
1181 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1182 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001183 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001184 }
1185 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001186
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001187 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001188 const char* slot = NULL;
1189 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001190 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001191 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001192 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001193 }
1194 if (!slot) continue;
1195 fname = find_item_given_name(images[i].img_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001196 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001197 if (!load_buf(transport, fname.c_str(), &buf)) {
1198 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001199 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001200 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001201
1202 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001203 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001204 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001205 fb_queue_erase(partition.c_str());
1206 }
1207 flash_buf(partition.c_str(), &buf);
1208 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001209 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001210 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001211
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001212 if (slot_override == "all") {
1213 set_active(transport, "a");
1214 } else {
1215 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001216 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001217}
1218
1219#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001220#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001221
Elliott Hughes45be42e2015-09-02 20:15:48 -07001222static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001223{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001224 if (argc <= 2) return 0;
1225 skip(2);
1226
1227 /*
1228 * Process unlock_bootloader, we have to load the message file
1229 * and send that to the remote device.
1230 */
1231 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001232
1233 int64_t sz;
1234 void* data = load_file(*argv, &sz);
1235 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001236 fb_queue_download("unlock_message", data, sz);
1237 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1238 skip(1);
1239 return 0;
1240}
1241
Elliott Hughes45be42e2015-09-02 20:15:48 -07001242static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001243{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001244 char command[256];
1245 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001246
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001247 command[0] = 0;
1248 while(1) {
1249 strcat(command,*argv);
1250 skip(1);
1251 if(argc == 0) break;
1252 strcat(command," ");
1253 }
1254
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001255 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001256 return 0;
1257}
1258
Colin Crossf8387882012-05-24 17:18:41 -07001259static int64_t parse_num(const char *arg)
1260{
1261 char *endptr;
1262 unsigned long long num;
1263
1264 num = strtoull(arg, &endptr, 0);
1265 if (endptr == arg) {
1266 return -1;
1267 }
1268
1269 if (*endptr == 'k' || *endptr == 'K') {
1270 if (num >= (-1ULL) / 1024) {
1271 return -1;
1272 }
1273 num *= 1024LL;
1274 endptr++;
1275 } else if (*endptr == 'm' || *endptr == 'M') {
1276 if (num >= (-1ULL) / (1024 * 1024)) {
1277 return -1;
1278 }
1279 num *= 1024LL * 1024LL;
1280 endptr++;
1281 } else if (*endptr == 'g' || *endptr == 'G') {
1282 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1283 return -1;
1284 }
1285 num *= 1024LL * 1024LL * 1024LL;
1286 endptr++;
1287 }
1288
1289 if (*endptr != '\0') {
1290 return -1;
1291 }
1292
1293 if (num > INT64_MAX) {
1294 return -1;
1295 }
1296
1297 return num;
1298}
1299
David Pursell0b156632015-10-30 11:22:01 -07001300static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001301 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001302 const char* type_override, const char* size_override,
1303 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001304 std::string partition_type, partition_size;
1305
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001306 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001307 const char* errMsg = nullptr;
1308 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001309 int fd;
1310
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001311 unsigned int limit = INT_MAX;
1312 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001313 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001314 }
1315 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001316 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001317 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001318
David Pursell0b156632015-10-30 11:22:01 -07001319 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001320 errMsg = "Can't determine partition type.\n";
1321 goto failed;
1322 }
JP Abgrall7e859742014-05-06 15:14:15 -07001323 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001324 if (partition_type != type_override) {
1325 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1326 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001327 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001328 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001329 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001330
David Pursell0b156632015-10-30 11:22:01 -07001331 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001332 errMsg = "Unable to get partition size\n";
1333 goto failed;
1334 }
JP Abgrall7e859742014-05-06 15:14:15 -07001335 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001336 if (partition_size != size_override) {
1337 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1338 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001339 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001340 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001341 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001342 // Some bootloaders (angler, for example), send spurious leading whitespace.
1343 partition_size = android::base::Trim(partition_size);
1344 // Some bootloaders (hammerhead, for example) use implicit hex.
1345 // This code used to use strtol with base 16.
1346 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001347
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001348 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001349 if (!gen) {
1350 if (skip_if_not_supported) {
1351 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001352 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001353 return;
1354 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001355 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1356 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001357 return;
1358 }
1359
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001360 int64_t size;
1361 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1362 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1363 return;
1364 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001365
1366 fd = fileno(tmpfile());
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001367 if (fs_generator_generate(gen, fd, size, initial_dir)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001368 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001369 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001370 return;
1371 }
1372
Elliott Hughes53ec4952016-05-11 12:39:27 -07001373 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001374 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001375 close(fd);
1376 return;
1377 }
1378 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001379 return;
1380
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001381failed:
1382 if (skip_if_not_supported) {
1383 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001384 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001385 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001386 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001387}
1388
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001389int main(int argc, char **argv)
1390{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001391 bool wants_wipe = false;
1392 bool wants_reboot = false;
1393 bool wants_reboot_bootloader = false;
1394 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001395 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001396 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001397 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001398 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001399 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001400 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001401 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001402 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001403
JP Abgrall7b8970c2013-03-07 17:06:41 -08001404 const struct option longopts[] = {
1405 {"base", required_argument, 0, 'b'},
1406 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001407 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001408 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001409 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001410 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001411 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001412 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001413 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001414 {"help", no_argument, 0, 'h'},
1415 {"unbuffered", no_argument, 0, 0},
1416 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001417 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001418 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001419 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001420 {"skip-secondary", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001421#if !defined(_WIN32)
1422 {"wipe-and-use-fbe", no_argument, 0, 0},
1423#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001424 {0, 0, 0, 0}
1425 };
Colin Cross8879f982012-05-22 17:53:34 -07001426
1427 serial = getenv("ANDROID_SERIAL");
1428
1429 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001430 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 -07001431 if (c < 0) {
1432 break;
1433 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001434 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001435 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001436 case 'a':
1437 wants_set_active = true;
1438 if (optarg)
1439 next_active = optarg;
1440 break;
Colin Cross8879f982012-05-22 17:53:34 -07001441 case 'b':
1442 base_addr = strtoul(optarg, 0, 16);
1443 break;
Colin Cross8879f982012-05-22 17:53:34 -07001444 case 'c':
1445 cmdline = optarg;
1446 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001447 case 'h':
1448 usage();
1449 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001450 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001451 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001452 unsigned long val;
1453
1454 val = strtoul(optarg, &endptr, 0);
1455 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1456 die("invalid vendor id '%s'", optarg);
1457 vendor_id = (unsigned short)val;
1458 break;
1459 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001460 case 'k':
1461 kernel_offset = strtoul(optarg, 0, 16);
1462 break;
1463 case 'l':
1464 long_listing = 1;
1465 break;
1466 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001467 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001468 if (!page_size) die("invalid page size");
1469 break;
1470 case 'p':
1471 product = optarg;
1472 break;
1473 case 'r':
1474 ramdisk_offset = strtoul(optarg, 0, 16);
1475 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001476 case 't':
1477 tags_offset = strtoul(optarg, 0, 16);
1478 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001479 case 's':
1480 serial = optarg;
1481 break;
1482 case 'S':
1483 sparse_limit = parse_num(optarg);
1484 if (sparse_limit < 0) {
1485 die("invalid sparse limit");
1486 }
1487 break;
1488 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001489 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001490 break;
1491 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001492 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001493 break;
Colin Cross8879f982012-05-22 17:53:34 -07001494 case '?':
1495 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001496 case 0:
1497 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001498 setvbuf(stdout, nullptr, _IONBF, 0);
1499 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001500 } else if (strcmp("version", longopts[longindex].name) == 0) {
1501 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1502 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001503 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1504 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001505 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1506 skip_secondary = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001507#if !defined(_WIN32)
1508 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1509 wants_wipe = true;
1510 set_fbe_marker = true;
1511#endif
1512 } else {
1513 fprintf(stderr, "Internal error in options processing for %s\n",
1514 longopts[longindex].name);
1515 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001516 }
1517 break;
Colin Cross8879f982012-05-22 17:53:34 -07001518 default:
1519 abort();
1520 }
1521 }
1522
1523 argc -= optind;
1524 argv += optind;
1525
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001526 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001527 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001528 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001529 }
1530
Colin Cross8fb6e062012-07-24 16:36:41 -07001531 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001532 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001533 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001534 return 0;
1535 }
1536
Colin Crossc7b75dc2012-08-29 18:17:06 -07001537 if (argc > 0 && !strcmp(*argv, "help")) {
1538 usage();
1539 return 0;
1540 }
1541
David Pursell0b156632015-10-30 11:22:01 -07001542 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001543 if (transport == nullptr) {
1544 return 1;
1545 }
1546
Daniel Rosenberg80919472016-06-30 19:25:31 -07001547 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1548 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1549 }
1550 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1551 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001552
1553 if (wants_set_active) {
1554 if (next_active == "") {
1555 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001556 std::string current_slot;
1557 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001558 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001559 } else {
1560 wants_set_active = false;
1561 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001562 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001563 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001564 }
1565 }
1566 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001567
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001568 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001569 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001570 require(2);
1571 fb_queue_display(argv[1], argv[1]);
1572 skip(2);
1573 } else if(!strcmp(*argv, "erase")) {
1574 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001575
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001576 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001577 std::string partition_type;
1578 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001579 fs_get_generator(partition_type) != nullptr) {
1580 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1581 partition_type.c_str());
1582 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001583
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001584 fb_queue_erase(partition.c_str());
1585 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001586 do_for_partitions(transport, argv[1], slot_override, erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001587 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001588 } else if(!strncmp(*argv, "format", strlen("format"))) {
1589 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001590 char *type_override = nullptr;
1591 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001592 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001593 /*
1594 * Parsing for: "format[:[type][:[size]]]"
1595 * Some valid things:
1596 * - select ontly the size, and leave default fs type:
1597 * format::0x4000000 userdata
1598 * - default fs type and size:
1599 * format userdata
1600 * format:: userdata
1601 */
1602 overrides = strchr(*argv, ':');
1603 if (overrides) {
1604 overrides++;
1605 size_override = strchr(overrides, ':');
1606 if (size_override) {
1607 size_override[0] = '\0';
1608 size_override++;
1609 }
1610 type_override = overrides;
1611 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001612 if (type_override && !type_override[0]) type_override = nullptr;
1613 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001614
1615 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001616 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001617 fb_queue_erase(partition.c_str());
1618 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001619 fb_perform_format(transport, partition.c_str(), 0,
1620 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001621 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001622 do_for_partitions(transport, argv[1], slot_override, format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001623 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001624 } else if(!strcmp(*argv, "signature")) {
1625 require(2);
1626 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001627 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001628 if (sz != 256) die("signature must be 256 bytes");
1629 fb_queue_download("signature", data, sz);
1630 fb_queue_command("signature", "installing signature");
1631 skip(2);
1632 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001633 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001634 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001635 if (argc > 0) {
1636 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001637 wants_reboot = false;
1638 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001639 skip(1);
1640 }
1641 }
1642 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001643 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001644 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001645 skip(1);
1646 } else if (!strcmp(*argv, "continue")) {
1647 fb_queue_command("continue", "resuming boot");
1648 skip(1);
1649 } else if(!strcmp(*argv, "boot")) {
1650 char *kname = 0;
1651 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001652 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001653 skip(1);
1654 if (argc > 0) {
1655 kname = argv[0];
1656 skip(1);
1657 }
1658 if (argc > 0) {
1659 rname = argv[0];
1660 skip(1);
1661 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001662 if (argc > 0) {
1663 sname = argv[0];
1664 skip(1);
1665 }
1666 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001667 if (data == 0) return 1;
1668 fb_queue_download("boot.img", data, sz);
1669 fb_queue_command("boot", "booting");
1670 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001671 char* pname = argv[1];
1672 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001673 require(2);
1674 if (argc > 2) {
1675 fname = argv[2];
1676 skip(3);
1677 } else {
1678 fname = find_item(pname, product);
1679 skip(2);
1680 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001681 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001682
1683 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001684 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001685 fb_queue_erase(partition.c_str());
1686 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001687 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001688 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001689 do_for_partitions(transport, pname, slot_override, flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001690 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001691 char *kname = argv[2];
1692 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001693 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001694 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001695 skip(3);
1696 if (argc > 0) {
1697 rname = argv[0];
1698 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001699 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001700 if (argc > 0) {
1701 sname = argv[0];
1702 skip(1);
1703 }
1704 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001705 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001706 auto flashraw = [&](const std::string &partition) {
1707 fb_queue_flash(partition.c_str(), data, sz);
1708 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001709 do_for_partitions(transport, argv[1], slot_override, flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001710 } else if(!strcmp(*argv, "flashall")) {
1711 skip(1);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001712 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001713 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1714 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001715 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001716 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001717 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001718 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001719 } else if(!strcmp(*argv, "update")) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001720 bool slot_all = (slot_override == "all");
1721 if (slot_all) {
1722 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1723 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001724 if (argc > 1) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001725 do_update(transport, argv[1], slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001726 skip(2);
1727 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001728 do_update(transport, "update.zip", slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001729 skip(1);
1730 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001731 wants_reboot = 1;
1732 } else if(!strcmp(*argv, "set_active")) {
1733 require(2);
Daniel Rosenberg80919472016-06-30 19:25:31 -07001734 std::string slot = verify_slot(transport, std::string(argv[1]), false);
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001735 fb_set_active(slot.c_str());
1736 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001737 } else if(!strcmp(*argv, "oem")) {
1738 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001739 } else if(!strcmp(*argv, "flashing")) {
1740 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1741 !strcmp(*(argv+1), "lock") ||
1742 !strcmp(*(argv+1), "unlock_critical") ||
1743 !strcmp(*(argv+1), "lock_critical") ||
1744 !strcmp(*(argv+1), "get_unlock_ability") ||
1745 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1746 !strcmp(*(argv+1), "lock_bootloader"))) {
1747 argc = do_oem_command(argc, argv);
1748 } else
1749 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1750 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001751 } else {
1752 usage();
1753 return 1;
1754 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001755 } else {
1756 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001757 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001758 }
1759 }
1760
1761 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001762 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001763 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001764 if (set_fbe_marker) {
1765 fprintf(stderr, "setting FBE marker...\n");
1766 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1767 if (initial_userdata_dir.empty()) {
1768 return 1;
1769 }
1770 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1771 delete_fbemarker_tmpdir(initial_userdata_dir);
1772 } else {
1773 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1774 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001775
1776 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001777 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001778 fprintf(stderr, "wiping cache...\n");
1779 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001780 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001781 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001782 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001783 if (wants_set_active) {
1784 fb_set_active(next_active.c_str());
1785 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001786 if (wants_reboot) {
1787 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001788 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001789 } else if (wants_reboot_bootloader) {
1790 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001791 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001792 }
1793
David Pursell0b156632015-10-30 11:22:01 -07001794 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001795}