blob: 1af6b3942bbb897c505a32adb85bca51ef7692cd [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031#include <stdio.h>
32#include <stdlib.h>
Colin Crossf8387882012-05-24 17:18:41 -070033#include <stdbool.h>
34#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include <string.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <unistd.h>
39#include <limits.h>
40#include <ctype.h>
Colin Cross8879f982012-05-22 17:53:34 -070041#include <getopt.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
43#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070044#include <sys/types.h>
Rom Lemarchand622810c2013-06-28 09:54:59 -070045#include <sys/stat.h>
Colin Crossf8387882012-05-24 17:18:41 -070046
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047#include <bootimg.h>
Colin Crossf8387882012-05-24 17:18:41 -070048#include <sparse/sparse.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049#include <zipfile/zipfile.h>
50
51#include "fastboot.h"
52
Colin Crossf8387882012-05-24 17:18:41 -070053#ifndef O_BINARY
54#define O_BINARY 0
55#endif
56
Rom Lemarchand622810c2013-06-28 09:54:59 -070057#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
58
Wink Savilleb98762f2011-04-04 17:54:59 -070059char cur_product[FB_RESPONSE_SZ + 1];
60
Brian Swetland2a63bb72009-04-28 16:05:07 -070061void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
62
JP Abgrall7b8970c2013-03-07 17:06:41 -080063boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset,
64 void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset,
65 void *second, unsigned second_size, unsigned second_offset,
66 unsigned page_size, unsigned base, unsigned tags_offset,
Brian Swetland2a63bb72009-04-28 16:05:07 -070067 unsigned *bootimg_size);
68
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069static usb_handle *usb = 0;
70static const char *serial = 0;
71static const char *product = 0;
72static const char *cmdline = 0;
73static int wipe_data = 0;
74static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070075static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070076static int64_t sparse_limit = -1;
77static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
JP Abgrall7b8970c2013-03-07 17:06:41 -080079unsigned page_size = 2048;
80unsigned base_addr = 0x10000000;
81unsigned kernel_offset = 0x00008000;
82unsigned ramdisk_offset = 0x01000000;
83unsigned second_offset = 0x00f00000;
84unsigned tags_offset = 0x00000100;
85
Rom Lemarchand622810c2013-06-28 09:54:59 -070086enum fb_buffer_type {
87 FB_BUFFER,
88 FB_BUFFER_SPARSE,
89};
90
91struct fastboot_buffer {
92 enum fb_buffer_type type;
93 void *data;
94 unsigned int sz;
95};
96
97static struct {
98 char img_name[13];
99 char sig_name[13];
100 char part_name[9];
101 bool is_optional;
102} images[3] = {
103 {"boot.img", "boot.sig", "boot", false},
104 {"recovery.img", "recovery.sig", "recovery", true},
105 {"system.img", "system.sig", "system", false},
106};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700107
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108void get_my_path(char *path);
109
110char *find_item(const char *item, const char *product)
111{
112 char *dir;
113 char *fn;
114 char path[PATH_MAX + 128];
115
116 if(!strcmp(item,"boot")) {
117 fn = "boot.img";
118 } else if(!strcmp(item,"recovery")) {
119 fn = "recovery.img";
120 } else if(!strcmp(item,"system")) {
121 fn = "system.img";
122 } else if(!strcmp(item,"userdata")) {
123 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700124 } else if(!strcmp(item,"cache")) {
125 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 } else if(!strcmp(item,"info")) {
127 fn = "android-info.txt";
128 } else {
129 fprintf(stderr,"unknown partition '%s'\n", item);
130 return 0;
131 }
132
133 if(product) {
134 get_my_path(path);
135 sprintf(path + strlen(path),
136 "../../../target/product/%s/%s", product, fn);
137 return strdup(path);
138 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800139
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 dir = getenv("ANDROID_PRODUCT_OUT");
141 if((dir == 0) || (dir[0] == 0)) {
142 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
143 return 0;
144 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800145
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 sprintf(path, "%s/%s", dir, fn);
147 return strdup(path);
148}
149
Rom Lemarchand622810c2013-06-28 09:54:59 -0700150static int64_t file_size(int fd)
Colin Crossf8387882012-05-24 17:18:41 -0700151{
Rom Lemarchand622810c2013-06-28 09:54:59 -0700152 struct stat st;
153 int ret;
Colin Crossf8387882012-05-24 17:18:41 -0700154
Rom Lemarchand622810c2013-06-28 09:54:59 -0700155 ret = fstat(fd, &st);
Colin Crossf8387882012-05-24 17:18:41 -0700156
Rom Lemarchand622810c2013-06-28 09:54:59 -0700157 return ret ? -1 : st.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700158}
159
Rom Lemarchand622810c2013-06-28 09:54:59 -0700160static void *load_fd(int fd, unsigned *_sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161{
162 char *data;
163 int sz;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800164 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165
166 data = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
Rom Lemarchand622810c2013-06-28 09:54:59 -0700168 sz = file_size(fd);
169 if (sz < 0) {
170 goto oops;
171 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
173 data = (char*) malloc(sz);
174 if(data == 0) goto oops;
175
176 if(read(fd, data, sz) != sz) goto oops;
177 close(fd);
178
179 if(_sz) *_sz = sz;
180 return data;
181
182oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800183 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 close(fd);
185 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800186 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 return 0;
188}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
Rom Lemarchand622810c2013-06-28 09:54:59 -0700190static void *load_file(const char *fn, unsigned *_sz)
191{
192 int fd;
193
194 fd = open(fn, O_RDONLY | O_BINARY);
195 if(fd < 0) return 0;
196
197 return load_fd(fd, _sz);
198}
199
JP Abgralla032ded2012-06-06 11:53:33 -0700200int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
201{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400203 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800204 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700205 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800206 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700207 (info->dev_vendor != 0x0fce) && // Sony Ericsson
208 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400209 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800210 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800211 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800212 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800213 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400214 (info->dev_vendor != 0x0bb4)) // HTC
215 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 if(info->ifc_class != 0xff) return -1;
217 if(info->ifc_subclass != 0x42) return -1;
218 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700219 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700221 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
222 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 return 0;
224}
225
JP Abgrall7b8970c2013-03-07 17:06:41 -0800226int match_fastboot(usb_ifc_info *info)
227{
228 return match_fastboot_with_serial(info, serial);
229}
230
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231int list_devices_callback(usb_ifc_info *info)
232{
JP Abgralla032ded2012-06-06 11:53:33 -0700233 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700235 if (!info->writable) {
236 serial = "no permissions"; // like "adb devices"
237 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 if (!serial[0]) {
239 serial = "????????????";
240 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700241 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700242 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700243 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700244 } else if (!info->device_path) {
245 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700246 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700247 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700248 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 }
250
251 return -1;
252}
253
254usb_handle *open_device(void)
255{
256 static usb_handle *usb = 0;
257 int announce = 1;
258
259 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800260
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 for(;;) {
262 usb = usb_open(match_fastboot);
263 if(usb) return usb;
264 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800265 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266 fprintf(stderr,"< waiting for device >\n");
267 }
268 sleep(1);
269 }
270}
271
272void list_devices(void) {
273 // We don't actually open a USB device here,
274 // just getting our callback called so we can
275 // list all the connected devices.
276 usb_open(list_devices_callback);
277}
278
279void usage(void)
280{
281 fprintf(stderr,
282/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
283 "usage: fastboot [ <option> ] <command>\n"
284 "\n"
285 "commands:\n"
286 " update <filename> reflash device from update.zip\n"
287 " flashall flash boot + recovery + system\n"
288 " flash <partition> [ <filename> ] write a file to a flash partition\n"
289 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800290 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 " getvar <variable> display a bootloader variable\n"
292 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
293 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
294 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700295 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 " reboot reboot device normally\n"
297 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800298 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 "\n"
300 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700301 " -w erase userdata and cache (and format\n"
302 " if supported by partition type)\n"
303 " -u do not first erase partition before\n"
304 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700305 " -s <specific device> specify device serial number\n"
306 " or path to device port\n"
307 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 " -p <product> specify product name\n"
309 " -c <cmdline> override kernel commandline\n"
310 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7b8970c2013-03-07 17:06:41 -0800311 " -b <base_addr> specify a custom kernel base address. default: 0x10000000\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800312 " -n <page size> specify the nand page size. default: 2048\n"
Colin Crossf8387882012-05-24 17:18:41 -0700313 " -S <size>[K|M|G] automatically sparse files greater than\n"
Colin Cross0bbfb392012-07-24 18:05:21 -0700314 " 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,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 unsigned *sz, const char *cmdline)
320{
321 void *kdata = 0, *rdata = 0;
322 unsigned ksize = 0, rsize = 0;
323 void *bdata;
324 unsigned bsize;
325
326 if(kernel == 0) {
327 fprintf(stderr, "no image specified\n");
328 return 0;
329 }
330
331 kdata = load_file(kernel, &ksize);
332 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800333 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 return 0;
335 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800336
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 /* is this actually a boot image? */
338 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
339 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800340
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 if(ramdisk) {
342 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
343 return 0;
344 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800345
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 *sz = ksize;
347 return kdata;
348 }
349
350 if(ramdisk) {
351 rdata = load_file(ramdisk, &rsize);
352 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800353 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 return 0;
355 }
356 }
357
358 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800359 bdata = mkbootimg(kdata, ksize, kernel_offset,
360 rdata, rsize, ramdisk_offset,
361 0, 0, second_offset,
362 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 if(bdata == 0) {
364 fprintf(stderr,"failed to create boot.img\n");
365 return 0;
366 }
367 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
368 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
369 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800370
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 return bdata;
372}
373
374void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
375{
376 void *data;
377 zipentry_t entry;
378 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800379
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 entry = lookup_zipentry(zip, name);
381 if (entry == NULL) {
382 fprintf(stderr, "archive does not contain '%s'\n", name);
383 return 0;
384 }
385
386 *sz = get_zipentry_size(entry);
387
388 datasz = *sz * 1.001;
389 data = malloc(datasz);
390
391 if(data == 0) {
392 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
393 return 0;
394 }
395
396 if (decompress_zipentry(entry, data, datasz)) {
397 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
398 free(data);
399 return 0;
400 }
401
402 return data;
403}
404
Rom Lemarchand622810c2013-06-28 09:54:59 -0700405static int unzip_to_file(zipfile_t zip, char *name)
406{
407 int fd;
408 char *data;
409 unsigned sz;
410
411 fd = fileno(tmpfile());
412 if (fd < 0) {
413 return -1;
414 }
415
416 data = unzip_file(zip, name, &sz);
417 if (data == 0) {
418 return -1;
419 }
420
421 if (write(fd, data, sz) != sz) {
422 fd = -1;
423 }
424
425 free(data);
426 lseek(fd, 0, SEEK_SET);
427 return fd;
428}
429
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430static char *strip(char *s)
431{
432 int n;
433 while(*s && isspace(*s)) s++;
434 n = strlen(s);
435 while(n-- > 0) {
436 if(!isspace(s[n])) break;
437 s[n] = 0;
438 }
439 return s;
440}
441
442#define MAX_OPTIONS 32
443static int setup_requirement_line(char *name)
444{
445 char *val[MAX_OPTIONS];
446 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700447 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 unsigned n, count;
449 char *x;
450 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800451
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452 if (!strncmp(name, "reject ", 7)) {
453 name += 7;
454 invert = 1;
455 } else if (!strncmp(name, "require ", 8)) {
456 name += 8;
457 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700458 } else if (!strncmp(name, "require-for-product:", 20)) {
459 // Get the product and point name past it
460 prod = name + 20;
461 name = strchr(name, ' ');
462 if (!name) return -1;
463 *name = 0;
464 name += 1;
465 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 }
467
468 x = strchr(name, '=');
469 if (x == 0) return 0;
470 *x = 0;
471 val[0] = x + 1;
472
473 for(count = 1; count < MAX_OPTIONS; count++) {
474 x = strchr(val[count - 1],'|');
475 if (x == 0) break;
476 *x = 0;
477 val[count] = x + 1;
478 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800479
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 name = strip(name);
481 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800482
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 name = strip(name);
484 if (name == 0) return -1;
485
486 /* work around an unfortunate name mismatch */
487 if (!strcmp(name,"board")) name = "product";
488
489 out = malloc(sizeof(char*) * count);
490 if (out == 0) return -1;
491
492 for(n = 0; n < count; n++) {
493 out[n] = strdup(strip(val[n]));
494 if (out[n] == 0) return -1;
495 }
496
Wink Savilleb98762f2011-04-04 17:54:59 -0700497 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498 return 0;
499}
500
501static void setup_requirements(char *data, unsigned sz)
502{
503 char *s;
504
505 s = data;
506 while (sz-- > 0) {
507 if(*s == '\n') {
508 *s++ = 0;
509 if (setup_requirement_line(data)) {
510 die("out of memory");
511 }
512 data = s;
513 } else {
514 s++;
515 }
516 }
517}
518
519void queue_info_dump(void)
520{
521 fb_queue_notice("--------------------------------------------");
522 fb_queue_display("version-bootloader", "Bootloader Version...");
523 fb_queue_display("version-baseband", "Baseband Version.....");
524 fb_queue_display("serialno", "Serial Number........");
525 fb_queue_notice("--------------------------------------------");
526}
527
Rom Lemarchand622810c2013-06-28 09:54:59 -0700528static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700529{
Colin Crossf8387882012-05-24 17:18:41 -0700530 struct sparse_file *s;
531 int files;
532 struct sparse_file **out_s;
533
Colin Crossf8387882012-05-24 17:18:41 -0700534 s = sparse_file_import_auto(fd, false);
535 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700536 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700537 }
538
539 files = sparse_file_resparse(s, max_size, NULL, 0);
540 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700541 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700542 }
543
544 out_s = calloc(sizeof(struct sparse_file *), files + 1);
545 if (!out_s) {
546 die("Failed to allocate sparse file array\n");
547 }
548
549 files = sparse_file_resparse(s, max_size, out_s, files);
550 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700551 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700552 }
553
554 return out_s;
555}
556
557static int64_t get_target_sparse_limit(struct usb_handle *usb)
558{
559 int64_t limit = 0;
560 char response[FB_RESPONSE_SZ + 1];
561 int status = fb_getvar(usb, response, "max-download-size");
562
563 if (!status) {
564 limit = strtoul(response, NULL, 0);
565 if (limit > 0) {
566 fprintf(stderr, "target reported max download size of %lld bytes\n",
567 limit);
568 }
569 }
570
571 return limit;
572}
573
574static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
575{
576 int64_t limit;
577
578 if (sparse_limit == 0) {
579 return 0;
580 } else if (sparse_limit > 0) {
581 limit = sparse_limit;
582 } else {
583 if (target_sparse_limit == -1) {
584 target_sparse_limit = get_target_sparse_limit(usb);
585 }
586 if (target_sparse_limit > 0) {
587 limit = target_sparse_limit;
588 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700589 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700590 }
591 }
592
593 if (size > limit) {
594 return limit;
595 }
596
597 return 0;
598}
599
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700600/* Until we get lazy inode table init working in make_ext4fs, we need to
601 * erase partitions of type ext4 before flashing a filesystem so no stale
602 * inodes are left lying around. Otherwise, e2fsck gets very upset.
603 */
604static int needs_erase(const char *part)
605{
606 /* The function fb_format_supported() currently returns the value
607 * we want, so just call it.
608 */
609 return fb_format_supported(usb, part);
610}
611
Rom Lemarchand622810c2013-06-28 09:54:59 -0700612static int load_buf_fd(usb_handle *usb, int fd,
613 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700614{
615 int64_t sz64;
616 void *data;
617 int64_t limit;
618
Rom Lemarchand622810c2013-06-28 09:54:59 -0700619 sz64 = file_size(fd);
620 if (sz64 < 0) {
621 return -1;
622 }
Colin Crossf8387882012-05-24 17:18:41 -0700623 limit = get_sparse_limit(usb, sz64);
624 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700625 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700626 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700627 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700628 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700629 buf->type = FB_BUFFER_SPARSE;
630 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700631 } else {
632 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700633 data = load_fd(fd, &sz);
634 if (data == 0) return -1;
635 buf->type = FB_BUFFER;
636 buf->data = data;
637 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700638 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700639
640 return 0;
641}
642
643static int load_buf(usb_handle *usb, const char *fname,
644 struct fastboot_buffer *buf)
645{
646 int fd;
647
648 fd = open(fname, O_RDONLY | O_BINARY);
649 if (fd < 0) {
650 die("cannot open '%s'\n", fname);
651 }
652
653 return load_buf_fd(usb, fd, buf);
654}
655
656static void flash_buf(const char *pname, struct fastboot_buffer *buf)
657{
658 struct sparse_file **s;
659
660 switch (buf->type) {
661 case FB_BUFFER_SPARSE:
662 s = buf->data;
663 while (*s) {
664 int64_t sz64 = sparse_file_len(*s, true, false);
665 fb_queue_flash_sparse(pname, *s++, sz64);
666 }
667 break;
668 case FB_BUFFER:
669 fb_queue_flash(pname, buf->data, buf->sz);
670 break;
671 default:
672 die("unknown buffer type: %d", buf->type);
673 }
674}
675
676void do_flash(usb_handle *usb, const char *pname, const char *fname)
677{
678 struct fastboot_buffer buf;
679
680 if (load_buf(usb, fname, &buf)) {
681 die("cannot load '%s'", fname);
682 }
683 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700684}
685
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686void do_update_signature(zipfile_t zip, char *fn)
687{
688 void *data;
689 unsigned sz;
690 data = unzip_file(zip, fn, &sz);
691 if (data == 0) return;
692 fb_queue_download("signature", data, sz);
693 fb_queue_command("signature", "installing signature");
694}
695
Rom Lemarchand622810c2013-06-28 09:54:59 -0700696void do_update(usb_handle *usb, char *fn, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800697{
698 void *zdata;
699 unsigned zsize;
700 void *data;
701 unsigned sz;
702 zipfile_t zip;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700703 int fd;
704 int rc;
705 struct fastboot_buffer buf;
706 int i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707
708 queue_info_dump();
709
Wink Savilleb98762f2011-04-04 17:54:59 -0700710 fb_queue_query_save("product", cur_product, sizeof(cur_product));
711
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800713 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800714
715 zip = init_zipfile(zdata, zsize);
716 if(zip == 0) die("failed to access zipdata in '%s'");
717
718 data = unzip_file(zip, "android-info.txt", &sz);
719 if (data == 0) {
720 char *tmp;
721 /* fallback for older zipfiles */
722 data = unzip_file(zip, "android-product.txt", &sz);
723 if ((data == 0) || (sz < 1)) {
724 die("update package has no android-info.txt or android-product.txt");
725 }
726 tmp = malloc(sz + 128);
727 if (tmp == 0) die("out of memory");
728 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
729 data = tmp;
730 sz = strlen(tmp);
731 }
732
733 setup_requirements(data, sz);
734
Rom Lemarchand622810c2013-06-28 09:54:59 -0700735 for (i = 0; i < ARRAY_SIZE(images); i++) {
736 fd = unzip_to_file(zip, images[i].img_name);
737 if (fd < 0) {
738 if (images[i].is_optional)
739 continue;
740 die("update package missing %s", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700741 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700742 rc = load_buf_fd(usb, fd, &buf);
743 if (rc) die("cannot load %s from flash", images[i].img_name);
744 do_update_signature(zip, images[i].sig_name);
745 if (erase_first && needs_erase(images[i].part_name)) {
746 fb_queue_erase(images[i].part_name);
747 }
748 flash_buf(images[i].part_name, &buf);
749 /* not closing the fd here since the sparse code keeps the fd around
750 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
751 * program exits.
752 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800754}
755
756void do_send_signature(char *fn)
757{
758 void *data;
759 unsigned sz;
760 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800761
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800762 xtn = strrchr(fn, '.');
763 if (!xtn) return;
764 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800765
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800766 strcpy(xtn,".sig");
767 data = load_file(fn, &sz);
768 strcpy(xtn,".img");
769 if (data == 0) return;
770 fb_queue_download("signature", data, sz);
771 fb_queue_command("signature", "installing signature");
772}
773
Rom Lemarchand622810c2013-06-28 09:54:59 -0700774void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800775{
776 char *fname;
777 void *data;
778 unsigned sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700779 struct fastboot_buffer buf;
780 int i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800781
782 queue_info_dump();
783
Wink Savilleb98762f2011-04-04 17:54:59 -0700784 fb_queue_query_save("product", cur_product, sizeof(cur_product));
785
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800786 fname = find_item("info", product);
787 if (fname == 0) die("cannot find android-info.txt");
788 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800789 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800790 setup_requirements(data, sz);
791
Rom Lemarchand622810c2013-06-28 09:54:59 -0700792 for (i = 0; i < ARRAY_SIZE(images); i++) {
793 fname = find_item(images[i].part_name, product);
794 if (load_buf(usb, fname, &buf)) {
795 if (images[i].is_optional)
796 continue;
797 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700798 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700799 do_send_signature(fname);
800 if (erase_first && needs_erase(images[i].part_name)) {
801 fb_queue_erase(images[i].part_name);
802 }
803 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800804 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805}
806
807#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800808#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809
810int do_oem_command(int argc, char **argv)
811{
812 int i;
813 char command[256];
814 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800815
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800816 command[0] = 0;
817 while(1) {
818 strcat(command,*argv);
819 skip(1);
820 if(argc == 0) break;
821 strcat(command," ");
822 }
823
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800824 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800825 return 0;
826}
827
Colin Crossf8387882012-05-24 17:18:41 -0700828static int64_t parse_num(const char *arg)
829{
830 char *endptr;
831 unsigned long long num;
832
833 num = strtoull(arg, &endptr, 0);
834 if (endptr == arg) {
835 return -1;
836 }
837
838 if (*endptr == 'k' || *endptr == 'K') {
839 if (num >= (-1ULL) / 1024) {
840 return -1;
841 }
842 num *= 1024LL;
843 endptr++;
844 } else if (*endptr == 'm' || *endptr == 'M') {
845 if (num >= (-1ULL) / (1024 * 1024)) {
846 return -1;
847 }
848 num *= 1024LL * 1024LL;
849 endptr++;
850 } else if (*endptr == 'g' || *endptr == 'G') {
851 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
852 return -1;
853 }
854 num *= 1024LL * 1024LL * 1024LL;
855 endptr++;
856 }
857
858 if (*endptr != '\0') {
859 return -1;
860 }
861
862 if (num > INT64_MAX) {
863 return -1;
864 }
865
866 return num;
867}
868
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800869int main(int argc, char **argv)
870{
871 int wants_wipe = 0;
872 int wants_reboot = 0;
873 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700874 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800875 void *data;
876 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700877 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700878 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700879 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800880
JP Abgrall7b8970c2013-03-07 17:06:41 -0800881 const struct option longopts[] = {
882 {"base", required_argument, 0, 'b'},
883 {"kernel_offset", required_argument, 0, 'k'},
884 {"page_size", required_argument, 0, 'n'},
885 {"ramdisk_offset", required_argument, 0, 'r'},
886 {"help", 0, 0, 'h'},
887 {0, 0, 0, 0}
888 };
Colin Cross8879f982012-05-22 17:53:34 -0700889
890 serial = getenv("ANDROID_SERIAL");
891
892 while (1) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800893 int option_index = 0;
894 c = getopt_long(argc, argv, "wub:k:n:r:s:S:lp:c:i:m:h", longopts, NULL);
Colin Cross8879f982012-05-22 17:53:34 -0700895 if (c < 0) {
896 break;
897 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800898 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700899 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700900 case 'b':
901 base_addr = strtoul(optarg, 0, 16);
902 break;
Colin Cross8879f982012-05-22 17:53:34 -0700903 case 'c':
904 cmdline = optarg;
905 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800906 case 'h':
907 usage();
908 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700909 case 'i': {
910 char *endptr = NULL;
911 unsigned long val;
912
913 val = strtoul(optarg, &endptr, 0);
914 if (!endptr || *endptr != '\0' || (val & ~0xffff))
915 die("invalid vendor id '%s'", optarg);
916 vendor_id = (unsigned short)val;
917 break;
918 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800919 case 'k':
920 kernel_offset = strtoul(optarg, 0, 16);
921 break;
922 case 'l':
923 long_listing = 1;
924 break;
925 case 'n':
926 page_size = (unsigned)strtoul(optarg, NULL, 0);
927 if (!page_size) die("invalid page size");
928 break;
929 case 'p':
930 product = optarg;
931 break;
932 case 'r':
933 ramdisk_offset = strtoul(optarg, 0, 16);
934 break;
935 case 's':
936 serial = optarg;
937 break;
938 case 'S':
939 sparse_limit = parse_num(optarg);
940 if (sparse_limit < 0) {
941 die("invalid sparse limit");
942 }
943 break;
944 case 'u':
945 erase_first = 0;
946 break;
947 case 'w':
948 wants_wipe = 1;
949 break;
Colin Cross8879f982012-05-22 17:53:34 -0700950 case '?':
951 return 1;
952 default:
953 abort();
954 }
955 }
956
957 argc -= optind;
958 argv += optind;
959
960 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800961 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700962 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800963 }
964
Colin Cross8fb6e062012-07-24 16:36:41 -0700965 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -0700966 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800967 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800968 return 0;
969 }
970
Colin Crossc7b75dc2012-08-29 18:17:06 -0700971 if (argc > 0 && !strcmp(*argv, "help")) {
972 usage();
973 return 0;
974 }
975
Colin Cross8879f982012-05-22 17:53:34 -0700976 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -0700977
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800978 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -0700979 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800980 require(2);
981 fb_queue_display(argv[1], argv[1]);
982 skip(2);
983 } else if(!strcmp(*argv, "erase")) {
984 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700985
986 if (fb_format_supported(usb, argv[1])) {
987 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
988 }
989
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800990 fb_queue_erase(argv[1]);
991 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800992 } else if(!strcmp(*argv, "format")) {
993 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700994 if (erase_first && needs_erase(argv[1])) {
995 fb_queue_erase(argv[1]);
996 }
JP Abgrall30ae5802012-05-07 20:25:24 -0700997 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800998 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800999 } else if(!strcmp(*argv, "signature")) {
1000 require(2);
1001 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001002 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001003 if (sz != 256) die("signature must be 256 bytes");
1004 fb_queue_download("signature", data, sz);
1005 fb_queue_command("signature", "installing signature");
1006 skip(2);
1007 } else if(!strcmp(*argv, "reboot")) {
1008 wants_reboot = 1;
1009 skip(1);
1010 } else if(!strcmp(*argv, "reboot-bootloader")) {
1011 wants_reboot_bootloader = 1;
1012 skip(1);
1013 } else if (!strcmp(*argv, "continue")) {
1014 fb_queue_command("continue", "resuming boot");
1015 skip(1);
1016 } else if(!strcmp(*argv, "boot")) {
1017 char *kname = 0;
1018 char *rname = 0;
1019 skip(1);
1020 if (argc > 0) {
1021 kname = argv[0];
1022 skip(1);
1023 }
1024 if (argc > 0) {
1025 rname = argv[0];
1026 skip(1);
1027 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001028 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001029 if (data == 0) return 1;
1030 fb_queue_download("boot.img", data, sz);
1031 fb_queue_command("boot", "booting");
1032 } else if(!strcmp(*argv, "flash")) {
1033 char *pname = argv[1];
1034 char *fname = 0;
1035 require(2);
1036 if (argc > 2) {
1037 fname = argv[2];
1038 skip(3);
1039 } else {
1040 fname = find_item(pname, product);
1041 skip(2);
1042 }
1043 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001044 if (erase_first && needs_erase(pname)) {
1045 fb_queue_erase(pname);
1046 }
Colin Crossf8387882012-05-24 17:18:41 -07001047 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001048 } else if(!strcmp(*argv, "flash:raw")) {
1049 char *pname = argv[1];
1050 char *kname = argv[2];
1051 char *rname = 0;
1052 require(3);
1053 if(argc > 3) {
1054 rname = argv[3];
1055 skip(4);
1056 } else {
1057 skip(3);
1058 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001059 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001060 if (data == 0) die("cannot load bootable image");
1061 fb_queue_flash(pname, data, sz);
1062 } else if(!strcmp(*argv, "flashall")) {
1063 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001064 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001065 wants_reboot = 1;
1066 } else if(!strcmp(*argv, "update")) {
1067 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001068 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001069 skip(2);
1070 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001071 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001072 skip(1);
1073 }
1074 wants_reboot = 1;
1075 } else if(!strcmp(*argv, "oem")) {
1076 argc = do_oem_command(argc, argv);
1077 } else {
1078 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001079 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001080 }
1081 }
1082
1083 if (wants_wipe) {
1084 fb_queue_erase("userdata");
JP Abgrall30ae5802012-05-07 20:25:24 -07001085 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001086 fb_queue_erase("cache");
JP Abgrall30ae5802012-05-07 20:25:24 -07001087 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001088 }
1089 if (wants_reboot) {
1090 fb_queue_reboot();
1091 } else if (wants_reboot_bootloader) {
1092 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001093 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001094 }
1095
Scott Anderson13081c62012-04-06 12:39:30 -07001096 if (fb_queue_is_empty())
1097 return 0;
1098
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001099 status = fb_execute_queue(usb);
1100 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001101}