blob: a5f31b495bc0ea38a2f1983c4e0651b6339e7900 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Colin Crossf8387882012-05-24 17:18:41 -070029#define _LARGEFILE64_SOURCE
30
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070031#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <errno.h>
33#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070034#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070035#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070036#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070037#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Colin Crossf8387882012-05-24 17:18:41 -070046
Elliott Hughes2fd45a92015-10-30 11:49:47 -070047#include <base/parseint.h>
Elliott Hughes77c0e662015-11-02 15:51:12 -080048#include <base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070049#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070050#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070052#include <base/strings.h>
53#include <base/parseint.h>
54
Elliott Hughes253c18d2015-03-18 22:47:09 -070055#include "bootimg_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070057#include "fs.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Colin Crossf8387882012-05-24 17:18:41 -070059#ifndef O_BINARY
60#define O_BINARY 0
61#endif
62
Rom Lemarchand622810c2013-06-28 09:54:59 -070063#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
64
Wink Savilleb98762f2011-04-04 17:54:59 -070065char cur_product[FB_RESPONSE_SZ + 1];
66
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067static const char *serial = 0;
68static const char *product = 0;
69static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070071static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070072static int64_t sparse_limit = -1;
73static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074
Elliott Hughesfc797672015-04-07 20:12:50 -070075static unsigned page_size = 2048;
76static unsigned base_addr = 0x10000000;
77static unsigned kernel_offset = 0x00008000;
78static unsigned ramdisk_offset = 0x01000000;
79static unsigned second_offset = 0x00f00000;
80static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080081
Rom Lemarchand622810c2013-06-28 09:54:59 -070082enum fb_buffer_type {
83 FB_BUFFER,
84 FB_BUFFER_SPARSE,
85};
86
87struct fastboot_buffer {
88 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070089 void* data;
90 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070091};
92
93static struct {
94 char img_name[13];
95 char sig_name[13];
96 char part_name[9];
97 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -070098} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -070099 {"boot.img", "boot.sig", "boot", false},
100 {"recovery.img", "recovery.sig", "recovery", true},
101 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700102 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700103};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700104
Elliott Hughesfc797672015-04-07 20:12:50 -0700105static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700107 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108 char path[PATH_MAX + 128];
109
110 if(!strcmp(item,"boot")) {
111 fn = "boot.img";
112 } else if(!strcmp(item,"recovery")) {
113 fn = "recovery.img";
114 } else if(!strcmp(item,"system")) {
115 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700116 } else if(!strcmp(item,"vendor")) {
117 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 } else if(!strcmp(item,"userdata")) {
119 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700120 } else if(!strcmp(item,"cache")) {
121 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 } else if(!strcmp(item,"info")) {
123 fn = "android-info.txt";
124 } else {
125 fprintf(stderr,"unknown partition '%s'\n", item);
126 return 0;
127 }
128
129 if(product) {
130 get_my_path(path);
131 sprintf(path + strlen(path),
132 "../../../target/product/%s/%s", product, fn);
133 return strdup(path);
134 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800135
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 dir = getenv("ANDROID_PRODUCT_OUT");
137 if((dir == 0) || (dir[0] == 0)) {
138 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
139 return 0;
140 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800141
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 sprintf(path, "%s/%s", dir, fn);
143 return strdup(path);
144}
145
Elliott Hughesfc797672015-04-07 20:12:50 -0700146static int64_t get_file_size(int fd) {
147 struct stat sb;
148 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700149}
150
Elliott Hughesfc797672015-04-07 20:12:50 -0700151static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800152 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700153 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154
Elliott Hughesfc797672015-04-07 20:12:50 -0700155 *sz = get_file_size(fd);
156 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700157 goto oops;
158 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159
Elliott Hughesfc797672015-04-07 20:12:50 -0700160 data = (char*) malloc(*sz);
161 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162
Elliott Hughesfc797672015-04-07 20:12:50 -0700163 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 close(fd);
165
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 return data;
167
168oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800169 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 close(fd);
171 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800172 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 return 0;
174}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
Elliott Hughesfc797672015-04-07 20:12:50 -0700176static void* load_file(const char* fn, int64_t* sz) {
177 int fd = open(fn, O_RDONLY | O_BINARY);
178 if (fd == -1) return nullptr;
179 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700180}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
Elliott Hughesfc797672015-04-07 20:12:50 -0700182static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700183 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700184 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700185 return -1;
186 }
187
188 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
189 return -1;
190 }
191
Scott Anderson13081c62012-04-06 12:39:30 -0700192 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700194 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
195 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 return 0;
197}
198
Elliott Hughesfc797672015-04-07 20:12:50 -0700199static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800200 return match_fastboot_with_serial(info, serial);
201}
202
Elliott Hughesfc797672015-04-07 20:12:50 -0700203static int list_devices_callback(usb_ifc_info* info) {
204 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700205 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700206 if (!info->writable) {
207 serial = "no permissions"; // like "adb devices"
208 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 if (!serial[0]) {
210 serial = "????????????";
211 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700212 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700213 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700214 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800215 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700216 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700217 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700218 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700219 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 }
221
222 return -1;
223}
224
Elliott Hughesfc797672015-04-07 20:12:50 -0700225static usb_handle* open_device() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 static usb_handle *usb = 0;
227 int announce = 1;
228
229 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800230
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 for(;;) {
232 usb = usb_open(match_fastboot);
233 if(usb) return usb;
234 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800235 announce = 0;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700236 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700238 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 }
240}
241
Elliott Hughesfc797672015-04-07 20:12:50 -0700242static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 // We don't actually open a USB device here,
244 // just getting our callback called so we can
245 // list all the connected devices.
246 usb_open(list_devices_callback);
247}
248
Elliott Hughesfc797672015-04-07 20:12:50 -0700249static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250 fprintf(stderr,
251/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
252 "usage: fastboot [ <option> ] <command>\n"
253 "\n"
254 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700255 " update <filename> Reflash device from update.zip.\n"
256 " flashall Flash boot, system, vendor, and --\n"
257 " if found -- recovery.\n"
258 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
259 " flashing lock Locks the device. Prevents flashing.\n"
260 " flashing unlock Unlocks the device. Allows flashing\n"
261 " any partition except\n"
262 " bootloader-related partitions.\n"
263 " flashing lock_critical Prevents flashing bootloader-related\n"
264 " partitions.\n"
265 " flashing unlock_critical Enables flashing bootloader-related\n"
266 " partitions.\n"
267 " flashing get_unlock_ability Queries bootloader to see if the\n"
268 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700269 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700270 " unlock nonce.\n"
271 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700272 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700273 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700274 " erase <partition> Erase a flash partition.\n"
275 " format[:[<fs type>][:[<size>]] <partition>\n"
276 " Format a flash partition. Can\n"
277 " override the fs type and/or size\n"
278 " the bootloader reports.\n"
279 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700280 " set_active <suffix> Sets the active slot. If slots are\n"
281 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700282 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
283 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
284 " Create bootimage and flash it.\n"
285 " devices [-l] List all connected devices [with\n"
286 " device paths].\n"
287 " continue Continue with autoboot.\n"
288 " reboot [bootloader] Reboot device [into bootloader].\n"
289 " reboot-bootloader Reboot device into bootloader.\n"
290 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 "\n"
292 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700293 " -w Erase userdata and cache (and format\n"
294 " if supported by partition type).\n"
295 " -u Do not erase partition before\n"
296 " formatting.\n"
297 " -s <specific device> Specify device serial number\n"
298 " or path to device port.\n"
299 " -p <product> Specify product name.\n"
300 " -c <cmdline> Override kernel commandline.\n"
301 " -i <vendor id> Specify a custom USB vendor id.\n"
302 " -b <base_addr> Specify a custom kernel base\n"
303 " address (default: 0x10000000).\n"
304 " -n <page size> Specify the nand page size\n"
305 " (default: 2048).\n"
306 " -S <size>[K|M|G] Automatically sparse files greater\n"
307 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700308 " --slot <suffix> Specify slot suffix to be used if the\n"
309 " device supports slots. This will be\n"
310 " added to all partition names that use\n"
311 " slots. 'all' can be given to refer\n"
312 " to all slots. If this is not given,\n"
313 " slotted partitions will default to\n"
314 " the current active slot.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316}
317
Elliott Hughesfc797672015-04-07 20:12:50 -0700318static void* load_bootable_image(const char* kernel, const char* ramdisk,
319 const char* secondstage, int64_t* sz,
320 const char* cmdline) {
321 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 fprintf(stderr, "no image specified\n");
323 return 0;
324 }
325
Elliott Hughesfc797672015-04-07 20:12:50 -0700326 int64_t ksize;
327 void* kdata = load_file(kernel, &ksize);
328 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800329 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 return 0;
331 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800332
Elliott Hughesfc797672015-04-07 20:12:50 -0700333 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700335 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800336
Elliott Hughesfc797672015-04-07 20:12:50 -0700337 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
339 return 0;
340 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800341
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342 *sz = ksize;
343 return kdata;
344 }
345
Elliott Hughesfc797672015-04-07 20:12:50 -0700346 void* rdata = nullptr;
347 int64_t rsize = 0;
348 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700350 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800351 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 return 0;
353 }
354 }
355
Elliott Hughesfc797672015-04-07 20:12:50 -0700356 void* sdata = nullptr;
357 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200358 if (secondstage) {
359 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700360 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200361 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
362 return 0;
363 }
364 }
365
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700367 int64_t bsize = 0;
368 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800369 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200370 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800371 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700372 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373 fprintf(stderr,"failed to create boot.img\n");
374 return 0;
375 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700376 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
377 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800379
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 return bdata;
381}
382
Elliott Hughesfc797672015-04-07 20:12:50 -0700383static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384{
Yusuke Sato07447542015-06-25 14:39:19 -0700385 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700386 ZipEntry zip_entry;
387 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700388 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000389 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 }
391
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700392 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700394 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700395 if (data == nullptr) {
396 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000397 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 }
399
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700400 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
401 if (error != 0) {
402 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000404 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000406
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407 return data;
408}
409
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700410#if defined(_WIN32)
411
412// TODO: move this to somewhere it can be shared.
413
414#include <windows.h>
415
416// Windows' tmpfile(3) requires administrator rights because
417// it creates temporary files in the root directory.
418static FILE* win32_tmpfile() {
419 char temp_path[PATH_MAX];
420 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
421 if (nchars == 0 || nchars >= sizeof(temp_path)) {
422 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
423 return nullptr;
424 }
425
426 char filename[PATH_MAX];
427 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
428 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
429 return nullptr;
430 }
431
432 return fopen(filename, "w+bTD");
433}
434
435#define tmpfile win32_tmpfile
436
437#endif
438
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700439static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
440 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700441 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700442 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
443 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700444 return -1;
445 }
446
Yusuke Sato07447542015-06-25 14:39:19 -0700447 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700448 ZipEntry zip_entry;
449 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
450 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000451 return -1;
452 }
453
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700454 int fd = fileno(fp);
455 int error = ExtractEntryToFile(zip, &zip_entry, fd);
456 if (error != 0) {
457 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
458 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700459 }
460
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000461 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700462 return fd;
463}
464
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465static char *strip(char *s)
466{
467 int n;
468 while(*s && isspace(*s)) s++;
469 n = strlen(s);
470 while(n-- > 0) {
471 if(!isspace(s[n])) break;
472 s[n] = 0;
473 }
474 return s;
475}
476
477#define MAX_OPTIONS 32
478static int setup_requirement_line(char *name)
479{
480 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700481 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482 unsigned n, count;
483 char *x;
484 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800485
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486 if (!strncmp(name, "reject ", 7)) {
487 name += 7;
488 invert = 1;
489 } else if (!strncmp(name, "require ", 8)) {
490 name += 8;
491 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700492 } else if (!strncmp(name, "require-for-product:", 20)) {
493 // Get the product and point name past it
494 prod = name + 20;
495 name = strchr(name, ' ');
496 if (!name) return -1;
497 *name = 0;
498 name += 1;
499 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 }
501
502 x = strchr(name, '=');
503 if (x == 0) return 0;
504 *x = 0;
505 val[0] = x + 1;
506
507 for(count = 1; count < MAX_OPTIONS; count++) {
508 x = strchr(val[count - 1],'|');
509 if (x == 0) break;
510 *x = 0;
511 val[count] = x + 1;
512 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800513
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514 name = strip(name);
515 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800516
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 name = strip(name);
518 if (name == 0) return -1;
519
Elliott Hughes253c18d2015-03-18 22:47:09 -0700520 const char* var = name;
521 // Work around an unfortunate name mismatch.
522 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523
Elliott Hughes253c18d2015-03-18 22:47:09 -0700524 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 if (out == 0) return -1;
526
527 for(n = 0; n < count; n++) {
528 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700529 if (out[n] == 0) {
530 for(size_t i = 0; i < n; ++i) {
531 free((char*) out[i]);
532 }
533 free(out);
534 return -1;
535 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536 }
537
Elliott Hughes253c18d2015-03-18 22:47:09 -0700538 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539 return 0;
540}
541
Elliott Hughesfc797672015-04-07 20:12:50 -0700542static void setup_requirements(char* data, int64_t sz) {
543 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700545 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546 *s++ = 0;
547 if (setup_requirement_line(data)) {
548 die("out of memory");
549 }
550 data = s;
551 } else {
552 s++;
553 }
554 }
555}
556
Elliott Hughesfc797672015-04-07 20:12:50 -0700557static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 fb_queue_notice("--------------------------------------------");
559 fb_queue_display("version-bootloader", "Bootloader Version...");
560 fb_queue_display("version-baseband", "Baseband Version.....");
561 fb_queue_display("serialno", "Serial Number........");
562 fb_queue_notice("--------------------------------------------");
563}
564
Rom Lemarchand622810c2013-06-28 09:54:59 -0700565static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700566{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700567 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700568 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700569 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700570 }
571
Elliott Hughesfc797672015-04-07 20:12:50 -0700572 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700573 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700574 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700575 }
576
Elliott Hughes253c18d2015-03-18 22:47:09 -0700577 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700578 if (!out_s) {
579 die("Failed to allocate sparse file array\n");
580 }
581
582 files = sparse_file_resparse(s, max_size, out_s, files);
583 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700584 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700585 }
586
587 return out_s;
588}
589
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700590static int64_t get_target_sparse_limit(usb_handle* usb) {
591 std::string max_download_size;
Elliott Hughes2030bac2015-11-03 08:14:43 -0800592 if (!fb_getvar(usb, "max-download-size", &max_download_size) || max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800593 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700594 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700595 }
596
Elliott Hughes77c0e662015-11-02 15:51:12 -0800597 // Some bootloaders (angler, for example) send spurious whitespace too.
598 max_download_size = android::base::Trim(max_download_size);
599
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700600 uint64_t limit;
601 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800602 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700603 return 0;
604 }
605 if (limit > 0) {
606 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
607 }
Colin Crossf8387882012-05-24 17:18:41 -0700608 return limit;
609}
610
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700611static int64_t get_sparse_limit(usb_handle* usb, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700612 int64_t limit;
613
614 if (sparse_limit == 0) {
615 return 0;
616 } else if (sparse_limit > 0) {
617 limit = sparse_limit;
618 } else {
619 if (target_sparse_limit == -1) {
620 target_sparse_limit = get_target_sparse_limit(usb);
621 }
622 if (target_sparse_limit > 0) {
623 limit = target_sparse_limit;
624 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700625 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700626 }
627 }
628
629 if (size > limit) {
630 return limit;
631 }
632
633 return 0;
634}
635
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700636// Until we get lazy inode table init working in make_ext4fs, we need to
637// erase partitions of type ext4 before flashing a filesystem so no stale
638// inodes are left lying around. Otherwise, e2fsck gets very upset.
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800639static bool needs_erase(usb_handle* usb, const char* partition) {
640 std::string partition_type;
641 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
642 return false;
643 }
644 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700645}
646
Elliott Hughesfc797672015-04-07 20:12:50 -0700647static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) {
648 int64_t sz = get_file_size(fd);
649 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000650 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700651 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700652
Elliott Hughesfc797672015-04-07 20:12:50 -0700653 lseek64(fd, 0, SEEK_SET);
654 int64_t limit = get_sparse_limit(usb, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700655 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700656 sparse_file** s = load_sparse_files(fd, limit);
657 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700658 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700659 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700660 buf->type = FB_BUFFER_SPARSE;
661 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700662 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700663 void* data = load_fd(fd, &sz);
664 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700665 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700666 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000667 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700668 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700669
670 return 0;
671}
672
673static int load_buf(usb_handle *usb, const char *fname,
674 struct fastboot_buffer *buf)
675{
676 int fd;
677
678 fd = open(fname, O_RDONLY | O_BINARY);
679 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700680 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700681 }
682
683 return load_buf_fd(usb, fd, buf);
684}
685
686static void flash_buf(const char *pname, struct fastboot_buffer *buf)
687{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700688 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700689
690 switch (buf->type) {
691 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700692 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700693 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700694 int64_t sz = sparse_file_len(*s, true, false);
695 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700696 }
697 break;
698 case FB_BUFFER:
699 fb_queue_flash(pname, buf->data, buf->sz);
700 break;
701 default:
702 die("unknown buffer type: %d", buf->type);
703 }
704}
705
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700706static std::vector<std::string> get_suffixes(usb_handle* usb) {
707 std::vector<std::string> suffixes;
708 std::string suffix_list;
709 if (!fb_getvar(usb, "slot-suffixes", &suffix_list)) {
710 die("Could not get suffixes.\n");
711 }
712 return android::base::Split(suffix_list, ",");
713}
714
715static std::string verify_slot(usb_handle* usb, const char *slot) {
716 if (strcmp(slot, "all") == 0) {
717 return "all";
718 }
719 std::vector<std::string> suffixes = get_suffixes(usb);
720 for (const std::string &suffix : suffixes) {
721 if (suffix == slot)
722 return slot;
723 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800724 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700725 for (const std::string &suffix : suffixes) {
726 fprintf(stderr, "%s\n", suffix.c_str());
727 }
728 exit(1);
729}
730
731static void do_for_partition(usb_handle* usb, const char *part, const char *slot, std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800732 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700733 std::string current_slot;
734
Daniel Rosenberga7974792015-11-11 16:13:13 -0800735 if (!fb_getvar(usb, std::string("has-slot:")+part, &has_slot)) {
736 /* If has-slot is not supported, the answer is no. */
737 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700738 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800739 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700740 if (!slot || slot[0] == 0) {
741 if (!fb_getvar(usb, "current-slot", &current_slot)) {
742 die("Failed to identify current slot.\n");
743 }
744 func(std::string(part) + '-' + current_slot);
745 } else {
746 func(std::string(part) + '-' + slot);
747 }
748 } else {
749 if (force_slot && slot && slot[0]) {
750 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n", part, slot);
751 }
752 func(part);
753 }
754}
755
756/* This function will find the real partition name given a base name, and a slot. If slot is NULL or empty,
757 * it will use the current slot. If slot is "all", it will return a list of all possible partition names.
758 * If force_slot is true, it will fail if a slot is specified, and the given partition does not support slots.
759 */
760static void do_for_partitions(usb_handle* usb, const char *part, const char *slot, std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800761 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700762
763 if (slot && strcmp(slot, "all") == 0) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800764 if (!fb_getvar(usb, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700765 die("Could not check if partition %s has slot.", part);
766 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800767 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700768 std::vector<std::string> suffixes = get_suffixes(usb);
769 for (std::string &suffix : suffixes) {
770 do_for_partition(usb, part, suffix.c_str(), func, force_slot);
771 }
772 } else {
773 do_for_partition(usb, part, "", func, force_slot);
774 }
775 } else {
776 do_for_partition(usb, part, slot, func, force_slot);
777 }
778}
779
Elliott Hughesfc797672015-04-07 20:12:50 -0700780static void do_flash(usb_handle* usb, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700781 struct fastboot_buffer buf;
782
783 if (load_buf(usb, fname, &buf)) {
784 die("cannot load '%s'", fname);
785 }
786 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700787}
788
Elliott Hughesfc797672015-04-07 20:12:50 -0700789static void do_update_signature(ZipArchiveHandle zip, char* fn) {
790 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700791 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700792 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800793 fb_queue_download("signature", data, sz);
794 fb_queue_command("signature", "installing signature");
795}
796
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700797static void do_update(usb_handle* usb, const char* filename, const char* slot_override, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798 queue_info_dump();
799
Wink Savilleb98762f2011-04-04 17:54:59 -0700800 fb_queue_query_save("product", cur_product, sizeof(cur_product));
801
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700802 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700803 int error = OpenArchive(filename, &zip);
804 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100805 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700806 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700807 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800808
Elliott Hughesfc797672015-04-07 20:12:50 -0700809 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700810 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700811 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100812 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700813 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800814 }
815
Elliott Hughes253c18d2015-03-18 22:47:09 -0700816 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800817
Elliott Hughesacdbe922015-06-02 21:37:05 -0700818 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700819 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700820 if (fd == -1) {
821 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700822 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700823 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100824 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700825 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700826 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700827 fastboot_buffer buf;
828 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700829 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700830
831 auto update = [&](const std::string &partition) {
832 do_update_signature(zip, images[i].sig_name);
833 if (erase_first && needs_erase(usb, partition.c_str())) {
834 fb_queue_erase(partition.c_str());
835 }
836 flash_buf(partition.c_str(), &buf);
837 /* not closing the fd here since the sparse code keeps the fd around
838 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
839 * program exits.
840 */
841 };
842 do_for_partitions(usb, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800843 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700844
845 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800846}
847
Elliott Hughesfc797672015-04-07 20:12:50 -0700848static void do_send_signature(char* fn) {
849 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800850 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700851
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800852 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800853
Elliott Hughesfc797672015-04-07 20:12:50 -0700854 strcpy(xtn, ".sig");
855
856 int64_t sz;
857 void* data = load_file(fn, &sz);
858 strcpy(xtn, ".img");
859 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800860 fb_queue_download("signature", data, sz);
861 fb_queue_command("signature", "installing signature");
862}
863
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700864static void do_flashall(usb_handle* usb, const char *slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800865 queue_info_dump();
866
Wink Savilleb98762f2011-04-04 17:54:59 -0700867 fb_queue_query_save("product", cur_product, sizeof(cur_product));
868
Elliott Hughes253c18d2015-03-18 22:47:09 -0700869 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700870 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800871
Elliott Hughesfc797672015-04-07 20:12:50 -0700872 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700873 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700874 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700875
876 setup_requirements(reinterpret_cast<char*>(data), sz);
877
878 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700879 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700880 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700881 if (load_buf(usb, fname, &buf)) {
882 if (images[i].is_optional)
883 continue;
884 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700885 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700886
887 auto flashall = [&](const std::string &partition) {
888 do_send_signature(fname);
889 if (erase_first && needs_erase(usb, partition.c_str())) {
890 fb_queue_erase(partition.c_str());
891 }
892 flash_buf(partition.c_str(), &buf);
893 };
894 do_for_partitions(usb, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800895 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800896}
897
898#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800899#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800900
Elliott Hughes45be42e2015-09-02 20:15:48 -0700901static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -0700902{
Patrick Tjin51e8b032015-09-01 08:15:23 -0700903 if (argc <= 2) return 0;
904 skip(2);
905
906 /*
907 * Process unlock_bootloader, we have to load the message file
908 * and send that to the remote device.
909 */
910 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -0700911
912 int64_t sz;
913 void* data = load_file(*argv, &sz);
914 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -0700915 fb_queue_download("unlock_message", data, sz);
916 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
917 skip(1);
918 return 0;
919}
920
Elliott Hughes45be42e2015-09-02 20:15:48 -0700921static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800922{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800923 char command[256];
924 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800925
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800926 command[0] = 0;
927 while(1) {
928 strcat(command,*argv);
929 skip(1);
930 if(argc == 0) break;
931 strcat(command," ");
932 }
933
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800934 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800935 return 0;
936}
937
Colin Crossf8387882012-05-24 17:18:41 -0700938static int64_t parse_num(const char *arg)
939{
940 char *endptr;
941 unsigned long long num;
942
943 num = strtoull(arg, &endptr, 0);
944 if (endptr == arg) {
945 return -1;
946 }
947
948 if (*endptr == 'k' || *endptr == 'K') {
949 if (num >= (-1ULL) / 1024) {
950 return -1;
951 }
952 num *= 1024LL;
953 endptr++;
954 } else if (*endptr == 'm' || *endptr == 'M') {
955 if (num >= (-1ULL) / (1024 * 1024)) {
956 return -1;
957 }
958 num *= 1024LL * 1024LL;
959 endptr++;
960 } else if (*endptr == 'g' || *endptr == 'G') {
961 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
962 return -1;
963 }
964 num *= 1024LL * 1024LL * 1024LL;
965 endptr++;
966 }
967
968 if (*endptr != '\0') {
969 return -1;
970 }
971
972 if (num > INT64_MAX) {
973 return -1;
974 }
975
976 return num;
977}
978
Elliott Hughesfc797672015-04-07 20:12:50 -0700979static void fb_perform_format(usb_handle* usb,
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700980 const char* partition, int skip_if_not_supported,
981 const char* type_override, const char* size_override) {
982 std::string partition_type, partition_size;
983
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700984 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700985 const char* errMsg = nullptr;
986 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700987 int fd;
988
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700989 unsigned int limit = INT_MAX;
990 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700991 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700992 }
993 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700994 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700995 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700996
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700997 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700998 errMsg = "Can't determine partition type.\n";
999 goto failed;
1000 }
JP Abgrall7e859742014-05-06 15:14:15 -07001001 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001002 if (partition_type != type_override) {
1003 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1004 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001005 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001006 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001007 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001008
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001009 if (!fb_getvar(usb, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001010 errMsg = "Unable to get partition size\n";
1011 goto failed;
1012 }
JP Abgrall7e859742014-05-06 15:14:15 -07001013 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001014 if (partition_size != size_override) {
1015 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1016 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001017 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001018 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001019 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001020
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001021 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001022 if (!gen) {
1023 if (skip_if_not_supported) {
1024 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001025 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001026 return;
1027 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001028 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1029 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001030 return;
1031 }
1032
Elliott Hughes2030bac2015-11-03 08:14:43 -08001033 // Some bootloaders (hammerhead, for example) use implicit hex.
1034 // This code used to use strtol with base 16.
1035 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
1036
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001037 int64_t size;
1038 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1039 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1040 return;
1041 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001042
1043 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001044 if (fs_generator_generate(gen, fd, size)) {
1045 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001046 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001047 return;
1048 }
1049
1050 if (load_buf_fd(usb, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001051 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001052 close(fd);
1053 return;
1054 }
1055 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001056 return;
1057
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001058failed:
1059 if (skip_if_not_supported) {
1060 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001061 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001062 }
1063 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1064}
1065
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001066int main(int argc, char **argv)
1067{
1068 int wants_wipe = 0;
1069 int wants_reboot = 0;
1070 int wants_reboot_bootloader = 0;
Elliott Hughesfc797672015-04-07 20:12:50 -07001071 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001072 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001073 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001074 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001075 std::string slot_override;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001076
JP Abgrall7b8970c2013-03-07 17:06:41 -08001077 const struct option longopts[] = {
1078 {"base", required_argument, 0, 'b'},
1079 {"kernel_offset", required_argument, 0, 'k'},
1080 {"page_size", required_argument, 0, 'n'},
1081 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001082 {"tags_offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001083 {"help", no_argument, 0, 'h'},
1084 {"unbuffered", no_argument, 0, 0},
1085 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001086 {"slot", required_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001087 {0, 0, 0, 0}
1088 };
Colin Cross8879f982012-05-22 17:53:34 -07001089
1090 serial = getenv("ANDROID_SERIAL");
1091
1092 while (1) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001093 int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -07001094 if (c < 0) {
1095 break;
1096 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001097 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001098 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -07001099 case 'b':
1100 base_addr = strtoul(optarg, 0, 16);
1101 break;
Colin Cross8879f982012-05-22 17:53:34 -07001102 case 'c':
1103 cmdline = optarg;
1104 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001105 case 'h':
1106 usage();
1107 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001108 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001109 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001110 unsigned long val;
1111
1112 val = strtoul(optarg, &endptr, 0);
1113 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1114 die("invalid vendor id '%s'", optarg);
1115 vendor_id = (unsigned short)val;
1116 break;
1117 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001118 case 'k':
1119 kernel_offset = strtoul(optarg, 0, 16);
1120 break;
1121 case 'l':
1122 long_listing = 1;
1123 break;
1124 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001125 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001126 if (!page_size) die("invalid page size");
1127 break;
1128 case 'p':
1129 product = optarg;
1130 break;
1131 case 'r':
1132 ramdisk_offset = strtoul(optarg, 0, 16);
1133 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001134 case 't':
1135 tags_offset = strtoul(optarg, 0, 16);
1136 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001137 case 's':
1138 serial = optarg;
1139 break;
1140 case 'S':
1141 sparse_limit = parse_num(optarg);
1142 if (sparse_limit < 0) {
1143 die("invalid sparse limit");
1144 }
1145 break;
1146 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001147 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001148 break;
1149 case 'w':
1150 wants_wipe = 1;
1151 break;
Colin Cross8879f982012-05-22 17:53:34 -07001152 case '?':
1153 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001154 case 0:
1155 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001156 setvbuf(stdout, nullptr, _IONBF, 0);
1157 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001158 } else if (strcmp("version", longopts[longindex].name) == 0) {
1159 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1160 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001161 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1162 slot_override = std::string(optarg);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001163 }
1164 break;
Colin Cross8879f982012-05-22 17:53:34 -07001165 default:
1166 abort();
1167 }
1168 }
1169
1170 argc -= optind;
1171 argv += optind;
1172
1173 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001174 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001175 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001176 }
1177
Colin Cross8fb6e062012-07-24 16:36:41 -07001178 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001179 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001180 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001181 return 0;
1182 }
1183
Colin Crossc7b75dc2012-08-29 18:17:06 -07001184 if (argc > 0 && !strcmp(*argv, "help")) {
1185 usage();
1186 return 0;
1187 }
1188
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001189 usb_handle* usb = open_device();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001190 if (slot_override != "")
1191 slot_override = verify_slot(usb, slot_override.c_str());
Elliott Hughes31dbed72009-10-07 15:38:53 -07001192
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001193 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001194 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001195 require(2);
1196 fb_queue_display(argv[1], argv[1]);
1197 skip(2);
1198 } else if(!strcmp(*argv, "erase")) {
1199 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001200
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001201 auto erase = [&](const std::string &partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001202 std::string partition_type;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001203 if (fb_getvar(usb, std::string("partition-type:") + argv[1], &partition_type) &&
1204 fs_get_generator(partition_type) != nullptr) {
1205 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1206 partition_type.c_str());
1207 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001208
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001209 fb_queue_erase(partition.c_str());
1210 };
1211 do_for_partitions(usb, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001212 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001213 } else if(!strncmp(*argv, "format", strlen("format"))) {
1214 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001215 char *type_override = nullptr;
1216 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001217 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001218 /*
1219 * Parsing for: "format[:[type][:[size]]]"
1220 * Some valid things:
1221 * - select ontly the size, and leave default fs type:
1222 * format::0x4000000 userdata
1223 * - default fs type and size:
1224 * format userdata
1225 * format:: userdata
1226 */
1227 overrides = strchr(*argv, ':');
1228 if (overrides) {
1229 overrides++;
1230 size_override = strchr(overrides, ':');
1231 if (size_override) {
1232 size_override[0] = '\0';
1233 size_override++;
1234 }
1235 type_override = overrides;
1236 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001237 if (type_override && !type_override[0]) type_override = nullptr;
1238 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001239
1240 auto format = [&](const std::string &partition) {
1241 if (erase_first && needs_erase(usb, partition.c_str())) {
1242 fb_queue_erase(partition.c_str());
1243 }
1244 fb_perform_format(usb, partition.c_str(), 0, type_override, size_override);
1245 };
1246 do_for_partitions(usb, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001247 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001248 } else if(!strcmp(*argv, "signature")) {
1249 require(2);
1250 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001251 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001252 if (sz != 256) die("signature must be 256 bytes");
1253 fb_queue_download("signature", data, sz);
1254 fb_queue_command("signature", "installing signature");
1255 skip(2);
1256 } else if(!strcmp(*argv, "reboot")) {
1257 wants_reboot = 1;
1258 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001259 if (argc > 0) {
1260 if (!strcmp(*argv, "bootloader")) {
1261 wants_reboot = 0;
1262 wants_reboot_bootloader = 1;
1263 skip(1);
1264 }
1265 }
1266 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001267 } else if(!strcmp(*argv, "reboot-bootloader")) {
1268 wants_reboot_bootloader = 1;
1269 skip(1);
1270 } else if (!strcmp(*argv, "continue")) {
1271 fb_queue_command("continue", "resuming boot");
1272 skip(1);
1273 } else if(!strcmp(*argv, "boot")) {
1274 char *kname = 0;
1275 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001276 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001277 skip(1);
1278 if (argc > 0) {
1279 kname = argv[0];
1280 skip(1);
1281 }
1282 if (argc > 0) {
1283 rname = argv[0];
1284 skip(1);
1285 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001286 if (argc > 0) {
1287 sname = argv[0];
1288 skip(1);
1289 }
1290 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001291 if (data == 0) return 1;
1292 fb_queue_download("boot.img", data, sz);
1293 fb_queue_command("boot", "booting");
1294 } else if(!strcmp(*argv, "flash")) {
1295 char *pname = argv[1];
1296 char *fname = 0;
1297 require(2);
1298 if (argc > 2) {
1299 fname = argv[2];
1300 skip(3);
1301 } else {
1302 fname = find_item(pname, product);
1303 skip(2);
1304 }
1305 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001306
1307 auto flash = [&](const std::string &partition) {
1308 if (erase_first && needs_erase(usb, partition.c_str())) {
1309 fb_queue_erase(partition.c_str());
1310 }
1311 do_flash(usb, partition.c_str(), fname);
1312 };
1313 do_for_partitions(usb, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001314 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001315 char *kname = argv[2];
1316 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001317 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001318 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001319 skip(3);
1320 if (argc > 0) {
1321 rname = argv[0];
1322 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001323 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001324 if (argc > 0) {
1325 sname = argv[0];
1326 skip(1);
1327 }
1328 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001329 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001330 auto flashraw = [&](const std::string &partition) {
1331 fb_queue_flash(partition.c_str(), data, sz);
1332 };
1333 do_for_partitions(usb, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001334 } else if(!strcmp(*argv, "flashall")) {
1335 skip(1);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001336 do_flashall(usb, slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001337 wants_reboot = 1;
1338 } else if(!strcmp(*argv, "update")) {
1339 if (argc > 1) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001340 do_update(usb, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001341 skip(2);
1342 } else {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001343 do_update(usb, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001344 skip(1);
1345 }
1346 wants_reboot = 1;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001347 } else if(!strcmp(*argv, "set_active")) {
1348 require(2);
1349 fb_set_active(argv[1]);
1350 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001351 } else if(!strcmp(*argv, "oem")) {
1352 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001353 } else if(!strcmp(*argv, "flashing")) {
1354 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1355 !strcmp(*(argv+1), "lock") ||
1356 !strcmp(*(argv+1), "unlock_critical") ||
1357 !strcmp(*(argv+1), "lock_critical") ||
1358 !strcmp(*(argv+1), "get_unlock_ability") ||
1359 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1360 !strcmp(*(argv+1), "lock_bootloader"))) {
1361 argc = do_oem_command(argc, argv);
1362 } else
1363 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1364 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001365 } else {
1366 usage();
1367 return 1;
1368 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001369 } else {
1370 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001371 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001372 }
1373 }
1374
1375 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001376 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001377 fb_queue_erase("userdata");
Elliott Hughesfc797672015-04-07 20:12:50 -07001378 fb_perform_format(usb, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001379
1380 std::string cache_type;
1381 if (fb_getvar(usb, "partition-type:cache", &cache_type) && !cache_type.empty()) {
1382 fprintf(stderr, "wiping cache...\n");
1383 fb_queue_erase("cache");
1384 fb_perform_format(usb, "cache", 1, nullptr, nullptr);
1385 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001386 }
1387 if (wants_reboot) {
1388 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001389 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001390 } else if (wants_reboot_bootloader) {
1391 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001392 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001393 }
1394
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001395 return fb_execute_queue(usb) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001396}