blob: dec690c78cc9d5ff96a08f8182da138ae30474c8 [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>
37#include <stdbool.h>
38#include <stdint.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070044#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070045#include <unistd.h>
Colin Crossf8387882012-05-24 17:18:41 -070046
Colin Crossf8387882012-05-24 17:18:41 -070047#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070048#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
Elliott Hughes253c18d2015-03-18 22:47:09 -070050#include "bootimg_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070052#include "fs.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Colin Crossf8387882012-05-24 17:18:41 -070054#ifndef O_BINARY
55#define O_BINARY 0
56#endif
57
Rom Lemarchand622810c2013-06-28 09:54:59 -070058#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
59
Wink Savilleb98762f2011-04-04 17:54:59 -070060char cur_product[FB_RESPONSE_SZ + 1];
61
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062static const char *serial = 0;
63static const char *product = 0;
64static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070066static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070067static int64_t sparse_limit = -1;
68static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
JP Abgrall7b8970c2013-03-07 17:06:41 -080070unsigned page_size = 2048;
71unsigned base_addr = 0x10000000;
72unsigned kernel_offset = 0x00008000;
73unsigned ramdisk_offset = 0x01000000;
74unsigned second_offset = 0x00f00000;
75unsigned tags_offset = 0x00000100;
76
Rom Lemarchand622810c2013-06-28 09:54:59 -070077enum fb_buffer_type {
78 FB_BUFFER,
79 FB_BUFFER_SPARSE,
80};
81
82struct fastboot_buffer {
83 enum fb_buffer_type type;
84 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +000085 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070086};
87
88static struct {
89 char img_name[13];
90 char sig_name[13];
91 char part_name[9];
92 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -070093} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -070094 {"boot.img", "boot.sig", "boot", false},
95 {"recovery.img", "recovery.sig", "recovery", true},
96 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -070097 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -070098};
Brian Swetland2a63bb72009-04-28 16:05:07 -070099
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100char *find_item(const char *item, const char *product)
101{
102 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700103 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 char path[PATH_MAX + 128];
105
106 if(!strcmp(item,"boot")) {
107 fn = "boot.img";
108 } else if(!strcmp(item,"recovery")) {
109 fn = "recovery.img";
110 } else if(!strcmp(item,"system")) {
111 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700112 } else if(!strcmp(item,"vendor")) {
113 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 } else if(!strcmp(item,"userdata")) {
115 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700116 } else if(!strcmp(item,"cache")) {
117 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 } else if(!strcmp(item,"info")) {
119 fn = "android-info.txt";
120 } else {
121 fprintf(stderr,"unknown partition '%s'\n", item);
122 return 0;
123 }
124
125 if(product) {
126 get_my_path(path);
127 sprintf(path + strlen(path),
128 "../../../target/product/%s/%s", product, fn);
129 return strdup(path);
130 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800131
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 dir = getenv("ANDROID_PRODUCT_OUT");
133 if((dir == 0) || (dir[0] == 0)) {
134 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
135 return 0;
136 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800137
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 sprintf(path, "%s/%s", dir, fn);
139 return strdup(path);
140}
141
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000142static int64_t file_size(int fd)
Colin Crossf8387882012-05-24 17:18:41 -0700143{
Rom Lemarchand622810c2013-06-28 09:54:59 -0700144 struct stat st;
145 int ret;
Colin Crossf8387882012-05-24 17:18:41 -0700146
Rom Lemarchand622810c2013-06-28 09:54:59 -0700147 ret = fstat(fd, &st);
Colin Crossf8387882012-05-24 17:18:41 -0700148
Rom Lemarchand622810c2013-06-28 09:54:59 -0700149 return ret ? -1 : st.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700150}
151
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000152static void *load_fd(int fd, unsigned *_sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000154 char *data;
155 int sz;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800156 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000158 data = 0;
159
160 sz = file_size(fd);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700161 if (sz < 0) {
162 goto oops;
163 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
165 data = (char*) malloc(sz);
166 if(data == 0) goto oops;
167
168 if(read(fd, data, sz) != sz) goto oops;
169 close(fd);
170
171 if(_sz) *_sz = sz;
172 return data;
173
174oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800175 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 close(fd);
177 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800178 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 return 0;
180}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000182static void *load_file(const char *fn, unsigned *_sz)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700183{
184 int fd;
185
186 fd = open(fn, O_RDONLY | O_BINARY);
187 if(fd < 0) return 0;
188
189 return load_fd(fd, _sz);
190}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
JP Abgralla032ded2012-06-06 11:53:33 -0700192int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
193{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400195 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800196 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700197 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800198 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700199 (info->dev_vendor != 0x0fce) && // Sony Ericsson
200 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400201 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800202 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800203 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800204 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800205 (info->dev_vendor != 0x0b05) && // Asus
Mohamad Ayyash6c483492016-02-16 20:12:34 -0800206 (info->dev_vendor != 0x0bb4) && // HTC
207 (info->dev_vendor != 0x07cf)) // CASIO
Mike Lockwood09070d92009-08-05 17:04:36 -0400208 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 if(info->ifc_class != 0xff) return -1;
210 if(info->ifc_subclass != 0x42) return -1;
211 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700212 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700214 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
215 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 return 0;
217}
218
JP Abgrall7b8970c2013-03-07 17:06:41 -0800219int match_fastboot(usb_ifc_info *info)
220{
221 return match_fastboot_with_serial(info, serial);
222}
223
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224int list_devices_callback(usb_ifc_info *info)
225{
JP Abgralla032ded2012-06-06 11:53:33 -0700226 if (match_fastboot_with_serial(info, NULL) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700227 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700228 if (!info->writable) {
229 serial = "no permissions"; // like "adb devices"
230 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 if (!serial[0]) {
232 serial = "????????????";
233 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700234 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700235 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700236 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800237 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700238 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700239 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700240 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700241 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
243
244 return -1;
245}
246
247usb_handle *open_device(void)
248{
249 static usb_handle *usb = 0;
250 int announce = 1;
251
252 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800253
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 for(;;) {
255 usb = usb_open(match_fastboot);
256 if(usb) return usb;
257 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800258 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259 fprintf(stderr,"< waiting for device >\n");
260 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700261 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 }
263}
264
265void list_devices(void) {
266 // We don't actually open a USB device here,
267 // just getting our callback called so we can
268 // list all the connected devices.
269 usb_open(list_devices_callback);
270}
271
272void usage(void)
273{
274 fprintf(stderr,
275/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
276 "usage: fastboot [ <option> ] <command>\n"
277 "\n"
278 "commands:\n"
279 " update <filename> reflash device from update.zip\n"
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700280 " flashall flash boot, system, vendor and if found,\n"
Daniel Rosenberg015d73f2014-09-15 13:44:07 -0700281 " recovery\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 " flash <partition> [ <filename> ] write a file to a flash partition\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700283 " flashing lock locks the device. Prevents flashing\n"
Badhri Jagan Sridharana873e1d2015-05-15 16:43:47 -0700284 " partitions\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700285 " flashing unlock unlocks the device. Allows user to\n"
286 " flash any partition except the ones\n"
Badhri Jagan Sridharana873e1d2015-05-15 16:43:47 -0700287 " that are related to bootloader\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700288 " flashing lock_critical Prevents flashing bootloader related\n"
Badhri Jagan Sridharana873e1d2015-05-15 16:43:47 -0700289 " partitions\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700290 " flashing unlock_critical Enables flashing bootloader related\n"
Badhri Jagan Sridharana873e1d2015-05-15 16:43:47 -0700291 " partitions\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700292 " flashing get_unlock_ability Queries bootloader to see if the\n"
Badhri Jagan Sridharana873e1d2015-05-15 16:43:47 -0700293 " device is unlocked\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700294 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
295 " unlock nonce\n"
296 " flashing unlock_bootloader <request> Issue unlock bootloader using request\n"
297 " flashing lock_bootloader Locks the bootloader to prevent\n"
298 " bootloader version rollback\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 " erase <partition> erase a flash partition\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700300 " format[:[<fs type>][:[<size>]] <partition> format a flash partition.\n"
301 " Can override the fs type and/or\n"
302 " size the bootloader reports.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 " getvar <variable> display a bootloader variable\n"
304 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
305 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
306 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700307 " continue continue with autoboot\n"
Elliott Hughesca85df02015-02-25 10:02:00 -0800308 " reboot [bootloader] reboot device, optionally into bootloader\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800310 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311 "\n"
312 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700313 " -w erase userdata and cache (and format\n"
314 " if supported by partition type)\n"
315 " -u do not first erase partition before\n"
316 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700317 " -s <specific device> specify device serial number\n"
318 " or path to device port\n"
319 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320 " -p <product> specify product name\n"
321 " -c <cmdline> override kernel commandline\n"
322 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700323 " -b <base_addr> specify a custom kernel base address.\n"
324 " default: 0x10000000\n"
325 " -n <page size> specify the nand page size.\n"
326 " default: 2048\n"
327 " -S <size>[K|M|G] automatically sparse files greater\n"
328 " than size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330}
331
JP Abgrall7b8970c2013-03-07 17:06:41 -0800332void *load_bootable_image(const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 unsigned *sz, const char *cmdline)
334{
335 void *kdata = 0, *rdata = 0;
336 unsigned ksize = 0, rsize = 0;
337 void *bdata;
338 unsigned bsize;
339
340 if(kernel == 0) {
341 fprintf(stderr, "no image specified\n");
342 return 0;
343 }
344
345 kdata = load_file(kernel, &ksize);
346 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800347 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 return 0;
349 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800350
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 /* is this actually a boot image? */
352 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
353 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800354
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 if(ramdisk) {
356 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
357 return 0;
358 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800359
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 *sz = ksize;
361 return kdata;
362 }
363
364 if(ramdisk) {
365 rdata = load_file(ramdisk, &rsize);
366 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800367 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368 return 0;
369 }
370 }
371
372 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800373 bdata = mkbootimg(kdata, ksize, kernel_offset,
374 rdata, rsize, ramdisk_offset,
375 0, 0, second_offset,
376 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377 if(bdata == 0) {
378 fprintf(stderr,"failed to create boot.img\n");
379 return 0;
380 }
381 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
382 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
383 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800384
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 return bdata;
386}
387
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700388static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, unsigned* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389{
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700390 ZipEntryName zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700391 ZipEntry zip_entry;
392 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700393 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000394 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 }
396
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700397 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700399 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
400 if (data == NULL) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700401 fprintf(stderr, "failed to allocate %u bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000402 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403 }
404
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700405 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
406 if (error != 0) {
407 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000409 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000411
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 return data;
413}
414
Elliott Hughes8bdd4bb2015-06-03 15:22:11 -0700415#if defined(_WIN32)
416
417// TODO: move this to somewhere it can be shared.
418
419#include <windows.h>
420
421// Windows' tmpfile(3) requires administrator rights because
422// it creates temporary files in the root directory.
423static FILE* win32_tmpfile() {
424 char temp_path[PATH_MAX];
425 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
426 if (nchars == 0 || nchars >= sizeof(temp_path)) {
427 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
428 return nullptr;
429 }
430
431 char filename[PATH_MAX];
432 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
433 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
434 return nullptr;
435 }
436
437 return fopen(filename, "w+bTD");
438}
439
440#define tmpfile win32_tmpfile
441
442#endif
443
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700444static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
445 FILE* fp = tmpfile();
446 if (fp == NULL) {
447 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
448 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700449 return -1;
450 }
451
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700452 ZipEntryName zip_entry_name(entry_name);
453 ZipEntry zip_entry;
454 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
455 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000456 return -1;
457 }
458
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700459 int fd = fileno(fp);
460 int error = ExtractEntryToFile(zip, &zip_entry, fd);
461 if (error != 0) {
462 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
463 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700464 }
465
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000466 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700467 return fd;
468}
469
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470static char *strip(char *s)
471{
472 int n;
473 while(*s && isspace(*s)) s++;
474 n = strlen(s);
475 while(n-- > 0) {
476 if(!isspace(s[n])) break;
477 s[n] = 0;
478 }
479 return s;
480}
481
482#define MAX_OPTIONS 32
483static int setup_requirement_line(char *name)
484{
485 char *val[MAX_OPTIONS];
Wink Savilleb98762f2011-04-04 17:54:59 -0700486 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 unsigned n, count;
488 char *x;
489 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800490
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 if (!strncmp(name, "reject ", 7)) {
492 name += 7;
493 invert = 1;
494 } else if (!strncmp(name, "require ", 8)) {
495 name += 8;
496 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700497 } else if (!strncmp(name, "require-for-product:", 20)) {
498 // Get the product and point name past it
499 prod = name + 20;
500 name = strchr(name, ' ');
501 if (!name) return -1;
502 *name = 0;
503 name += 1;
504 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 }
506
507 x = strchr(name, '=');
508 if (x == 0) return 0;
509 *x = 0;
510 val[0] = x + 1;
511
512 for(count = 1; count < MAX_OPTIONS; count++) {
513 x = strchr(val[count - 1],'|');
514 if (x == 0) break;
515 *x = 0;
516 val[count] = x + 1;
517 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800518
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 name = strip(name);
520 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800521
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522 name = strip(name);
523 if (name == 0) return -1;
524
Elliott Hughes253c18d2015-03-18 22:47:09 -0700525 const char* var = name;
526 // Work around an unfortunate name mismatch.
527 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528
Elliott Hughes253c18d2015-03-18 22:47:09 -0700529 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 if (out == 0) return -1;
531
532 for(n = 0; n < count; n++) {
533 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700534 if (out[n] == 0) {
535 for(size_t i = 0; i < n; ++i) {
536 free((char*) out[i]);
537 }
538 free(out);
539 return -1;
540 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541 }
542
Elliott Hughes253c18d2015-03-18 22:47:09 -0700543 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544 return 0;
545}
546
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000547static void setup_requirements(char *data, unsigned sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000549 char *s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800550
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000551 s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800552 while (sz-- > 0) {
553 if(*s == '\n') {
554 *s++ = 0;
555 if (setup_requirement_line(data)) {
556 die("out of memory");
557 }
558 data = s;
559 } else {
560 s++;
561 }
562 }
563}
564
565void queue_info_dump(void)
566{
567 fb_queue_notice("--------------------------------------------");
568 fb_queue_display("version-bootloader", "Bootloader Version...");
569 fb_queue_display("version-baseband", "Baseband Version.....");
570 fb_queue_display("serialno", "Serial Number........");
571 fb_queue_notice("--------------------------------------------");
572}
573
Rom Lemarchand622810c2013-06-28 09:54:59 -0700574static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700575{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700576 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700577 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700578 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700579 }
580
Elliott Hughes253c18d2015-03-18 22:47:09 -0700581 int files = sparse_file_resparse(s, max_size, NULL, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700582 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700583 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700584 }
585
Elliott Hughes253c18d2015-03-18 22:47:09 -0700586 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700587 if (!out_s) {
588 die("Failed to allocate sparse file array\n");
589 }
590
591 files = sparse_file_resparse(s, max_size, out_s, files);
592 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700593 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700594 }
595
596 return out_s;
597}
598
599static int64_t get_target_sparse_limit(struct usb_handle *usb)
600{
601 int64_t limit = 0;
602 char response[FB_RESPONSE_SZ + 1];
603 int status = fb_getvar(usb, response, "max-download-size");
604
605 if (!status) {
606 limit = strtoul(response, NULL, 0);
607 if (limit > 0) {
Ying Wangcf86e2f2014-05-15 20:06:40 -0700608 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n",
Colin Crossf8387882012-05-24 17:18:41 -0700609 limit);
610 }
611 }
612
613 return limit;
614}
615
616static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
617{
618 int64_t limit;
619
620 if (sparse_limit == 0) {
621 return 0;
622 } else if (sparse_limit > 0) {
623 limit = sparse_limit;
624 } else {
625 if (target_sparse_limit == -1) {
626 target_sparse_limit = get_target_sparse_limit(usb);
627 }
628 if (target_sparse_limit > 0) {
629 limit = target_sparse_limit;
630 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700631 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700632 }
633 }
634
635 if (size > limit) {
636 return limit;
637 }
638
639 return 0;
640}
641
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700642/* Until we get lazy inode table init working in make_ext4fs, we need to
643 * erase partitions of type ext4 before flashing a filesystem so no stale
644 * inodes are left lying around. Otherwise, e2fsck gets very upset.
645 */
Elliott Hughesc688c232015-06-02 13:34:07 -0700646static int needs_erase(usb_handle* usb, const char *part)
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700647{
648 /* The function fb_format_supported() currently returns the value
649 * we want, so just call it.
650 */
JP Abgrall7e859742014-05-06 15:14:15 -0700651 return fb_format_supported(usb, part, NULL);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700652}
653
Rom Lemarchand622810c2013-06-28 09:54:59 -0700654static int load_buf_fd(usb_handle *usb, int fd,
655 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700656{
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700657 int64_t sz64;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000658 void *data;
659 int64_t limit;
Colin Crossf8387882012-05-24 17:18:41 -0700660
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000661
662 sz64 = file_size(fd);
663 if (sz64 < 0) {
664 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700665 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700666
667 lseek(fd, 0, SEEK_SET);
Colin Crossf8387882012-05-24 17:18:41 -0700668 limit = get_sparse_limit(usb, sz64);
669 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700670 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700671 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700672 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700673 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700674 buf->type = FB_BUFFER_SPARSE;
675 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700676 } else {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000677 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700678 data = load_fd(fd, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000679 if (data == 0) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700680 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700681 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000682 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700683 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700684
685 return 0;
686}
687
688static int load_buf(usb_handle *usb, const char *fname,
689 struct fastboot_buffer *buf)
690{
691 int fd;
692
693 fd = open(fname, O_RDONLY | O_BINARY);
694 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700695 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700696 }
697
698 return load_buf_fd(usb, fd, buf);
699}
700
701static void flash_buf(const char *pname, struct fastboot_buffer *buf)
702{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700703 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700704
705 switch (buf->type) {
706 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700707 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700708 while (*s) {
709 int64_t sz64 = sparse_file_len(*s, true, false);
710 fb_queue_flash_sparse(pname, *s++, sz64);
711 }
712 break;
713 case FB_BUFFER:
714 fb_queue_flash(pname, buf->data, buf->sz);
715 break;
716 default:
717 die("unknown buffer type: %d", buf->type);
718 }
719}
720
721void do_flash(usb_handle *usb, const char *pname, const char *fname)
722{
723 struct fastboot_buffer buf;
724
725 if (load_buf(usb, fname, &buf)) {
726 die("cannot load '%s'", fname);
727 }
728 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700729}
730
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700731void do_update_signature(ZipArchiveHandle zip, char *fn)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800732{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000733 unsigned sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700734 void* data = unzip_file(zip, fn, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000735 if (data == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800736 fb_queue_download("signature", data, sz);
737 fb_queue_command("signature", "installing signature");
738}
739
Elliott Hughes253c18d2015-03-18 22:47:09 -0700740void do_update(usb_handle *usb, const char *filename, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800742 queue_info_dump();
743
Wink Savilleb98762f2011-04-04 17:54:59 -0700744 fb_queue_query_save("product", cur_product, sizeof(cur_product));
745
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700746 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700747 int error = OpenArchive(filename, &zip);
748 if (error != 0) {
Narayan Kamath241bcf02015-05-11 16:59:46 +0100749 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700750 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700751 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800752
Elliott Hughes253c18d2015-03-18 22:47:09 -0700753 unsigned sz;
754 void* data = unzip_file(zip, "android-info.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000755 if (data == 0) {
Narayan Kamath241bcf02015-05-11 16:59:46 +0100756 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700757 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800758 }
759
Elliott Hughes253c18d2015-03-18 22:47:09 -0700760 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800761
Elliott Hughes966339b2015-06-02 21:37:05 -0700762 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700763 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughes966339b2015-06-02 21:37:05 -0700764 if (fd == -1) {
765 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700766 continue;
Elliott Hughes966339b2015-06-02 21:37:05 -0700767 }
Narayan Kamath241bcf02015-05-11 16:59:46 +0100768 CloseArchive(zip);
Elliott Hughes966339b2015-06-02 21:37:05 -0700769 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700770 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700771 fastboot_buffer buf;
772 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700773 if (rc) die("cannot load %s from flash", images[i].img_name);
774 do_update_signature(zip, images[i].sig_name);
Elliott Hughesc688c232015-06-02 13:34:07 -0700775 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700776 fb_queue_erase(images[i].part_name);
777 }
778 flash_buf(images[i].part_name, &buf);
779 /* not closing the fd here since the sparse code keeps the fd around
780 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
781 * program exits.
782 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800783 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700784
785 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800786}
787
788void do_send_signature(char *fn)
789{
790 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000791 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800793
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800794 xtn = strrchr(fn, '.');
795 if (!xtn) return;
796 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800797
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798 strcpy(xtn,".sig");
799 data = load_file(fn, &sz);
800 strcpy(xtn,".img");
801 if (data == 0) return;
802 fb_queue_download("signature", data, sz);
803 fb_queue_command("signature", "installing signature");
804}
805
Rom Lemarchand622810c2013-06-28 09:54:59 -0700806void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800807{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800808 queue_info_dump();
809
Wink Savilleb98762f2011-04-04 17:54:59 -0700810 fb_queue_query_save("product", cur_product, sizeof(cur_product));
811
Elliott Hughes253c18d2015-03-18 22:47:09 -0700812 char* fname = find_item("info", product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800813 if (fname == 0) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800814
Elliott Hughes253c18d2015-03-18 22:47:09 -0700815 unsigned sz;
816 void* data = load_file(fname, &sz);
817 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
818
819 setup_requirements(reinterpret_cast<char*>(data), sz);
820
821 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700822 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700823 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700824 if (load_buf(usb, fname, &buf)) {
825 if (images[i].is_optional)
826 continue;
827 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700828 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700829 do_send_signature(fname);
Elliott Hughesc688c232015-06-02 13:34:07 -0700830 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700831 fb_queue_erase(images[i].part_name);
832 }
833 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800834 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800835}
836
837#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800838#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800839
Patrick Tjin51e8b032015-09-01 08:15:23 -0700840int do_bypass_unlock_command(int argc, char **argv)
841{
842 unsigned sz;
843 void *data;
844
845 if (argc <= 2) return 0;
846 skip(2);
847
848 /*
849 * Process unlock_bootloader, we have to load the message file
850 * and send that to the remote device.
851 */
852 require(1);
853 data = load_file(*argv, &sz);
854 if (data == 0) die("could not load '%s': %s", *argv, strerror(errno));
855 fb_queue_download("unlock_message", data, sz);
856 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
857 skip(1);
858 return 0;
859}
860
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800861int do_oem_command(int argc, char **argv)
862{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800863 char command[256];
864 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800865
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800866 command[0] = 0;
867 while(1) {
868 strcat(command,*argv);
869 skip(1);
870 if(argc == 0) break;
871 strcat(command," ");
872 }
873
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800874 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800875 return 0;
876}
877
Colin Crossf8387882012-05-24 17:18:41 -0700878static int64_t parse_num(const char *arg)
879{
880 char *endptr;
881 unsigned long long num;
882
883 num = strtoull(arg, &endptr, 0);
884 if (endptr == arg) {
885 return -1;
886 }
887
888 if (*endptr == 'k' || *endptr == 'K') {
889 if (num >= (-1ULL) / 1024) {
890 return -1;
891 }
892 num *= 1024LL;
893 endptr++;
894 } else if (*endptr == 'm' || *endptr == 'M') {
895 if (num >= (-1ULL) / (1024 * 1024)) {
896 return -1;
897 }
898 num *= 1024LL * 1024LL;
899 endptr++;
900 } else if (*endptr == 'g' || *endptr == 'G') {
901 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
902 return -1;
903 }
904 num *= 1024LL * 1024LL * 1024LL;
905 endptr++;
906 }
907
908 if (*endptr != '\0') {
909 return -1;
910 }
911
912 if (num > INT64_MAX) {
913 return -1;
914 }
915
916 return num;
917}
918
Elliott Hughesc688c232015-06-02 13:34:07 -0700919void fb_perform_format(usb_handle* usb,
920 const char *partition, int skip_if_not_supported,
JP Abgrall7e859742014-05-06 15:14:15 -0700921 const char *type_override, const char *size_override)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700922{
JP Abgrall7e859742014-05-06 15:14:15 -0700923 char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1];
924 char *pType = pTypeBuff;
925 char *pSize = pSizeBuff;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700926 unsigned int limit = INT_MAX;
927 struct fastboot_buffer buf;
928 const char *errMsg = NULL;
929 const struct fs_generator *gen;
930 uint64_t pSz;
931 int status;
932 int fd;
933
934 if (target_sparse_limit > 0 && target_sparse_limit < limit)
935 limit = target_sparse_limit;
936 if (sparse_limit > 0 && sparse_limit < limit)
937 limit = sparse_limit;
938
939 status = fb_getvar(usb, pType, "partition-type:%s", partition);
940 if (status) {
941 errMsg = "Can't determine partition type.\n";
942 goto failed;
943 }
JP Abgrall7e859742014-05-06 15:14:15 -0700944 if (type_override) {
945 if (strcmp(type_override, pType)) {
946 fprintf(stderr,
947 "Warning: %s type is %s, but %s was requested for formating.\n",
948 partition, pType, type_override);
949 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700950 pType = (char *)type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700951 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700952
953 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
954 if (status) {
955 errMsg = "Unable to get partition size\n";
956 goto failed;
957 }
JP Abgrall7e859742014-05-06 15:14:15 -0700958 if (size_override) {
959 if (strcmp(size_override, pSize)) {
960 fprintf(stderr,
961 "Warning: %s size is %s, but %s was requested for formating.\n",
962 partition, pSize, size_override);
963 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700964 pSize = (char *)size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700965 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700966
967 gen = fs_get_generator(pType);
968 if (!gen) {
969 if (skip_if_not_supported) {
970 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
971 fprintf(stderr, "File system type %s not supported.\n", pType);
972 return;
973 }
974 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
975 return;
976 }
977
978 pSz = strtoll(pSize, (char **)NULL, 16);
979
980 fd = fileno(tmpfile());
981 if (fs_generator_generate(gen, fd, pSz)) {
982 close(fd);
983 fprintf(stderr, "Cannot generate image.\n");
984 return;
985 }
986
987 if (load_buf_fd(usb, fd, &buf)) {
988 fprintf(stderr, "Cannot read image.\n");
989 close(fd);
990 return;
991 }
992 flash_buf(partition, &buf);
993
994 return;
995
996
997failed:
998 if (skip_if_not_supported) {
999 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
1000 if (errMsg)
1001 fprintf(stderr, "%s", errMsg);
1002 }
1003 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1004}
1005
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001006int main(int argc, char **argv)
1007{
1008 int wants_wipe = 0;
1009 int wants_reboot = 0;
1010 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001011 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001012 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +00001013 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001014 int status;
Colin Cross8879f982012-05-22 17:53:34 -07001015 int c;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001016 int longindex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001017
JP Abgrall7b8970c2013-03-07 17:06:41 -08001018 const struct option longopts[] = {
1019 {"base", required_argument, 0, 'b'},
1020 {"kernel_offset", required_argument, 0, 'k'},
1021 {"page_size", required_argument, 0, 'n'},
1022 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001023 {"tags_offset", required_argument, 0, 't'},
Elliott Hughese283ca22015-06-02 13:50:00 -07001024 {"help", no_argument, 0, 'h'},
1025 {"unbuffered", no_argument, 0, 0},
1026 {"version", no_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001027 {0, 0, 0, 0}
1028 };
Colin Cross8879f982012-05-22 17:53:34 -07001029
1030 serial = getenv("ANDROID_SERIAL");
1031
1032 while (1) {
Florian Bäuerle27ded482014-11-24 11:29:34 +01001033 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 -07001034 if (c < 0) {
1035 break;
1036 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001037 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001038 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -07001039 case 'b':
1040 base_addr = strtoul(optarg, 0, 16);
1041 break;
Colin Cross8879f982012-05-22 17:53:34 -07001042 case 'c':
1043 cmdline = optarg;
1044 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001045 case 'h':
1046 usage();
1047 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001048 case 'i': {
1049 char *endptr = NULL;
1050 unsigned long val;
1051
1052 val = strtoul(optarg, &endptr, 0);
1053 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1054 die("invalid vendor id '%s'", optarg);
1055 vendor_id = (unsigned short)val;
1056 break;
1057 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001058 case 'k':
1059 kernel_offset = strtoul(optarg, 0, 16);
1060 break;
1061 case 'l':
1062 long_listing = 1;
1063 break;
1064 case 'n':
1065 page_size = (unsigned)strtoul(optarg, NULL, 0);
1066 if (!page_size) die("invalid page size");
1067 break;
1068 case 'p':
1069 product = optarg;
1070 break;
1071 case 'r':
1072 ramdisk_offset = strtoul(optarg, 0, 16);
1073 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001074 case 't':
1075 tags_offset = strtoul(optarg, 0, 16);
1076 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001077 case 's':
1078 serial = optarg;
1079 break;
1080 case 'S':
1081 sparse_limit = parse_num(optarg);
1082 if (sparse_limit < 0) {
1083 die("invalid sparse limit");
1084 }
1085 break;
1086 case 'u':
1087 erase_first = 0;
1088 break;
1089 case 'w':
1090 wants_wipe = 1;
1091 break;
Colin Cross8879f982012-05-22 17:53:34 -07001092 case '?':
1093 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001094 case 0:
1095 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
1096 setvbuf(stdout, NULL, _IONBF, 0);
1097 setvbuf(stderr, NULL, _IONBF, 0);
Elliott Hughese283ca22015-06-02 13:50:00 -07001098 } else if (strcmp("version", longopts[longindex].name) == 0) {
1099 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1100 return 0;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001101 }
1102 break;
Colin Cross8879f982012-05-22 17:53:34 -07001103 default:
1104 abort();
1105 }
1106 }
1107
1108 argc -= optind;
1109 argv += optind;
1110
1111 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001112 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001113 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001114 }
1115
Colin Cross8fb6e062012-07-24 16:36:41 -07001116 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001117 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001118 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001119 return 0;
1120 }
1121
Colin Crossc7b75dc2012-08-29 18:17:06 -07001122 if (argc > 0 && !strcmp(*argv, "help")) {
1123 usage();
1124 return 0;
1125 }
1126
Elliott Hughesc688c232015-06-02 13:34:07 -07001127 usb_handle* usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001128
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001129 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001130 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001131 require(2);
1132 fb_queue_display(argv[1], argv[1]);
1133 skip(2);
1134 } else if(!strcmp(*argv, "erase")) {
1135 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001136
JP Abgrall7e859742014-05-06 15:14:15 -07001137 if (fb_format_supported(usb, argv[1], NULL)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001138 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1139 }
1140
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001141 fb_queue_erase(argv[1]);
1142 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001143 } else if(!strncmp(*argv, "format", strlen("format"))) {
1144 char *overrides;
1145 char *type_override = NULL;
1146 char *size_override = NULL;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001147 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001148 /*
1149 * Parsing for: "format[:[type][:[size]]]"
1150 * Some valid things:
1151 * - select ontly the size, and leave default fs type:
1152 * format::0x4000000 userdata
1153 * - default fs type and size:
1154 * format userdata
1155 * format:: userdata
1156 */
1157 overrides = strchr(*argv, ':');
1158 if (overrides) {
1159 overrides++;
1160 size_override = strchr(overrides, ':');
1161 if (size_override) {
1162 size_override[0] = '\0';
1163 size_override++;
1164 }
1165 type_override = overrides;
1166 }
1167 if (type_override && !type_override[0]) type_override = NULL;
1168 if (size_override && !size_override[0]) size_override = NULL;
Elliott Hughesc688c232015-06-02 13:34:07 -07001169 if (erase_first && needs_erase(usb, argv[1])) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001170 fb_queue_erase(argv[1]);
1171 }
Elliott Hughesc688c232015-06-02 13:34:07 -07001172 fb_perform_format(usb, argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001173 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001174 } else if(!strcmp(*argv, "signature")) {
1175 require(2);
1176 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001177 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001178 if (sz != 256) die("signature must be 256 bytes");
1179 fb_queue_download("signature", data, sz);
1180 fb_queue_command("signature", "installing signature");
1181 skip(2);
1182 } else if(!strcmp(*argv, "reboot")) {
1183 wants_reboot = 1;
1184 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001185 if (argc > 0) {
1186 if (!strcmp(*argv, "bootloader")) {
1187 wants_reboot = 0;
1188 wants_reboot_bootloader = 1;
1189 skip(1);
1190 }
1191 }
1192 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001193 } else if(!strcmp(*argv, "reboot-bootloader")) {
1194 wants_reboot_bootloader = 1;
1195 skip(1);
1196 } else if (!strcmp(*argv, "continue")) {
1197 fb_queue_command("continue", "resuming boot");
1198 skip(1);
1199 } else if(!strcmp(*argv, "boot")) {
1200 char *kname = 0;
1201 char *rname = 0;
1202 skip(1);
1203 if (argc > 0) {
1204 kname = argv[0];
1205 skip(1);
1206 }
1207 if (argc > 0) {
1208 rname = argv[0];
1209 skip(1);
1210 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001211 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001212 if (data == 0) return 1;
1213 fb_queue_download("boot.img", data, sz);
1214 fb_queue_command("boot", "booting");
1215 } else if(!strcmp(*argv, "flash")) {
1216 char *pname = argv[1];
1217 char *fname = 0;
1218 require(2);
1219 if (argc > 2) {
1220 fname = argv[2];
1221 skip(3);
1222 } else {
1223 fname = find_item(pname, product);
1224 skip(2);
1225 }
1226 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Elliott Hughesc688c232015-06-02 13:34:07 -07001227 if (erase_first && needs_erase(usb, pname)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001228 fb_queue_erase(pname);
1229 }
Colin Crossf8387882012-05-24 17:18:41 -07001230 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001231 } else if(!strcmp(*argv, "flash:raw")) {
1232 char *pname = argv[1];
1233 char *kname = argv[2];
1234 char *rname = 0;
1235 require(3);
1236 if(argc > 3) {
1237 rname = argv[3];
1238 skip(4);
1239 } else {
1240 skip(3);
1241 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001242 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001243 if (data == 0) die("cannot load bootable image");
1244 fb_queue_flash(pname, data, sz);
1245 } else if(!strcmp(*argv, "flashall")) {
1246 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001247 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001248 wants_reboot = 1;
1249 } else if(!strcmp(*argv, "update")) {
1250 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001251 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001252 skip(2);
1253 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001254 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001255 skip(1);
1256 }
1257 wants_reboot = 1;
1258 } else if(!strcmp(*argv, "oem")) {
1259 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001260 } else if(!strcmp(*argv, "flashing")) {
1261 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1262 !strcmp(*(argv+1), "lock") ||
1263 !strcmp(*(argv+1), "unlock_critical") ||
1264 !strcmp(*(argv+1), "lock_critical") ||
1265 !strcmp(*(argv+1), "get_unlock_ability") ||
1266 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1267 !strcmp(*(argv+1), "lock_bootloader"))) {
1268 argc = do_oem_command(argc, argv);
1269 } else
1270 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1271 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharana873e1d2015-05-15 16:43:47 -07001272 } else {
1273 usage();
1274 return 1;
1275 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001276 } else {
1277 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001278 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001279 }
1280 }
1281
1282 if (wants_wipe) {
1283 fb_queue_erase("userdata");
Elliott Hughesc688c232015-06-02 13:34:07 -07001284 fb_perform_format(usb, "userdata", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001285 fb_queue_erase("cache");
Elliott Hughesc688c232015-06-02 13:34:07 -07001286 fb_perform_format(usb, "cache", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001287 }
1288 if (wants_reboot) {
1289 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001290 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001291 } else if (wants_reboot_bootloader) {
1292 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001293 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001294 }
1295
Scott Anderson13081c62012-04-06 12:39:30 -07001296 if (fb_queue_is_empty())
1297 return 0;
1298
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001299 status = fb_execute_queue(usb);
1300 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001301}