blob: 6e01446df154b4a9dc4e020a0549e0c0bb9a984f [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"
280 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
281 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
282 " Create bootimage and flash it.\n"
283 " devices [-l] List all connected devices [with\n"
284 " device paths].\n"
285 " continue Continue with autoboot.\n"
286 " reboot [bootloader] Reboot device [into bootloader].\n"
287 " reboot-bootloader Reboot device into bootloader.\n"
288 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 "\n"
290 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700291 " -w Erase userdata and cache (and format\n"
292 " if supported by partition type).\n"
293 " -u Do not erase partition before\n"
294 " formatting.\n"
295 " -s <specific device> Specify device serial number\n"
296 " or path to device port.\n"
297 " -p <product> Specify product name.\n"
298 " -c <cmdline> Override kernel commandline.\n"
299 " -i <vendor id> Specify a custom USB vendor id.\n"
300 " -b <base_addr> Specify a custom kernel base\n"
301 " address (default: 0x10000000).\n"
302 " -n <page size> Specify the nand page size\n"
303 " (default: 2048).\n"
304 " -S <size>[K|M|G] Automatically sparse files greater\n"
305 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700306 " --slot <suffix> Specify slot suffix to be used if the\n"
307 " device supports slots. This will be\n"
308 " added to all partition names that use\n"
309 " slots. 'all' can be given to refer\n"
310 " to all slots. If this is not given,\n"
311 " slotted partitions will default to\n"
312 " the current active slot.\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800313 " --set_active[=<suffix>] Sets the active slot. If no suffix is\n"
314 " provided, this will default to the value\n"
315 " given by --slot. If slots are not\n"
316 " supported, this does nothing.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318}
Elliott Hughesfc797672015-04-07 20:12:50 -0700319static void* load_bootable_image(const char* kernel, const char* ramdisk,
320 const char* secondstage, int64_t* sz,
321 const char* cmdline) {
322 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 fprintf(stderr, "no image specified\n");
324 return 0;
325 }
326
Elliott Hughesfc797672015-04-07 20:12:50 -0700327 int64_t ksize;
328 void* kdata = load_file(kernel, &ksize);
329 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800330 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 return 0;
332 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800333
Elliott Hughesfc797672015-04-07 20:12:50 -0700334 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700336 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800337
Elliott Hughesfc797672015-04-07 20:12:50 -0700338 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
340 return 0;
341 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800342
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 *sz = ksize;
344 return kdata;
345 }
346
Elliott Hughesfc797672015-04-07 20:12:50 -0700347 void* rdata = nullptr;
348 int64_t rsize = 0;
349 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700351 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800352 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 return 0;
354 }
355 }
356
Elliott Hughesfc797672015-04-07 20:12:50 -0700357 void* sdata = nullptr;
358 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200359 if (secondstage) {
360 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700361 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200362 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
363 return 0;
364 }
365 }
366
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700368 int64_t bsize = 0;
369 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800370 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200371 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800372 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700373 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 fprintf(stderr,"failed to create boot.img\n");
375 return 0;
376 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700377 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
378 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800380
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381 return bdata;
382}
383
Elliott Hughesfc797672015-04-07 20:12:50 -0700384static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385{
Yusuke Sato07447542015-06-25 14:39:19 -0700386 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700387 ZipEntry zip_entry;
388 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700389 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000390 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391 }
392
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700393 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700395 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700396 if (data == nullptr) {
397 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000398 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399 }
400
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700401 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
402 if (error != 0) {
403 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000405 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000407
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408 return data;
409}
410
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700411#if defined(_WIN32)
412
413// TODO: move this to somewhere it can be shared.
414
415#include <windows.h>
416
417// Windows' tmpfile(3) requires administrator rights because
418// it creates temporary files in the root directory.
419static FILE* win32_tmpfile() {
420 char temp_path[PATH_MAX];
421 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
422 if (nchars == 0 || nchars >= sizeof(temp_path)) {
423 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
424 return nullptr;
425 }
426
427 char filename[PATH_MAX];
428 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
429 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
430 return nullptr;
431 }
432
433 return fopen(filename, "w+bTD");
434}
435
436#define tmpfile win32_tmpfile
437
438#endif
439
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700440static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
441 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700442 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700443 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
444 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700445 return -1;
446 }
447
Yusuke Sato07447542015-06-25 14:39:19 -0700448 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700449 ZipEntry zip_entry;
450 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
451 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000452 return -1;
453 }
454
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700455 int fd = fileno(fp);
456 int error = ExtractEntryToFile(zip, &zip_entry, fd);
457 if (error != 0) {
458 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
459 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700460 }
461
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000462 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700463 return fd;
464}
465
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466static char *strip(char *s)
467{
468 int n;
469 while(*s && isspace(*s)) s++;
470 n = strlen(s);
471 while(n-- > 0) {
472 if(!isspace(s[n])) break;
473 s[n] = 0;
474 }
475 return s;
476}
477
478#define MAX_OPTIONS 32
479static int setup_requirement_line(char *name)
480{
481 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700482 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 unsigned n, count;
484 char *x;
485 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800486
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 if (!strncmp(name, "reject ", 7)) {
488 name += 7;
489 invert = 1;
490 } else if (!strncmp(name, "require ", 8)) {
491 name += 8;
492 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700493 } else if (!strncmp(name, "require-for-product:", 20)) {
494 // Get the product and point name past it
495 prod = name + 20;
496 name = strchr(name, ' ');
497 if (!name) return -1;
498 *name = 0;
499 name += 1;
500 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 }
502
503 x = strchr(name, '=');
504 if (x == 0) return 0;
505 *x = 0;
506 val[0] = x + 1;
507
508 for(count = 1; count < MAX_OPTIONS; count++) {
509 x = strchr(val[count - 1],'|');
510 if (x == 0) break;
511 *x = 0;
512 val[count] = x + 1;
513 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800514
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 name = strip(name);
516 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800517
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 name = strip(name);
519 if (name == 0) return -1;
520
Elliott Hughes253c18d2015-03-18 22:47:09 -0700521 const char* var = name;
522 // Work around an unfortunate name mismatch.
523 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524
Elliott Hughes253c18d2015-03-18 22:47:09 -0700525 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 if (out == 0) return -1;
527
528 for(n = 0; n < count; n++) {
529 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700530 if (out[n] == 0) {
531 for(size_t i = 0; i < n; ++i) {
532 free((char*) out[i]);
533 }
534 free(out);
535 return -1;
536 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537 }
538
Elliott Hughes253c18d2015-03-18 22:47:09 -0700539 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540 return 0;
541}
542
Elliott Hughesfc797672015-04-07 20:12:50 -0700543static void setup_requirements(char* data, int64_t sz) {
544 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700546 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800547 *s++ = 0;
548 if (setup_requirement_line(data)) {
549 die("out of memory");
550 }
551 data = s;
552 } else {
553 s++;
554 }
555 }
556}
557
Elliott Hughesfc797672015-04-07 20:12:50 -0700558static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800559 fb_queue_notice("--------------------------------------------");
560 fb_queue_display("version-bootloader", "Bootloader Version...");
561 fb_queue_display("version-baseband", "Baseband Version.....");
562 fb_queue_display("serialno", "Serial Number........");
563 fb_queue_notice("--------------------------------------------");
564}
565
Rom Lemarchand622810c2013-06-28 09:54:59 -0700566static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700567{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700568 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700569 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700570 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700571 }
572
Elliott Hughesfc797672015-04-07 20:12:50 -0700573 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700574 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700575 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700576 }
577
Elliott Hughes253c18d2015-03-18 22:47:09 -0700578 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700579 if (!out_s) {
580 die("Failed to allocate sparse file array\n");
581 }
582
583 files = sparse_file_resparse(s, max_size, out_s, files);
584 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700585 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700586 }
587
588 return out_s;
589}
590
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700591static int64_t get_target_sparse_limit(usb_handle* usb) {
592 std::string max_download_size;
Elliott Hughes2030bac2015-11-03 08:14:43 -0800593 if (!fb_getvar(usb, "max-download-size", &max_download_size) || max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800594 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700595 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700596 }
597
Elliott Hughes77c0e662015-11-02 15:51:12 -0800598 // Some bootloaders (angler, for example) send spurious whitespace too.
599 max_download_size = android::base::Trim(max_download_size);
600
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700601 uint64_t limit;
602 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800603 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700604 return 0;
605 }
606 if (limit > 0) {
607 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
608 }
Colin Crossf8387882012-05-24 17:18:41 -0700609 return limit;
610}
611
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700612static int64_t get_sparse_limit(usb_handle* usb, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700613 int64_t limit;
614
615 if (sparse_limit == 0) {
616 return 0;
617 } else if (sparse_limit > 0) {
618 limit = sparse_limit;
619 } else {
620 if (target_sparse_limit == -1) {
621 target_sparse_limit = get_target_sparse_limit(usb);
622 }
623 if (target_sparse_limit > 0) {
624 limit = target_sparse_limit;
625 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700626 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700627 }
628 }
629
630 if (size > limit) {
631 return limit;
632 }
633
634 return 0;
635}
636
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700637// Until we get lazy inode table init working in make_ext4fs, we need to
638// erase partitions of type ext4 before flashing a filesystem so no stale
639// inodes are left lying around. Otherwise, e2fsck gets very upset.
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800640static bool needs_erase(usb_handle* usb, const char* partition) {
641 std::string partition_type;
642 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
643 return false;
644 }
645 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700646}
647
Elliott Hughesfc797672015-04-07 20:12:50 -0700648static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) {
649 int64_t sz = get_file_size(fd);
650 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000651 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700652 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700653
Elliott Hughesfc797672015-04-07 20:12:50 -0700654 lseek64(fd, 0, SEEK_SET);
655 int64_t limit = get_sparse_limit(usb, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700656 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700657 sparse_file** s = load_sparse_files(fd, limit);
658 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700659 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700660 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700661 buf->type = FB_BUFFER_SPARSE;
662 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700663 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700664 void* data = load_fd(fd, &sz);
665 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700666 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700667 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000668 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700669 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700670
671 return 0;
672}
673
674static int load_buf(usb_handle *usb, const char *fname,
675 struct fastboot_buffer *buf)
676{
677 int fd;
678
679 fd = open(fname, O_RDONLY | O_BINARY);
680 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700681 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700682 }
683
684 return load_buf_fd(usb, fd, buf);
685}
686
687static void flash_buf(const char *pname, struct fastboot_buffer *buf)
688{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700689 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700690
691 switch (buf->type) {
692 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700693 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700694 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700695 int64_t sz = sparse_file_len(*s, true, false);
696 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700697 }
698 break;
699 case FB_BUFFER:
700 fb_queue_flash(pname, buf->data, buf->sz);
701 break;
702 default:
703 die("unknown buffer type: %d", buf->type);
704 }
705}
706
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700707static std::vector<std::string> get_suffixes(usb_handle* usb) {
708 std::vector<std::string> suffixes;
709 std::string suffix_list;
710 if (!fb_getvar(usb, "slot-suffixes", &suffix_list)) {
711 die("Could not get suffixes.\n");
712 }
713 return android::base::Split(suffix_list, ",");
714}
715
716static std::string verify_slot(usb_handle* usb, const char *slot) {
717 if (strcmp(slot, "all") == 0) {
718 return "all";
719 }
720 std::vector<std::string> suffixes = get_suffixes(usb);
721 for (const std::string &suffix : suffixes) {
722 if (suffix == slot)
723 return slot;
724 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800725 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700726 for (const std::string &suffix : suffixes) {
727 fprintf(stderr, "%s\n", suffix.c_str());
728 }
729 exit(1);
730}
731
732static 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 -0800733 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700734 std::string current_slot;
735
Daniel Rosenberga7974792015-11-11 16:13:13 -0800736 if (!fb_getvar(usb, std::string("has-slot:")+part, &has_slot)) {
737 /* If has-slot is not supported, the answer is no. */
738 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700739 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800740 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700741 if (!slot || slot[0] == 0) {
742 if (!fb_getvar(usb, "current-slot", &current_slot)) {
743 die("Failed to identify current slot.\n");
744 }
745 func(std::string(part) + '-' + current_slot);
746 } else {
747 func(std::string(part) + '-' + slot);
748 }
749 } else {
750 if (force_slot && slot && slot[0]) {
751 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n", part, slot);
752 }
753 func(part);
754 }
755}
756
757/* This function will find the real partition name given a base name, and a slot. If slot is NULL or empty,
758 * it will use the current slot. If slot is "all", it will return a list of all possible partition names.
759 * If force_slot is true, it will fail if a slot is specified, and the given partition does not support slots.
760 */
761static 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 -0800762 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700763
764 if (slot && strcmp(slot, "all") == 0) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800765 if (!fb_getvar(usb, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700766 die("Could not check if partition %s has slot.", part);
767 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800768 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700769 std::vector<std::string> suffixes = get_suffixes(usb);
770 for (std::string &suffix : suffixes) {
771 do_for_partition(usb, part, suffix.c_str(), func, force_slot);
772 }
773 } else {
774 do_for_partition(usb, part, "", func, force_slot);
775 }
776 } else {
777 do_for_partition(usb, part, slot, func, force_slot);
778 }
779}
780
Elliott Hughesfc797672015-04-07 20:12:50 -0700781static void do_flash(usb_handle* usb, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700782 struct fastboot_buffer buf;
783
784 if (load_buf(usb, fname, &buf)) {
785 die("cannot load '%s'", fname);
786 }
787 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700788}
789
Elliott Hughesfc797672015-04-07 20:12:50 -0700790static void do_update_signature(ZipArchiveHandle zip, char* fn) {
791 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700792 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700793 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800794 fb_queue_download("signature", data, sz);
795 fb_queue_command("signature", "installing signature");
796}
797
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700798static 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 -0800799 queue_info_dump();
800
Wink Savilleb98762f2011-04-04 17:54:59 -0700801 fb_queue_query_save("product", cur_product, sizeof(cur_product));
802
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700803 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700804 int error = OpenArchive(filename, &zip);
805 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100806 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700807 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700808 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809
Elliott Hughesfc797672015-04-07 20:12:50 -0700810 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700811 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700812 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100813 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700814 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800815 }
816
Elliott Hughes253c18d2015-03-18 22:47:09 -0700817 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800818
Elliott Hughesacdbe922015-06-02 21:37:05 -0700819 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700820 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700821 if (fd == -1) {
822 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700823 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700824 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100825 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700826 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700827 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700828 fastboot_buffer buf;
829 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700830 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700831
832 auto update = [&](const std::string &partition) {
833 do_update_signature(zip, images[i].sig_name);
834 if (erase_first && needs_erase(usb, partition.c_str())) {
835 fb_queue_erase(partition.c_str());
836 }
837 flash_buf(partition.c_str(), &buf);
838 /* not closing the fd here since the sparse code keeps the fd around
839 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
840 * program exits.
841 */
842 };
843 do_for_partitions(usb, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800844 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700845
846 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800847}
848
Elliott Hughesfc797672015-04-07 20:12:50 -0700849static void do_send_signature(char* fn) {
850 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800851 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700852
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800853 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800854
Elliott Hughesfc797672015-04-07 20:12:50 -0700855 strcpy(xtn, ".sig");
856
857 int64_t sz;
858 void* data = load_file(fn, &sz);
859 strcpy(xtn, ".img");
860 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800861 fb_queue_download("signature", data, sz);
862 fb_queue_command("signature", "installing signature");
863}
864
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700865static void do_flashall(usb_handle* usb, const char *slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800866 queue_info_dump();
867
Wink Savilleb98762f2011-04-04 17:54:59 -0700868 fb_queue_query_save("product", cur_product, sizeof(cur_product));
869
Elliott Hughes253c18d2015-03-18 22:47:09 -0700870 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700871 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800872
Elliott Hughesfc797672015-04-07 20:12:50 -0700873 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700874 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700875 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700876
877 setup_requirements(reinterpret_cast<char*>(data), sz);
878
879 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700880 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700881 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700882 if (load_buf(usb, fname, &buf)) {
883 if (images[i].is_optional)
884 continue;
885 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700886 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700887
888 auto flashall = [&](const std::string &partition) {
889 do_send_signature(fname);
890 if (erase_first && needs_erase(usb, partition.c_str())) {
891 fb_queue_erase(partition.c_str());
892 }
893 flash_buf(partition.c_str(), &buf);
894 };
895 do_for_partitions(usb, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800896 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800897}
898
899#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800900#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800901
Elliott Hughes45be42e2015-09-02 20:15:48 -0700902static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -0700903{
Patrick Tjin51e8b032015-09-01 08:15:23 -0700904 if (argc <= 2) return 0;
905 skip(2);
906
907 /*
908 * Process unlock_bootloader, we have to load the message file
909 * and send that to the remote device.
910 */
911 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -0700912
913 int64_t sz;
914 void* data = load_file(*argv, &sz);
915 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -0700916 fb_queue_download("unlock_message", data, sz);
917 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
918 skip(1);
919 return 0;
920}
921
Elliott Hughes45be42e2015-09-02 20:15:48 -0700922static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800923{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800924 char command[256];
925 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800926
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800927 command[0] = 0;
928 while(1) {
929 strcat(command,*argv);
930 skip(1);
931 if(argc == 0) break;
932 strcat(command," ");
933 }
934
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800935 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800936 return 0;
937}
938
Colin Crossf8387882012-05-24 17:18:41 -0700939static int64_t parse_num(const char *arg)
940{
941 char *endptr;
942 unsigned long long num;
943
944 num = strtoull(arg, &endptr, 0);
945 if (endptr == arg) {
946 return -1;
947 }
948
949 if (*endptr == 'k' || *endptr == 'K') {
950 if (num >= (-1ULL) / 1024) {
951 return -1;
952 }
953 num *= 1024LL;
954 endptr++;
955 } else if (*endptr == 'm' || *endptr == 'M') {
956 if (num >= (-1ULL) / (1024 * 1024)) {
957 return -1;
958 }
959 num *= 1024LL * 1024LL;
960 endptr++;
961 } else if (*endptr == 'g' || *endptr == 'G') {
962 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
963 return -1;
964 }
965 num *= 1024LL * 1024LL * 1024LL;
966 endptr++;
967 }
968
969 if (*endptr != '\0') {
970 return -1;
971 }
972
973 if (num > INT64_MAX) {
974 return -1;
975 }
976
977 return num;
978}
979
Elliott Hughesfc797672015-04-07 20:12:50 -0700980static void fb_perform_format(usb_handle* usb,
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700981 const char* partition, int skip_if_not_supported,
982 const char* type_override, const char* size_override) {
983 std::string partition_type, partition_size;
984
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700985 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700986 const char* errMsg = nullptr;
987 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700988 int fd;
989
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700990 unsigned int limit = INT_MAX;
991 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700992 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700993 }
994 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700995 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700996 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700997
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700998 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700999 errMsg = "Can't determine partition type.\n";
1000 goto failed;
1001 }
JP Abgrall7e859742014-05-06 15:14:15 -07001002 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001003 if (partition_type != type_override) {
1004 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1005 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001006 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001007 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001008 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001009
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001010 if (!fb_getvar(usb, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001011 errMsg = "Unable to get partition size\n";
1012 goto failed;
1013 }
JP Abgrall7e859742014-05-06 15:14:15 -07001014 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001015 if (partition_size != size_override) {
1016 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1017 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001018 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001019 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001020 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001021
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001022 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001023 if (!gen) {
1024 if (skip_if_not_supported) {
1025 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001026 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001027 return;
1028 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001029 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1030 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001031 return;
1032 }
1033
Elliott Hughes2030bac2015-11-03 08:14:43 -08001034 // Some bootloaders (hammerhead, for example) use implicit hex.
1035 // This code used to use strtol with base 16.
1036 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
1037
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001038 int64_t size;
1039 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1040 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1041 return;
1042 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001043
1044 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001045 if (fs_generator_generate(gen, fd, size)) {
1046 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001047 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001048 return;
1049 }
1050
1051 if (load_buf_fd(usb, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001052 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001053 close(fd);
1054 return;
1055 }
1056 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001057 return;
1058
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001059failed:
1060 if (skip_if_not_supported) {
1061 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001062 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001063 }
1064 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1065}
1066
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001067int main(int argc, char **argv)
1068{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001069 bool wants_wipe = false;
1070 bool wants_reboot = false;
1071 bool wants_reboot_bootloader = false;
1072 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001073 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001074 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001075 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001076 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001077 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001078 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001079
JP Abgrall7b8970c2013-03-07 17:06:41 -08001080 const struct option longopts[] = {
1081 {"base", required_argument, 0, 'b'},
1082 {"kernel_offset", required_argument, 0, 'k'},
1083 {"page_size", required_argument, 0, 'n'},
1084 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001085 {"tags_offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001086 {"help", no_argument, 0, 'h'},
1087 {"unbuffered", no_argument, 0, 0},
1088 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001089 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001090 {"set_active", optional_argument, 0, 'a'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001091 {0, 0, 0, 0}
1092 };
Colin Cross8879f982012-05-22 17:53:34 -07001093
1094 serial = getenv("ANDROID_SERIAL");
1095
1096 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001097 int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:ha::", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -07001098 if (c < 0) {
1099 break;
1100 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001101 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001102 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001103 case 'a':
1104 wants_set_active = true;
1105 if (optarg)
1106 next_active = optarg;
1107 break;
Colin Cross8879f982012-05-22 17:53:34 -07001108 case 'b':
1109 base_addr = strtoul(optarg, 0, 16);
1110 break;
Colin Cross8879f982012-05-22 17:53:34 -07001111 case 'c':
1112 cmdline = optarg;
1113 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001114 case 'h':
1115 usage();
1116 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001117 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001118 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001119 unsigned long val;
1120
1121 val = strtoul(optarg, &endptr, 0);
1122 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1123 die("invalid vendor id '%s'", optarg);
1124 vendor_id = (unsigned short)val;
1125 break;
1126 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001127 case 'k':
1128 kernel_offset = strtoul(optarg, 0, 16);
1129 break;
1130 case 'l':
1131 long_listing = 1;
1132 break;
1133 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001134 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001135 if (!page_size) die("invalid page size");
1136 break;
1137 case 'p':
1138 product = optarg;
1139 break;
1140 case 'r':
1141 ramdisk_offset = strtoul(optarg, 0, 16);
1142 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001143 case 't':
1144 tags_offset = strtoul(optarg, 0, 16);
1145 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001146 case 's':
1147 serial = optarg;
1148 break;
1149 case 'S':
1150 sparse_limit = parse_num(optarg);
1151 if (sparse_limit < 0) {
1152 die("invalid sparse limit");
1153 }
1154 break;
1155 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001156 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001157 break;
1158 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001159 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001160 break;
Colin Cross8879f982012-05-22 17:53:34 -07001161 case '?':
1162 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001163 case 0:
1164 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001165 setvbuf(stdout, nullptr, _IONBF, 0);
1166 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001167 } else if (strcmp("version", longopts[longindex].name) == 0) {
1168 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1169 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001170 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1171 slot_override = std::string(optarg);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001172 }
1173 break;
Colin Cross8879f982012-05-22 17:53:34 -07001174 default:
1175 abort();
1176 }
1177 }
1178
1179 argc -= optind;
1180 argv += optind;
1181
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001182 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001183 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001184 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001185 }
1186
Colin Cross8fb6e062012-07-24 16:36:41 -07001187 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001188 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001189 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001190 return 0;
1191 }
1192
Colin Crossc7b75dc2012-08-29 18:17:06 -07001193 if (argc > 0 && !strcmp(*argv, "help")) {
1194 usage();
1195 return 0;
1196 }
1197
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001198 usb_handle* usb = open_device();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001199 if (slot_override != "")
1200 slot_override = verify_slot(usb, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001201 if (next_active != "")
1202 next_active = verify_slot(usb, next_active.c_str());
1203
1204 if (wants_set_active) {
1205 if (next_active == "") {
1206 if (slot_override == "") {
1207 wants_set_active = false;
1208 } else {
1209 next_active = slot_override;
1210 }
1211 }
1212 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001213
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001214 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001215 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001216 require(2);
1217 fb_queue_display(argv[1], argv[1]);
1218 skip(2);
1219 } else if(!strcmp(*argv, "erase")) {
1220 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001221
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001222 auto erase = [&](const std::string &partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001223 std::string partition_type;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001224 if (fb_getvar(usb, std::string("partition-type:") + argv[1], &partition_type) &&
1225 fs_get_generator(partition_type) != nullptr) {
1226 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1227 partition_type.c_str());
1228 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001229
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001230 fb_queue_erase(partition.c_str());
1231 };
1232 do_for_partitions(usb, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001233 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001234 } else if(!strncmp(*argv, "format", strlen("format"))) {
1235 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001236 char *type_override = nullptr;
1237 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001238 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001239 /*
1240 * Parsing for: "format[:[type][:[size]]]"
1241 * Some valid things:
1242 * - select ontly the size, and leave default fs type:
1243 * format::0x4000000 userdata
1244 * - default fs type and size:
1245 * format userdata
1246 * format:: userdata
1247 */
1248 overrides = strchr(*argv, ':');
1249 if (overrides) {
1250 overrides++;
1251 size_override = strchr(overrides, ':');
1252 if (size_override) {
1253 size_override[0] = '\0';
1254 size_override++;
1255 }
1256 type_override = overrides;
1257 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001258 if (type_override && !type_override[0]) type_override = nullptr;
1259 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001260
1261 auto format = [&](const std::string &partition) {
1262 if (erase_first && needs_erase(usb, partition.c_str())) {
1263 fb_queue_erase(partition.c_str());
1264 }
1265 fb_perform_format(usb, partition.c_str(), 0, type_override, size_override);
1266 };
1267 do_for_partitions(usb, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001268 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001269 } else if(!strcmp(*argv, "signature")) {
1270 require(2);
1271 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001272 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001273 if (sz != 256) die("signature must be 256 bytes");
1274 fb_queue_download("signature", data, sz);
1275 fb_queue_command("signature", "installing signature");
1276 skip(2);
1277 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001278 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001279 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001280 if (argc > 0) {
1281 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001282 wants_reboot = false;
1283 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001284 skip(1);
1285 }
1286 }
1287 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001288 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001289 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001290 skip(1);
1291 } else if (!strcmp(*argv, "continue")) {
1292 fb_queue_command("continue", "resuming boot");
1293 skip(1);
1294 } else if(!strcmp(*argv, "boot")) {
1295 char *kname = 0;
1296 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001297 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001298 skip(1);
1299 if (argc > 0) {
1300 kname = argv[0];
1301 skip(1);
1302 }
1303 if (argc > 0) {
1304 rname = argv[0];
1305 skip(1);
1306 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001307 if (argc > 0) {
1308 sname = argv[0];
1309 skip(1);
1310 }
1311 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001312 if (data == 0) return 1;
1313 fb_queue_download("boot.img", data, sz);
1314 fb_queue_command("boot", "booting");
1315 } else if(!strcmp(*argv, "flash")) {
1316 char *pname = argv[1];
1317 char *fname = 0;
1318 require(2);
1319 if (argc > 2) {
1320 fname = argv[2];
1321 skip(3);
1322 } else {
1323 fname = find_item(pname, product);
1324 skip(2);
1325 }
1326 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001327
1328 auto flash = [&](const std::string &partition) {
1329 if (erase_first && needs_erase(usb, partition.c_str())) {
1330 fb_queue_erase(partition.c_str());
1331 }
1332 do_flash(usb, partition.c_str(), fname);
1333 };
1334 do_for_partitions(usb, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001335 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001336 char *kname = argv[2];
1337 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001338 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001339 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001340 skip(3);
1341 if (argc > 0) {
1342 rname = argv[0];
1343 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001344 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001345 if (argc > 0) {
1346 sname = argv[0];
1347 skip(1);
1348 }
1349 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001350 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001351 auto flashraw = [&](const std::string &partition) {
1352 fb_queue_flash(partition.c_str(), data, sz);
1353 };
1354 do_for_partitions(usb, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001355 } else if(!strcmp(*argv, "flashall")) {
1356 skip(1);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001357 do_flashall(usb, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001358 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001359 } else if(!strcmp(*argv, "update")) {
1360 if (argc > 1) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001361 do_update(usb, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001362 skip(2);
1363 } else {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001364 do_update(usb, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001365 skip(1);
1366 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001367 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001368 } else if(!strcmp(*argv, "oem")) {
1369 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001370 } else if(!strcmp(*argv, "flashing")) {
1371 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1372 !strcmp(*(argv+1), "lock") ||
1373 !strcmp(*(argv+1), "unlock_critical") ||
1374 !strcmp(*(argv+1), "lock_critical") ||
1375 !strcmp(*(argv+1), "get_unlock_ability") ||
1376 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1377 !strcmp(*(argv+1), "lock_bootloader"))) {
1378 argc = do_oem_command(argc, argv);
1379 } else
1380 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1381 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001382 } else {
1383 usage();
1384 return 1;
1385 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001386 } else {
1387 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001388 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001389 }
1390 }
1391
1392 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001393 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001394 fb_queue_erase("userdata");
Elliott Hughesfc797672015-04-07 20:12:50 -07001395 fb_perform_format(usb, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001396
1397 std::string cache_type;
1398 if (fb_getvar(usb, "partition-type:cache", &cache_type) && !cache_type.empty()) {
1399 fprintf(stderr, "wiping cache...\n");
1400 fb_queue_erase("cache");
1401 fb_perform_format(usb, "cache", 1, nullptr, nullptr);
1402 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001403 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001404 if (wants_set_active) {
1405 fb_set_active(next_active.c_str());
1406 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001407 if (wants_reboot) {
1408 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001409 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001410 } else if (wants_reboot_bootloader) {
1411 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001412 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001413 }
1414
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001415 return fb_execute_queue(usb) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001416}