blob: 5d7b1511a09ba38dd619f5ad0eb66729659e9aab [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
Elliott Hughese1746fd2015-08-10 15:30:54 -0700192int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
193 // Require a matching vendor id if the user specified one with -i.
194 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
195 return -1;
196 }
197
198 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
199 return -1;
200 }
201
Scott Anderson13081c62012-04-06 12:39:30 -0700202 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700204 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
205 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 return 0;
207}
208
JP Abgrall7b8970c2013-03-07 17:06:41 -0800209int match_fastboot(usb_ifc_info *info)
210{
211 return match_fastboot_with_serial(info, serial);
212}
213
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214int list_devices_callback(usb_ifc_info *info)
215{
JP Abgralla032ded2012-06-06 11:53:33 -0700216 if (match_fastboot_with_serial(info, NULL) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700217 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700218 if (!info->writable) {
219 serial = "no permissions"; // like "adb devices"
220 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 if (!serial[0]) {
222 serial = "????????????";
223 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700224 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700225 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700226 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800227 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700228 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700229 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700230 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700231 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 }
233
234 return -1;
235}
236
237usb_handle *open_device(void)
238{
239 static usb_handle *usb = 0;
240 int announce = 1;
241
242 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800243
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 for(;;) {
245 usb = usb_open(match_fastboot);
246 if(usb) return usb;
247 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800248 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 fprintf(stderr,"< waiting for device >\n");
250 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700251 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 }
253}
254
255void list_devices(void) {
256 // We don't actually open a USB device here,
257 // just getting our callback called so we can
258 // list all the connected devices.
259 usb_open(list_devices_callback);
260}
261
262void usage(void)
263{
264 fprintf(stderr,
265/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
266 "usage: fastboot [ <option> ] <command>\n"
267 "\n"
268 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700269 " update <filename> Reflash device from update.zip.\n"
270 " flashall Flash boot, system, vendor, and --\n"
271 " if found -- recovery.\n"
272 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
273 " flashing lock Locks the device. Prevents flashing.\n"
274 " flashing unlock Unlocks the device. Allows flashing\n"
275 " any partition except\n"
276 " bootloader-related partitions.\n"
277 " flashing lock_critical Prevents flashing bootloader-related\n"
278 " partitions.\n"
279 " flashing unlock_critical Enables flashing bootloader-related\n"
280 " partitions.\n"
281 " flashing get_unlock_ability Queries bootloader to see if the\n"
282 " device is unlocked.\n"
283 " erase <partition> Erase a flash partition.\n"
284 " format[:[<fs type>][:[<size>]] <partition>\n"
285 " Format a flash partition. Can\n"
286 " override the fs type and/or size\n"
287 " the bootloader reports.\n"
288 " getvar <variable> Display a bootloader variable.\n"
289 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
290 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
291 " Create bootimage and flash it.\n"
292 " devices [-l] List all connected devices [with\n"
293 " device paths].\n"
294 " continue Continue with autoboot.\n"
295 " reboot [bootloader] Reboot device [into bootloader].\n"
296 " reboot-bootloader Reboot device into bootloader.\n"
297 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 "\n"
299 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700300 " -w Erase userdata and cache (and format\n"
301 " if supported by partition type).\n"
302 " -u Do not erase partition before\n"
303 " formatting.\n"
304 " -s <specific device> Specify device serial number\n"
305 " or path to device port.\n"
306 " -p <product> Specify product name.\n"
307 " -c <cmdline> Override kernel commandline.\n"
308 " -i <vendor id> Specify a custom USB vendor id.\n"
309 " -b <base_addr> Specify a custom kernel base\n"
310 " address (default: 0x10000000).\n"
311 " -n <page size> Specify the nand page size\n"
312 " (default: 2048).\n"
313 " -S <size>[K|M|G] Automatically sparse files greater\n"
314 " than 'size'. 0 to disable.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316}
317
JP Abgrall7b8970c2013-03-07 17:06:41 -0800318void *load_bootable_image(const char *kernel, const char *ramdisk,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200319 const char *secondstage, unsigned *sz,
320 const char *cmdline)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321{
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200322 void *kdata = 0, *rdata = 0, *sdata = 0;
323 unsigned ksize = 0, rsize = 0, ssize = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 void *bdata;
325 unsigned bsize;
326
327 if(kernel == 0) {
328 fprintf(stderr, "no image specified\n");
329 return 0;
330 }
331
332 kdata = load_file(kernel, &ksize);
333 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800334 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 return 0;
336 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800337
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 /* is this actually a boot image? */
339 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
340 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800341
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342 if(ramdisk) {
343 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
344 return 0;
345 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800346
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 *sz = ksize;
348 return kdata;
349 }
350
351 if(ramdisk) {
352 rdata = load_file(ramdisk, &rsize);
353 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800354 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 return 0;
356 }
357 }
358
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200359 if (secondstage) {
360 sdata = load_file(secondstage, &ssize);
361 if(sdata == 0) {
362 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
363 return 0;
364 }
365 }
366
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800368 bdata = mkbootimg(kdata, ksize, kernel_offset,
369 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200370 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800371 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 if(bdata == 0) {
373 fprintf(stderr,"failed to create boot.img\n");
374 return 0;
375 }
376 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
377 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
378 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800379
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 return bdata;
381}
382
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700383static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, unsigned* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384{
Yusuke Sato07447542015-06-25 14:39:19 -0700385 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700386 ZipEntry zip_entry;
387 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700388 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000389 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 }
391
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700392 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700394 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
395 if (data == NULL) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700396 fprintf(stderr, "failed to allocate %u bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000397 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 }
399
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700400 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
401 if (error != 0) {
402 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000404 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000406
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407 return data;
408}
409
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700410#if defined(_WIN32)
411
412// TODO: move this to somewhere it can be shared.
413
414#include <windows.h>
415
416// Windows' tmpfile(3) requires administrator rights because
417// it creates temporary files in the root directory.
418static FILE* win32_tmpfile() {
419 char temp_path[PATH_MAX];
420 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
421 if (nchars == 0 || nchars >= sizeof(temp_path)) {
422 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
423 return nullptr;
424 }
425
426 char filename[PATH_MAX];
427 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
428 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
429 return nullptr;
430 }
431
432 return fopen(filename, "w+bTD");
433}
434
435#define tmpfile win32_tmpfile
436
437#endif
438
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700439static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
440 FILE* fp = tmpfile();
441 if (fp == NULL) {
442 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
443 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700444 return -1;
445 }
446
Yusuke Sato07447542015-06-25 14:39:19 -0700447 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700448 ZipEntry zip_entry;
449 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
450 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000451 return -1;
452 }
453
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700454 int fd = fileno(fp);
455 int error = ExtractEntryToFile(zip, &zip_entry, fd);
456 if (error != 0) {
457 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
458 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700459 }
460
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000461 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700462 return fd;
463}
464
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465static char *strip(char *s)
466{
467 int n;
468 while(*s && isspace(*s)) s++;
469 n = strlen(s);
470 while(n-- > 0) {
471 if(!isspace(s[n])) break;
472 s[n] = 0;
473 }
474 return s;
475}
476
477#define MAX_OPTIONS 32
478static int setup_requirement_line(char *name)
479{
480 char *val[MAX_OPTIONS];
Wink Savilleb98762f2011-04-04 17:54:59 -0700481 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482 unsigned n, count;
483 char *x;
484 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800485
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486 if (!strncmp(name, "reject ", 7)) {
487 name += 7;
488 invert = 1;
489 } else if (!strncmp(name, "require ", 8)) {
490 name += 8;
491 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700492 } else if (!strncmp(name, "require-for-product:", 20)) {
493 // Get the product and point name past it
494 prod = name + 20;
495 name = strchr(name, ' ');
496 if (!name) return -1;
497 *name = 0;
498 name += 1;
499 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 }
501
502 x = strchr(name, '=');
503 if (x == 0) return 0;
504 *x = 0;
505 val[0] = x + 1;
506
507 for(count = 1; count < MAX_OPTIONS; count++) {
508 x = strchr(val[count - 1],'|');
509 if (x == 0) break;
510 *x = 0;
511 val[count] = x + 1;
512 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800513
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514 name = strip(name);
515 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800516
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 name = strip(name);
518 if (name == 0) return -1;
519
Elliott Hughes253c18d2015-03-18 22:47:09 -0700520 const char* var = name;
521 // Work around an unfortunate name mismatch.
522 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523
Elliott Hughes253c18d2015-03-18 22:47:09 -0700524 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 if (out == 0) return -1;
526
527 for(n = 0; n < count; n++) {
528 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700529 if (out[n] == 0) {
530 for(size_t i = 0; i < n; ++i) {
531 free((char*) out[i]);
532 }
533 free(out);
534 return -1;
535 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536 }
537
Elliott Hughes253c18d2015-03-18 22:47:09 -0700538 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539 return 0;
540}
541
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000542static void setup_requirements(char *data, unsigned sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000544 char *s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000546 s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800547 while (sz-- > 0) {
548 if(*s == '\n') {
549 *s++ = 0;
550 if (setup_requirement_line(data)) {
551 die("out of memory");
552 }
553 data = s;
554 } else {
555 s++;
556 }
557 }
558}
559
560void queue_info_dump(void)
561{
562 fb_queue_notice("--------------------------------------------");
563 fb_queue_display("version-bootloader", "Bootloader Version...");
564 fb_queue_display("version-baseband", "Baseband Version.....");
565 fb_queue_display("serialno", "Serial Number........");
566 fb_queue_notice("--------------------------------------------");
567}
568
Rom Lemarchand622810c2013-06-28 09:54:59 -0700569static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700570{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700571 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700572 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700573 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700574 }
575
Elliott Hughes253c18d2015-03-18 22:47:09 -0700576 int files = sparse_file_resparse(s, max_size, NULL, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700577 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700578 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700579 }
580
Elliott Hughes253c18d2015-03-18 22:47:09 -0700581 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700582 if (!out_s) {
583 die("Failed to allocate sparse file array\n");
584 }
585
586 files = sparse_file_resparse(s, max_size, out_s, files);
587 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700588 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700589 }
590
591 return out_s;
592}
593
594static int64_t get_target_sparse_limit(struct usb_handle *usb)
595{
596 int64_t limit = 0;
597 char response[FB_RESPONSE_SZ + 1];
598 int status = fb_getvar(usb, response, "max-download-size");
599
600 if (!status) {
601 limit = strtoul(response, NULL, 0);
602 if (limit > 0) {
Ying Wangcf86e2f2014-05-15 20:06:40 -0700603 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n",
Colin Crossf8387882012-05-24 17:18:41 -0700604 limit);
605 }
606 }
607
608 return limit;
609}
610
611static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
612{
613 int64_t limit;
614
615 if (sparse_limit == 0) {
616 return 0;
617 } else if (sparse_limit > 0) {
618 limit = sparse_limit;
619 } else {
620 if (target_sparse_limit == -1) {
621 target_sparse_limit = get_target_sparse_limit(usb);
622 }
623 if (target_sparse_limit > 0) {
624 limit = target_sparse_limit;
625 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700626 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700627 }
628 }
629
630 if (size > limit) {
631 return limit;
632 }
633
634 return 0;
635}
636
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700637/* Until we get lazy inode table init working in make_ext4fs, we need to
638 * erase partitions of type ext4 before flashing a filesystem so no stale
639 * inodes are left lying around. Otherwise, e2fsck gets very upset.
640 */
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700641static int needs_erase(usb_handle* usb, const char *part)
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700642{
643 /* The function fb_format_supported() currently returns the value
644 * we want, so just call it.
645 */
JP Abgrall7e859742014-05-06 15:14:15 -0700646 return fb_format_supported(usb, part, NULL);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700647}
648
Rom Lemarchand622810c2013-06-28 09:54:59 -0700649static int load_buf_fd(usb_handle *usb, int fd,
650 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700651{
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700652 int64_t sz64;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000653 void *data;
654 int64_t limit;
Colin Crossf8387882012-05-24 17:18:41 -0700655
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000656
657 sz64 = file_size(fd);
658 if (sz64 < 0) {
659 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700660 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700661
662 lseek(fd, 0, SEEK_SET);
Colin Crossf8387882012-05-24 17:18:41 -0700663 limit = get_sparse_limit(usb, sz64);
664 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700665 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700666 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700667 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700668 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700669 buf->type = FB_BUFFER_SPARSE;
670 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700671 } else {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000672 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700673 data = load_fd(fd, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000674 if (data == 0) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700675 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700676 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000677 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700678 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700679
680 return 0;
681}
682
683static int load_buf(usb_handle *usb, const char *fname,
684 struct fastboot_buffer *buf)
685{
686 int fd;
687
688 fd = open(fname, O_RDONLY | O_BINARY);
689 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700690 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700691 }
692
693 return load_buf_fd(usb, fd, buf);
694}
695
696static void flash_buf(const char *pname, struct fastboot_buffer *buf)
697{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700698 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700699
700 switch (buf->type) {
701 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700702 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700703 while (*s) {
704 int64_t sz64 = sparse_file_len(*s, true, false);
705 fb_queue_flash_sparse(pname, *s++, sz64);
706 }
707 break;
708 case FB_BUFFER:
709 fb_queue_flash(pname, buf->data, buf->sz);
710 break;
711 default:
712 die("unknown buffer type: %d", buf->type);
713 }
714}
715
716void do_flash(usb_handle *usb, const char *pname, const char *fname)
717{
718 struct fastboot_buffer buf;
719
720 if (load_buf(usb, fname, &buf)) {
721 die("cannot load '%s'", fname);
722 }
723 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700724}
725
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700726void do_update_signature(ZipArchiveHandle zip, char *fn)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800727{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000728 unsigned sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700729 void* data = unzip_file(zip, fn, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000730 if (data == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800731 fb_queue_download("signature", data, sz);
732 fb_queue_command("signature", "installing signature");
733}
734
Elliott Hughes253c18d2015-03-18 22:47:09 -0700735void do_update(usb_handle *usb, const char *filename, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800736{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800737 queue_info_dump();
738
Wink Savilleb98762f2011-04-04 17:54:59 -0700739 fb_queue_query_save("product", cur_product, sizeof(cur_product));
740
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700741 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700742 int error = OpenArchive(filename, &zip);
743 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100744 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700745 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700746 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800747
Elliott Hughes253c18d2015-03-18 22:47:09 -0700748 unsigned sz;
749 void* data = unzip_file(zip, "android-info.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000750 if (data == 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100751 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700752 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753 }
754
Elliott Hughes253c18d2015-03-18 22:47:09 -0700755 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756
Elliott Hughesacdbe922015-06-02 21:37:05 -0700757 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700758 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700759 if (fd == -1) {
760 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700761 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700762 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100763 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700764 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700765 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700766 fastboot_buffer buf;
767 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700768 if (rc) die("cannot load %s from flash", images[i].img_name);
769 do_update_signature(zip, images[i].sig_name);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700770 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700771 fb_queue_erase(images[i].part_name);
772 }
773 flash_buf(images[i].part_name, &buf);
774 /* not closing the fd here since the sparse code keeps the fd around
775 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
776 * program exits.
777 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800778 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700779
780 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800781}
782
783void do_send_signature(char *fn)
784{
785 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000786 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800787 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800788
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800789 xtn = strrchr(fn, '.');
790 if (!xtn) return;
791 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800792
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800793 strcpy(xtn,".sig");
794 data = load_file(fn, &sz);
795 strcpy(xtn,".img");
796 if (data == 0) return;
797 fb_queue_download("signature", data, sz);
798 fb_queue_command("signature", "installing signature");
799}
800
Rom Lemarchand622810c2013-06-28 09:54:59 -0700801void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800802{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800803 queue_info_dump();
804
Wink Savilleb98762f2011-04-04 17:54:59 -0700805 fb_queue_query_save("product", cur_product, sizeof(cur_product));
806
Elliott Hughes253c18d2015-03-18 22:47:09 -0700807 char* fname = find_item("info", product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800808 if (fname == 0) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809
Elliott Hughes253c18d2015-03-18 22:47:09 -0700810 unsigned sz;
811 void* data = load_file(fname, &sz);
812 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
813
814 setup_requirements(reinterpret_cast<char*>(data), sz);
815
816 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700817 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700818 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700819 if (load_buf(usb, fname, &buf)) {
820 if (images[i].is_optional)
821 continue;
822 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700823 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700824 do_send_signature(fname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700825 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700826 fb_queue_erase(images[i].part_name);
827 }
828 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800829 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800830}
831
832#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800833#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800834
835int do_oem_command(int argc, char **argv)
836{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800837 char command[256];
838 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800839
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800840 command[0] = 0;
841 while(1) {
842 strcat(command,*argv);
843 skip(1);
844 if(argc == 0) break;
845 strcat(command," ");
846 }
847
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800848 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800849 return 0;
850}
851
Colin Crossf8387882012-05-24 17:18:41 -0700852static int64_t parse_num(const char *arg)
853{
854 char *endptr;
855 unsigned long long num;
856
857 num = strtoull(arg, &endptr, 0);
858 if (endptr == arg) {
859 return -1;
860 }
861
862 if (*endptr == 'k' || *endptr == 'K') {
863 if (num >= (-1ULL) / 1024) {
864 return -1;
865 }
866 num *= 1024LL;
867 endptr++;
868 } else if (*endptr == 'm' || *endptr == 'M') {
869 if (num >= (-1ULL) / (1024 * 1024)) {
870 return -1;
871 }
872 num *= 1024LL * 1024LL;
873 endptr++;
874 } else if (*endptr == 'g' || *endptr == 'G') {
875 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
876 return -1;
877 }
878 num *= 1024LL * 1024LL * 1024LL;
879 endptr++;
880 }
881
882 if (*endptr != '\0') {
883 return -1;
884 }
885
886 if (num > INT64_MAX) {
887 return -1;
888 }
889
890 return num;
891}
892
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700893void fb_perform_format(usb_handle* usb,
894 const char *partition, int skip_if_not_supported,
JP Abgrall7e859742014-05-06 15:14:15 -0700895 const char *type_override, const char *size_override)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700896{
JP Abgrall7e859742014-05-06 15:14:15 -0700897 char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1];
898 char *pType = pTypeBuff;
899 char *pSize = pSizeBuff;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700900 unsigned int limit = INT_MAX;
901 struct fastboot_buffer buf;
902 const char *errMsg = NULL;
903 const struct fs_generator *gen;
904 uint64_t pSz;
905 int status;
906 int fd;
907
908 if (target_sparse_limit > 0 && target_sparse_limit < limit)
909 limit = target_sparse_limit;
910 if (sparse_limit > 0 && sparse_limit < limit)
911 limit = sparse_limit;
912
913 status = fb_getvar(usb, pType, "partition-type:%s", partition);
914 if (status) {
915 errMsg = "Can't determine partition type.\n";
916 goto failed;
917 }
JP Abgrall7e859742014-05-06 15:14:15 -0700918 if (type_override) {
919 if (strcmp(type_override, pType)) {
920 fprintf(stderr,
921 "Warning: %s type is %s, but %s was requested for formating.\n",
922 partition, pType, type_override);
923 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700924 pType = (char *)type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700925 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700926
927 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
928 if (status) {
929 errMsg = "Unable to get partition size\n";
930 goto failed;
931 }
JP Abgrall7e859742014-05-06 15:14:15 -0700932 if (size_override) {
933 if (strcmp(size_override, pSize)) {
934 fprintf(stderr,
935 "Warning: %s size is %s, but %s was requested for formating.\n",
936 partition, pSize, size_override);
937 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700938 pSize = (char *)size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700939 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700940
941 gen = fs_get_generator(pType);
942 if (!gen) {
943 if (skip_if_not_supported) {
944 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
945 fprintf(stderr, "File system type %s not supported.\n", pType);
946 return;
947 }
948 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
949 return;
950 }
951
952 pSz = strtoll(pSize, (char **)NULL, 16);
953
954 fd = fileno(tmpfile());
955 if (fs_generator_generate(gen, fd, pSz)) {
956 close(fd);
957 fprintf(stderr, "Cannot generate image.\n");
958 return;
959 }
960
961 if (load_buf_fd(usb, fd, &buf)) {
962 fprintf(stderr, "Cannot read image.\n");
963 close(fd);
964 return;
965 }
966 flash_buf(partition, &buf);
967
968 return;
969
970
971failed:
972 if (skip_if_not_supported) {
973 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
974 if (errMsg)
975 fprintf(stderr, "%s", errMsg);
976 }
977 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
978}
979
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800980int main(int argc, char **argv)
981{
982 int wants_wipe = 0;
983 int wants_reboot = 0;
984 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700985 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800986 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000987 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700988 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700989 int c;
Florian Bäuerle27ded482014-11-24 11:29:34 +0100990 int longindex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800991
JP Abgrall7b8970c2013-03-07 17:06:41 -0800992 const struct option longopts[] = {
993 {"base", required_argument, 0, 'b'},
994 {"kernel_offset", required_argument, 0, 'k'},
995 {"page_size", required_argument, 0, 'n'},
996 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800997 {"tags_offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -0700998 {"help", no_argument, 0, 'h'},
999 {"unbuffered", no_argument, 0, 0},
1000 {"version", no_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001001 {0, 0, 0, 0}
1002 };
Colin Cross8879f982012-05-22 17:53:34 -07001003
1004 serial = getenv("ANDROID_SERIAL");
1005
1006 while (1) {
Florian Bäuerle27ded482014-11-24 11:29:34 +01001007 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 -07001008 if (c < 0) {
1009 break;
1010 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001011 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001012 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -07001013 case 'b':
1014 base_addr = strtoul(optarg, 0, 16);
1015 break;
Colin Cross8879f982012-05-22 17:53:34 -07001016 case 'c':
1017 cmdline = optarg;
1018 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001019 case 'h':
1020 usage();
1021 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001022 case 'i': {
1023 char *endptr = NULL;
1024 unsigned long val;
1025
1026 val = strtoul(optarg, &endptr, 0);
1027 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1028 die("invalid vendor id '%s'", optarg);
1029 vendor_id = (unsigned short)val;
1030 break;
1031 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001032 case 'k':
1033 kernel_offset = strtoul(optarg, 0, 16);
1034 break;
1035 case 'l':
1036 long_listing = 1;
1037 break;
1038 case 'n':
1039 page_size = (unsigned)strtoul(optarg, NULL, 0);
1040 if (!page_size) die("invalid page size");
1041 break;
1042 case 'p':
1043 product = optarg;
1044 break;
1045 case 'r':
1046 ramdisk_offset = strtoul(optarg, 0, 16);
1047 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001048 case 't':
1049 tags_offset = strtoul(optarg, 0, 16);
1050 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001051 case 's':
1052 serial = optarg;
1053 break;
1054 case 'S':
1055 sparse_limit = parse_num(optarg);
1056 if (sparse_limit < 0) {
1057 die("invalid sparse limit");
1058 }
1059 break;
1060 case 'u':
1061 erase_first = 0;
1062 break;
1063 case 'w':
1064 wants_wipe = 1;
1065 break;
Colin Cross8879f982012-05-22 17:53:34 -07001066 case '?':
1067 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001068 case 0:
1069 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
1070 setvbuf(stdout, NULL, _IONBF, 0);
1071 setvbuf(stderr, NULL, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001072 } else if (strcmp("version", longopts[longindex].name) == 0) {
1073 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1074 return 0;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001075 }
1076 break;
Colin Cross8879f982012-05-22 17:53:34 -07001077 default:
1078 abort();
1079 }
1080 }
1081
1082 argc -= optind;
1083 argv += optind;
1084
1085 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001086 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001087 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001088 }
1089
Colin Cross8fb6e062012-07-24 16:36:41 -07001090 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001091 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001092 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001093 return 0;
1094 }
1095
Colin Crossc7b75dc2012-08-29 18:17:06 -07001096 if (argc > 0 && !strcmp(*argv, "help")) {
1097 usage();
1098 return 0;
1099 }
1100
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001101 usb_handle* usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001102
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001103 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001104 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001105 require(2);
1106 fb_queue_display(argv[1], argv[1]);
1107 skip(2);
1108 } else if(!strcmp(*argv, "erase")) {
1109 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001110
JP Abgrall7e859742014-05-06 15:14:15 -07001111 if (fb_format_supported(usb, argv[1], NULL)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001112 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1113 }
1114
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001115 fb_queue_erase(argv[1]);
1116 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001117 } else if(!strncmp(*argv, "format", strlen("format"))) {
1118 char *overrides;
1119 char *type_override = NULL;
1120 char *size_override = NULL;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001121 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001122 /*
1123 * Parsing for: "format[:[type][:[size]]]"
1124 * Some valid things:
1125 * - select ontly the size, and leave default fs type:
1126 * format::0x4000000 userdata
1127 * - default fs type and size:
1128 * format userdata
1129 * format:: userdata
1130 */
1131 overrides = strchr(*argv, ':');
1132 if (overrides) {
1133 overrides++;
1134 size_override = strchr(overrides, ':');
1135 if (size_override) {
1136 size_override[0] = '\0';
1137 size_override++;
1138 }
1139 type_override = overrides;
1140 }
1141 if (type_override && !type_override[0]) type_override = NULL;
1142 if (size_override && !size_override[0]) size_override = NULL;
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001143 if (erase_first && needs_erase(usb, argv[1])) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001144 fb_queue_erase(argv[1]);
1145 }
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001146 fb_perform_format(usb, argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001147 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001148 } else if(!strcmp(*argv, "signature")) {
1149 require(2);
1150 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001151 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001152 if (sz != 256) die("signature must be 256 bytes");
1153 fb_queue_download("signature", data, sz);
1154 fb_queue_command("signature", "installing signature");
1155 skip(2);
1156 } else if(!strcmp(*argv, "reboot")) {
1157 wants_reboot = 1;
1158 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001159 if (argc > 0) {
1160 if (!strcmp(*argv, "bootloader")) {
1161 wants_reboot = 0;
1162 wants_reboot_bootloader = 1;
1163 skip(1);
1164 }
1165 }
1166 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001167 } else if(!strcmp(*argv, "reboot-bootloader")) {
1168 wants_reboot_bootloader = 1;
1169 skip(1);
1170 } else if (!strcmp(*argv, "continue")) {
1171 fb_queue_command("continue", "resuming boot");
1172 skip(1);
1173 } else if(!strcmp(*argv, "boot")) {
1174 char *kname = 0;
1175 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001176 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001177 skip(1);
1178 if (argc > 0) {
1179 kname = argv[0];
1180 skip(1);
1181 }
1182 if (argc > 0) {
1183 rname = argv[0];
1184 skip(1);
1185 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001186 if (argc > 0) {
1187 sname = argv[0];
1188 skip(1);
1189 }
1190 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001191 if (data == 0) return 1;
1192 fb_queue_download("boot.img", data, sz);
1193 fb_queue_command("boot", "booting");
1194 } else if(!strcmp(*argv, "flash")) {
1195 char *pname = argv[1];
1196 char *fname = 0;
1197 require(2);
1198 if (argc > 2) {
1199 fname = argv[2];
1200 skip(3);
1201 } else {
1202 fname = find_item(pname, product);
1203 skip(2);
1204 }
1205 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001206 if (erase_first && needs_erase(usb, pname)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001207 fb_queue_erase(pname);
1208 }
Colin Crossf8387882012-05-24 17:18:41 -07001209 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001210 } else if(!strcmp(*argv, "flash:raw")) {
1211 char *pname = argv[1];
1212 char *kname = argv[2];
1213 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001214 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001215 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001216 skip(3);
1217 if (argc > 0) {
1218 rname = argv[0];
1219 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001220 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001221 if (argc > 0) {
1222 sname = argv[0];
1223 skip(1);
1224 }
1225 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001226 if (data == 0) die("cannot load bootable image");
1227 fb_queue_flash(pname, data, sz);
1228 } else if(!strcmp(*argv, "flashall")) {
1229 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001230 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001231 wants_reboot = 1;
1232 } else if(!strcmp(*argv, "update")) {
1233 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001234 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001235 skip(2);
1236 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001237 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001238 skip(1);
1239 }
1240 wants_reboot = 1;
1241 } else if(!strcmp(*argv, "oem")) {
1242 argc = do_oem_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001243 } else if(!strcmp(*argv, "flashing") && argc == 2) {
1244 if(!strcmp(*(argv+1), "unlock") || !strcmp(*(argv+1), "lock")
1245 || !strcmp(*(argv+1), "unlock_critical")
1246 || !strcmp(*(argv+1), "lock_critical")
1247 || !strcmp(*(argv+1), "get_unlock_ability")) {
1248 argc = do_oem_command(argc, argv);
1249 } else {
1250 usage();
1251 return 1;
1252 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001253 } else {
1254 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001255 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001256 }
1257 }
1258
1259 if (wants_wipe) {
1260 fb_queue_erase("userdata");
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001261 fb_perform_format(usb, "userdata", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001262 fb_queue_erase("cache");
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001263 fb_perform_format(usb, "cache", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001264 }
1265 if (wants_reboot) {
1266 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001267 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001268 } else if (wants_reboot_bootloader) {
1269 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001270 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001271 }
1272
Scott Anderson13081c62012-04-06 12:39:30 -07001273 if (fb_queue_is_empty())
1274 return 0;
1275
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001276 status = fb_execute_queue(usb);
1277 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001278}