blob: 8a6877ad313de842916550d090455a813a93cc23 [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
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070029#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <errno.h>
31#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070032#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070033#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070034#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070035#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070041#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070042#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080043
Elliott Hughes290a2282016-11-14 17:08:47 -080044#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes82ff3152016-08-31 15:07:18 -070050#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070051#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080053#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070054#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/strings.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050056#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070057#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070058#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
Elliott Hughes253c18d2015-03-18 22:47:09 -070060#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080061#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070063#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080064#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070065#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080066#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070067#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068
Chris Fries0ea946c2017-04-12 10:25:57 -050069using android::base::unique_fd;
70
Colin Crossf8387882012-05-24 17:18:41 -070071#ifndef O_BINARY
72#define O_BINARY 0
73#endif
74
Wink Savilleb98762f2011-04-04 17:54:59 -070075char cur_product[FB_RESPONSE_SZ + 1];
76
David Pursell2ec418a2016-01-20 08:32:08 -080077static const char* serial = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -080078static 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;
Chris Fries6a999712017-04-04 09:52:47 -050081// Don't resparse files in too-big chunks.
82// libsparse will support INT_MAX, but this results in large allocations, so
83// let's keep it at 1GB to avoid memory pressure on the host.
84static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Colin Crossf8387882012-05-24 17:18:41 -070085static int64_t sparse_limit = -1;
86static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Elliott Hughesfc797672015-04-07 20:12:50 -070088static unsigned page_size = 2048;
89static unsigned base_addr = 0x10000000;
90static unsigned kernel_offset = 0x00008000;
91static unsigned ramdisk_offset = 0x01000000;
92static unsigned second_offset = 0x00f00000;
93static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080094
Paul Crowley8f7f56e2015-11-27 09:29:37 +000095static const std::string convert_fbe_marker_filename("convert_fbe");
96
Rom Lemarchand622810c2013-06-28 09:54:59 -070097enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -050098 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -070099 FB_BUFFER_SPARSE,
100};
101
102struct fastboot_buffer {
103 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700104 void* data;
105 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500106 int fd;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700107};
108
109static struct {
Elliott Hughes45964a82017-05-03 22:43:23 -0700110 const char* nickname;
111 const char* img_name;
112 const char* sig_name;
113 const char* part_name;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700114 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700115 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700116} images[] = {
Elliott Hughes45964a82017-05-03 22:43:23 -0700117 // clang-format off
118 { "boot", "boot.img", "boot.sig", "boot", false, false },
119 { nullptr, "boot_other.img", "boot.sig", "boot", true, true },
120 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false },
121 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
122 { "system", "system.img", "system.sig", "system", false, false },
123 { nullptr, "system_other.img", "system.sig", "system", true, true },
124 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
125 { "vendor", "vendor.img", "vendor.sig", "vendor", true, false },
126 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, true },
127 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700128};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700129
Elliott Hughes45964a82017-05-03 22:43:23 -0700130static std::string find_item_given_name(const char* img_name) {
131 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700132 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700133 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700134 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700135 return android::base::StringPrintf("%s/%s", dir, img_name);
136}
137
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700138static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700139 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700140 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700141 return find_item_given_name(images[i].img_name);
142 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 }
144
Elliott Hughesd6365a72017-05-08 18:04:49 -0700145 if (item == "userdata") return find_item_given_name("userdata.img");
146 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700147
Elliott Hughesd6365a72017-05-08 18:04:49 -0700148 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700149 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150}
151
Elliott Hughesfc797672015-04-07 20:12:50 -0700152static int64_t get_file_size(int fd) {
153 struct stat sb;
154 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700155}
156
Elliott Hughesfc797672015-04-07 20:12:50 -0700157static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800158 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700159 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160
Elliott Hughesfc797672015-04-07 20:12:50 -0700161 *sz = get_file_size(fd);
162 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700163 goto oops;
164 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165
Elliott Hughesfc797672015-04-07 20:12:50 -0700166 data = (char*) malloc(*sz);
167 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
Elliott Hughesfc797672015-04-07 20:12:50 -0700169 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 close(fd);
171
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 return data;
173
174oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800175 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 close(fd);
177 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800178 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 return 0;
180}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
Elliott Hughes2810d002016-04-25 14:31:18 -0700182static void* load_file(const std::string& path, int64_t* sz) {
183 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700184 if (fd == -1) return nullptr;
185 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700186}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187
Elliott Hughesfc797672015-04-07 20:12:50 -0700188static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700189 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700190 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700191 return -1;
192 }
193
194 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
195 return -1;
196 }
197
Scott Anderson13081c62012-04-06 12:39:30 -0700198 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700200 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
201 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 return 0;
203}
204
Elliott Hughesfc797672015-04-07 20:12:50 -0700205static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800206 return match_fastboot_with_serial(info, serial);
207}
208
Elliott Hughesfc797672015-04-07 20:12:50 -0700209static int list_devices_callback(usb_ifc_info* info) {
210 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800211 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700212 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800213 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700214 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 if (!serial[0]) {
216 serial = "????????????";
217 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700218 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700219 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800220 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700221 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800222 printf("%-22s fastboot", serial.c_str());
223 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700224 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800225 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 }
227
228 return -1;
229}
230
David Pursell2ec418a2016-01-20 08:32:08 -0800231// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
232// a specific device, otherwise the first USB device found will be used.
233//
234// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
235// Otherwise it blocks until the target is available.
236//
237// The returned Transport is a singleton, so multiple calls to this function will return the same
238// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700239static Transport* open_device() {
240 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800241 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242
David Pursell2ec418a2016-01-20 08:32:08 -0800243 if (transport != nullptr) {
244 return transport;
245 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800246
David Pursell4601c972016-02-05 15:35:09 -0800247 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800248 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800249 int port = 0;
250 if (serial != nullptr) {
251 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800252
David Pursell4601c972016-02-05 15:35:09 -0800253 if (android::base::StartsWith(serial, "tcp:")) {
254 protocol = Socket::Protocol::kTcp;
255 port = tcp::kDefaultPort;
256 net_address = serial + strlen("tcp:");
257 } else if (android::base::StartsWith(serial, "udp:")) {
258 protocol = Socket::Protocol::kUdp;
259 port = udp::kDefaultPort;
260 net_address = serial + strlen("udp:");
261 }
262
263 if (net_address != nullptr) {
264 std::string error;
265 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
266 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
267 error.c_str());
268 return nullptr;
269 }
David Pursell2ec418a2016-01-20 08:32:08 -0800270 }
271 }
272
273 while (true) {
274 if (!host.empty()) {
275 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800276 if (protocol == Socket::Protocol::kTcp) {
277 transport = tcp::Connect(host, port, &error).release();
278 } else if (protocol == Socket::Protocol::kUdp) {
279 transport = udp::Connect(host, port, &error).release();
280 }
281
David Pursell2ec418a2016-01-20 08:32:08 -0800282 if (transport == nullptr && announce) {
283 fprintf(stderr, "error: %s\n", error.c_str());
284 }
285 } else {
286 transport = usb_open(match_fastboot);
287 }
288
289 if (transport != nullptr) {
290 return transport;
291 }
292
David Pursell0b156632015-10-30 11:22:01 -0700293 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800294 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700295 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800297 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 }
299}
300
Elliott Hughesfc797672015-04-07 20:12:50 -0700301static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 // We don't actually open a USB device here,
303 // just getting our callback called so we can
304 // list all the connected devices.
305 usb_open(list_devices_callback);
306}
307
Elliott Hughesd6365a72017-05-08 18:04:49 -0700308static void syntax_error(const char* fmt, ...) {
309 fprintf(stderr, "fastboot: usage: ");
310
311 va_list ap;
312 va_start(ap, fmt);
313 vfprintf(stderr, fmt, ap);
314 va_end(ap);
315
316 fprintf(stderr, "\n");
317 exit(1);
318}
319
320static int show_help() {
321 // clang-format off
322 fprintf(stdout,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
324 "usage: fastboot [ <option> ] <command>\n"
325 "\n"
326 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700327 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700328 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700329 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700330 " if found -- recovery. If the device\n"
331 " supports slots, the slot that has\n"
332 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700333 " Secondary images may be flashed to\n"
334 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700335 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
336 " flashing lock Locks the device. Prevents flashing.\n"
337 " flashing unlock Unlocks the device. Allows flashing\n"
338 " any partition except\n"
339 " bootloader-related partitions.\n"
340 " flashing lock_critical Prevents flashing bootloader-related\n"
341 " partitions.\n"
342 " flashing unlock_critical Enables flashing bootloader-related\n"
343 " partitions.\n"
344 " flashing get_unlock_ability Queries bootloader to see if the\n"
345 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700346 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700347 " unlock nonce.\n"
348 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700349 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700350 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700351 " erase <partition> Erase a flash partition.\n"
352 " format[:[<fs type>][:[<size>]] <partition>\n"
353 " Format a flash partition. Can\n"
354 " override the fs type and/or size\n"
355 " the bootloader reports.\n"
356 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700357 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800358 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700359 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
Elliott Hughesd6365a72017-05-08 18:04:49 -0700360 " flash:raw <bootable-partition> <kernel> [ <ramdisk> [ <second> ] ]\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700361 " Create bootimage and flash it.\n"
362 " devices [-l] List all connected devices [with\n"
363 " device paths].\n"
364 " continue Continue with autoboot.\n"
Alexey Polyudove0bfb752017-01-03 19:39:13 -0800365 " reboot [bootloader|emergency] Reboot device [into bootloader or emergency mode].\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700366 " reboot-bootloader Reboot device into bootloader.\n"
Jocelyn Bohr98cc2832017-01-26 19:20:53 -0800367 " oem <parameter1> ... <parameterN> Executes oem specific command.\n"
368 " stage <infile> Sends contents of <infile> to stage for\n"
369 " the next command. Supported only on\n"
370 " Android Things devices.\n"
Jocelyn Bohr91fefad2017-01-27 14:17:56 -0800371 " get_staged <outfile> Receives data to <outfile> staged by the\n"
372 " last command. Supported only on Android\n"
373 " Things devices.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700374 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 "\n"
376 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700377 " -w Erase userdata and cache (and format\n"
378 " if supported by partition type).\n"
379 " -u Do not erase partition before\n"
380 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800381 " -s <specific device> Specify a device. For USB, provide either\n"
382 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700383 " For ethernet, provide an address in the\n"
384 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800385 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700386 " -c <cmdline> Override kernel commandline.\n"
387 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800388 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700389 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800390 " --kernel-offset Specify a custom kernel offset.\n"
391 " (default: 0x00008000)\n"
392 " --ramdisk-offset Specify a custom ramdisk offset.\n"
393 " (default: 0x01000000)\n"
394 " --tags-offset Specify a custom tags offset.\n"
395 " (default: 0x00000100)\n"
396 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700397 " (default: 2048).\n"
398 " -S <size>[K|M|G] Automatically sparse files greater\n"
399 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700400 " --slot <slot> Specify slot name to be used if the\n"
401 " device supports slots. All operations\n"
402 " on partitions that support slots will\n"
403 " be done on the slot specified.\n"
404 " 'all' can be given to refer to all slots.\n"
405 " 'other' can be given to refer to a\n"
406 " non-current slot. If this flag is not\n"
407 " used, slotted partitions will default\n"
408 " to the current active slot.\n"
409 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800410 " provided, this will default to the value\n"
411 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800412 " supported, this does nothing. This will\n"
413 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700414 " --skip-secondary Will not flash secondary slots when\n"
415 " performing a flashall or update. This\n"
416 " will preserve data on other slots.\n"
Mitchell Wills31dce302016-09-26 10:26:21 -0700417 " --skip-reboot Will not reboot the device when\n"
418 " performing commands that normally\n"
419 " trigger a reboot.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000420#if !defined(_WIN32)
421 " --wipe-and-use-fbe On devices which support it,\n"
422 " erase userdata and cache, and\n"
423 " enable file-based encryption\n"
424#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800425 " --unbuffered Do not buffer input or output.\n"
426 " --version Display version.\n"
427 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700429 // clang-format off
430 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000432
Elliott Hughesd6365a72017-05-08 18:04:49 -0700433static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
434 const std::string& second_stage, int64_t* sz,
Elliott Hughesfc797672015-04-07 20:12:50 -0700435 const char* cmdline) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700436 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700437 void* kdata = load_file(kernel.c_str(), &ksize);
438 if (kdata == nullptr) die("cannot load '%s': %s\n", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800439
Elliott Hughesfc797672015-04-07 20:12:50 -0700440 // Is this actually a boot image?
Elliott Hughesd6365a72017-05-08 18:04:49 -0700441 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700442 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800443
Elliott Hughesd6365a72017-05-08 18:04:49 -0700444 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk\n");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800445
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446 *sz = ksize;
447 return kdata;
448 }
449
Elliott Hughesfc797672015-04-07 20:12:50 -0700450 void* rdata = nullptr;
451 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700452 if (!ramdisk.empty()) {
453 rdata = load_file(ramdisk.c_str(), &rsize);
454 if (rdata == nullptr) die("cannot load '%s': %s\n", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 }
456
Elliott Hughesfc797672015-04-07 20:12:50 -0700457 void* sdata = nullptr;
458 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700459 if (!second_stage.empty()) {
460 sdata = load_file(second_stage.c_str(), &ssize);
461 if (sdata == nullptr) die("cannot load '%s': %s\n", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200462 }
463
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700465 int64_t bsize = 0;
466 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800467 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200468 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800469 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesd6365a72017-05-08 18:04:49 -0700470 if (bdata == nullptr) die("failed to create boot.img\n");
471
Elliott Hughesfc797672015-04-07 20:12:50 -0700472 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
473 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800475
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 return bdata;
477}
478
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700479static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700480 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700481 ZipEntry zip_entry;
482 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700483 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000484 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 }
486
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700487 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700489 fprintf(stderr, "extracting %s (%" PRId64 " MB)...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700490 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700491 if (data == nullptr) {
492 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000493 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 }
495
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700496 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
497 if (error != 0) {
498 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000500 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000502
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 return data;
504}
505
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700506#if defined(_WIN32)
507
508// TODO: move this to somewhere it can be shared.
509
510#include <windows.h>
511
512// Windows' tmpfile(3) requires administrator rights because
513// it creates temporary files in the root directory.
514static FILE* win32_tmpfile() {
515 char temp_path[PATH_MAX];
516 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
517 if (nchars == 0 || nchars >= sizeof(temp_path)) {
518 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
519 return nullptr;
520 }
521
522 char filename[PATH_MAX];
523 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
524 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
525 return nullptr;
526 }
527
528 return fopen(filename, "w+bTD");
529}
530
531#define tmpfile win32_tmpfile
532
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000533static std::string make_temporary_directory() {
534 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
535 return "";
536}
537
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700538static int make_temporary_fd() {
539 // TODO: reimplement to avoid leaking a FILE*.
540 return fileno(tmpfile());
541}
542
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000543#else
544
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700545static std::string make_temporary_template() {
546 const char* tmpdir = getenv("TMPDIR");
547 if (tmpdir == nullptr) tmpdir = P_tmpdir;
548 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
549}
550
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000551static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700552 std::string result(make_temporary_template());
553 if (mkdtemp(&result[0]) == nullptr) {
554 fprintf(stderr, "Unable to create temporary directory: %s\n", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000555 return "";
556 }
557 return result;
558}
559
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700560static int make_temporary_fd() {
561 std::string path_template(make_temporary_template());
562 int fd = mkstemp(&path_template[0]);
563 if (fd == -1) {
564 fprintf(stderr, "Unable to create temporary file: %s\n", strerror(errno));
565 return -1;
566 }
567 unlink(path_template.c_str());
568 return fd;
569}
570
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700571#endif
572
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000573static std::string create_fbemarker_tmpdir() {
574 std::string dir = make_temporary_directory();
575 if (dir.empty()) {
576 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
577 return "";
578 }
579 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
580 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
581 if (fd == -1) {
582 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
583 marker_file.c_str(), errno, strerror(errno));
584 return "";
585 }
586 close(fd);
587 return dir;
588}
589
590static void delete_fbemarker_tmpdir(const std::string& dir) {
591 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
592 if (unlink(marker_file.c_str()) == -1) {
593 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
594 marker_file.c_str(), errno, strerror(errno));
595 return;
596 }
597 if (rmdir(dir.c_str()) == -1) {
598 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
599 dir.c_str(), errno, strerror(errno));
600 return;
601 }
602}
603
Elliott Hughes45964a82017-05-03 22:43:23 -0700604static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700605 unique_fd fd(make_temporary_fd());
606 if (fd == -1) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700607 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
608 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700609 return -1;
610 }
611
Yusuke Sato07447542015-06-25 14:39:19 -0700612 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700613 ZipEntry zip_entry;
614 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
615 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000616 return -1;
617 }
618
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700619 fprintf(stderr, "extracting %s (%" PRIu32 " MB)...\n", entry_name,
620 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700621 int error = ExtractEntryToFile(zip, &zip_entry, fd);
622 if (error != 0) {
623 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
624 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700625 }
626
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000627 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700628 // TODO: We're leaking 'fp' here.
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700629 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700630}
631
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632static char *strip(char *s)
633{
634 int n;
635 while(*s && isspace(*s)) s++;
636 n = strlen(s);
637 while(n-- > 0) {
638 if(!isspace(s[n])) break;
639 s[n] = 0;
640 }
641 return s;
642}
643
644#define MAX_OPTIONS 32
645static int setup_requirement_line(char *name)
646{
647 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700648 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800649 unsigned n, count;
650 char *x;
651 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800652
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653 if (!strncmp(name, "reject ", 7)) {
654 name += 7;
655 invert = 1;
656 } else if (!strncmp(name, "require ", 8)) {
657 name += 8;
658 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700659 } else if (!strncmp(name, "require-for-product:", 20)) {
660 // Get the product and point name past it
661 prod = name + 20;
662 name = strchr(name, ' ');
663 if (!name) return -1;
664 *name = 0;
665 name += 1;
666 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667 }
668
669 x = strchr(name, '=');
670 if (x == 0) return 0;
671 *x = 0;
672 val[0] = x + 1;
673
674 for(count = 1; count < MAX_OPTIONS; count++) {
675 x = strchr(val[count - 1],'|');
676 if (x == 0) break;
677 *x = 0;
678 val[count] = x + 1;
679 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800680
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681 name = strip(name);
682 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800683
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800684 name = strip(name);
685 if (name == 0) return -1;
686
Elliott Hughes253c18d2015-03-18 22:47:09 -0700687 const char* var = name;
688 // Work around an unfortunate name mismatch.
689 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690
Elliott Hughes253c18d2015-03-18 22:47:09 -0700691 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 if (out == 0) return -1;
693
694 for(n = 0; n < count; n++) {
695 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700696 if (out[n] == 0) {
697 for(size_t i = 0; i < n; ++i) {
698 free((char*) out[i]);
699 }
700 free(out);
701 return -1;
702 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703 }
704
Elliott Hughes253c18d2015-03-18 22:47:09 -0700705 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706 return 0;
707}
708
Elliott Hughesfc797672015-04-07 20:12:50 -0700709static void setup_requirements(char* data, int64_t sz) {
710 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700712 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713 *s++ = 0;
714 if (setup_requirement_line(data)) {
715 die("out of memory");
716 }
717 data = s;
718 } else {
719 s++;
720 }
721 }
722}
723
Elliott Hughesfc797672015-04-07 20:12:50 -0700724static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800725 fb_queue_notice("--------------------------------------------");
726 fb_queue_display("version-bootloader", "Bootloader Version...");
727 fb_queue_display("version-baseband", "Baseband Version.....");
728 fb_queue_display("serialno", "Serial Number........");
729 fb_queue_notice("--------------------------------------------");
730}
731
Rom Lemarchand622810c2013-06-28 09:54:59 -0700732static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700733{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700734 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700735 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700736 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700737 }
738
Elliott Hughesfc797672015-04-07 20:12:50 -0700739 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700740 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700741 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700742 }
743
Elliott Hughes253c18d2015-03-18 22:47:09 -0700744 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700745 if (!out_s) {
746 die("Failed to allocate sparse file array\n");
747 }
748
749 files = sparse_file_resparse(s, max_size, out_s, files);
750 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700751 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700752 }
753
754 return out_s;
755}
756
David Pursell0b156632015-10-30 11:22:01 -0700757static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700758 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700759 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
760 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800761 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700762 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700763 }
764
Elliott Hughes77c0e662015-11-02 15:51:12 -0800765 // Some bootloaders (angler, for example) send spurious whitespace too.
766 max_download_size = android::base::Trim(max_download_size);
767
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700768 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700769 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800770 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700771 return 0;
772 }
773 if (limit > 0) {
774 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
775 }
Colin Crossf8387882012-05-24 17:18:41 -0700776 return limit;
777}
778
David Pursell0b156632015-10-30 11:22:01 -0700779static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700780 int64_t limit;
781
782 if (sparse_limit == 0) {
783 return 0;
784 } else if (sparse_limit > 0) {
785 limit = sparse_limit;
786 } else {
787 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700788 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700789 }
790 if (target_sparse_limit > 0) {
791 limit = target_sparse_limit;
792 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700793 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700794 }
795 }
796
797 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500798 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700799 }
800
801 return 0;
802}
803
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700804// Until we get lazy inode table init working in make_ext4fs, we need to
805// erase partitions of type ext4 before flashing a filesystem so no stale
806// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700807static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800808 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700809 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800810 return false;
811 }
812 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700813}
814
Elliott Hughes53ec4952016-05-11 12:39:27 -0700815static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700816 int64_t sz = get_file_size(fd);
817 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700818 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700819 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700820
Elliott Hughesfc797672015-04-07 20:12:50 -0700821 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700822 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700823 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700824 sparse_file** s = load_sparse_files(fd, limit);
825 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700826 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700827 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700828 buf->type = FB_BUFFER_SPARSE;
829 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700830 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500831 buf->type = FB_BUFFER_FD;
832 buf->data = nullptr;
833 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000834 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700835 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700836
Elliott Hughes53ec4952016-05-11 12:39:27 -0700837 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700838}
839
Elliott Hughes53ec4952016-05-11 12:39:27 -0700840static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500841 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
842
Elliott Hughes53ec4952016-05-11 12:39:27 -0700843 if (fd == -1) {
844 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700845 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500846
847 struct stat s;
848 if (fstat(fd, &s)) {
849 return false;
850 }
851 if (!S_ISREG(s.st_mode)) {
852 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
853 return false;
854 }
855
856 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700857}
858
859static void flash_buf(const char *pname, struct fastboot_buffer *buf)
860{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700861 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700862
863 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800864 case FB_BUFFER_SPARSE: {
865 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700866 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700867 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700868 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800869 sparse_files.emplace_back(*s, sz);
870 ++s;
871 }
872
873 for (size_t i = 0; i < sparse_files.size(); ++i) {
874 const auto& pair = sparse_files[i];
875 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700876 }
877 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800878 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500879 case FB_BUFFER_FD:
880 fb_queue_flash_fd(pname, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700881 break;
882 default:
883 die("unknown buffer type: %d", buf->type);
884 }
885}
886
Daniel Rosenberg80919472016-06-30 19:25:31 -0700887static std::string get_current_slot(Transport* transport)
888{
889 std::string current_slot;
890 if (fb_getvar(transport, "current-slot", &current_slot)) {
891 if (current_slot == "_a") return "a"; // Legacy support
892 if (current_slot == "_b") return "b"; // Legacy support
893 return current_slot;
894 }
895 return "";
896}
897
898// Legacy support
899static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700900 std::vector<std::string> suffixes;
901 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700902 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700903 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700904 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700905 suffixes = android::base::Split(suffix_list, ",");
906 // Unfortunately some devices will return an error message in the
907 // guise of a valid value. If we only see only one suffix, it's probably
908 // not real.
909 if (suffixes.size() == 1) {
910 suffixes.clear();
911 }
912 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700913}
914
Daniel Rosenberg80919472016-06-30 19:25:31 -0700915// Legacy support
916static bool supports_AB_obsolete(Transport* transport) {
917 return !get_suffixes_obsolete(transport).empty();
918}
919
920static int get_slot_count(Transport* transport) {
921 std::string var;
922 int count;
923 if (!fb_getvar(transport, "slot-count", &var)) {
924 if (supports_AB_obsolete(transport)) return 2; // Legacy support
925 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700926 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700927 return count;
928}
929
Alex Lightbb9b8a52016-06-29 09:26:44 -0700930static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700931 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700932}
933
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700934// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700935static std::string get_other_slot(const std::string& current_slot, int count) {
936 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700937
Daniel Rosenberg80919472016-06-30 19:25:31 -0700938 char next = (current_slot[0] - 'a' + 1)%count + 'a';
939 return std::string(1, next);
940}
941
942static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
943 return get_other_slot(current_slot, get_slot_count(transport));
944}
945
946static std::string get_other_slot(Transport* transport, int count) {
947 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700948}
949
Alex Lightbb9b8a52016-06-29 09:26:44 -0700950static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700951 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700952}
953
Daniel Rosenberg80919472016-06-30 19:25:31 -0700954static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
955 std::string slot = slot_name;
956 if (slot == "_a") slot = "a"; // Legacy support
957 if (slot == "_b") slot = "b"; // Legacy support
958 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800959 if (allow_all) {
960 return "all";
961 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700962 int count = get_slot_count(transport);
963 if (count > 0) {
964 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800965 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800966 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800967 }
968 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700969 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800970
Daniel Rosenberg80919472016-06-30 19:25:31 -0700971 int count = get_slot_count(transport);
972 if (count == 0) die("Device does not support slots.\n");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800973
Daniel Rosenberg80919472016-06-30 19:25:31 -0700974 if (slot == "other") {
975 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700976 if (other == "") {
977 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800978 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700979 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800980 }
981
Daniel Rosenberg80919472016-06-30 19:25:31 -0700982 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
983
984 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
985 for (int i=0; i<count; i++) {
986 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700987 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700988
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700989 exit(1);
990}
991
Daniel Rosenberg80919472016-06-30 19:25:31 -0700992static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800993 return verify_slot(transport, slot, true);
994}
995
Daniel Rosenberg80919472016-06-30 19:25:31 -0700996static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700997 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800998 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700999 std::string current_slot;
1000
Daniel Rosenberg80919472016-06-30 19:25:31 -07001001 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001002 /* If has-slot is not supported, the answer is no. */
1003 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001004 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001005 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001006 if (slot == "") {
1007 current_slot = get_current_slot(transport);
1008 if (current_slot == "") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001009 die("Failed to identify current slot.\n");
1010 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001011 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001012 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001013 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001014 }
1015 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001016 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001017 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001018 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001019 }
1020 func(part);
1021 }
1022}
1023
David Pursell0b156632015-10-30 11:22:01 -07001024/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1025 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1026 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1027 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001028 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001029static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001030 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001031 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001032
Daniel Rosenberg80919472016-06-30 19:25:31 -07001033 if (slot == "all") {
1034 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
1035 die("Could not check if partition %s has slot.", part.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001036 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001037 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001038 for (int i=0; i < get_slot_count(transport); i++) {
1039 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001040 }
1041 } else {
David Pursell0b156632015-10-30 11:22:01 -07001042 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001043 }
1044 } else {
David Pursell0b156632015-10-30 11:22:01 -07001045 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001046 }
1047}
1048
David Pursell0b156632015-10-30 11:22:01 -07001049static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001050 struct fastboot_buffer buf;
1051
Elliott Hughes53ec4952016-05-11 12:39:27 -07001052 if (!load_buf(transport, fname, &buf)) {
1053 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001054 }
1055 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001056}
1057
Elliott Hughes45964a82017-05-03 22:43:23 -07001058static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001059 int64_t sz;
Elliott Hughes45964a82017-05-03 22:43:23 -07001060 void* data = unzip_file(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001061 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001062 fb_queue_download("signature", data, sz);
1063 fb_queue_command("signature", "installing signature");
1064}
1065
Daniel Rosenberg13454092016-06-27 19:43:11 -07001066// Sets slot_override as the active slot. If slot_override is blank,
1067// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001068static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001069 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001070 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001071 if (supports_AB_obsolete(transport)) {
1072 separator = "_"; // Legacy support
1073 } else {
1074 return;
1075 }
1076 }
1077 if (slot_override != "") {
1078 fb_set_active((separator + slot_override).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001079 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001080 std::string current_slot = get_current_slot(transport);
1081 if (current_slot != "") {
1082 fb_set_active((separator + current_slot).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001083 }
1084 }
1085}
1086
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001087static 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 -08001088 queue_info_dump();
1089
Wink Savilleb98762f2011-04-04 17:54:59 -07001090 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1091
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001092 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001093 int error = OpenArchive(filename, &zip);
1094 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001095 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001096 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001097 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001098
Elliott Hughesfc797672015-04-07 20:12:50 -07001099 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001100 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001101 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001102 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001103 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001104 }
1105
Elliott Hughes253c18d2015-03-18 22:47:09 -07001106 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001107
Alex Lightbb9b8a52016-06-29 09:26:44 -07001108 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001109 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001110 if (slot_override != "") {
1111 secondary = get_other_slot(transport, slot_override);
1112 } else {
1113 secondary = get_other_slot(transport);
1114 }
1115 if (secondary == "") {
1116 if (supports_AB(transport)) {
1117 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1118 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001119 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001120 }
1121 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001122 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001123 const char* slot = slot_override.c_str();
1124 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001125 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001126 slot = secondary.c_str();
1127 } else {
1128 continue;
1129 }
1130 }
1131
Elliott Hughes253c18d2015-03-18 22:47:09 -07001132 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001133 if (fd == -1) {
1134 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001135 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001136 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001137 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001138 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001139 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001140 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001141 if (!load_buf_fd(transport, fd, &buf)) {
1142 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1143 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001144
1145 auto update = [&](const std::string &partition) {
1146 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001147 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001148 fb_queue_erase(partition.c_str());
1149 }
1150 flash_buf(partition.c_str(), &buf);
1151 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001152 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001153 * program exits.
1154 */
1155 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001156 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001157 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001158
1159 CloseArchive(zip);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001160 if (slot_override == "all") {
1161 set_active(transport, "a");
1162 } else {
1163 set_active(transport, slot_override);
1164 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001165}
1166
Alex Lightbb9b8a52016-06-29 09:26:44 -07001167static void do_send_signature(const std::string& fn) {
1168 std::size_t extension_loc = fn.find(".img");
1169 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001170
Alex Lightbb9b8a52016-06-29 09:26:44 -07001171 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001172
1173 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001174 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001175 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001176
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001177 fb_queue_download("signature", data, sz);
1178 fb_queue_command("signature", "installing signature");
1179}
1180
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001181static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001182 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001183 queue_info_dump();
1184
Wink Savilleb98762f2011-04-04 17:54:59 -07001185 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1186
Elliott Hughes45964a82017-05-03 22:43:23 -07001187 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001188 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001189
Elliott Hughesfc797672015-04-07 20:12:50 -07001190 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001191 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001192 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001193
1194 setup_requirements(reinterpret_cast<char*>(data), sz);
1195
Alex Lightbb9b8a52016-06-29 09:26:44 -07001196 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001197 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001198 if (slot_override != "") {
1199 secondary = get_other_slot(transport, slot_override);
1200 } else {
1201 secondary = get_other_slot(transport);
1202 }
1203 if (secondary == "") {
1204 if (supports_AB(transport)) {
1205 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1206 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001207 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001208 }
1209 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001210
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001211 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001212 const char* slot = NULL;
1213 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001214 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001215 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001216 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001217 }
1218 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001219 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001220 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001221 if (!load_buf(transport, fname.c_str(), &buf)) {
1222 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001223 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001224 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001225
1226 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001227 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001228 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001229 fb_queue_erase(partition.c_str());
1230 }
1231 flash_buf(partition.c_str(), &buf);
1232 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001233 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001234 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001235
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001236 if (slot_override == "all") {
1237 set_active(transport, "a");
1238 } else {
1239 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001240 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001241}
1242
Elliott Hughesd6365a72017-05-08 18:04:49 -07001243static std::string next_arg(std::vector<std::string>* args) {
1244 if (args->empty()) syntax_error("expected argument");
1245 std::string result = args->front();
1246 args->erase(args->begin());
1247 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001248}
1249
Elliott Hughesd6365a72017-05-08 18:04:49 -07001250static void do_bypass_unlock_command(std::vector<std::string>* args) {
1251 if (args->empty()) syntax_error("missing unlock_bootloader request");
1252
1253 std::string filename = next_arg(args);
1254
1255 int64_t sz;
1256 void* data = load_file(filename.c_str(), &sz);
1257 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
1258 fb_queue_download("unlock_message", data, sz);
1259 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1260}
1261
1262static void do_oem_command(std::vector<std::string>* args) {
1263 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001264
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001265 std::string command("oem");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001266 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001267 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001268 }
Elliott Hughes4a667302017-03-15 09:37:28 -07001269 fb_queue_command(command.c_str(), "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001270}
1271
Colin Crossf8387882012-05-24 17:18:41 -07001272static int64_t parse_num(const char *arg)
1273{
1274 char *endptr;
1275 unsigned long long num;
1276
1277 num = strtoull(arg, &endptr, 0);
1278 if (endptr == arg) {
1279 return -1;
1280 }
1281
1282 if (*endptr == 'k' || *endptr == 'K') {
1283 if (num >= (-1ULL) / 1024) {
1284 return -1;
1285 }
1286 num *= 1024LL;
1287 endptr++;
1288 } else if (*endptr == 'm' || *endptr == 'M') {
1289 if (num >= (-1ULL) / (1024 * 1024)) {
1290 return -1;
1291 }
1292 num *= 1024LL * 1024LL;
1293 endptr++;
1294 } else if (*endptr == 'g' || *endptr == 'G') {
1295 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1296 return -1;
1297 }
1298 num *= 1024LL * 1024LL * 1024LL;
1299 endptr++;
1300 }
1301
1302 if (*endptr != '\0') {
1303 return -1;
1304 }
1305
1306 if (num > INT64_MAX) {
1307 return -1;
1308 }
1309
1310 return num;
1311}
1312
Connor O'Brience16a8a2017-02-06 14:39:31 -08001313static std::string fb_fix_numeric_var(std::string var) {
1314 // Some bootloaders (angler, for example), send spurious leading whitespace.
1315 var = android::base::Trim(var);
1316 // Some bootloaders (hammerhead, for example) use implicit hex.
1317 // This code used to use strtol with base 16.
1318 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1319 return var;
1320}
1321
1322static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1323 std::string sizeString;
1324 if (!fb_getvar(transport, name.c_str(), &sizeString)) {
1325 /* This device does not report flash block sizes, so return 0 */
1326 return 0;
1327 }
1328 sizeString = fb_fix_numeric_var(sizeString);
1329
1330 unsigned size;
1331 if (!android::base::ParseUint(sizeString, &size)) {
1332 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1333 return 0;
1334 }
1335 if (size < 4096 || (size & (size - 1)) != 0) {
1336 fprintf(stderr, "Invalid %s %u: must be a power of 2 and at least 4096.\n",
1337 name.c_str(), size);
1338 return 0;
1339 }
1340 return size;
1341}
1342
David Pursell0b156632015-10-30 11:22:01 -07001343static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001344 const char* partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001345 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001346 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001347 std::string partition_type, partition_size;
1348
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001349 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001350 const char* errMsg = nullptr;
1351 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001352 int fd;
1353
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001354 unsigned int limit = INT_MAX;
1355 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001356 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001357 }
1358 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001359 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001360 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001361
David Pursell0b156632015-10-30 11:22:01 -07001362 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001363 errMsg = "Can't determine partition type.\n";
1364 goto failed;
1365 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001366 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001367 if (partition_type != type_override) {
1368 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughesd6365a72017-05-08 18:04:49 -07001369 partition, partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001370 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001371 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001372 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001373
David Pursell0b156632015-10-30 11:22:01 -07001374 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001375 errMsg = "Unable to get partition size\n";
1376 goto failed;
1377 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001378 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001379 if (partition_size != size_override) {
1380 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughesd6365a72017-05-08 18:04:49 -07001381 partition, partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001382 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001383 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001384 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001385 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001386
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001387 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001388 if (!gen) {
1389 if (skip_if_not_supported) {
1390 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001391 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001392 return;
1393 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001394 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1395 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001396 return;
1397 }
1398
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001399 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001400 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001401 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1402 return;
1403 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001404
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001405 fd = make_temporary_fd();
1406 if (fd == -1) return;
Connor O'Brience16a8a2017-02-06 14:39:31 -08001407
1408 unsigned eraseBlkSize, logicalBlkSize;
1409 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1410 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1411
1412 if (fs_generator_generate(gen, fd, size, initial_dir, eraseBlkSize, logicalBlkSize)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001413 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001414 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001415 return;
1416 }
1417
Elliott Hughes53ec4952016-05-11 12:39:27 -07001418 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001419 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001420 close(fd);
1421 return;
1422 }
1423 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001424 return;
1425
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001426failed:
1427 if (skip_if_not_supported) {
1428 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001429 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001430 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001431 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001432}
1433
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001434int main(int argc, char **argv)
1435{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001436 bool wants_wipe = false;
1437 bool wants_reboot = false;
1438 bool wants_reboot_bootloader = false;
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001439 bool wants_reboot_emergency = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001440 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001441 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001442 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001443 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001444 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001445 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001446 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001447 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001448 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001449 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001450
JP Abgrall7b8970c2013-03-07 17:06:41 -08001451 const struct option longopts[] = {
1452 {"base", required_argument, 0, 'b'},
1453 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001454 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001455 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001456 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001457 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001458 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001459 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001460 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001461 {"help", no_argument, 0, 'h'},
1462 {"unbuffered", no_argument, 0, 0},
1463 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001464 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001465 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001466 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001467 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001468 {"skip-reboot", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001469#if !defined(_WIN32)
1470 {"wipe-and-use-fbe", no_argument, 0, 0},
1471#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001472 {0, 0, 0, 0}
1473 };
Colin Cross8879f982012-05-22 17:53:34 -07001474
1475 serial = getenv("ANDROID_SERIAL");
1476
1477 while (1) {
Elliott Hughes45964a82017-05-03 22:43:23 -07001478 int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lc:i:m:ha::", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -07001479 if (c < 0) {
1480 break;
1481 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001482 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001483 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001484 case 'a':
1485 wants_set_active = true;
1486 if (optarg)
1487 next_active = optarg;
1488 break;
Colin Cross8879f982012-05-22 17:53:34 -07001489 case 'b':
1490 base_addr = strtoul(optarg, 0, 16);
1491 break;
Colin Cross8879f982012-05-22 17:53:34 -07001492 case 'c':
1493 cmdline = optarg;
1494 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001495 case 'h':
Elliott Hughesd6365a72017-05-08 18:04:49 -07001496 return show_help();
Colin Cross8879f982012-05-22 17:53:34 -07001497 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001498 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001499 unsigned long val;
1500
1501 val = strtoul(optarg, &endptr, 0);
1502 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1503 die("invalid vendor id '%s'", optarg);
1504 vendor_id = (unsigned short)val;
1505 break;
1506 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001507 case 'k':
1508 kernel_offset = strtoul(optarg, 0, 16);
1509 break;
1510 case 'l':
1511 long_listing = 1;
1512 break;
1513 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001514 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001515 if (!page_size) die("invalid page size");
1516 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001517 case 'r':
1518 ramdisk_offset = strtoul(optarg, 0, 16);
1519 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001520 case 't':
1521 tags_offset = strtoul(optarg, 0, 16);
1522 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001523 case 's':
1524 serial = optarg;
1525 break;
1526 case 'S':
1527 sparse_limit = parse_num(optarg);
1528 if (sparse_limit < 0) {
1529 die("invalid sparse limit");
1530 }
1531 break;
1532 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001533 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001534 break;
1535 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001536 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001537 break;
Colin Cross8879f982012-05-22 17:53:34 -07001538 case '?':
1539 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001540 case 0:
1541 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001542 setvbuf(stdout, nullptr, _IONBF, 0);
1543 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001544 } else if (strcmp("version", longopts[longindex].name) == 0) {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001545 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001546 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001547 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001548 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1549 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001550 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1551 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001552 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1553 skip_reboot = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001554#if !defined(_WIN32)
1555 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1556 wants_wipe = true;
1557 set_fbe_marker = true;
1558#endif
1559 } else {
1560 fprintf(stderr, "Internal error in options processing for %s\n",
1561 longopts[longindex].name);
1562 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001563 }
1564 break;
Colin Cross8879f982012-05-22 17:53:34 -07001565 default:
1566 abort();
1567 }
1568 }
1569
1570 argc -= optind;
1571 argv += optind;
1572
Elliott Hughesd6365a72017-05-08 18:04:49 -07001573 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001574
Colin Cross8fb6e062012-07-24 16:36:41 -07001575 if (argc > 0 && !strcmp(*argv, "devices")) {
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")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001581 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001582 }
1583
David Pursell0b156632015-10-30 11:22:01 -07001584 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001585 if (transport == nullptr) {
1586 return 1;
1587 }
1588
Daniel Rosenberg80919472016-06-30 19:25:31 -07001589 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1590 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1591 }
1592 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1593 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001594
1595 if (wants_set_active) {
1596 if (next_active == "") {
1597 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001598 std::string current_slot;
1599 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001600 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001601 } else {
1602 wants_set_active = false;
1603 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001604 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001605 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001606 }
1607 }
1608 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001609
Elliott Hughesd6365a72017-05-08 18:04:49 -07001610 std::vector<std::string> args(argv, argv + argc);
1611 while (!args.empty()) {
1612 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001613
Elliott Hughesd6365a72017-05-08 18:04:49 -07001614 if (command == "getvar") {
1615 std::string variable = next_arg(&args);
1616 fb_queue_display(variable.c_str(), variable.c_str());
1617 } else if (command == "erase") {
1618 std::string partition = next_arg(&args);
1619 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001620 std::string partition_type;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001621 if (fb_getvar(transport, std::string("partition-type:") + partition,
1622 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001623 fs_get_generator(partition_type) != nullptr) {
1624 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1625 partition_type.c_str());
1626 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001627
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001628 fb_queue_erase(partition.c_str());
1629 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001630 do_for_partitions(transport, partition, slot_override, erase, true);
1631 } else if (android::base::StartsWith(command, "format")) {
1632 // Parsing for: "format[:[type][:[size]]]"
1633 // Some valid things:
1634 // - select only the size, and leave default fs type:
1635 // format::0x4000000 userdata
1636 // - default fs type and size:
1637 // format userdata
1638 // format:: userdata
1639 std::vector<std::string> pieces = android::base::Split(command, ":");
1640 std::string type_override;
1641 if (pieces.size() > 1) type_override = pieces[1].c_str();
1642 std::string size_override;
1643 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001644
Elliott Hughesd6365a72017-05-08 18:04:49 -07001645 std::string partition = next_arg(&args);
1646
1647 auto format = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001648 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001649 fb_queue_erase(partition.c_str());
1650 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001651 fb_perform_format(transport, partition.c_str(), 0, type_override, size_override,
1652 "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001653 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001654 do_for_partitions(transport, partition.c_str(), slot_override, format, true);
1655 } else if (command == "signature") {
1656 std::string filename = next_arg(&args);
1657 data = load_file(filename.c_str(), &sz);
1658 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001659 if (sz != 256) die("signature must be 256 bytes");
1660 fb_queue_download("signature", data, sz);
1661 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001662 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001663 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001664
1665 if (args.size() == 1) {
1666 std::string what = next_arg(&args);
1667 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001668 wants_reboot = false;
1669 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001670 } else if (what == "emergency") {
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001671 wants_reboot = false;
1672 wants_reboot_emergency = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001673 } else {
1674 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001675 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001676
Elliott Hughesca85df02015-02-25 10:02:00 -08001677 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001678 if (!args.empty()) syntax_error("junk after reboot command");
1679 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001680 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001681 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001682 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001683 } else if (command == "boot") {
1684 std::string kernel = next_arg(&args);
1685 std::string ramdisk;
1686 if (!args.empty()) ramdisk = next_arg(&args);
1687 std::string second_stage;
1688 if (!args.empty()) second_stage = next_arg(&args);
1689
1690 data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001691 fb_queue_download("boot.img", data, sz);
1692 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001693 } else if (command == "flash") {
1694 std::string pname = next_arg(&args);
1695
Elliott Hughes2810d002016-04-25 14:31:18 -07001696 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001697 if (!args.empty()) {
1698 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001699 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001700 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001701 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001702 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001703
1704 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001705 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001706 fb_queue_erase(partition.c_str());
1707 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001708 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001709 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001710 do_for_partitions(transport, pname.c_str(), slot_override, flash, true);
1711 } else if (command == "flash:raw") {
1712 std::string partition = next_arg(&args);
1713 std::string kernel = next_arg(&args);
1714 std::string ramdisk;
1715 if (!args.empty()) ramdisk = next_arg(&args);
1716 std::string second_stage;
1717 if (!args.empty()) second_stage = next_arg(&args);
1718
1719 data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline);
1720 auto flashraw = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001721 fb_queue_flash(partition.c_str(), data, sz);
1722 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001723 do_for_partitions(transport, partition, slot_override, flashraw, true);
1724 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001725 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001726 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1727 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001728 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001729 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001730 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001731 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001732 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001733 bool slot_all = (slot_override == "all");
1734 if (slot_all) {
1735 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1736 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001737 std::string filename = "update.zip";
1738 if (!args.empty()) {
1739 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001740 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001741 do_update(transport, filename.c_str(), slot_override, erase_first,
1742 skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001743 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001744 } else if (command == "set_active") {
1745 std::string slot = verify_slot(transport, next_arg(&args), false);
1746
David Pursell04396f62016-12-15 16:16:10 -08001747 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1748 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1749 // do have slot-suffixes.
1750 std::string var;
1751 if (!fb_getvar(transport, "slot-count", &var) &&
1752 fb_getvar(transport, "slot-suffixes", &var)) {
1753 slot = "_" + slot;
1754 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001755 fb_set_active(slot.c_str());
Elliott Hughesd6365a72017-05-08 18:04:49 -07001756 } else if (command == "stage") {
1757 std::string filename = next_arg(&args);
1758
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001759 struct fastboot_buffer buf;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001760 if (!load_buf(transport, filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
1761 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001762 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001763 fb_queue_download_fd(filename.c_str(), buf.fd, buf.sz);
1764 } else if (command == "get_staged") {
1765 std::string filename = next_arg(&args);
1766 fb_queue_upload(filename.c_str());
1767 } else if (command == "oem") {
1768 do_oem_command(&args);
1769 } else if (command == "flashing") {
1770 if (args.empty()) {
1771 syntax_error("missing 'flashing' command");
1772 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1773 args[0] == "unlock_critical" ||
1774 args[0] == "lock_critical" ||
1775 args[0] == "get_unlock_ability" ||
1776 args[0] == "get_unlock_bootloader_nonce" ||
1777 args[0] == "lock_bootloader")) {
1778 do_oem_command(&args);
1779 } else if (args.size() == 2 && args[0] == "unlock_bootloader") {
1780 do_bypass_unlock_command(&args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001781 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001782 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001783 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001784 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001785 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001786 }
1787 }
1788
1789 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001790 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001791 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001792 if (set_fbe_marker) {
1793 fprintf(stderr, "setting FBE marker...\n");
1794 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1795 if (initial_userdata_dir.empty()) {
1796 return 1;
1797 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001798 fb_perform_format(transport, "userdata", 1, "", "", initial_userdata_dir);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001799 delete_fbemarker_tmpdir(initial_userdata_dir);
1800 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001801 fb_perform_format(transport, "userdata", 1, "", "", "");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001802 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001803
1804 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001805 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001806 fprintf(stderr, "wiping cache...\n");
1807 fb_queue_erase("cache");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001808 fb_perform_format(transport, "cache", 1, "", "", "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001809 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001810 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001811 if (wants_set_active) {
1812 fb_set_active(next_active.c_str());
1813 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001814 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001815 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001816 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001817 } else if (wants_reboot_bootloader) {
1818 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001819 fb_queue_wait_for_disconnect();
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001820 } else if (wants_reboot_emergency) {
1821 fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
1822 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001823 }
1824
David Pursell0b156632015-10-30 11:22:01 -07001825 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001826}