blob: 1f2408f9deb054460279dbd238df1e9196c3ee10 [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
Rom Lemarchand622810c2013-06-28 09:54:59 -070088enum fb_buffer_type {
89 FB_BUFFER,
90 FB_BUFFER_SPARSE,
91};
92
93struct fastboot_buffer {
94 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070095 void* data;
96 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070097};
98
99static struct {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700100 char img_name[17];
101 char sig_name[17];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700102 char part_name[9];
103 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700104 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700105} images[] = {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700106 {"boot.img", "boot.sig", "boot", false, false},
107 {"boot_other.img", "boot.sig", "boot", true, true},
108 {"recovery.img", "recovery.sig", "recovery", true, false},
109 {"system.img", "system.sig", "system", false, false},
110 {"system_other.img", "system.sig", "system", true, true},
111 {"vendor.img", "vendor.sig", "vendor", true, false},
112 {"vendor_other.img", "vendor.sig", "vendor", true, true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700113};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700114
Alex Lightbb9b8a52016-06-29 09:26:44 -0700115static std::string find_item_given_name(const char* img_name, const char* product) {
116 if(product) {
117 std::string path = get_my_path();
118 path.erase(path.find_last_of('/'));
119 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
120 path.c_str(), product, img_name);
121 }
122
123 char *dir = getenv("ANDROID_PRODUCT_OUT");
124 if (dir == nullptr || dir[0] == '\0') {
125 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
126 }
127
128 return android::base::StringPrintf("%s/%s", dir, img_name);
129}
130
131std::string find_item(const char* item, const char* product) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700132 const char *fn;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700133
Elliott Hughes2810d002016-04-25 14:31:18 -0700134 if (!strcmp(item,"boot")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 fn = "boot.img";
136 } else if(!strcmp(item,"recovery")) {
137 fn = "recovery.img";
138 } else if(!strcmp(item,"system")) {
139 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700140 } else if(!strcmp(item,"vendor")) {
141 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 } else if(!strcmp(item,"userdata")) {
143 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700144 } else if(!strcmp(item,"cache")) {
145 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 } else if(!strcmp(item,"info")) {
147 fn = "android-info.txt";
148 } else {
149 fprintf(stderr,"unknown partition '%s'\n", item);
Elliott Hughes2810d002016-04-25 14:31:18 -0700150 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 }
152
Alex Lightbb9b8a52016-06-29 09:26:44 -0700153 return find_item_given_name(fn, product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154}
155
Elliott Hughesfc797672015-04-07 20:12:50 -0700156static int64_t get_file_size(int fd) {
157 struct stat sb;
158 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700159}
160
Elliott Hughesfc797672015-04-07 20:12:50 -0700161static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800162 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700163 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
Elliott Hughesfc797672015-04-07 20:12:50 -0700165 *sz = get_file_size(fd);
166 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700167 goto oops;
168 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169
Elliott Hughesfc797672015-04-07 20:12:50 -0700170 data = (char*) malloc(*sz);
171 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
Elliott Hughesfc797672015-04-07 20:12:50 -0700173 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 close(fd);
175
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 return data;
177
178oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800179 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 close(fd);
181 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800182 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 return 0;
184}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185
Elliott Hughes2810d002016-04-25 14:31:18 -0700186static void* load_file(const std::string& path, int64_t* sz) {
187 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700188 if (fd == -1) return nullptr;
189 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700190}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Elliott Hughesfc797672015-04-07 20:12:50 -0700192static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700193 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700194 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700195 return -1;
196 }
197
198 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
199 return -1;
200 }
201
Scott Anderson13081c62012-04-06 12:39:30 -0700202 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700204 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
205 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 return 0;
207}
208
Elliott Hughesfc797672015-04-07 20:12:50 -0700209static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800210 return match_fastboot_with_serial(info, serial);
211}
212
Elliott Hughesfc797672015-04-07 20:12:50 -0700213static int list_devices_callback(usb_ifc_info* info) {
214 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800215 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700216 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800217 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700218 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 if (!serial[0]) {
220 serial = "????????????";
221 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700222 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700223 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800224 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700225 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800226 printf("%-22s fastboot", serial.c_str());
227 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700228 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800229 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 }
231
232 return -1;
233}
234
David Pursell2ec418a2016-01-20 08:32:08 -0800235// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
236// a specific device, otherwise the first USB device found will be used.
237//
238// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
239// Otherwise it blocks until the target is available.
240//
241// The returned Transport is a singleton, so multiple calls to this function will return the same
242// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700243static Transport* open_device() {
244 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800245 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246
David Pursell2ec418a2016-01-20 08:32:08 -0800247 if (transport != nullptr) {
248 return transport;
249 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800250
David Pursell4601c972016-02-05 15:35:09 -0800251 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800252 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800253 int port = 0;
254 if (serial != nullptr) {
255 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800256
David Pursell4601c972016-02-05 15:35:09 -0800257 if (android::base::StartsWith(serial, "tcp:")) {
258 protocol = Socket::Protocol::kTcp;
259 port = tcp::kDefaultPort;
260 net_address = serial + strlen("tcp:");
261 } else if (android::base::StartsWith(serial, "udp:")) {
262 protocol = Socket::Protocol::kUdp;
263 port = udp::kDefaultPort;
264 net_address = serial + strlen("udp:");
265 }
266
267 if (net_address != nullptr) {
268 std::string error;
269 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
270 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
271 error.c_str());
272 return nullptr;
273 }
David Pursell2ec418a2016-01-20 08:32:08 -0800274 }
275 }
276
277 while (true) {
278 if (!host.empty()) {
279 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800280 if (protocol == Socket::Protocol::kTcp) {
281 transport = tcp::Connect(host, port, &error).release();
282 } else if (protocol == Socket::Protocol::kUdp) {
283 transport = udp::Connect(host, port, &error).release();
284 }
285
David Pursell2ec418a2016-01-20 08:32:08 -0800286 if (transport == nullptr && announce) {
287 fprintf(stderr, "error: %s\n", error.c_str());
288 }
289 } else {
290 transport = usb_open(match_fastboot);
291 }
292
293 if (transport != nullptr) {
294 return transport;
295 }
296
David Pursell0b156632015-10-30 11:22:01 -0700297 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800298 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700299 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700301 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 }
303}
304
Elliott Hughesfc797672015-04-07 20:12:50 -0700305static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306 // We don't actually open a USB device here,
307 // just getting our callback called so we can
308 // list all the connected devices.
309 usb_open(list_devices_callback);
310}
311
Elliott Hughesfc797672015-04-07 20:12:50 -0700312static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313 fprintf(stderr,
314/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
315 "usage: fastboot [ <option> ] <command>\n"
316 "\n"
317 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700318 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700319 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700320 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700321 " if found -- recovery. If the device\n"
322 " supports slots, the slot that has\n"
323 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700324 " Secondary images may be flashed to\n"
325 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700326 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
327 " flashing lock Locks the device. Prevents flashing.\n"
328 " flashing unlock Unlocks the device. Allows flashing\n"
329 " any partition except\n"
330 " bootloader-related partitions.\n"
331 " flashing lock_critical Prevents flashing bootloader-related\n"
332 " partitions.\n"
333 " flashing unlock_critical Enables flashing bootloader-related\n"
334 " partitions.\n"
335 " flashing get_unlock_ability Queries bootloader to see if the\n"
336 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700337 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700338 " unlock nonce.\n"
339 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700340 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700341 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700342 " erase <partition> Erase a flash partition.\n"
343 " format[:[<fs type>][:[<size>]] <partition>\n"
344 " Format a flash partition. Can\n"
345 " override the fs type and/or size\n"
346 " the bootloader reports.\n"
347 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700348 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800349 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700350 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
351 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
352 " Create bootimage and flash it.\n"
353 " devices [-l] List all connected devices [with\n"
354 " device paths].\n"
355 " continue Continue with autoboot.\n"
356 " reboot [bootloader] Reboot device [into bootloader].\n"
357 " reboot-bootloader Reboot device into bootloader.\n"
358 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359 "\n"
360 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700361 " -w Erase userdata and cache (and format\n"
362 " if supported by partition type).\n"
363 " -u Do not erase partition before\n"
364 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800365 " -s <specific device> Specify a device. For USB, provide either\n"
366 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700367 " For ethernet, provide an address in the\n"
368 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800369 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700370 " -p <product> Specify product name.\n"
371 " -c <cmdline> Override kernel commandline.\n"
372 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800373 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700374 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800375 " --kernel-offset Specify a custom kernel offset.\n"
376 " (default: 0x00008000)\n"
377 " --ramdisk-offset Specify a custom ramdisk offset.\n"
378 " (default: 0x01000000)\n"
379 " --tags-offset Specify a custom tags offset.\n"
380 " (default: 0x00000100)\n"
381 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700382 " (default: 2048).\n"
383 " -S <size>[K|M|G] Automatically sparse files greater\n"
384 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700385 " --slot <slot> Specify slot name to be used if the\n"
386 " device supports slots. All operations\n"
387 " on partitions that support slots will\n"
388 " be done on the slot specified.\n"
389 " 'all' can be given to refer to all slots.\n"
390 " 'other' can be given to refer to a\n"
391 " non-current slot. If this flag is not\n"
392 " used, slotted partitions will default\n"
393 " to the current active slot.\n"
394 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800395 " provided, this will default to the value\n"
396 " given by --slot. If slots are not\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700397 " supported, this sets the current slot\n"
398 " to be active. This will run after all\n"
399 " non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700400 " --skip-secondary Will not flash secondary slots when\n"
401 " performing a flashall or update. This\n"
402 " will preserve data on other slots.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800403 " --unbuffered Do not buffer input or output.\n"
404 " --version Display version.\n"
405 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407}
Elliott Hughesfc797672015-04-07 20:12:50 -0700408static void* load_bootable_image(const char* kernel, const char* ramdisk,
409 const char* secondstage, int64_t* sz,
410 const char* cmdline) {
411 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 fprintf(stderr, "no image specified\n");
413 return 0;
414 }
415
Elliott Hughesfc797672015-04-07 20:12:50 -0700416 int64_t ksize;
417 void* kdata = load_file(kernel, &ksize);
418 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800419 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 return 0;
421 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800422
Elliott Hughesfc797672015-04-07 20:12:50 -0700423 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700425 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800426
Elliott Hughesfc797672015-04-07 20:12:50 -0700427 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
429 return 0;
430 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800431
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432 *sz = ksize;
433 return kdata;
434 }
435
Elliott Hughesfc797672015-04-07 20:12:50 -0700436 void* rdata = nullptr;
437 int64_t rsize = 0;
438 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700440 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800441 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 return 0;
443 }
444 }
445
Elliott Hughesfc797672015-04-07 20:12:50 -0700446 void* sdata = nullptr;
447 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200448 if (secondstage) {
449 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700450 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200451 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
452 return 0;
453 }
454 }
455
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700457 int64_t bsize = 0;
458 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800459 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200460 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800461 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700462 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 fprintf(stderr,"failed to create boot.img\n");
464 return 0;
465 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700466 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
467 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800469
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 return bdata;
471}
472
Elliott Hughesfc797672015-04-07 20:12:50 -0700473static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474{
Yusuke Sato07447542015-06-25 14:39:19 -0700475 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700476 ZipEntry zip_entry;
477 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700478 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000479 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 }
481
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700482 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700484 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700485 if (data == nullptr) {
486 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000487 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488 }
489
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700490 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
491 if (error != 0) {
492 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000494 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000496
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 return data;
498}
499
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700500#if defined(_WIN32)
501
502// TODO: move this to somewhere it can be shared.
503
504#include <windows.h>
505
506// Windows' tmpfile(3) requires administrator rights because
507// it creates temporary files in the root directory.
508static FILE* win32_tmpfile() {
509 char temp_path[PATH_MAX];
510 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
511 if (nchars == 0 || nchars >= sizeof(temp_path)) {
512 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
513 return nullptr;
514 }
515
516 char filename[PATH_MAX];
517 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
518 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
519 return nullptr;
520 }
521
522 return fopen(filename, "w+bTD");
523}
524
525#define tmpfile win32_tmpfile
526
527#endif
528
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700529static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
530 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700531 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700532 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
533 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700534 return -1;
535 }
536
Yusuke Sato07447542015-06-25 14:39:19 -0700537 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700538 ZipEntry zip_entry;
539 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
540 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700541 fclose(fp);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000542 return -1;
543 }
544
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700545 int fd = fileno(fp);
546 int error = ExtractEntryToFile(zip, &zip_entry, fd);
547 if (error != 0) {
548 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700549 fclose(fp);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700550 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700551 }
552
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000553 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700554 // TODO: We're leaking 'fp' here.
Rom Lemarchand622810c2013-06-28 09:54:59 -0700555 return fd;
556}
557
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558static char *strip(char *s)
559{
560 int n;
561 while(*s && isspace(*s)) s++;
562 n = strlen(s);
563 while(n-- > 0) {
564 if(!isspace(s[n])) break;
565 s[n] = 0;
566 }
567 return s;
568}
569
570#define MAX_OPTIONS 32
571static int setup_requirement_line(char *name)
572{
573 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700574 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575 unsigned n, count;
576 char *x;
577 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800578
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800579 if (!strncmp(name, "reject ", 7)) {
580 name += 7;
581 invert = 1;
582 } else if (!strncmp(name, "require ", 8)) {
583 name += 8;
584 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700585 } else if (!strncmp(name, "require-for-product:", 20)) {
586 // Get the product and point name past it
587 prod = name + 20;
588 name = strchr(name, ' ');
589 if (!name) return -1;
590 *name = 0;
591 name += 1;
592 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593 }
594
595 x = strchr(name, '=');
596 if (x == 0) return 0;
597 *x = 0;
598 val[0] = x + 1;
599
600 for(count = 1; count < MAX_OPTIONS; count++) {
601 x = strchr(val[count - 1],'|');
602 if (x == 0) break;
603 *x = 0;
604 val[count] = x + 1;
605 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800606
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800607 name = strip(name);
608 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800609
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800610 name = strip(name);
611 if (name == 0) return -1;
612
Elliott Hughes253c18d2015-03-18 22:47:09 -0700613 const char* var = name;
614 // Work around an unfortunate name mismatch.
615 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800616
Elliott Hughes253c18d2015-03-18 22:47:09 -0700617 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800618 if (out == 0) return -1;
619
620 for(n = 0; n < count; n++) {
621 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700622 if (out[n] == 0) {
623 for(size_t i = 0; i < n; ++i) {
624 free((char*) out[i]);
625 }
626 free(out);
627 return -1;
628 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800629 }
630
Elliott Hughes253c18d2015-03-18 22:47:09 -0700631 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632 return 0;
633}
634
Elliott Hughesfc797672015-04-07 20:12:50 -0700635static void setup_requirements(char* data, int64_t sz) {
636 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800637 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700638 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800639 *s++ = 0;
640 if (setup_requirement_line(data)) {
641 die("out of memory");
642 }
643 data = s;
644 } else {
645 s++;
646 }
647 }
648}
649
Elliott Hughesfc797672015-04-07 20:12:50 -0700650static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800651 fb_queue_notice("--------------------------------------------");
652 fb_queue_display("version-bootloader", "Bootloader Version...");
653 fb_queue_display("version-baseband", "Baseband Version.....");
654 fb_queue_display("serialno", "Serial Number........");
655 fb_queue_notice("--------------------------------------------");
656}
657
Rom Lemarchand622810c2013-06-28 09:54:59 -0700658static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700659{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700660 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700661 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700662 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700663 }
664
Elliott Hughesfc797672015-04-07 20:12:50 -0700665 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700666 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700667 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700668 }
669
Elliott Hughes253c18d2015-03-18 22:47:09 -0700670 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700671 if (!out_s) {
672 die("Failed to allocate sparse file array\n");
673 }
674
675 files = sparse_file_resparse(s, max_size, out_s, files);
676 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700677 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700678 }
679
680 return out_s;
681}
682
David Pursell0b156632015-10-30 11:22:01 -0700683static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700684 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700685 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
686 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800687 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700688 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700689 }
690
Elliott Hughes77c0e662015-11-02 15:51:12 -0800691 // Some bootloaders (angler, for example) send spurious whitespace too.
692 max_download_size = android::base::Trim(max_download_size);
693
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700694 uint64_t limit;
695 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800696 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700697 return 0;
698 }
699 if (limit > 0) {
700 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
701 }
Colin Crossf8387882012-05-24 17:18:41 -0700702 return limit;
703}
704
David Pursell0b156632015-10-30 11:22:01 -0700705static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700706 int64_t limit;
707
708 if (sparse_limit == 0) {
709 return 0;
710 } else if (sparse_limit > 0) {
711 limit = sparse_limit;
712 } else {
713 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700714 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700715 }
716 if (target_sparse_limit > 0) {
717 limit = target_sparse_limit;
718 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700719 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700720 }
721 }
722
723 if (size > limit) {
724 return limit;
725 }
726
727 return 0;
728}
729
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700730// Until we get lazy inode table init working in make_ext4fs, we need to
731// erase partitions of type ext4 before flashing a filesystem so no stale
732// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700733static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800734 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700735 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800736 return false;
737 }
738 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700739}
740
Elliott Hughes53ec4952016-05-11 12:39:27 -0700741static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700742 int64_t sz = get_file_size(fd);
743 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700744 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700745 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700746
Elliott Hughesfc797672015-04-07 20:12:50 -0700747 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700748 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700749 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700750 sparse_file** s = load_sparse_files(fd, limit);
751 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700752 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700753 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700754 buf->type = FB_BUFFER_SPARSE;
755 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700756 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700757 void* data = load_fd(fd, &sz);
758 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700759 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700760 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000761 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700762 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700763
Elliott Hughes53ec4952016-05-11 12:39:27 -0700764 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700765}
766
Elliott Hughes53ec4952016-05-11 12:39:27 -0700767static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
768 int fd = open(fname, O_RDONLY | O_BINARY);
769 if (fd == -1) {
770 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700771 }
David Pursell0b156632015-10-30 11:22:01 -0700772 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700773}
774
775static void flash_buf(const char *pname, struct fastboot_buffer *buf)
776{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700777 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700778
779 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800780 case FB_BUFFER_SPARSE: {
781 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700782 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700783 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700784 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800785 sparse_files.emplace_back(*s, sz);
786 ++s;
787 }
788
789 for (size_t i = 0; i < sparse_files.size(); ++i) {
790 const auto& pair = sparse_files[i];
791 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700792 }
793 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800794 }
795
Rom Lemarchand622810c2013-06-28 09:54:59 -0700796 case FB_BUFFER:
797 fb_queue_flash(pname, buf->data, buf->sz);
798 break;
799 default:
800 die("unknown buffer type: %d", buf->type);
801 }
802}
803
Daniel Rosenberg80919472016-06-30 19:25:31 -0700804static std::string get_current_slot(Transport* transport)
805{
806 std::string current_slot;
807 if (fb_getvar(transport, "current-slot", &current_slot)) {
808 if (current_slot == "_a") return "a"; // Legacy support
809 if (current_slot == "_b") return "b"; // Legacy support
810 return current_slot;
811 }
812 return "";
813}
814
815// Legacy support
816static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700817 std::vector<std::string> suffixes;
818 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700819 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700820 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700821 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700822 suffixes = android::base::Split(suffix_list, ",");
823 // Unfortunately some devices will return an error message in the
824 // guise of a valid value. If we only see only one suffix, it's probably
825 // not real.
826 if (suffixes.size() == 1) {
827 suffixes.clear();
828 }
829 return suffixes;
830}
831
Daniel Rosenberg80919472016-06-30 19:25:31 -0700832// Legacy support
833static bool supports_AB_obsolete(Transport* transport) {
834 return !get_suffixes_obsolete(transport).empty();
835}
836
837static int get_slot_count(Transport* transport) {
838 std::string var;
839 int count;
840 if (!fb_getvar(transport, "slot-count", &var)) {
841 if (supports_AB_obsolete(transport)) return 2; // Legacy support
842 }
843 if (!android::base::ParseInt(var.c_str(), &count)) return 0;
844 return count;
845}
846
Alex Lightbb9b8a52016-06-29 09:26:44 -0700847static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700848 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700849}
850
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700851// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700852static std::string get_other_slot(const std::string& current_slot, int count) {
853 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700854
Daniel Rosenberg80919472016-06-30 19:25:31 -0700855 char next = (current_slot[0] - 'a' + 1)%count + 'a';
856 return std::string(1, next);
857}
858
859static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
860 return get_other_slot(current_slot, get_slot_count(transport));
861}
862
863static std::string get_other_slot(Transport* transport, int count) {
864 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700865}
866
Alex Lightbb9b8a52016-06-29 09:26:44 -0700867static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700868 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700869}
870
Daniel Rosenberg80919472016-06-30 19:25:31 -0700871static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
872 std::string slot = slot_name;
873 if (slot == "_a") slot = "a"; // Legacy support
874 if (slot == "_b") slot = "b"; // Legacy support
875 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800876 if (allow_all) {
877 return "all";
878 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700879 int count = get_slot_count(transport);
880 if (count > 0) {
881 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800882 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800883 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800884 }
885 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700886 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800887
Daniel Rosenberg80919472016-06-30 19:25:31 -0700888 int count = get_slot_count(transport);
889 if (count == 0) die("Device does not support slots.\n");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800890
Daniel Rosenberg80919472016-06-30 19:25:31 -0700891 if (slot == "other") {
892 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700893 if (other == "") {
894 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800895 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700896 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800897 }
898
Daniel Rosenberg80919472016-06-30 19:25:31 -0700899 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
900
901 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
902 for (int i=0; i<count; i++) {
903 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700904 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700905
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700906 exit(1);
907}
908
Daniel Rosenberg80919472016-06-30 19:25:31 -0700909static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800910 return verify_slot(transport, slot, true);
911}
912
Daniel Rosenberg80919472016-06-30 19:25:31 -0700913static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700914 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800915 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700916 std::string current_slot;
917
Daniel Rosenberg80919472016-06-30 19:25:31 -0700918 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800919 /* If has-slot is not supported, the answer is no. */
920 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700921 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800922 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700923 if (slot == "") {
924 current_slot = get_current_slot(transport);
925 if (current_slot == "") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700926 die("Failed to identify current slot.\n");
927 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700928 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700929 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700930 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700931 }
932 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700933 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700934 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700935 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700936 }
937 func(part);
938 }
939}
940
David Pursell0b156632015-10-30 11:22:01 -0700941/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
942 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
943 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
944 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700945 */
Daniel Rosenberg80919472016-06-30 19:25:31 -0700946static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700947 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800948 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700949
Daniel Rosenberg80919472016-06-30 19:25:31 -0700950 if (slot == "all") {
951 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
952 die("Could not check if partition %s has slot.", part.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700953 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800954 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700955 for (int i=0; i < get_slot_count(transport); i++) {
956 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700957 }
958 } else {
David Pursell0b156632015-10-30 11:22:01 -0700959 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700960 }
961 } else {
David Pursell0b156632015-10-30 11:22:01 -0700962 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700963 }
964}
965
David Pursell0b156632015-10-30 11:22:01 -0700966static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700967 struct fastboot_buffer buf;
968
Elliott Hughes53ec4952016-05-11 12:39:27 -0700969 if (!load_buf(transport, fname, &buf)) {
970 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700971 }
972 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700973}
974
Elliott Hughesfc797672015-04-07 20:12:50 -0700975static void do_update_signature(ZipArchiveHandle zip, char* fn) {
976 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700977 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700978 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800979 fb_queue_download("signature", data, sz);
980 fb_queue_command("signature", "installing signature");
981}
982
Daniel Rosenberg13454092016-06-27 19:43:11 -0700983// Sets slot_override as the active slot. If slot_override is blank,
984// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -0700985static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700986 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -0700987 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700988 if (supports_AB_obsolete(transport)) {
989 separator = "_"; // Legacy support
990 } else {
991 return;
992 }
993 }
994 if (slot_override != "") {
995 fb_set_active((separator + slot_override).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -0700996 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700997 std::string current_slot = get_current_slot(transport);
998 if (current_slot != "") {
999 fb_set_active((separator + current_slot).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001000 }
1001 }
1002}
1003
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001004static 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 -08001005 queue_info_dump();
1006
Wink Savilleb98762f2011-04-04 17:54:59 -07001007 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1008
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001009 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001010 int error = OpenArchive(filename, &zip);
1011 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001012 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001013 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001014 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001015
Elliott Hughesfc797672015-04-07 20:12:50 -07001016 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001017 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001018 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001019 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001020 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001021 }
1022
Elliott Hughes253c18d2015-03-18 22:47:09 -07001023 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001024
Alex Lightbb9b8a52016-06-29 09:26:44 -07001025 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001026 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001027 if (slot_override != "") {
1028 secondary = get_other_slot(transport, slot_override);
1029 } else {
1030 secondary = get_other_slot(transport);
1031 }
1032 if (secondary == "") {
1033 if (supports_AB(transport)) {
1034 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1035 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001036 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001037 }
1038 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001039 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001040 const char* slot = slot_override.c_str();
1041 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001042 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001043 slot = secondary.c_str();
1044 } else {
1045 continue;
1046 }
1047 }
1048
Elliott Hughes253c18d2015-03-18 22:47:09 -07001049 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001050 if (fd == -1) {
1051 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001052 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001053 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001054 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001055 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001056 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001057 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001058 if (!load_buf_fd(transport, fd, &buf)) {
1059 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1060 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001061
1062 auto update = [&](const std::string &partition) {
1063 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001064 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001065 fb_queue_erase(partition.c_str());
1066 }
1067 flash_buf(partition.c_str(), &buf);
1068 /* not closing the fd here since the sparse code keeps the fd around
1069 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1070 * program exits.
1071 */
1072 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001073 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001074 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001075
1076 CloseArchive(zip);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001077 if (slot_override == "all") {
1078 set_active(transport, "a");
1079 } else {
1080 set_active(transport, slot_override);
1081 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001082}
1083
Alex Lightbb9b8a52016-06-29 09:26:44 -07001084static void do_send_signature(const std::string& fn) {
1085 std::size_t extension_loc = fn.find(".img");
1086 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001087
Alex Lightbb9b8a52016-06-29 09:26:44 -07001088 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001089
1090 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001091 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001092 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001093
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001094 fb_queue_download("signature", data, sz);
1095 fb_queue_command("signature", "installing signature");
1096}
1097
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001098static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001099 std::string fname;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001100 queue_info_dump();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001101
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001102 fb_queue_query_save("product", cur_product, sizeof(cur_product));
Wink Savilleb98762f2011-04-04 17:54:59 -07001103
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001104 fname = find_item("info", product);
1105 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001106
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001107 int64_t sz;
1108 void* data = load_file(fname.c_str(), &sz);
1109 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001110
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001111 setup_requirements(reinterpret_cast<char*>(data), sz);
1112
Alex Lightbb9b8a52016-06-29 09:26:44 -07001113 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001114 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001115 if (slot_override != "") {
1116 secondary = get_other_slot(transport, slot_override);
1117 } else {
1118 secondary = get_other_slot(transport);
1119 }
1120 if (secondary == "") {
1121 if (supports_AB(transport)) {
1122 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1123 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001124 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001125 }
1126 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001127
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001128 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001129 const char* slot = NULL;
1130 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001131 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001132 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001133 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001134 }
1135 if (!slot) continue;
1136 fname = find_item_given_name(images[i].img_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001137 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001138 if (!load_buf(transport, fname.c_str(), &buf)) {
1139 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001140 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001141 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001142
1143 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001144 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001145 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001146 fb_queue_erase(partition.c_str());
1147 }
1148 flash_buf(partition.c_str(), &buf);
1149 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001150 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001151 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001152
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001153 if (slot_override == "all") {
1154 set_active(transport, "a");
1155 } else {
1156 set_active(transport, slot_override);
1157 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001158}
1159
1160#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001161#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001162
Elliott Hughes45be42e2015-09-02 20:15:48 -07001163static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001164{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001165 if (argc <= 2) return 0;
1166 skip(2);
1167
1168 /*
1169 * Process unlock_bootloader, we have to load the message file
1170 * and send that to the remote device.
1171 */
1172 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001173
1174 int64_t sz;
1175 void* data = load_file(*argv, &sz);
1176 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001177 fb_queue_download("unlock_message", data, sz);
1178 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1179 skip(1);
1180 return 0;
1181}
1182
Elliott Hughes45be42e2015-09-02 20:15:48 -07001183static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001184{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001185 char command[256];
1186 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001187
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001188 command[0] = 0;
1189 while(1) {
1190 strcat(command,*argv);
1191 skip(1);
1192 if(argc == 0) break;
1193 strcat(command," ");
1194 }
1195
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001196 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001197 return 0;
1198}
1199
Colin Crossf8387882012-05-24 17:18:41 -07001200static int64_t parse_num(const char *arg)
1201{
1202 char *endptr;
1203 unsigned long long num;
1204
1205 num = strtoull(arg, &endptr, 0);
1206 if (endptr == arg) {
1207 return -1;
1208 }
1209
1210 if (*endptr == 'k' || *endptr == 'K') {
1211 if (num >= (-1ULL) / 1024) {
1212 return -1;
1213 }
1214 num *= 1024LL;
1215 endptr++;
1216 } else if (*endptr == 'm' || *endptr == 'M') {
1217 if (num >= (-1ULL) / (1024 * 1024)) {
1218 return -1;
1219 }
1220 num *= 1024LL * 1024LL;
1221 endptr++;
1222 } else if (*endptr == 'g' || *endptr == 'G') {
1223 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1224 return -1;
1225 }
1226 num *= 1024LL * 1024LL * 1024LL;
1227 endptr++;
1228 }
1229
1230 if (*endptr != '\0') {
1231 return -1;
1232 }
1233
1234 if (num > INT64_MAX) {
1235 return -1;
1236 }
1237
1238 return num;
1239}
1240
David Pursell0b156632015-10-30 11:22:01 -07001241static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001242 const char* partition, int skip_if_not_supported,
1243 const char* type_override, const char* size_override) {
1244 std::string partition_type, partition_size;
1245
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001246 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001247 const char* errMsg = nullptr;
1248 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001249 int fd;
1250
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001251 unsigned int limit = INT_MAX;
1252 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001253 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001254 }
1255 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001256 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001257 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001258
David Pursell0b156632015-10-30 11:22:01 -07001259 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001260 errMsg = "Can't determine partition type.\n";
1261 goto failed;
1262 }
JP Abgrall7e859742014-05-06 15:14:15 -07001263 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001264 if (partition_type != type_override) {
1265 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1266 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001267 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001268 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001269 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001270
David Pursell0b156632015-10-30 11:22:01 -07001271 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001272 errMsg = "Unable to get partition size\n";
1273 goto failed;
1274 }
JP Abgrall7e859742014-05-06 15:14:15 -07001275 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001276 if (partition_size != size_override) {
1277 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1278 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001279 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001280 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001281 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001282 // Some bootloaders (angler, for example), send spurious leading whitespace.
1283 partition_size = android::base::Trim(partition_size);
1284 // Some bootloaders (hammerhead, for example) use implicit hex.
1285 // This code used to use strtol with base 16.
1286 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001287
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001288 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001289 if (!gen) {
1290 if (skip_if_not_supported) {
1291 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001292 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001293 return;
1294 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001295 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1296 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001297 return;
1298 }
1299
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001300 int64_t size;
1301 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1302 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1303 return;
1304 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001305
1306 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001307 if (fs_generator_generate(gen, fd, size)) {
1308 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001309 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001310 return;
1311 }
1312
Elliott Hughes53ec4952016-05-11 12:39:27 -07001313 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001314 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001315 close(fd);
1316 return;
1317 }
1318 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001319 return;
1320
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001321failed:
1322 if (skip_if_not_supported) {
1323 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001324 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001325 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001326 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001327}
1328
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001329int main(int argc, char **argv)
1330{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001331 bool wants_wipe = false;
1332 bool wants_reboot = false;
1333 bool wants_reboot_bootloader = false;
1334 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001335 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001336 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001337 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001338 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001339 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001340 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001341 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001342
JP Abgrall7b8970c2013-03-07 17:06:41 -08001343 const struct option longopts[] = {
1344 {"base", required_argument, 0, 'b'},
1345 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001346 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001347 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001348 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001349 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001350 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001351 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001352 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001353 {"help", no_argument, 0, 'h'},
1354 {"unbuffered", no_argument, 0, 0},
1355 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001356 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001357 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001358 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001359 {"skip-secondary", no_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001360 {0, 0, 0, 0}
1361 };
Colin Cross8879f982012-05-22 17:53:34 -07001362
1363 serial = getenv("ANDROID_SERIAL");
1364
1365 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001366 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 -07001367 if (c < 0) {
1368 break;
1369 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001370 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001371 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001372 case 'a':
1373 wants_set_active = true;
1374 if (optarg)
1375 next_active = optarg;
1376 break;
Colin Cross8879f982012-05-22 17:53:34 -07001377 case 'b':
1378 base_addr = strtoul(optarg, 0, 16);
1379 break;
Colin Cross8879f982012-05-22 17:53:34 -07001380 case 'c':
1381 cmdline = optarg;
1382 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001383 case 'h':
1384 usage();
1385 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001386 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001387 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001388 unsigned long val;
1389
1390 val = strtoul(optarg, &endptr, 0);
1391 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1392 die("invalid vendor id '%s'", optarg);
1393 vendor_id = (unsigned short)val;
1394 break;
1395 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001396 case 'k':
1397 kernel_offset = strtoul(optarg, 0, 16);
1398 break;
1399 case 'l':
1400 long_listing = 1;
1401 break;
1402 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001403 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001404 if (!page_size) die("invalid page size");
1405 break;
1406 case 'p':
1407 product = optarg;
1408 break;
1409 case 'r':
1410 ramdisk_offset = strtoul(optarg, 0, 16);
1411 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001412 case 't':
1413 tags_offset = strtoul(optarg, 0, 16);
1414 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001415 case 's':
1416 serial = optarg;
1417 break;
1418 case 'S':
1419 sparse_limit = parse_num(optarg);
1420 if (sparse_limit < 0) {
1421 die("invalid sparse limit");
1422 }
1423 break;
1424 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001425 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001426 break;
1427 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001428 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001429 break;
Colin Cross8879f982012-05-22 17:53:34 -07001430 case '?':
1431 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001432 case 0:
1433 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001434 setvbuf(stdout, nullptr, _IONBF, 0);
1435 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001436 } else if (strcmp("version", longopts[longindex].name) == 0) {
1437 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1438 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001439 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1440 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001441 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1442 skip_secondary = true;
1443 } else {
1444 fprintf(stderr, "Internal error in options processing for %s\n",
1445 longopts[longindex].name);
1446 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001447 }
1448 break;
Colin Cross8879f982012-05-22 17:53:34 -07001449 default:
1450 abort();
1451 }
1452 }
1453
1454 argc -= optind;
1455 argv += optind;
1456
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001457 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001458 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001459 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001460 }
1461
Colin Cross8fb6e062012-07-24 16:36:41 -07001462 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001463 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001464 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001465 return 0;
1466 }
1467
Colin Crossc7b75dc2012-08-29 18:17:06 -07001468 if (argc > 0 && !strcmp(*argv, "help")) {
1469 usage();
1470 return 0;
1471 }
1472
David Pursell0b156632015-10-30 11:22:01 -07001473 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001474 if (transport == nullptr) {
1475 return 1;
1476 }
1477
Daniel Rosenberg80919472016-06-30 19:25:31 -07001478 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1479 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1480 }
1481 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1482 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001483
1484 if (wants_set_active) {
1485 if (next_active == "") {
1486 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001487 std::string current_slot;
1488 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001489 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001490 } else {
1491 wants_set_active = false;
1492 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001493 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001494 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001495 }
1496 }
1497 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001498
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001499 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001500 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001501 require(2);
1502 fb_queue_display(argv[1], argv[1]);
1503 skip(2);
1504 } else if(!strcmp(*argv, "erase")) {
1505 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001506
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001507 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001508 std::string partition_type;
1509 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001510 fs_get_generator(partition_type) != nullptr) {
1511 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1512 partition_type.c_str());
1513 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001514
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001515 fb_queue_erase(partition.c_str());
1516 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001517 do_for_partitions(transport, argv[1], slot_override, erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001518 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001519 } else if(!strncmp(*argv, "format", strlen("format"))) {
1520 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001521 char *type_override = nullptr;
1522 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001523 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001524 /*
1525 * Parsing for: "format[:[type][:[size]]]"
1526 * Some valid things:
1527 * - select ontly the size, and leave default fs type:
1528 * format::0x4000000 userdata
1529 * - default fs type and size:
1530 * format userdata
1531 * format:: userdata
1532 */
1533 overrides = strchr(*argv, ':');
1534 if (overrides) {
1535 overrides++;
1536 size_override = strchr(overrides, ':');
1537 if (size_override) {
1538 size_override[0] = '\0';
1539 size_override++;
1540 }
1541 type_override = overrides;
1542 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001543 if (type_override && !type_override[0]) type_override = nullptr;
1544 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001545
1546 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001547 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001548 fb_queue_erase(partition.c_str());
1549 }
David Pursell0b156632015-10-30 11:22:01 -07001550 fb_perform_format(transport, partition.c_str(), 0, type_override, size_override);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001551 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001552 do_for_partitions(transport, argv[1], slot_override, format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001553 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001554 } else if(!strcmp(*argv, "signature")) {
1555 require(2);
1556 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001557 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001558 if (sz != 256) die("signature must be 256 bytes");
1559 fb_queue_download("signature", data, sz);
1560 fb_queue_command("signature", "installing signature");
1561 skip(2);
1562 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001563 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001564 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001565 if (argc > 0) {
1566 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001567 wants_reboot = false;
1568 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001569 skip(1);
1570 }
1571 }
1572 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001573 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001574 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001575 skip(1);
1576 } else if (!strcmp(*argv, "continue")) {
1577 fb_queue_command("continue", "resuming boot");
1578 skip(1);
1579 } else if(!strcmp(*argv, "boot")) {
1580 char *kname = 0;
1581 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001582 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001583 skip(1);
1584 if (argc > 0) {
1585 kname = argv[0];
1586 skip(1);
1587 }
1588 if (argc > 0) {
1589 rname = argv[0];
1590 skip(1);
1591 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001592 if (argc > 0) {
1593 sname = argv[0];
1594 skip(1);
1595 }
1596 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001597 if (data == 0) return 1;
1598 fb_queue_download("boot.img", data, sz);
1599 fb_queue_command("boot", "booting");
1600 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001601 char* pname = argv[1];
1602 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001603 require(2);
1604 if (argc > 2) {
1605 fname = argv[2];
1606 skip(3);
1607 } else {
1608 fname = find_item(pname, product);
1609 skip(2);
1610 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001611 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001612
1613 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001614 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001615 fb_queue_erase(partition.c_str());
1616 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001617 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001618 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001619 do_for_partitions(transport, pname, slot_override, flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001620 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001621 char *kname = argv[2];
1622 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001623 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001624 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001625 skip(3);
1626 if (argc > 0) {
1627 rname = argv[0];
1628 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001629 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001630 if (argc > 0) {
1631 sname = argv[0];
1632 skip(1);
1633 }
1634 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001635 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001636 auto flashraw = [&](const std::string &partition) {
1637 fb_queue_flash(partition.c_str(), data, sz);
1638 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001639 do_for_partitions(transport, argv[1], slot_override, flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001640 } else if(!strcmp(*argv, "flashall")) {
1641 skip(1);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001642 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001643 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1644 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001645 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001646 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001647 }
1648 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001649 } else if(!strcmp(*argv, "update")) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001650 bool slot_all = (slot_override == "all");
1651 if (slot_all) {
1652 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1653 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001654 if (argc > 1) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001655 do_update(transport, argv[1], slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001656 skip(2);
1657 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001658 do_update(transport, "update.zip", slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001659 skip(1);
1660 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001661 wants_reboot = 1;
1662 } else if(!strcmp(*argv, "set_active")) {
1663 require(2);
Daniel Rosenberg80919472016-06-30 19:25:31 -07001664 std::string slot = verify_slot(transport, std::string(argv[1]), false);
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001665 fb_set_active(slot.c_str());
1666 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001667 } else if(!strcmp(*argv, "oem")) {
1668 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001669 } else if(!strcmp(*argv, "flashing")) {
1670 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1671 !strcmp(*(argv+1), "lock") ||
1672 !strcmp(*(argv+1), "unlock_critical") ||
1673 !strcmp(*(argv+1), "lock_critical") ||
1674 !strcmp(*(argv+1), "get_unlock_ability") ||
1675 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1676 !strcmp(*(argv+1), "lock_bootloader"))) {
1677 argc = do_oem_command(argc, argv);
1678 } else
1679 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1680 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001681 } else {
1682 usage();
1683 return 1;
1684 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001685 } else {
1686 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001687 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001688 }
1689 }
1690
1691 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001692 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001693 fb_queue_erase("userdata");
David Pursell0b156632015-10-30 11:22:01 -07001694 fb_perform_format(transport, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001695
1696 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001697 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001698 fprintf(stderr, "wiping cache...\n");
1699 fb_queue_erase("cache");
David Pursell0b156632015-10-30 11:22:01 -07001700 fb_perform_format(transport, "cache", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001701 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001702 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001703 if (wants_set_active) {
1704 fb_set_active(next_active.c_str());
1705 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001706 if (wants_reboot) {
1707 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001708 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001709 } else if (wants_reboot_bootloader) {
1710 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001711 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001712 }
1713
David Pursell0b156632015-10-30 11:22:01 -07001714 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001715}