blob: 0901310c88045e0579c8b631323625f87f2a1851 [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>
Colin Crossf8387882012-05-24 17:18:41 -070045
Colin Crossf8387882012-05-24 17:18:41 -070046#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070047#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
Elliott Hughes253c18d2015-03-18 22:47:09 -070049#include "bootimg_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070051#include "fs.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
Colin Crossf8387882012-05-24 17:18:41 -070053#ifndef O_BINARY
54#define O_BINARY 0
55#endif
56
Rom Lemarchand622810c2013-06-28 09:54:59 -070057#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
58
Wink Savilleb98762f2011-04-04 17:54:59 -070059char cur_product[FB_RESPONSE_SZ + 1];
60
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061static const char *serial = 0;
62static const char *product = 0;
63static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070065static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070066static int64_t sparse_limit = -1;
67static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068
Elliott Hughesfc797672015-04-07 20:12:50 -070069static unsigned page_size = 2048;
70static unsigned base_addr = 0x10000000;
71static unsigned kernel_offset = 0x00008000;
72static unsigned ramdisk_offset = 0x01000000;
73static unsigned second_offset = 0x00f00000;
74static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080075
Rom Lemarchand622810c2013-06-28 09:54:59 -070076enum fb_buffer_type {
77 FB_BUFFER,
78 FB_BUFFER_SPARSE,
79};
80
81struct fastboot_buffer {
82 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070083 void* data;
84 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070085};
86
87static struct {
88 char img_name[13];
89 char sig_name[13];
90 char part_name[9];
91 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -070092} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -070093 {"boot.img", "boot.sig", "boot", false},
94 {"recovery.img", "recovery.sig", "recovery", true},
95 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -070096 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -070097};
Brian Swetland2a63bb72009-04-28 16:05:07 -070098
Elliott Hughesfc797672015-04-07 20:12:50 -070099static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700101 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 char path[PATH_MAX + 128];
103
104 if(!strcmp(item,"boot")) {
105 fn = "boot.img";
106 } else if(!strcmp(item,"recovery")) {
107 fn = "recovery.img";
108 } else if(!strcmp(item,"system")) {
109 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700110 } else if(!strcmp(item,"vendor")) {
111 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 } else if(!strcmp(item,"userdata")) {
113 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700114 } else if(!strcmp(item,"cache")) {
115 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 } else if(!strcmp(item,"info")) {
117 fn = "android-info.txt";
118 } else {
119 fprintf(stderr,"unknown partition '%s'\n", item);
120 return 0;
121 }
122
123 if(product) {
124 get_my_path(path);
125 sprintf(path + strlen(path),
126 "../../../target/product/%s/%s", product, fn);
127 return strdup(path);
128 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800129
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 dir = getenv("ANDROID_PRODUCT_OUT");
131 if((dir == 0) || (dir[0] == 0)) {
132 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
133 return 0;
134 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800135
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 sprintf(path, "%s/%s", dir, fn);
137 return strdup(path);
138}
139
Elliott Hughesfc797672015-04-07 20:12:50 -0700140static int64_t get_file_size(int fd) {
141 struct stat sb;
142 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700143}
144
Elliott Hughesfc797672015-04-07 20:12:50 -0700145static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800146 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700147 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148
Elliott Hughesfc797672015-04-07 20:12:50 -0700149 *sz = get_file_size(fd);
150 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700151 goto oops;
152 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153
Elliott Hughesfc797672015-04-07 20:12:50 -0700154 data = (char*) malloc(*sz);
155 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156
Elliott Hughesfc797672015-04-07 20:12:50 -0700157 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 close(fd);
159
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 return data;
161
162oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800163 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 close(fd);
165 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800166 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 return 0;
168}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169
Elliott Hughesfc797672015-04-07 20:12:50 -0700170static void* load_file(const char* fn, int64_t* sz) {
171 int fd = open(fn, O_RDONLY | O_BINARY);
172 if (fd == -1) return nullptr;
173 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700174}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
Elliott Hughesfc797672015-04-07 20:12:50 -0700176static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700177 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700178 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700179 return -1;
180 }
181
182 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
183 return -1;
184 }
185
Scott Anderson13081c62012-04-06 12:39:30 -0700186 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700188 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
189 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 return 0;
191}
192
Elliott Hughesfc797672015-04-07 20:12:50 -0700193static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800194 return match_fastboot_with_serial(info, serial);
195}
196
Elliott Hughesfc797672015-04-07 20:12:50 -0700197static int list_devices_callback(usb_ifc_info* info) {
198 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700199 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700200 if (!info->writable) {
201 serial = "no permissions"; // like "adb devices"
202 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 if (!serial[0]) {
204 serial = "????????????";
205 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700206 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700207 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700208 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800209 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700210 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700211 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700212 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700213 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 }
215
216 return -1;
217}
218
Elliott Hughesfc797672015-04-07 20:12:50 -0700219static usb_handle* open_device() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 static usb_handle *usb = 0;
221 int announce = 1;
222
223 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800224
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 for(;;) {
226 usb = usb_open(match_fastboot);
227 if(usb) return usb;
228 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800229 announce = 0;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700230 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700232 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 }
234}
235
Elliott Hughesfc797672015-04-07 20:12:50 -0700236static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 // We don't actually open a USB device here,
238 // just getting our callback called so we can
239 // list all the connected devices.
240 usb_open(list_devices_callback);
241}
242
Elliott Hughesfc797672015-04-07 20:12:50 -0700243static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 fprintf(stderr,
245/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
246 "usage: fastboot [ <option> ] <command>\n"
247 "\n"
248 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700249 " update <filename> Reflash device from update.zip.\n"
250 " flashall Flash boot, system, vendor, and --\n"
251 " if found -- recovery.\n"
252 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
253 " flashing lock Locks the device. Prevents flashing.\n"
254 " flashing unlock Unlocks the device. Allows flashing\n"
255 " any partition except\n"
256 " bootloader-related partitions.\n"
257 " flashing lock_critical Prevents flashing bootloader-related\n"
258 " partitions.\n"
259 " flashing unlock_critical Enables flashing bootloader-related\n"
260 " partitions.\n"
261 " flashing get_unlock_ability Queries bootloader to see if the\n"
262 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700263 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700264 " unlock nonce.\n"
265 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700266 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700267 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700268 " erase <partition> Erase a flash partition.\n"
269 " format[:[<fs type>][:[<size>]] <partition>\n"
270 " Format a flash partition. Can\n"
271 " override the fs type and/or size\n"
272 " the bootloader reports.\n"
273 " getvar <variable> Display a bootloader variable.\n"
274 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
275 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
276 " Create bootimage and flash it.\n"
277 " devices [-l] List all connected devices [with\n"
278 " device paths].\n"
279 " continue Continue with autoboot.\n"
280 " reboot [bootloader] Reboot device [into bootloader].\n"
281 " reboot-bootloader Reboot device into bootloader.\n"
282 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283 "\n"
284 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700285 " -w Erase userdata and cache (and format\n"
286 " if supported by partition type).\n"
287 " -u Do not erase partition before\n"
288 " formatting.\n"
289 " -s <specific device> Specify device serial number\n"
290 " or path to device port.\n"
291 " -p <product> Specify product name.\n"
292 " -c <cmdline> Override kernel commandline.\n"
293 " -i <vendor id> Specify a custom USB vendor id.\n"
294 " -b <base_addr> Specify a custom kernel base\n"
295 " address (default: 0x10000000).\n"
296 " -n <page size> Specify the nand page size\n"
297 " (default: 2048).\n"
298 " -S <size>[K|M|G] Automatically sparse files greater\n"
299 " than 'size'. 0 to disable.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301}
302
Elliott Hughesfc797672015-04-07 20:12:50 -0700303static void* load_bootable_image(const char* kernel, const char* ramdisk,
304 const char* secondstage, int64_t* sz,
305 const char* cmdline) {
306 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 fprintf(stderr, "no image specified\n");
308 return 0;
309 }
310
Elliott Hughesfc797672015-04-07 20:12:50 -0700311 int64_t ksize;
312 void* kdata = load_file(kernel, &ksize);
313 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800314 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 return 0;
316 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800317
Elliott Hughesfc797672015-04-07 20:12:50 -0700318 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700320 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800321
Elliott Hughesfc797672015-04-07 20:12:50 -0700322 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
324 return 0;
325 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800326
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 *sz = ksize;
328 return kdata;
329 }
330
Elliott Hughesfc797672015-04-07 20:12:50 -0700331 void* rdata = nullptr;
332 int64_t rsize = 0;
333 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700335 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800336 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 return 0;
338 }
339 }
340
Elliott Hughesfc797672015-04-07 20:12:50 -0700341 void* sdata = nullptr;
342 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200343 if (secondstage) {
344 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700345 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200346 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
347 return 0;
348 }
349 }
350
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700352 int64_t bsize = 0;
353 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800354 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200355 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800356 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700357 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 fprintf(stderr,"failed to create boot.img\n");
359 return 0;
360 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700361 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
362 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800364
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 return bdata;
366}
367
Elliott Hughesfc797672015-04-07 20:12:50 -0700368static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369{
Yusuke Sato07447542015-06-25 14:39:19 -0700370 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700371 ZipEntry zip_entry;
372 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700373 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000374 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 }
376
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700377 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700379 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700380 if (data == nullptr) {
381 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000382 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383 }
384
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700385 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
386 if (error != 0) {
387 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000389 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000391
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 return data;
393}
394
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700395#if defined(_WIN32)
396
397// TODO: move this to somewhere it can be shared.
398
399#include <windows.h>
400
401// Windows' tmpfile(3) requires administrator rights because
402// it creates temporary files in the root directory.
403static FILE* win32_tmpfile() {
404 char temp_path[PATH_MAX];
405 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
406 if (nchars == 0 || nchars >= sizeof(temp_path)) {
407 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
408 return nullptr;
409 }
410
411 char filename[PATH_MAX];
412 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
413 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
414 return nullptr;
415 }
416
417 return fopen(filename, "w+bTD");
418}
419
420#define tmpfile win32_tmpfile
421
422#endif
423
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700424static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
425 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700426 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700427 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
428 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700429 return -1;
430 }
431
Yusuke Sato07447542015-06-25 14:39:19 -0700432 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700433 ZipEntry zip_entry;
434 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
435 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000436 return -1;
437 }
438
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700439 int fd = fileno(fp);
440 int error = ExtractEntryToFile(zip, &zip_entry, fd);
441 if (error != 0) {
442 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
443 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700444 }
445
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000446 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700447 return fd;
448}
449
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450static char *strip(char *s)
451{
452 int n;
453 while(*s && isspace(*s)) s++;
454 n = strlen(s);
455 while(n-- > 0) {
456 if(!isspace(s[n])) break;
457 s[n] = 0;
458 }
459 return s;
460}
461
462#define MAX_OPTIONS 32
463static int setup_requirement_line(char *name)
464{
465 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700466 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 unsigned n, count;
468 char *x;
469 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800470
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 if (!strncmp(name, "reject ", 7)) {
472 name += 7;
473 invert = 1;
474 } else if (!strncmp(name, "require ", 8)) {
475 name += 8;
476 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700477 } else if (!strncmp(name, "require-for-product:", 20)) {
478 // Get the product and point name past it
479 prod = name + 20;
480 name = strchr(name, ' ');
481 if (!name) return -1;
482 *name = 0;
483 name += 1;
484 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 }
486
487 x = strchr(name, '=');
488 if (x == 0) return 0;
489 *x = 0;
490 val[0] = x + 1;
491
492 for(count = 1; count < MAX_OPTIONS; count++) {
493 x = strchr(val[count - 1],'|');
494 if (x == 0) break;
495 *x = 0;
496 val[count] = x + 1;
497 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800498
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 name = strip(name);
500 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800501
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 name = strip(name);
503 if (name == 0) return -1;
504
Elliott Hughes253c18d2015-03-18 22:47:09 -0700505 const char* var = name;
506 // Work around an unfortunate name mismatch.
507 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508
Elliott Hughes253c18d2015-03-18 22:47:09 -0700509 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 if (out == 0) return -1;
511
512 for(n = 0; n < count; n++) {
513 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700514 if (out[n] == 0) {
515 for(size_t i = 0; i < n; ++i) {
516 free((char*) out[i]);
517 }
518 free(out);
519 return -1;
520 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521 }
522
Elliott Hughes253c18d2015-03-18 22:47:09 -0700523 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524 return 0;
525}
526
Elliott Hughesfc797672015-04-07 20:12:50 -0700527static void setup_requirements(char* data, int64_t sz) {
528 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700530 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531 *s++ = 0;
532 if (setup_requirement_line(data)) {
533 die("out of memory");
534 }
535 data = s;
536 } else {
537 s++;
538 }
539 }
540}
541
Elliott Hughesfc797672015-04-07 20:12:50 -0700542static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543 fb_queue_notice("--------------------------------------------");
544 fb_queue_display("version-bootloader", "Bootloader Version...");
545 fb_queue_display("version-baseband", "Baseband Version.....");
546 fb_queue_display("serialno", "Serial Number........");
547 fb_queue_notice("--------------------------------------------");
548}
549
Rom Lemarchand622810c2013-06-28 09:54:59 -0700550static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700551{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700552 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700553 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700554 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700555 }
556
Elliott Hughesfc797672015-04-07 20:12:50 -0700557 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700558 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700559 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700560 }
561
Elliott Hughes253c18d2015-03-18 22:47:09 -0700562 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700563 if (!out_s) {
564 die("Failed to allocate sparse file array\n");
565 }
566
567 files = sparse_file_resparse(s, max_size, out_s, files);
568 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700569 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700570 }
571
572 return out_s;
573}
574
575static int64_t get_target_sparse_limit(struct usb_handle *usb)
576{
577 int64_t limit = 0;
578 char response[FB_RESPONSE_SZ + 1];
579 int status = fb_getvar(usb, response, "max-download-size");
580
581 if (!status) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700582 limit = strtoul(response, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700583 if (limit > 0) {
Ying Wangcf86e2f2014-05-15 20:06:40 -0700584 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n",
Colin Crossf8387882012-05-24 17:18:41 -0700585 limit);
586 }
587 }
588
589 return limit;
590}
591
592static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
593{
594 int64_t limit;
595
596 if (sparse_limit == 0) {
597 return 0;
598 } else if (sparse_limit > 0) {
599 limit = sparse_limit;
600 } else {
601 if (target_sparse_limit == -1) {
602 target_sparse_limit = get_target_sparse_limit(usb);
603 }
604 if (target_sparse_limit > 0) {
605 limit = target_sparse_limit;
606 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700607 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700608 }
609 }
610
611 if (size > limit) {
612 return limit;
613 }
614
615 return 0;
616}
617
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700618/* Until we get lazy inode table init working in make_ext4fs, we need to
619 * erase partitions of type ext4 before flashing a filesystem so no stale
620 * inodes are left lying around. Otherwise, e2fsck gets very upset.
621 */
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700622static int needs_erase(usb_handle* usb, const char *part)
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700623{
624 /* The function fb_format_supported() currently returns the value
625 * we want, so just call it.
626 */
Elliott Hughesfc797672015-04-07 20:12:50 -0700627 return fb_format_supported(usb, part, nullptr);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700628}
629
Elliott Hughesfc797672015-04-07 20:12:50 -0700630static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) {
631 int64_t sz = get_file_size(fd);
632 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000633 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700634 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700635
Elliott Hughesfc797672015-04-07 20:12:50 -0700636 lseek64(fd, 0, SEEK_SET);
637 int64_t limit = get_sparse_limit(usb, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700638 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700639 sparse_file** s = load_sparse_files(fd, limit);
640 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700641 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700642 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700643 buf->type = FB_BUFFER_SPARSE;
644 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700645 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700646 void* data = load_fd(fd, &sz);
647 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700648 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700649 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000650 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700651 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700652
653 return 0;
654}
655
656static int load_buf(usb_handle *usb, const char *fname,
657 struct fastboot_buffer *buf)
658{
659 int fd;
660
661 fd = open(fname, O_RDONLY | O_BINARY);
662 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700663 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700664 }
665
666 return load_buf_fd(usb, fd, buf);
667}
668
669static void flash_buf(const char *pname, struct fastboot_buffer *buf)
670{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700671 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700672
673 switch (buf->type) {
674 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700675 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700676 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700677 int64_t sz = sparse_file_len(*s, true, false);
678 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700679 }
680 break;
681 case FB_BUFFER:
682 fb_queue_flash(pname, buf->data, buf->sz);
683 break;
684 default:
685 die("unknown buffer type: %d", buf->type);
686 }
687}
688
Elliott Hughesfc797672015-04-07 20:12:50 -0700689static void do_flash(usb_handle* usb, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700690 struct fastboot_buffer buf;
691
692 if (load_buf(usb, fname, &buf)) {
693 die("cannot load '%s'", fname);
694 }
695 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700696}
697
Elliott Hughesfc797672015-04-07 20:12:50 -0700698static void do_update_signature(ZipArchiveHandle zip, char* fn) {
699 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700700 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700701 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702 fb_queue_download("signature", data, sz);
703 fb_queue_command("signature", "installing signature");
704}
705
Elliott Hughesfc797672015-04-07 20:12:50 -0700706static void do_update(usb_handle* usb, const char* filename, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 queue_info_dump();
708
Wink Savilleb98762f2011-04-04 17:54:59 -0700709 fb_queue_query_save("product", cur_product, sizeof(cur_product));
710
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700711 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700712 int error = OpenArchive(filename, &zip);
713 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100714 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700715 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700716 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717
Elliott Hughesfc797672015-04-07 20:12:50 -0700718 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700719 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700720 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100721 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700722 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800723 }
724
Elliott Hughes253c18d2015-03-18 22:47:09 -0700725 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726
Elliott Hughesacdbe922015-06-02 21:37:05 -0700727 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700728 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700729 if (fd == -1) {
730 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700731 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700732 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100733 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700734 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700735 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700736 fastboot_buffer buf;
737 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700738 if (rc) die("cannot load %s from flash", images[i].img_name);
739 do_update_signature(zip, images[i].sig_name);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700740 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700741 fb_queue_erase(images[i].part_name);
742 }
743 flash_buf(images[i].part_name, &buf);
744 /* not closing the fd here since the sparse code keeps the fd around
745 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
746 * program exits.
747 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800748 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700749
750 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800751}
752
Elliott Hughesfc797672015-04-07 20:12:50 -0700753static void do_send_signature(char* fn) {
754 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800755 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700756
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800757 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800758
Elliott Hughesfc797672015-04-07 20:12:50 -0700759 strcpy(xtn, ".sig");
760
761 int64_t sz;
762 void* data = load_file(fn, &sz);
763 strcpy(xtn, ".img");
764 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765 fb_queue_download("signature", data, sz);
766 fb_queue_command("signature", "installing signature");
767}
768
Elliott Hughesfc797672015-04-07 20:12:50 -0700769static void do_flashall(usb_handle* usb, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800770 queue_info_dump();
771
Wink Savilleb98762f2011-04-04 17:54:59 -0700772 fb_queue_query_save("product", cur_product, sizeof(cur_product));
773
Elliott Hughes253c18d2015-03-18 22:47:09 -0700774 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700775 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800776
Elliott Hughesfc797672015-04-07 20:12:50 -0700777 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700778 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700779 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700780
781 setup_requirements(reinterpret_cast<char*>(data), sz);
782
783 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700784 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700785 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700786 if (load_buf(usb, fname, &buf)) {
787 if (images[i].is_optional)
788 continue;
789 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700790 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700791 do_send_signature(fname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700792 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700793 fb_queue_erase(images[i].part_name);
794 }
795 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800796 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800797}
798
799#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800800#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800801
Elliott Hughes45be42e2015-09-02 20:15:48 -0700802static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -0700803{
Patrick Tjin51e8b032015-09-01 08:15:23 -0700804 if (argc <= 2) return 0;
805 skip(2);
806
807 /*
808 * Process unlock_bootloader, we have to load the message file
809 * and send that to the remote device.
810 */
811 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -0700812
813 int64_t sz;
814 void* data = load_file(*argv, &sz);
815 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -0700816 fb_queue_download("unlock_message", data, sz);
817 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
818 skip(1);
819 return 0;
820}
821
Elliott Hughes45be42e2015-09-02 20:15:48 -0700822static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800823{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800824 char command[256];
825 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800826
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800827 command[0] = 0;
828 while(1) {
829 strcat(command,*argv);
830 skip(1);
831 if(argc == 0) break;
832 strcat(command," ");
833 }
834
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800835 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800836 return 0;
837}
838
Colin Crossf8387882012-05-24 17:18:41 -0700839static int64_t parse_num(const char *arg)
840{
841 char *endptr;
842 unsigned long long num;
843
844 num = strtoull(arg, &endptr, 0);
845 if (endptr == arg) {
846 return -1;
847 }
848
849 if (*endptr == 'k' || *endptr == 'K') {
850 if (num >= (-1ULL) / 1024) {
851 return -1;
852 }
853 num *= 1024LL;
854 endptr++;
855 } else if (*endptr == 'm' || *endptr == 'M') {
856 if (num >= (-1ULL) / (1024 * 1024)) {
857 return -1;
858 }
859 num *= 1024LL * 1024LL;
860 endptr++;
861 } else if (*endptr == 'g' || *endptr == 'G') {
862 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
863 return -1;
864 }
865 num *= 1024LL * 1024LL * 1024LL;
866 endptr++;
867 }
868
869 if (*endptr != '\0') {
870 return -1;
871 }
872
873 if (num > INT64_MAX) {
874 return -1;
875 }
876
877 return num;
878}
879
Elliott Hughesfc797672015-04-07 20:12:50 -0700880static void fb_perform_format(usb_handle* usb,
881 const char *partition, int skip_if_not_supported,
882 const char *type_override, const char *size_override) {
JP Abgrall7e859742014-05-06 15:14:15 -0700883 char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1];
884 char *pType = pTypeBuff;
885 char *pSize = pSizeBuff;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700886 unsigned int limit = INT_MAX;
887 struct fastboot_buffer buf;
Elliott Hughesfc797672015-04-07 20:12:50 -0700888 const char *errMsg = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700889 const struct fs_generator *gen;
890 uint64_t pSz;
891 int status;
892 int fd;
893
894 if (target_sparse_limit > 0 && target_sparse_limit < limit)
895 limit = target_sparse_limit;
896 if (sparse_limit > 0 && sparse_limit < limit)
897 limit = sparse_limit;
898
899 status = fb_getvar(usb, pType, "partition-type:%s", partition);
900 if (status) {
901 errMsg = "Can't determine partition type.\n";
902 goto failed;
903 }
JP Abgrall7e859742014-05-06 15:14:15 -0700904 if (type_override) {
905 if (strcmp(type_override, pType)) {
906 fprintf(stderr,
907 "Warning: %s type is %s, but %s was requested for formating.\n",
908 partition, pType, type_override);
909 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700910 pType = (char *)type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700911 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700912
913 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
914 if (status) {
915 errMsg = "Unable to get partition size\n";
916 goto failed;
917 }
JP Abgrall7e859742014-05-06 15:14:15 -0700918 if (size_override) {
919 if (strcmp(size_override, pSize)) {
920 fprintf(stderr,
921 "Warning: %s size is %s, but %s was requested for formating.\n",
922 partition, pSize, size_override);
923 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700924 pSize = (char *)size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700925 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700926
927 gen = fs_get_generator(pType);
928 if (!gen) {
929 if (skip_if_not_supported) {
930 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
931 fprintf(stderr, "File system type %s not supported.\n", pType);
932 return;
933 }
934 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
935 return;
936 }
937
Elliott Hughesfc797672015-04-07 20:12:50 -0700938 pSz = strtoll(pSize, (char **)nullptr, 16);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700939
940 fd = fileno(tmpfile());
941 if (fs_generator_generate(gen, fd, pSz)) {
942 close(fd);
943 fprintf(stderr, "Cannot generate image.\n");
944 return;
945 }
946
947 if (load_buf_fd(usb, fd, &buf)) {
948 fprintf(stderr, "Cannot read image.\n");
949 close(fd);
950 return;
951 }
952 flash_buf(partition, &buf);
953
954 return;
955
956
957failed:
958 if (skip_if_not_supported) {
959 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
960 if (errMsg)
961 fprintf(stderr, "%s", errMsg);
962 }
963 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
964}
965
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800966int main(int argc, char **argv)
967{
968 int wants_wipe = 0;
969 int wants_reboot = 0;
970 int wants_reboot_bootloader = 0;
Elliott Hughesfc797672015-04-07 20:12:50 -0700971 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800972 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -0700973 int64_t sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700974 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700975 int c;
Florian Bäuerle27ded482014-11-24 11:29:34 +0100976 int longindex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800977
JP Abgrall7b8970c2013-03-07 17:06:41 -0800978 const struct option longopts[] = {
979 {"base", required_argument, 0, 'b'},
980 {"kernel_offset", required_argument, 0, 'k'},
981 {"page_size", required_argument, 0, 'n'},
982 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800983 {"tags_offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -0700984 {"help", no_argument, 0, 'h'},
985 {"unbuffered", no_argument, 0, 0},
986 {"version", no_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800987 {0, 0, 0, 0}
988 };
Colin Cross8879f982012-05-22 17:53:34 -0700989
990 serial = getenv("ANDROID_SERIAL");
991
992 while (1) {
Florian Bäuerle27ded482014-11-24 11:29:34 +0100993 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 -0700994 if (c < 0) {
995 break;
996 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800997 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700998 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700999 case 'b':
1000 base_addr = strtoul(optarg, 0, 16);
1001 break;
Colin Cross8879f982012-05-22 17:53:34 -07001002 case 'c':
1003 cmdline = optarg;
1004 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001005 case 'h':
1006 usage();
1007 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001008 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001009 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001010 unsigned long val;
1011
1012 val = strtoul(optarg, &endptr, 0);
1013 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1014 die("invalid vendor id '%s'", optarg);
1015 vendor_id = (unsigned short)val;
1016 break;
1017 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001018 case 'k':
1019 kernel_offset = strtoul(optarg, 0, 16);
1020 break;
1021 case 'l':
1022 long_listing = 1;
1023 break;
1024 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001025 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001026 if (!page_size) die("invalid page size");
1027 break;
1028 case 'p':
1029 product = optarg;
1030 break;
1031 case 'r':
1032 ramdisk_offset = strtoul(optarg, 0, 16);
1033 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001034 case 't':
1035 tags_offset = strtoul(optarg, 0, 16);
1036 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001037 case 's':
1038 serial = optarg;
1039 break;
1040 case 'S':
1041 sparse_limit = parse_num(optarg);
1042 if (sparse_limit < 0) {
1043 die("invalid sparse limit");
1044 }
1045 break;
1046 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001047 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001048 break;
1049 case 'w':
1050 wants_wipe = 1;
1051 break;
Colin Cross8879f982012-05-22 17:53:34 -07001052 case '?':
1053 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001054 case 0:
1055 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001056 setvbuf(stdout, nullptr, _IONBF, 0);
1057 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001058 } else if (strcmp("version", longopts[longindex].name) == 0) {
1059 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1060 return 0;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001061 }
1062 break;
Colin Cross8879f982012-05-22 17:53:34 -07001063 default:
1064 abort();
1065 }
1066 }
1067
1068 argc -= optind;
1069 argv += optind;
1070
1071 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001072 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001073 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001074 }
1075
Colin Cross8fb6e062012-07-24 16:36:41 -07001076 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001077 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001078 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001079 return 0;
1080 }
1081
Colin Crossc7b75dc2012-08-29 18:17:06 -07001082 if (argc > 0 && !strcmp(*argv, "help")) {
1083 usage();
1084 return 0;
1085 }
1086
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001087 usb_handle* usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001088
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001089 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001090 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001091 require(2);
1092 fb_queue_display(argv[1], argv[1]);
1093 skip(2);
1094 } else if(!strcmp(*argv, "erase")) {
1095 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001096
Elliott Hughesfc797672015-04-07 20:12:50 -07001097 if (fb_format_supported(usb, argv[1], nullptr)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001098 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1099 }
1100
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001101 fb_queue_erase(argv[1]);
1102 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001103 } else if(!strncmp(*argv, "format", strlen("format"))) {
1104 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001105 char *type_override = nullptr;
1106 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001107 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001108 /*
1109 * Parsing for: "format[:[type][:[size]]]"
1110 * Some valid things:
1111 * - select ontly the size, and leave default fs type:
1112 * format::0x4000000 userdata
1113 * - default fs type and size:
1114 * format userdata
1115 * format:: userdata
1116 */
1117 overrides = strchr(*argv, ':');
1118 if (overrides) {
1119 overrides++;
1120 size_override = strchr(overrides, ':');
1121 if (size_override) {
1122 size_override[0] = '\0';
1123 size_override++;
1124 }
1125 type_override = overrides;
1126 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001127 if (type_override && !type_override[0]) type_override = nullptr;
1128 if (size_override && !size_override[0]) size_override = nullptr;
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001129 if (erase_first && needs_erase(usb, argv[1])) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001130 fb_queue_erase(argv[1]);
1131 }
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001132 fb_perform_format(usb, argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001133 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001134 } else if(!strcmp(*argv, "signature")) {
1135 require(2);
1136 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001137 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001138 if (sz != 256) die("signature must be 256 bytes");
1139 fb_queue_download("signature", data, sz);
1140 fb_queue_command("signature", "installing signature");
1141 skip(2);
1142 } else if(!strcmp(*argv, "reboot")) {
1143 wants_reboot = 1;
1144 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001145 if (argc > 0) {
1146 if (!strcmp(*argv, "bootloader")) {
1147 wants_reboot = 0;
1148 wants_reboot_bootloader = 1;
1149 skip(1);
1150 }
1151 }
1152 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001153 } else if(!strcmp(*argv, "reboot-bootloader")) {
1154 wants_reboot_bootloader = 1;
1155 skip(1);
1156 } else if (!strcmp(*argv, "continue")) {
1157 fb_queue_command("continue", "resuming boot");
1158 skip(1);
1159 } else if(!strcmp(*argv, "boot")) {
1160 char *kname = 0;
1161 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001162 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001163 skip(1);
1164 if (argc > 0) {
1165 kname = argv[0];
1166 skip(1);
1167 }
1168 if (argc > 0) {
1169 rname = argv[0];
1170 skip(1);
1171 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001172 if (argc > 0) {
1173 sname = argv[0];
1174 skip(1);
1175 }
1176 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001177 if (data == 0) return 1;
1178 fb_queue_download("boot.img", data, sz);
1179 fb_queue_command("boot", "booting");
1180 } else if(!strcmp(*argv, "flash")) {
1181 char *pname = argv[1];
1182 char *fname = 0;
1183 require(2);
1184 if (argc > 2) {
1185 fname = argv[2];
1186 skip(3);
1187 } else {
1188 fname = find_item(pname, product);
1189 skip(2);
1190 }
1191 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001192 if (erase_first && needs_erase(usb, pname)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001193 fb_queue_erase(pname);
1194 }
Colin Crossf8387882012-05-24 17:18:41 -07001195 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001196 } else if(!strcmp(*argv, "flash:raw")) {
1197 char *pname = argv[1];
1198 char *kname = argv[2];
1199 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001200 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001201 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001202 skip(3);
1203 if (argc > 0) {
1204 rname = argv[0];
1205 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001206 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001207 if (argc > 0) {
1208 sname = argv[0];
1209 skip(1);
1210 }
1211 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001212 if (data == 0) die("cannot load bootable image");
1213 fb_queue_flash(pname, data, sz);
1214 } else if(!strcmp(*argv, "flashall")) {
1215 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001216 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001217 wants_reboot = 1;
1218 } else if(!strcmp(*argv, "update")) {
1219 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001220 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001221 skip(2);
1222 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001223 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001224 skip(1);
1225 }
1226 wants_reboot = 1;
1227 } else if(!strcmp(*argv, "oem")) {
1228 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001229 } else if(!strcmp(*argv, "flashing")) {
1230 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1231 !strcmp(*(argv+1), "lock") ||
1232 !strcmp(*(argv+1), "unlock_critical") ||
1233 !strcmp(*(argv+1), "lock_critical") ||
1234 !strcmp(*(argv+1), "get_unlock_ability") ||
1235 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1236 !strcmp(*(argv+1), "lock_bootloader"))) {
1237 argc = do_oem_command(argc, argv);
1238 } else
1239 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1240 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001241 } else {
1242 usage();
1243 return 1;
1244 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001245 } else {
1246 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001247 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001248 }
1249 }
1250
1251 if (wants_wipe) {
1252 fb_queue_erase("userdata");
Elliott Hughesfc797672015-04-07 20:12:50 -07001253 fb_perform_format(usb, "userdata", 1, nullptr, nullptr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001254 fb_queue_erase("cache");
Elliott Hughesfc797672015-04-07 20:12:50 -07001255 fb_perform_format(usb, "cache", 1, nullptr, nullptr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001256 }
1257 if (wants_reboot) {
1258 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001259 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001260 } else if (wants_reboot_bootloader) {
1261 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001262 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001263 }
1264
Scott Anderson13081c62012-04-06 12:39:30 -07001265 if (fb_queue_is_empty())
1266 return 0;
1267
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001268 status = fb_execute_queue(usb);
1269 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001270}