blob: 704dc43bf6abf0144798041f32bb947a941ea49c [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
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070047#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080048#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080049#include <utility>
50#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070051
Elliott Hughes82ff3152016-08-31 15:07:18 -070052#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070053#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080054#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080055#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070056#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080057#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070058#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070059#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes253c18d2015-03-18 22:47:09 -070061#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080062#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070064#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080065#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070066#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080067#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Colin Crossf8387882012-05-24 17:18:41 -070070#ifndef O_BINARY
71#define O_BINARY 0
72#endif
73
Wink Savilleb98762f2011-04-04 17:54:59 -070074char cur_product[FB_RESPONSE_SZ + 1];
75
David Pursell2ec418a2016-01-20 08:32:08 -080076static const char* serial = nullptr;
77static const char* product = nullptr;
78static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070080static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070081static int64_t sparse_limit = -1;
82static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083
Elliott Hughesfc797672015-04-07 20:12:50 -070084static unsigned page_size = 2048;
85static unsigned base_addr = 0x10000000;
86static unsigned kernel_offset = 0x00008000;
87static unsigned ramdisk_offset = 0x01000000;
88static unsigned second_offset = 0x00f00000;
89static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080090
Paul Crowley8f7f56e2015-11-27 09:29:37 +000091static const std::string convert_fbe_marker_filename("convert_fbe");
92
Rom Lemarchand622810c2013-06-28 09:54:59 -070093enum fb_buffer_type {
94 FB_BUFFER,
95 FB_BUFFER_SPARSE,
96};
97
98struct fastboot_buffer {
99 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700100 void* data;
101 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700102};
103
104static struct {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700105 char img_name[17];
106 char sig_name[17];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700107 char part_name[9];
108 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700109 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700110} images[] = {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700111 {"boot.img", "boot.sig", "boot", false, false},
112 {"boot_other.img", "boot.sig", "boot", true, true},
113 {"recovery.img", "recovery.sig", "recovery", true, false},
114 {"system.img", "system.sig", "system", false, false},
115 {"system_other.img", "system.sig", "system", true, true},
116 {"vendor.img", "vendor.sig", "vendor", true, false},
117 {"vendor_other.img", "vendor.sig", "vendor", true, true},
Vineeta Srivastava20ef9862017-03-31 13:22:34 -0700118 {"vbmeta.img", "vbmeta.sig", "vbmeta", true, false},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700119};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700120
Alex Lightbb9b8a52016-06-29 09:26:44 -0700121static std::string find_item_given_name(const char* img_name, const char* product) {
122 if(product) {
Elliott Hughes82ff3152016-08-31 15:07:18 -0700123 std::string path = android::base::GetExecutablePath();
Alex Lightbb9b8a52016-06-29 09:26:44 -0700124 path.erase(path.find_last_of('/'));
125 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
126 path.c_str(), product, img_name);
127 }
128
129 char *dir = getenv("ANDROID_PRODUCT_OUT");
130 if (dir == nullptr || dir[0] == '\0') {
131 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
132 }
133
134 return android::base::StringPrintf("%s/%s", dir, img_name);
135}
136
137std::string find_item(const char* item, const char* product) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700138 const char *fn;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700139
Elliott Hughes2810d002016-04-25 14:31:18 -0700140 if (!strcmp(item,"boot")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 fn = "boot.img";
142 } else if(!strcmp(item,"recovery")) {
143 fn = "recovery.img";
144 } else if(!strcmp(item,"system")) {
145 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700146 } else if(!strcmp(item,"vendor")) {
147 fn = "vendor.img";
Vineeta Srivastava20ef9862017-03-31 13:22:34 -0700148 } else if(!strcmp(item,"vbmeta")) {
149 fn = "vbmeta.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 } else if(!strcmp(item,"userdata")) {
151 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700152 } else if(!strcmp(item,"cache")) {
153 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154 } else if(!strcmp(item,"info")) {
155 fn = "android-info.txt";
156 } else {
157 fprintf(stderr,"unknown partition '%s'\n", item);
Elliott Hughes2810d002016-04-25 14:31:18 -0700158 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 }
160
Alex Lightbb9b8a52016-06-29 09:26:44 -0700161 return find_item_given_name(fn, product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162}
163
Elliott Hughesfc797672015-04-07 20:12:50 -0700164static int64_t get_file_size(int fd) {
165 struct stat sb;
166 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700167}
168
Elliott Hughesfc797672015-04-07 20:12:50 -0700169static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800170 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700171 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
Elliott Hughesfc797672015-04-07 20:12:50 -0700173 *sz = get_file_size(fd);
174 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700175 goto oops;
176 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177
Elliott Hughesfc797672015-04-07 20:12:50 -0700178 data = (char*) malloc(*sz);
179 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180
Elliott Hughesfc797672015-04-07 20:12:50 -0700181 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 close(fd);
183
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 return data;
185
186oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800187 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 close(fd);
189 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800190 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 return 0;
192}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193
Elliott Hughes2810d002016-04-25 14:31:18 -0700194static void* load_file(const std::string& path, int64_t* sz) {
195 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700196 if (fd == -1) return nullptr;
197 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700198}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199
Elliott Hughesfc797672015-04-07 20:12:50 -0700200static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700201 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700202 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700203 return -1;
204 }
205
206 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
207 return -1;
208 }
209
Scott Anderson13081c62012-04-06 12:39:30 -0700210 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700212 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
213 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 return 0;
215}
216
Elliott Hughesfc797672015-04-07 20:12:50 -0700217static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800218 return match_fastboot_with_serial(info, serial);
219}
220
Elliott Hughesfc797672015-04-07 20:12:50 -0700221static int list_devices_callback(usb_ifc_info* info) {
222 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800223 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700224 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800225 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700226 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 if (!serial[0]) {
228 serial = "????????????";
229 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700230 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700231 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800232 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700233 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800234 printf("%-22s fastboot", serial.c_str());
235 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700236 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800237 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 }
239
240 return -1;
241}
242
David Pursell2ec418a2016-01-20 08:32:08 -0800243// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
244// a specific device, otherwise the first USB device found will be used.
245//
246// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
247// Otherwise it blocks until the target is available.
248//
249// The returned Transport is a singleton, so multiple calls to this function will return the same
250// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700251static Transport* open_device() {
252 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800253 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254
David Pursell2ec418a2016-01-20 08:32:08 -0800255 if (transport != nullptr) {
256 return transport;
257 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800258
David Pursell4601c972016-02-05 15:35:09 -0800259 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800260 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800261 int port = 0;
262 if (serial != nullptr) {
263 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800264
David Pursell4601c972016-02-05 15:35:09 -0800265 if (android::base::StartsWith(serial, "tcp:")) {
266 protocol = Socket::Protocol::kTcp;
267 port = tcp::kDefaultPort;
268 net_address = serial + strlen("tcp:");
269 } else if (android::base::StartsWith(serial, "udp:")) {
270 protocol = Socket::Protocol::kUdp;
271 port = udp::kDefaultPort;
272 net_address = serial + strlen("udp:");
273 }
274
275 if (net_address != nullptr) {
276 std::string error;
277 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
278 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
279 error.c_str());
280 return nullptr;
281 }
David Pursell2ec418a2016-01-20 08:32:08 -0800282 }
283 }
284
285 while (true) {
286 if (!host.empty()) {
287 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800288 if (protocol == Socket::Protocol::kTcp) {
289 transport = tcp::Connect(host, port, &error).release();
290 } else if (protocol == Socket::Protocol::kUdp) {
291 transport = udp::Connect(host, port, &error).release();
292 }
293
David Pursell2ec418a2016-01-20 08:32:08 -0800294 if (transport == nullptr && announce) {
295 fprintf(stderr, "error: %s\n", error.c_str());
296 }
297 } else {
298 transport = usb_open(match_fastboot);
299 }
300
301 if (transport != nullptr) {
302 return transport;
303 }
304
David Pursell0b156632015-10-30 11:22:01 -0700305 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800306 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700307 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800309 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310 }
311}
312
Elliott Hughesfc797672015-04-07 20:12:50 -0700313static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 // We don't actually open a USB device here,
315 // just getting our callback called so we can
316 // list all the connected devices.
317 usb_open(list_devices_callback);
318}
319
Elliott Hughesfc797672015-04-07 20:12:50 -0700320static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321 fprintf(stderr,
322/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
323 "usage: fastboot [ <option> ] <command>\n"
324 "\n"
325 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700326 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700327 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700328 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700329 " if found -- recovery. If the device\n"
330 " supports slots, the slot that has\n"
331 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700332 " Secondary images may be flashed to\n"
333 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700334 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
335 " flashing lock Locks the device. Prevents flashing.\n"
336 " flashing unlock Unlocks the device. Allows flashing\n"
337 " any partition except\n"
338 " bootloader-related partitions.\n"
339 " flashing lock_critical Prevents flashing bootloader-related\n"
340 " partitions.\n"
341 " flashing unlock_critical Enables flashing bootloader-related\n"
342 " partitions.\n"
343 " flashing get_unlock_ability Queries bootloader to see if the\n"
344 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700345 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700346 " unlock nonce.\n"
347 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700348 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700349 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700350 " erase <partition> Erase a flash partition.\n"
351 " format[:[<fs type>][:[<size>]] <partition>\n"
352 " Format a flash partition. Can\n"
353 " override the fs type and/or size\n"
354 " the bootloader reports.\n"
355 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700356 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800357 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700358 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
359 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
360 " Create bootimage and flash it.\n"
361 " devices [-l] List all connected devices [with\n"
362 " device paths].\n"
363 " continue Continue with autoboot.\n"
Alexey Polyudove0bfb752017-01-03 19:39:13 -0800364 " reboot [bootloader|emergency] Reboot device [into bootloader or emergency mode].\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700365 " reboot-bootloader Reboot device into bootloader.\n"
366 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 "\n"
368 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700369 " -w Erase userdata and cache (and format\n"
370 " if supported by partition type).\n"
371 " -u Do not erase partition before\n"
372 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800373 " -s <specific device> Specify a device. For USB, provide either\n"
374 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700375 " For ethernet, provide an address in the\n"
376 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800377 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700378 " -p <product> Specify product name.\n"
379 " -c <cmdline> Override kernel commandline.\n"
380 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800381 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700382 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800383 " --kernel-offset Specify a custom kernel offset.\n"
384 " (default: 0x00008000)\n"
385 " --ramdisk-offset Specify a custom ramdisk offset.\n"
386 " (default: 0x01000000)\n"
387 " --tags-offset Specify a custom tags offset.\n"
388 " (default: 0x00000100)\n"
389 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700390 " (default: 2048).\n"
391 " -S <size>[K|M|G] Automatically sparse files greater\n"
392 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700393 " --slot <slot> Specify slot name to be used if the\n"
394 " device supports slots. All operations\n"
395 " on partitions that support slots will\n"
396 " be done on the slot specified.\n"
397 " 'all' can be given to refer to all slots.\n"
398 " 'other' can be given to refer to a\n"
399 " non-current slot. If this flag is not\n"
400 " used, slotted partitions will default\n"
401 " to the current active slot.\n"
402 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800403 " provided, this will default to the value\n"
404 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800405 " supported, this does nothing. This will\n"
406 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700407 " --skip-secondary Will not flash secondary slots when\n"
408 " performing a flashall or update. This\n"
409 " will preserve data on other slots.\n"
Mitchell Wills31dce302016-09-26 10:26:21 -0700410 " --skip-reboot Will not reboot the device when\n"
411 " performing commands that normally\n"
412 " trigger a reboot.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000413#if !defined(_WIN32)
414 " --wipe-and-use-fbe On devices which support it,\n"
415 " erase userdata and cache, and\n"
416 " enable file-based encryption\n"
417#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800418 " --unbuffered Do not buffer input or output.\n"
419 " --version Display version.\n"
420 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000423
Elliott Hughesfc797672015-04-07 20:12:50 -0700424static void* load_bootable_image(const char* kernel, const char* ramdisk,
425 const char* secondstage, int64_t* sz,
426 const char* cmdline) {
427 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428 fprintf(stderr, "no image specified\n");
429 return 0;
430 }
431
Elliott Hughesfc797672015-04-07 20:12:50 -0700432 int64_t ksize;
433 void* kdata = load_file(kernel, &ksize);
434 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800435 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 return 0;
437 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800438
Elliott Hughesfc797672015-04-07 20:12:50 -0700439 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700441 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800442
Elliott Hughesfc797672015-04-07 20:12:50 -0700443 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
445 return 0;
446 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800447
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 *sz = ksize;
449 return kdata;
450 }
451
Elliott Hughesfc797672015-04-07 20:12:50 -0700452 void* rdata = nullptr;
453 int64_t rsize = 0;
454 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700456 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800457 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458 return 0;
459 }
460 }
461
Elliott Hughesfc797672015-04-07 20:12:50 -0700462 void* sdata = nullptr;
463 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200464 if (secondstage) {
465 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700466 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200467 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
468 return 0;
469 }
470 }
471
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700473 int64_t bsize = 0;
474 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800475 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200476 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800477 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700478 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 fprintf(stderr,"failed to create boot.img\n");
480 return 0;
481 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700482 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
483 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800485
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486 return bdata;
487}
488
Elliott Hughesfc797672015-04-07 20:12:50 -0700489static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490{
Yusuke Sato07447542015-06-25 14:39:19 -0700491 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700492 ZipEntry zip_entry;
493 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700494 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000495 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496 }
497
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700498 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700500 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700501 if (data == nullptr) {
502 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000503 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 }
505
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700506 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
507 if (error != 0) {
508 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000510 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000512
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513 return data;
514}
515
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700516#if defined(_WIN32)
517
518// TODO: move this to somewhere it can be shared.
519
520#include <windows.h>
521
522// Windows' tmpfile(3) requires administrator rights because
523// it creates temporary files in the root directory.
524static FILE* win32_tmpfile() {
525 char temp_path[PATH_MAX];
526 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
527 if (nchars == 0 || nchars >= sizeof(temp_path)) {
528 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
529 return nullptr;
530 }
531
532 char filename[PATH_MAX];
533 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
534 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
535 return nullptr;
536 }
537
538 return fopen(filename, "w+bTD");
539}
540
541#define tmpfile win32_tmpfile
542
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000543static std::string make_temporary_directory() {
544 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
545 return "";
546}
547
548#else
549
550static std::string make_temporary_directory() {
551 const char *tmpdir = getenv("TMPDIR");
552 if (tmpdir == nullptr) {
553 tmpdir = P_tmpdir;
554 }
555 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
556 if (mkdtemp(&result[0]) == NULL) {
557 fprintf(stderr, "Unable to create temporary directory: %s\n",
558 strerror(errno));
559 return "";
560 }
561 return result;
562}
563
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700564#endif
565
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000566static std::string create_fbemarker_tmpdir() {
567 std::string dir = make_temporary_directory();
568 if (dir.empty()) {
569 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
570 return "";
571 }
572 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
573 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
574 if (fd == -1) {
575 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
576 marker_file.c_str(), errno, strerror(errno));
577 return "";
578 }
579 close(fd);
580 return dir;
581}
582
583static void delete_fbemarker_tmpdir(const std::string& dir) {
584 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
585 if (unlink(marker_file.c_str()) == -1) {
586 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
587 marker_file.c_str(), errno, strerror(errno));
588 return;
589 }
590 if (rmdir(dir.c_str()) == -1) {
591 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
592 dir.c_str(), errno, strerror(errno));
593 return;
594 }
595}
596
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700597static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
598 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700599 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700600 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
601 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700602 return -1;
603 }
604
Yusuke Sato07447542015-06-25 14:39:19 -0700605 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700606 ZipEntry zip_entry;
607 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
608 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700609 fclose(fp);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000610 return -1;
611 }
612
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700613 int fd = fileno(fp);
614 int error = ExtractEntryToFile(zip, &zip_entry, fd);
615 if (error != 0) {
616 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700617 fclose(fp);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700618 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700619 }
620
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000621 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700622 // TODO: We're leaking 'fp' here.
Rom Lemarchand622810c2013-06-28 09:54:59 -0700623 return fd;
624}
625
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800626static char *strip(char *s)
627{
628 int n;
629 while(*s && isspace(*s)) s++;
630 n = strlen(s);
631 while(n-- > 0) {
632 if(!isspace(s[n])) break;
633 s[n] = 0;
634 }
635 return s;
636}
637
638#define MAX_OPTIONS 32
639static int setup_requirement_line(char *name)
640{
641 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700642 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800643 unsigned n, count;
644 char *x;
645 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800646
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800647 if (!strncmp(name, "reject ", 7)) {
648 name += 7;
649 invert = 1;
650 } else if (!strncmp(name, "require ", 8)) {
651 name += 8;
652 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700653 } else if (!strncmp(name, "require-for-product:", 20)) {
654 // Get the product and point name past it
655 prod = name + 20;
656 name = strchr(name, ' ');
657 if (!name) return -1;
658 *name = 0;
659 name += 1;
660 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 }
662
663 x = strchr(name, '=');
664 if (x == 0) return 0;
665 *x = 0;
666 val[0] = x + 1;
667
668 for(count = 1; count < MAX_OPTIONS; count++) {
669 x = strchr(val[count - 1],'|');
670 if (x == 0) break;
671 *x = 0;
672 val[count] = x + 1;
673 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800674
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675 name = strip(name);
676 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800677
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678 name = strip(name);
679 if (name == 0) return -1;
680
Elliott Hughes253c18d2015-03-18 22:47:09 -0700681 const char* var = name;
682 // Work around an unfortunate name mismatch.
683 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800684
Elliott Hughes253c18d2015-03-18 22:47:09 -0700685 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 if (out == 0) return -1;
687
688 for(n = 0; n < count; n++) {
689 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700690 if (out[n] == 0) {
691 for(size_t i = 0; i < n; ++i) {
692 free((char*) out[i]);
693 }
694 free(out);
695 return -1;
696 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800697 }
698
Elliott Hughes253c18d2015-03-18 22:47:09 -0700699 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800700 return 0;
701}
702
Elliott Hughesfc797672015-04-07 20:12:50 -0700703static void setup_requirements(char* data, int64_t sz) {
704 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700706 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 *s++ = 0;
708 if (setup_requirement_line(data)) {
709 die("out of memory");
710 }
711 data = s;
712 } else {
713 s++;
714 }
715 }
716}
717
Elliott Hughesfc797672015-04-07 20:12:50 -0700718static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800719 fb_queue_notice("--------------------------------------------");
720 fb_queue_display("version-bootloader", "Bootloader Version...");
721 fb_queue_display("version-baseband", "Baseband Version.....");
722 fb_queue_display("serialno", "Serial Number........");
723 fb_queue_notice("--------------------------------------------");
724}
725
Rom Lemarchand622810c2013-06-28 09:54:59 -0700726static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700727{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700728 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700729 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700730 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700731 }
732
Elliott Hughesfc797672015-04-07 20:12:50 -0700733 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700734 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700735 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700736 }
737
Elliott Hughes253c18d2015-03-18 22:47:09 -0700738 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700739 if (!out_s) {
740 die("Failed to allocate sparse file array\n");
741 }
742
743 files = sparse_file_resparse(s, max_size, out_s, files);
744 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700745 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700746 }
747
748 return out_s;
749}
750
David Pursell0b156632015-10-30 11:22:01 -0700751static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700752 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700753 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
754 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800755 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700756 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700757 }
758
Elliott Hughes77c0e662015-11-02 15:51:12 -0800759 // Some bootloaders (angler, for example) send spurious whitespace too.
760 max_download_size = android::base::Trim(max_download_size);
761
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700762 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700763 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800764 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700765 return 0;
766 }
767 if (limit > 0) {
768 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
769 }
Colin Crossf8387882012-05-24 17:18:41 -0700770 return limit;
771}
772
David Pursell0b156632015-10-30 11:22:01 -0700773static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700774 int64_t limit;
775
776 if (sparse_limit == 0) {
777 return 0;
778 } else if (sparse_limit > 0) {
779 limit = sparse_limit;
780 } else {
781 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700782 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700783 }
784 if (target_sparse_limit > 0) {
785 limit = target_sparse_limit;
786 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700787 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700788 }
789 }
790
791 if (size > limit) {
792 return limit;
793 }
794
795 return 0;
796}
797
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700798// Until we get lazy inode table init working in make_ext4fs, we need to
799// erase partitions of type ext4 before flashing a filesystem so no stale
800// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700801static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800802 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700803 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800804 return false;
805 }
806 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700807}
808
Elliott Hughes53ec4952016-05-11 12:39:27 -0700809static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700810 int64_t sz = get_file_size(fd);
811 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700812 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700813 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700814
Elliott Hughesfc797672015-04-07 20:12:50 -0700815 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700816 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700817 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700818 sparse_file** s = load_sparse_files(fd, limit);
819 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700820 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700821 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700822 buf->type = FB_BUFFER_SPARSE;
823 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700824 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700825 void* data = load_fd(fd, &sz);
826 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700827 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700828 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000829 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700830 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700831
Elliott Hughes53ec4952016-05-11 12:39:27 -0700832 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700833}
834
Elliott Hughes53ec4952016-05-11 12:39:27 -0700835static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
836 int fd = open(fname, O_RDONLY | O_BINARY);
837 if (fd == -1) {
838 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700839 }
David Pursell0b156632015-10-30 11:22:01 -0700840 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700841}
842
843static void flash_buf(const char *pname, struct fastboot_buffer *buf)
844{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700845 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700846
847 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800848 case FB_BUFFER_SPARSE: {
849 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700850 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700851 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700852 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800853 sparse_files.emplace_back(*s, sz);
854 ++s;
855 }
856
857 for (size_t i = 0; i < sparse_files.size(); ++i) {
858 const auto& pair = sparse_files[i];
859 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700860 }
861 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800862 }
863
Rom Lemarchand622810c2013-06-28 09:54:59 -0700864 case FB_BUFFER:
865 fb_queue_flash(pname, buf->data, buf->sz);
866 break;
867 default:
868 die("unknown buffer type: %d", buf->type);
869 }
870}
871
Daniel Rosenberg80919472016-06-30 19:25:31 -0700872static std::string get_current_slot(Transport* transport)
873{
874 std::string current_slot;
875 if (fb_getvar(transport, "current-slot", &current_slot)) {
876 if (current_slot == "_a") return "a"; // Legacy support
877 if (current_slot == "_b") return "b"; // Legacy support
878 return current_slot;
879 }
880 return "";
881}
882
883// Legacy support
884static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700885 std::vector<std::string> suffixes;
886 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700887 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700888 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700889 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700890 suffixes = android::base::Split(suffix_list, ",");
891 // Unfortunately some devices will return an error message in the
892 // guise of a valid value. If we only see only one suffix, it's probably
893 // not real.
894 if (suffixes.size() == 1) {
895 suffixes.clear();
896 }
897 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700898}
899
Daniel Rosenberg80919472016-06-30 19:25:31 -0700900// Legacy support
901static bool supports_AB_obsolete(Transport* transport) {
902 return !get_suffixes_obsolete(transport).empty();
903}
904
905static int get_slot_count(Transport* transport) {
906 std::string var;
907 int count;
908 if (!fb_getvar(transport, "slot-count", &var)) {
909 if (supports_AB_obsolete(transport)) return 2; // Legacy support
910 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700911 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700912 return count;
913}
914
Alex Lightbb9b8a52016-06-29 09:26:44 -0700915static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700916 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700917}
918
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700919// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700920static std::string get_other_slot(const std::string& current_slot, int count) {
921 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700922
Daniel Rosenberg80919472016-06-30 19:25:31 -0700923 char next = (current_slot[0] - 'a' + 1)%count + 'a';
924 return std::string(1, next);
925}
926
927static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
928 return get_other_slot(current_slot, get_slot_count(transport));
929}
930
931static std::string get_other_slot(Transport* transport, int count) {
932 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700933}
934
Alex Lightbb9b8a52016-06-29 09:26:44 -0700935static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700936 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700937}
938
Daniel Rosenberg80919472016-06-30 19:25:31 -0700939static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
940 std::string slot = slot_name;
941 if (slot == "_a") slot = "a"; // Legacy support
942 if (slot == "_b") slot = "b"; // Legacy support
943 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800944 if (allow_all) {
945 return "all";
946 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700947 int count = get_slot_count(transport);
948 if (count > 0) {
949 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800950 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800951 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800952 }
953 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700954 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800955
Daniel Rosenberg80919472016-06-30 19:25:31 -0700956 int count = get_slot_count(transport);
957 if (count == 0) die("Device does not support slots.\n");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800958
Daniel Rosenberg80919472016-06-30 19:25:31 -0700959 if (slot == "other") {
960 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700961 if (other == "") {
962 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800963 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700964 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800965 }
966
Daniel Rosenberg80919472016-06-30 19:25:31 -0700967 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
968
969 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
970 for (int i=0; i<count; i++) {
971 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700972 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700973
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700974 exit(1);
975}
976
Daniel Rosenberg80919472016-06-30 19:25:31 -0700977static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800978 return verify_slot(transport, slot, true);
979}
980
Daniel Rosenberg80919472016-06-30 19:25:31 -0700981static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700982 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800983 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700984 std::string current_slot;
985
Daniel Rosenberg80919472016-06-30 19:25:31 -0700986 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800987 /* If has-slot is not supported, the answer is no. */
988 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700989 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800990 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700991 if (slot == "") {
992 current_slot = get_current_slot(transport);
993 if (current_slot == "") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700994 die("Failed to identify current slot.\n");
995 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700996 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700997 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700998 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700999 }
1000 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001001 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001002 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001003 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001004 }
1005 func(part);
1006 }
1007}
1008
David Pursell0b156632015-10-30 11:22:01 -07001009/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1010 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1011 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1012 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001013 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001014static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001015 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001016 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001017
Daniel Rosenberg80919472016-06-30 19:25:31 -07001018 if (slot == "all") {
1019 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
1020 die("Could not check if partition %s has slot.", part.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001021 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001022 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001023 for (int i=0; i < get_slot_count(transport); i++) {
1024 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001025 }
1026 } else {
David Pursell0b156632015-10-30 11:22:01 -07001027 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001028 }
1029 } else {
David Pursell0b156632015-10-30 11:22:01 -07001030 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001031 }
1032}
1033
David Pursell0b156632015-10-30 11:22:01 -07001034static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001035 struct fastboot_buffer buf;
1036
Elliott Hughes53ec4952016-05-11 12:39:27 -07001037 if (!load_buf(transport, fname, &buf)) {
1038 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001039 }
1040 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001041}
1042
Elliott Hughesfc797672015-04-07 20:12:50 -07001043static void do_update_signature(ZipArchiveHandle zip, char* fn) {
1044 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001045 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001046 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001047 fb_queue_download("signature", data, sz);
1048 fb_queue_command("signature", "installing signature");
1049}
1050
Daniel Rosenberg13454092016-06-27 19:43:11 -07001051// Sets slot_override as the active slot. If slot_override is blank,
1052// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001053static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001054 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001055 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001056 if (supports_AB_obsolete(transport)) {
1057 separator = "_"; // Legacy support
1058 } else {
1059 return;
1060 }
1061 }
1062 if (slot_override != "") {
1063 fb_set_active((separator + slot_override).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001064 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001065 std::string current_slot = get_current_slot(transport);
1066 if (current_slot != "") {
1067 fb_set_active((separator + current_slot).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001068 }
1069 }
1070}
1071
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001072static 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 -08001073 queue_info_dump();
1074
Wink Savilleb98762f2011-04-04 17:54:59 -07001075 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1076
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001077 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001078 int error = OpenArchive(filename, &zip);
1079 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001080 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001081 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001082 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001083
Elliott Hughesfc797672015-04-07 20:12:50 -07001084 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001085 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001086 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001087 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001088 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001089 }
1090
Elliott Hughes253c18d2015-03-18 22:47:09 -07001091 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001092
Alex Lightbb9b8a52016-06-29 09:26:44 -07001093 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001094 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001095 if (slot_override != "") {
1096 secondary = get_other_slot(transport, slot_override);
1097 } else {
1098 secondary = get_other_slot(transport);
1099 }
1100 if (secondary == "") {
1101 if (supports_AB(transport)) {
1102 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1103 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001104 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001105 }
1106 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001107 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001108 const char* slot = slot_override.c_str();
1109 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001110 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001111 slot = secondary.c_str();
1112 } else {
1113 continue;
1114 }
1115 }
1116
Elliott Hughes253c18d2015-03-18 22:47:09 -07001117 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001118 if (fd == -1) {
1119 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001120 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001121 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001122 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001123 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001124 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001125 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001126 if (!load_buf_fd(transport, fd, &buf)) {
1127 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1128 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001129
1130 auto update = [&](const std::string &partition) {
1131 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001132 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001133 fb_queue_erase(partition.c_str());
1134 }
1135 flash_buf(partition.c_str(), &buf);
1136 /* not closing the fd here since the sparse code keeps the fd around
1137 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1138 * program exits.
1139 */
1140 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001141 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001142 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001143
1144 CloseArchive(zip);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001145 if (slot_override == "all") {
1146 set_active(transport, "a");
1147 } else {
1148 set_active(transport, slot_override);
1149 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001150}
1151
Alex Lightbb9b8a52016-06-29 09:26:44 -07001152static void do_send_signature(const std::string& fn) {
1153 std::size_t extension_loc = fn.find(".img");
1154 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001155
Alex Lightbb9b8a52016-06-29 09:26:44 -07001156 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001157
1158 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001159 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001160 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001161
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001162 fb_queue_download("signature", data, sz);
1163 fb_queue_command("signature", "installing signature");
1164}
1165
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001166static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001167 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001168 queue_info_dump();
1169
Wink Savilleb98762f2011-04-04 17:54:59 -07001170 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1171
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001172 fname = find_item("info", product);
Elliott Hughes2810d002016-04-25 14:31:18 -07001173 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001174
Elliott Hughesfc797672015-04-07 20:12:50 -07001175 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001176 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001177 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001178
1179 setup_requirements(reinterpret_cast<char*>(data), sz);
1180
Alex Lightbb9b8a52016-06-29 09:26:44 -07001181 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001182 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001183 if (slot_override != "") {
1184 secondary = get_other_slot(transport, slot_override);
1185 } else {
1186 secondary = get_other_slot(transport);
1187 }
1188 if (secondary == "") {
1189 if (supports_AB(transport)) {
1190 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1191 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001192 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001193 }
1194 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001195
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001196 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001197 const char* slot = NULL;
1198 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001199 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001200 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001201 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001202 }
1203 if (!slot) continue;
1204 fname = find_item_given_name(images[i].img_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001205 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001206 if (!load_buf(transport, fname.c_str(), &buf)) {
1207 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001208 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001209 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001210
1211 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001212 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001213 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001214 fb_queue_erase(partition.c_str());
1215 }
1216 flash_buf(partition.c_str(), &buf);
1217 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001218 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001219 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001220
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001221 if (slot_override == "all") {
1222 set_active(transport, "a");
1223 } else {
1224 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001225 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001226}
1227
1228#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001229#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001230
Elliott Hughes45be42e2015-09-02 20:15:48 -07001231static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001232{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001233 if (argc <= 2) return 0;
1234 skip(2);
1235
1236 /*
1237 * Process unlock_bootloader, we have to load the message file
1238 * and send that to the remote device.
1239 */
1240 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001241
1242 int64_t sz;
1243 void* data = load_file(*argv, &sz);
1244 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001245 fb_queue_download("unlock_message", data, sz);
1246 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1247 skip(1);
1248 return 0;
1249}
1250
Elliott Hughes4a667302017-03-15 09:37:28 -07001251static int do_oem_command(int argc, char** argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001252 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001253
Elliott Hughes4a667302017-03-15 09:37:28 -07001254 std::string command;
1255 while (argc > 0) {
1256 command += *argv;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001257 skip(1);
Elliott Hughes4a667302017-03-15 09:37:28 -07001258 if (argc != 0) command += " ";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001259 }
1260
Elliott Hughes4a667302017-03-15 09:37:28 -07001261 fb_queue_command(command.c_str(), "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001262 return 0;
1263}
1264
Colin Crossf8387882012-05-24 17:18:41 -07001265static int64_t parse_num(const char *arg)
1266{
1267 char *endptr;
1268 unsigned long long num;
1269
1270 num = strtoull(arg, &endptr, 0);
1271 if (endptr == arg) {
1272 return -1;
1273 }
1274
1275 if (*endptr == 'k' || *endptr == 'K') {
1276 if (num >= (-1ULL) / 1024) {
1277 return -1;
1278 }
1279 num *= 1024LL;
1280 endptr++;
1281 } else if (*endptr == 'm' || *endptr == 'M') {
1282 if (num >= (-1ULL) / (1024 * 1024)) {
1283 return -1;
1284 }
1285 num *= 1024LL * 1024LL;
1286 endptr++;
1287 } else if (*endptr == 'g' || *endptr == 'G') {
1288 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1289 return -1;
1290 }
1291 num *= 1024LL * 1024LL * 1024LL;
1292 endptr++;
1293 }
1294
1295 if (*endptr != '\0') {
1296 return -1;
1297 }
1298
1299 if (num > INT64_MAX) {
1300 return -1;
1301 }
1302
1303 return num;
1304}
1305
Connor O'Brience16a8a2017-02-06 14:39:31 -08001306static std::string fb_fix_numeric_var(std::string var) {
1307 // Some bootloaders (angler, for example), send spurious leading whitespace.
1308 var = android::base::Trim(var);
1309 // Some bootloaders (hammerhead, for example) use implicit hex.
1310 // This code used to use strtol with base 16.
1311 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1312 return var;
1313}
1314
1315static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1316 std::string sizeString;
1317 if (!fb_getvar(transport, name.c_str(), &sizeString)) {
1318 /* This device does not report flash block sizes, so return 0 */
1319 return 0;
1320 }
1321 sizeString = fb_fix_numeric_var(sizeString);
1322
1323 unsigned size;
1324 if (!android::base::ParseUint(sizeString, &size)) {
1325 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1326 return 0;
1327 }
1328 if (size < 4096 || (size & (size - 1)) != 0) {
1329 fprintf(stderr, "Invalid %s %u: must be a power of 2 and at least 4096.\n",
1330 name.c_str(), size);
1331 return 0;
1332 }
1333 return size;
1334}
1335
David Pursell0b156632015-10-30 11:22:01 -07001336static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001337 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001338 const char* type_override, const char* size_override,
1339 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001340 std::string partition_type, partition_size;
1341
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001342 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001343 const char* errMsg = nullptr;
1344 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001345 int fd;
1346
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001347 unsigned int limit = INT_MAX;
1348 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001349 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001350 }
1351 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001352 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001353 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001354
David Pursell0b156632015-10-30 11:22:01 -07001355 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001356 errMsg = "Can't determine partition type.\n";
1357 goto failed;
1358 }
JP Abgrall7e859742014-05-06 15:14:15 -07001359 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001360 if (partition_type != type_override) {
1361 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1362 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001363 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001364 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001365 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001366
David Pursell0b156632015-10-30 11:22:01 -07001367 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001368 errMsg = "Unable to get partition size\n";
1369 goto failed;
1370 }
JP Abgrall7e859742014-05-06 15:14:15 -07001371 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001372 if (partition_size != size_override) {
1373 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1374 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001375 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001376 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001377 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001378 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001379
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001380 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001381 if (!gen) {
1382 if (skip_if_not_supported) {
1383 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001384 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001385 return;
1386 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001387 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1388 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001389 return;
1390 }
1391
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001392 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001393 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001394 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1395 return;
1396 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001397
1398 fd = fileno(tmpfile());
Connor O'Brience16a8a2017-02-06 14:39:31 -08001399
1400 unsigned eraseBlkSize, logicalBlkSize;
1401 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1402 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1403
1404 if (fs_generator_generate(gen, fd, size, initial_dir, eraseBlkSize, logicalBlkSize)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001405 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001406 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001407 return;
1408 }
1409
Elliott Hughes53ec4952016-05-11 12:39:27 -07001410 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001411 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001412 close(fd);
1413 return;
1414 }
1415 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001416 return;
1417
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001418failed:
1419 if (skip_if_not_supported) {
1420 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001421 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001422 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001423 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001424}
1425
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001426int main(int argc, char **argv)
1427{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001428 bool wants_wipe = false;
1429 bool wants_reboot = false;
1430 bool wants_reboot_bootloader = false;
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001431 bool wants_reboot_emergency = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001432 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001433 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001434 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001435 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001436 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001437 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001438 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001439 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001440 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001441 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001442
JP Abgrall7b8970c2013-03-07 17:06:41 -08001443 const struct option longopts[] = {
1444 {"base", required_argument, 0, 'b'},
1445 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001446 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001447 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001448 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001449 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001450 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001451 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001452 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001453 {"help", no_argument, 0, 'h'},
1454 {"unbuffered", no_argument, 0, 0},
1455 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001456 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001457 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001458 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001459 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001460 {"skip-reboot", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001461#if !defined(_WIN32)
1462 {"wipe-and-use-fbe", no_argument, 0, 0},
1463#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001464 {0, 0, 0, 0}
1465 };
Colin Cross8879f982012-05-22 17:53:34 -07001466
1467 serial = getenv("ANDROID_SERIAL");
1468
1469 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001470 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 -07001471 if (c < 0) {
1472 break;
1473 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001474 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001475 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001476 case 'a':
1477 wants_set_active = true;
1478 if (optarg)
1479 next_active = optarg;
1480 break;
Colin Cross8879f982012-05-22 17:53:34 -07001481 case 'b':
1482 base_addr = strtoul(optarg, 0, 16);
1483 break;
Colin Cross8879f982012-05-22 17:53:34 -07001484 case 'c':
1485 cmdline = optarg;
1486 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001487 case 'h':
1488 usage();
1489 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001490 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001491 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001492 unsigned long val;
1493
1494 val = strtoul(optarg, &endptr, 0);
1495 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1496 die("invalid vendor id '%s'", optarg);
1497 vendor_id = (unsigned short)val;
1498 break;
1499 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001500 case 'k':
1501 kernel_offset = strtoul(optarg, 0, 16);
1502 break;
1503 case 'l':
1504 long_listing = 1;
1505 break;
1506 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001507 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001508 if (!page_size) die("invalid page size");
1509 break;
1510 case 'p':
1511 product = optarg;
1512 break;
1513 case 'r':
1514 ramdisk_offset = strtoul(optarg, 0, 16);
1515 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001516 case 't':
1517 tags_offset = strtoul(optarg, 0, 16);
1518 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001519 case 's':
1520 serial = optarg;
1521 break;
1522 case 'S':
1523 sparse_limit = parse_num(optarg);
1524 if (sparse_limit < 0) {
1525 die("invalid sparse limit");
1526 }
1527 break;
1528 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001529 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001530 break;
1531 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001532 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001533 break;
Colin Cross8879f982012-05-22 17:53:34 -07001534 case '?':
1535 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001536 case 0:
1537 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001538 setvbuf(stdout, nullptr, _IONBF, 0);
1539 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001540 } else if (strcmp("version", longopts[longindex].name) == 0) {
1541 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001542 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001543 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001544 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1545 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001546 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1547 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001548 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1549 skip_reboot = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001550#if !defined(_WIN32)
1551 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1552 wants_wipe = true;
1553 set_fbe_marker = true;
1554#endif
1555 } else {
1556 fprintf(stderr, "Internal error in options processing for %s\n",
1557 longopts[longindex].name);
1558 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001559 }
1560 break;
Colin Cross8879f982012-05-22 17:53:34 -07001561 default:
1562 abort();
1563 }
1564 }
1565
1566 argc -= optind;
1567 argv += optind;
1568
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001569 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001570 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001571 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001572 }
1573
Colin Cross8fb6e062012-07-24 16:36:41 -07001574 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001575 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001576 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001577 return 0;
1578 }
1579
Colin Crossc7b75dc2012-08-29 18:17:06 -07001580 if (argc > 0 && !strcmp(*argv, "help")) {
1581 usage();
1582 return 0;
1583 }
1584
David Pursell0b156632015-10-30 11:22:01 -07001585 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001586 if (transport == nullptr) {
1587 return 1;
1588 }
1589
Daniel Rosenberg80919472016-06-30 19:25:31 -07001590 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1591 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1592 }
1593 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1594 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001595
1596 if (wants_set_active) {
1597 if (next_active == "") {
1598 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001599 std::string current_slot;
1600 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001601 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001602 } else {
1603 wants_set_active = false;
1604 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001605 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001606 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001607 }
1608 }
1609 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001610
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001611 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001612 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001613 require(2);
1614 fb_queue_display(argv[1], argv[1]);
1615 skip(2);
1616 } else if(!strcmp(*argv, "erase")) {
1617 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001618
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001619 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001620 std::string partition_type;
1621 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001622 fs_get_generator(partition_type) != nullptr) {
1623 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1624 partition_type.c_str());
1625 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001626
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001627 fb_queue_erase(partition.c_str());
1628 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001629 do_for_partitions(transport, argv[1], slot_override, erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001630 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001631 } else if(!strncmp(*argv, "format", strlen("format"))) {
1632 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001633 char *type_override = nullptr;
1634 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001635 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001636 /*
1637 * Parsing for: "format[:[type][:[size]]]"
1638 * Some valid things:
1639 * - select ontly the size, and leave default fs type:
1640 * format::0x4000000 userdata
1641 * - default fs type and size:
1642 * format userdata
1643 * format:: userdata
1644 */
1645 overrides = strchr(*argv, ':');
1646 if (overrides) {
1647 overrides++;
1648 size_override = strchr(overrides, ':');
1649 if (size_override) {
1650 size_override[0] = '\0';
1651 size_override++;
1652 }
1653 type_override = overrides;
1654 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001655 if (type_override && !type_override[0]) type_override = nullptr;
1656 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001657
1658 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001659 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001660 fb_queue_erase(partition.c_str());
1661 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001662 fb_perform_format(transport, partition.c_str(), 0,
1663 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001664 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001665 do_for_partitions(transport, argv[1], slot_override, format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001666 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001667 } else if(!strcmp(*argv, "signature")) {
1668 require(2);
1669 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001670 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001671 if (sz != 256) die("signature must be 256 bytes");
1672 fb_queue_download("signature", data, sz);
1673 fb_queue_command("signature", "installing signature");
1674 skip(2);
1675 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001676 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001677 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001678 if (argc > 0) {
1679 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001680 wants_reboot = false;
1681 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001682 skip(1);
Pavlin Radoslavov6adbd4e2017-01-25 19:07:46 -08001683 } else if (!strcmp(*argv, "emergency")) {
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001684 wants_reboot = false;
1685 wants_reboot_emergency = true;
1686 skip(1);
1687 }
Elliott Hughesca85df02015-02-25 10:02:00 -08001688 }
1689 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001690 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001691 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001692 skip(1);
1693 } else if (!strcmp(*argv, "continue")) {
1694 fb_queue_command("continue", "resuming boot");
1695 skip(1);
1696 } else if(!strcmp(*argv, "boot")) {
1697 char *kname = 0;
1698 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001699 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001700 skip(1);
1701 if (argc > 0) {
1702 kname = argv[0];
1703 skip(1);
1704 }
1705 if (argc > 0) {
1706 rname = argv[0];
1707 skip(1);
1708 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001709 if (argc > 0) {
1710 sname = argv[0];
1711 skip(1);
1712 }
1713 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001714 if (data == 0) return 1;
1715 fb_queue_download("boot.img", data, sz);
1716 fb_queue_command("boot", "booting");
1717 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001718 char* pname = argv[1];
1719 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001720 require(2);
1721 if (argc > 2) {
1722 fname = argv[2];
1723 skip(3);
1724 } else {
1725 fname = find_item(pname, product);
1726 skip(2);
1727 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001728 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001729
1730 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001731 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001732 fb_queue_erase(partition.c_str());
1733 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001734 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001735 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001736 do_for_partitions(transport, pname, slot_override, flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001737 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001738 char *kname = argv[2];
1739 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001740 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001741 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001742 skip(3);
1743 if (argc > 0) {
1744 rname = argv[0];
1745 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001746 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001747 if (argc > 0) {
1748 sname = argv[0];
1749 skip(1);
1750 }
1751 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001752 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001753 auto flashraw = [&](const std::string &partition) {
1754 fb_queue_flash(partition.c_str(), data, sz);
1755 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001756 do_for_partitions(transport, argv[1], slot_override, flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001757 } else if(!strcmp(*argv, "flashall")) {
1758 skip(1);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001759 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001760 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1761 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001762 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001763 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001764 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001765 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001766 } else if(!strcmp(*argv, "update")) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001767 bool slot_all = (slot_override == "all");
1768 if (slot_all) {
1769 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1770 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001771 if (argc > 1) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001772 do_update(transport, argv[1], slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001773 skip(2);
1774 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001775 do_update(transport, "update.zip", slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001776 skip(1);
1777 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001778 wants_reboot = true;
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001779 } else if(!strcmp(*argv, "set_active")) {
1780 require(2);
Daniel Rosenberg80919472016-06-30 19:25:31 -07001781 std::string slot = verify_slot(transport, std::string(argv[1]), false);
David Pursell04396f62016-12-15 16:16:10 -08001782 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1783 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1784 // do have slot-suffixes.
1785 std::string var;
1786 if (!fb_getvar(transport, "slot-count", &var) &&
1787 fb_getvar(transport, "slot-suffixes", &var)) {
1788 slot = "_" + slot;
1789 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001790 fb_set_active(slot.c_str());
1791 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001792 } else if(!strcmp(*argv, "oem")) {
1793 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001794 } else if(!strcmp(*argv, "flashing")) {
1795 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1796 !strcmp(*(argv+1), "lock") ||
1797 !strcmp(*(argv+1), "unlock_critical") ||
1798 !strcmp(*(argv+1), "lock_critical") ||
1799 !strcmp(*(argv+1), "get_unlock_ability") ||
1800 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1801 !strcmp(*(argv+1), "lock_bootloader"))) {
1802 argc = do_oem_command(argc, argv);
1803 } else
1804 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1805 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001806 } else {
1807 usage();
1808 return 1;
1809 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001810 } else {
1811 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001812 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001813 }
1814 }
1815
1816 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001817 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001818 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001819 if (set_fbe_marker) {
1820 fprintf(stderr, "setting FBE marker...\n");
1821 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1822 if (initial_userdata_dir.empty()) {
1823 return 1;
1824 }
1825 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1826 delete_fbemarker_tmpdir(initial_userdata_dir);
1827 } else {
1828 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1829 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001830
1831 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001832 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001833 fprintf(stderr, "wiping cache...\n");
1834 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001835 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001836 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001837 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001838 if (wants_set_active) {
1839 fb_set_active(next_active.c_str());
1840 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001841 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001842 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001843 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001844 } else if (wants_reboot_bootloader) {
1845 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001846 fb_queue_wait_for_disconnect();
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001847 } else if (wants_reboot_emergency) {
1848 fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
1849 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001850 }
1851
David Pursell0b156632015-10-30 11:22:01 -07001852 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001853}