The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 12 | * the documentation and/or other materials provided with the |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 13 | * 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 Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | * 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 Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 29 | #define _LARGEFILE64_SOURCE |
| 30 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <stdarg.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 34 | #include <stdbool.h> |
| 35 | #include <stdint.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | #include <string.h> |
| 37 | #include <errno.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <unistd.h> |
| 40 | #include <limits.h> |
| 41 | #include <ctype.h> |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 42 | #include <getopt.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | |
| 44 | #include <sys/time.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 45 | #include <sys/types.h> |
| 46 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | #include <bootimg.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 48 | #include <sparse/sparse.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | #include <zipfile/zipfile.h> |
| 50 | |
| 51 | #include "fastboot.h" |
| 52 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 53 | #ifndef O_BINARY |
| 54 | #define O_BINARY 0 |
| 55 | #endif |
| 56 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 57 | char cur_product[FB_RESPONSE_SZ + 1]; |
| 58 | |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 59 | void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline); |
| 60 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 61 | boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset, |
| 62 | void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset, |
| 63 | void *second, unsigned second_size, unsigned second_offset, |
| 64 | unsigned page_size, unsigned base, unsigned tags_offset, |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 65 | unsigned *bootimg_size); |
| 66 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | static usb_handle *usb = 0; |
| 68 | static const char *serial = 0; |
| 69 | static const char *product = 0; |
| 70 | static const char *cmdline = 0; |
| 71 | static int wipe_data = 0; |
| 72 | static unsigned short vendor_id = 0; |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 73 | static int long_listing = 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 74 | static int64_t sparse_limit = -1; |
| 75 | static int64_t target_sparse_limit = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 77 | unsigned page_size = 2048; |
| 78 | unsigned base_addr = 0x10000000; |
| 79 | unsigned kernel_offset = 0x00008000; |
| 80 | unsigned ramdisk_offset = 0x01000000; |
| 81 | unsigned second_offset = 0x00f00000; |
| 82 | unsigned tags_offset = 0x00000100; |
| 83 | |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 84 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 85 | void die(const char *fmt, ...) |
| 86 | { |
| 87 | va_list ap; |
| 88 | va_start(ap, fmt); |
| 89 | fprintf(stderr,"error: "); |
| 90 | vfprintf(stderr, fmt, ap); |
| 91 | fprintf(stderr,"\n"); |
| 92 | va_end(ap); |
| 93 | exit(1); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 94 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | |
| 96 | void get_my_path(char *path); |
| 97 | |
| 98 | char *find_item(const char *item, const char *product) |
| 99 | { |
| 100 | char *dir; |
| 101 | char *fn; |
| 102 | char path[PATH_MAX + 128]; |
| 103 | |
| 104 | if(!strcmp(item,"boot")) { |
| 105 | fn = "boot.img"; |
| 106 | } else if(!strcmp(item,"recovery")) { |
| 107 | fn = "recovery.img"; |
| 108 | } else if(!strcmp(item,"system")) { |
| 109 | fn = "system.img"; |
| 110 | } else if(!strcmp(item,"userdata")) { |
| 111 | fn = "userdata.img"; |
Jean-Baptiste Queru | d7608a4 | 2011-09-30 14:39:25 -0700 | [diff] [blame] | 112 | } else if(!strcmp(item,"cache")) { |
| 113 | fn = "cache.img"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 114 | } else if(!strcmp(item,"info")) { |
| 115 | fn = "android-info.txt"; |
| 116 | } else { |
| 117 | fprintf(stderr,"unknown partition '%s'\n", item); |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | if(product) { |
| 122 | get_my_path(path); |
| 123 | sprintf(path + strlen(path), |
| 124 | "../../../target/product/%s/%s", product, fn); |
| 125 | return strdup(path); |
| 126 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 127 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 128 | dir = getenv("ANDROID_PRODUCT_OUT"); |
| 129 | if((dir == 0) || (dir[0] == 0)) { |
| 130 | die("neither -p product specified nor ANDROID_PRODUCT_OUT set"); |
| 131 | return 0; |
| 132 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 133 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 134 | sprintf(path, "%s/%s", dir, fn); |
| 135 | return strdup(path); |
| 136 | } |
| 137 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 138 | #if defined(__APPLE__) && defined(__MACH__) |
| 139 | #define lseek64 lseek |
| 140 | #define off64_t off_t |
| 141 | #endif |
| 142 | |
Rom Lemarchand | c9cce4b | 2013-06-28 09:45:08 -0700 | [diff] [blame^] | 143 | static int64_t file_size(const char *fn) |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 144 | { |
| 145 | off64_t off; |
| 146 | int fd; |
| 147 | |
Rom Lemarchand | c9cce4b | 2013-06-28 09:45:08 -0700 | [diff] [blame^] | 148 | fd = open(fn, O_RDONLY | O_BINARY); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 149 | if (fd < 0) return -1; |
| 150 | |
| 151 | off = lseek64(fd, 0, SEEK_END); |
| 152 | close(fd); |
| 153 | |
| 154 | return off; |
| 155 | } |
| 156 | |
Rom Lemarchand | c9cce4b | 2013-06-28 09:45:08 -0700 | [diff] [blame^] | 157 | static void *load_file(const char *fn, unsigned *_sz) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 158 | { |
| 159 | char *data; |
| 160 | int sz; |
| 161 | int fd; |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 162 | int errno_tmp; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 163 | |
| 164 | data = 0; |
Rom Lemarchand | c9cce4b | 2013-06-28 09:45:08 -0700 | [diff] [blame^] | 165 | fd = open(fn, O_RDONLY | O_BINARY); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 166 | if(fd < 0) return 0; |
| 167 | |
| 168 | sz = lseek(fd, 0, SEEK_END); |
| 169 | if(sz < 0) goto oops; |
| 170 | |
| 171 | if(lseek(fd, 0, SEEK_SET) != 0) goto oops; |
| 172 | |
| 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 | |
| 182 | oops: |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 183 | errno_tmp = errno; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 184 | close(fd); |
| 185 | if(data != 0) free(data); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 186 | errno = errno_tmp; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 | return 0; |
| 188 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | |
JP Abgrall | a032ded | 2012-06-06 11:53:33 -0700 | [diff] [blame] | 190 | int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial) |
| 191 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 192 | if(!(vendor_id && (info->dev_vendor == vendor_id)) && |
Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 193 | (info->dev_vendor != 0x18d1) && // Google |
Wu, Hao | f60e863 | 2012-01-17 12:04:11 -0800 | [diff] [blame] | 194 | (info->dev_vendor != 0x8087) && // Intel |
The Android Open Source Project | f614d64 | 2009-03-18 17:39:49 -0700 | [diff] [blame] | 195 | (info->dev_vendor != 0x0451) && |
Robert CH Chou | e25ff1c | 2009-09-21 09:51:35 +0800 | [diff] [blame] | 196 | (info->dev_vendor != 0x0502) && |
Dima Zavin | 509f739 | 2010-05-14 14:48:30 -0700 | [diff] [blame] | 197 | (info->dev_vendor != 0x0fce) && // Sony Ericsson |
| 198 | (info->dev_vendor != 0x05c6) && // Qualcomm |
Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 199 | (info->dev_vendor != 0x22b8) && // Motorola |
Erik Gilling | 37e9e90 | 2010-01-20 17:40:05 -0800 | [diff] [blame] | 200 | (info->dev_vendor != 0x0955) && // Nvidia |
Xavier Ducrohet | af82f21 | 2010-01-21 17:39:25 -0800 | [diff] [blame] | 201 | (info->dev_vendor != 0x413c) && // DELL |
Xavier Ducrohet | 746f324 | 2012-01-13 16:03:37 -0800 | [diff] [blame] | 202 | (info->dev_vendor != 0x2314) && // INQ Mobile |
Ramanan Rajeswaran | 73c019b | 2012-02-24 13:00:34 -0800 | [diff] [blame] | 203 | (info->dev_vendor != 0x0b05) && // Asus |
Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 204 | (info->dev_vendor != 0x0bb4)) // HTC |
| 205 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 206 | if(info->ifc_class != 0xff) return -1; |
| 207 | if(info->ifc_subclass != 0x42) return -1; |
| 208 | if(info->ifc_protocol != 0x03) return -1; |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 209 | // require matching serial number or device path if requested |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | // at the command line with the -s option. |
JP Abgrall | a032ded | 2012-06-06 11:53:33 -0700 | [diff] [blame] | 211 | if (local_serial && (strcmp(local_serial, info->serial_number) != 0 && |
| 212 | strcmp(local_serial, info->device_path) != 0)) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 213 | return 0; |
| 214 | } |
| 215 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 216 | int match_fastboot(usb_ifc_info *info) |
| 217 | { |
| 218 | return match_fastboot_with_serial(info, serial); |
| 219 | } |
| 220 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 221 | int list_devices_callback(usb_ifc_info *info) |
| 222 | { |
JP Abgrall | a032ded | 2012-06-06 11:53:33 -0700 | [diff] [blame] | 223 | if (match_fastboot_with_serial(info, NULL) == 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 224 | char* serial = info->serial_number; |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 225 | if (!info->writable) { |
| 226 | serial = "no permissions"; // like "adb devices" |
| 227 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 228 | if (!serial[0]) { |
| 229 | serial = "????????????"; |
| 230 | } |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 231 | // output compatible with "adb devices" |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 232 | if (!long_listing) { |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 233 | printf("%s\tfastboot\n", serial); |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 234 | } else if (!info->device_path) { |
| 235 | printf("%-22s fastboot\n", serial); |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 236 | } else { |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 237 | printf("%-22s fastboot %s\n", serial, info->device_path); |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 238 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | usb_handle *open_device(void) |
| 245 | { |
| 246 | static usb_handle *usb = 0; |
| 247 | int announce = 1; |
| 248 | |
| 249 | if(usb) return usb; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 250 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 251 | for(;;) { |
| 252 | usb = usb_open(match_fastboot); |
| 253 | if(usb) return usb; |
| 254 | if(announce) { |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 255 | announce = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 256 | fprintf(stderr,"< waiting for device >\n"); |
| 257 | } |
| 258 | sleep(1); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void list_devices(void) { |
| 263 | // We don't actually open a USB device here, |
| 264 | // just getting our callback called so we can |
| 265 | // list all the connected devices. |
| 266 | usb_open(list_devices_callback); |
| 267 | } |
| 268 | |
| 269 | void usage(void) |
| 270 | { |
| 271 | fprintf(stderr, |
| 272 | /* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */ |
| 273 | "usage: fastboot [ <option> ] <command>\n" |
| 274 | "\n" |
| 275 | "commands:\n" |
| 276 | " update <filename> reflash device from update.zip\n" |
| 277 | " flashall flash boot + recovery + system\n" |
| 278 | " flash <partition> [ <filename> ] write a file to a flash partition\n" |
| 279 | " erase <partition> erase a flash partition\n" |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 280 | " format <partition> format a flash partition \n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 281 | " getvar <variable> display a bootloader variable\n" |
| 282 | " boot <kernel> [ <ramdisk> ] download and boot kernel\n" |
| 283 | " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n" |
| 284 | " devices list all connected devices\n" |
Bruce Beare | 24ce4bc | 2010-10-14 09:43:26 -0700 | [diff] [blame] | 285 | " continue continue with autoboot\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 286 | " reboot reboot device normally\n" |
| 287 | " reboot-bootloader reboot device into bootloader\n" |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 288 | " help show this help message\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 289 | "\n" |
| 290 | "options:\n" |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 291 | " -w erase userdata and cache (and format\n" |
| 292 | " if supported by partition type)\n" |
| 293 | " -u do not first erase partition before\n" |
| 294 | " formatting\n" |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 295 | " -s <specific device> specify device serial number\n" |
| 296 | " or path to device port\n" |
| 297 | " -l with \"devices\", lists device paths\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 298 | " -p <product> specify product name\n" |
| 299 | " -c <cmdline> override kernel commandline\n" |
| 300 | " -i <vendor id> specify a custom USB vendor id\n" |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 301 | " -b <base_addr> specify a custom kernel base address. default: 0x10000000\n" |
Dima Zavin | 931175a | 2010-02-12 20:26:33 -0800 | [diff] [blame] | 302 | " -n <page size> specify the nand page size. default: 2048\n" |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 303 | " -S <size>[K|M|G] automatically sparse files greater than\n" |
Colin Cross | 0bbfb39 | 2012-07-24 18:05:21 -0700 | [diff] [blame] | 304 | " size. 0 to disable\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 305 | ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 306 | } |
| 307 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 308 | void *load_bootable_image(const char *kernel, const char *ramdisk, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 | unsigned *sz, const char *cmdline) |
| 310 | { |
| 311 | void *kdata = 0, *rdata = 0; |
| 312 | unsigned ksize = 0, rsize = 0; |
| 313 | void *bdata; |
| 314 | unsigned bsize; |
| 315 | |
| 316 | if(kernel == 0) { |
| 317 | fprintf(stderr, "no image specified\n"); |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | kdata = load_file(kernel, &ksize); |
| 322 | if(kdata == 0) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 323 | fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 324 | return 0; |
| 325 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 326 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 327 | /* is this actually a boot image? */ |
| 328 | if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 329 | if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 330 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 331 | if(ramdisk) { |
| 332 | fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n"); |
| 333 | return 0; |
| 334 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 335 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 336 | *sz = ksize; |
| 337 | return kdata; |
| 338 | } |
| 339 | |
| 340 | if(ramdisk) { |
| 341 | rdata = load_file(ramdisk, &rsize); |
| 342 | if(rdata == 0) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 343 | fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | fprintf(stderr,"creating boot image...\n"); |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 349 | bdata = mkbootimg(kdata, ksize, kernel_offset, |
| 350 | rdata, rsize, ramdisk_offset, |
| 351 | 0, 0, second_offset, |
| 352 | page_size, base_addr, tags_offset, &bsize); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 353 | if(bdata == 0) { |
| 354 | fprintf(stderr,"failed to create boot.img\n"); |
| 355 | return 0; |
| 356 | } |
| 357 | if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline); |
| 358 | fprintf(stderr,"creating boot image - %d bytes\n", bsize); |
| 359 | *sz = bsize; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 360 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 361 | return bdata; |
| 362 | } |
| 363 | |
| 364 | void *unzip_file(zipfile_t zip, const char *name, unsigned *sz) |
| 365 | { |
| 366 | void *data; |
| 367 | zipentry_t entry; |
| 368 | unsigned datasz; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 369 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 370 | entry = lookup_zipentry(zip, name); |
| 371 | if (entry == NULL) { |
| 372 | fprintf(stderr, "archive does not contain '%s'\n", name); |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | *sz = get_zipentry_size(entry); |
| 377 | |
| 378 | datasz = *sz * 1.001; |
| 379 | data = malloc(datasz); |
| 380 | |
| 381 | if(data == 0) { |
| 382 | fprintf(stderr, "failed to allocate %d bytes\n", *sz); |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | if (decompress_zipentry(entry, data, datasz)) { |
| 387 | fprintf(stderr, "failed to unzip '%s' from archive\n", name); |
| 388 | free(data); |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | return data; |
| 393 | } |
| 394 | |
| 395 | static char *strip(char *s) |
| 396 | { |
| 397 | int n; |
| 398 | while(*s && isspace(*s)) s++; |
| 399 | n = strlen(s); |
| 400 | while(n-- > 0) { |
| 401 | if(!isspace(s[n])) break; |
| 402 | s[n] = 0; |
| 403 | } |
| 404 | return s; |
| 405 | } |
| 406 | |
| 407 | #define MAX_OPTIONS 32 |
| 408 | static int setup_requirement_line(char *name) |
| 409 | { |
| 410 | char *val[MAX_OPTIONS]; |
| 411 | const char **out; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 412 | char *prod = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 413 | unsigned n, count; |
| 414 | char *x; |
| 415 | int invert = 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 416 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 417 | if (!strncmp(name, "reject ", 7)) { |
| 418 | name += 7; |
| 419 | invert = 1; |
| 420 | } else if (!strncmp(name, "require ", 8)) { |
| 421 | name += 8; |
| 422 | invert = 0; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 423 | } else if (!strncmp(name, "require-for-product:", 20)) { |
| 424 | // Get the product and point name past it |
| 425 | prod = name + 20; |
| 426 | name = strchr(name, ' '); |
| 427 | if (!name) return -1; |
| 428 | *name = 0; |
| 429 | name += 1; |
| 430 | invert = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | x = strchr(name, '='); |
| 434 | if (x == 0) return 0; |
| 435 | *x = 0; |
| 436 | val[0] = x + 1; |
| 437 | |
| 438 | for(count = 1; count < MAX_OPTIONS; count++) { |
| 439 | x = strchr(val[count - 1],'|'); |
| 440 | if (x == 0) break; |
| 441 | *x = 0; |
| 442 | val[count] = x + 1; |
| 443 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 444 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 445 | name = strip(name); |
| 446 | for(n = 0; n < count; n++) val[n] = strip(val[n]); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 447 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 448 | name = strip(name); |
| 449 | if (name == 0) return -1; |
| 450 | |
| 451 | /* work around an unfortunate name mismatch */ |
| 452 | if (!strcmp(name,"board")) name = "product"; |
| 453 | |
| 454 | out = malloc(sizeof(char*) * count); |
| 455 | if (out == 0) return -1; |
| 456 | |
| 457 | for(n = 0; n < count; n++) { |
| 458 | out[n] = strdup(strip(val[n])); |
| 459 | if (out[n] == 0) return -1; |
| 460 | } |
| 461 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 462 | fb_queue_require(prod, name, invert, n, out); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | static void setup_requirements(char *data, unsigned sz) |
| 467 | { |
| 468 | char *s; |
| 469 | |
| 470 | s = data; |
| 471 | while (sz-- > 0) { |
| 472 | if(*s == '\n') { |
| 473 | *s++ = 0; |
| 474 | if (setup_requirement_line(data)) { |
| 475 | die("out of memory"); |
| 476 | } |
| 477 | data = s; |
| 478 | } else { |
| 479 | s++; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | void queue_info_dump(void) |
| 485 | { |
| 486 | fb_queue_notice("--------------------------------------------"); |
| 487 | fb_queue_display("version-bootloader", "Bootloader Version..."); |
| 488 | fb_queue_display("version-baseband", "Baseband Version....."); |
| 489 | fb_queue_display("serialno", "Serial Number........"); |
| 490 | fb_queue_notice("--------------------------------------------"); |
| 491 | } |
| 492 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 493 | |
| 494 | struct sparse_file **load_sparse_files(const char *fname, int max_size) |
| 495 | { |
| 496 | int fd; |
| 497 | struct sparse_file *s; |
| 498 | int files; |
| 499 | struct sparse_file **out_s; |
| 500 | |
| 501 | fd = open(fname, O_RDONLY | O_BINARY); |
| 502 | if (fd < 0) { |
| 503 | die("cannot open '%s'\n", fname); |
| 504 | } |
| 505 | |
| 506 | s = sparse_file_import_auto(fd, false); |
| 507 | if (!s) { |
| 508 | die("cannot sparse read file '%s'\n", fname); |
| 509 | } |
| 510 | |
| 511 | files = sparse_file_resparse(s, max_size, NULL, 0); |
| 512 | if (files < 0) { |
| 513 | die("Failed to resparse '%s'\n", fname); |
| 514 | } |
| 515 | |
| 516 | out_s = calloc(sizeof(struct sparse_file *), files + 1); |
| 517 | if (!out_s) { |
| 518 | die("Failed to allocate sparse file array\n"); |
| 519 | } |
| 520 | |
| 521 | files = sparse_file_resparse(s, max_size, out_s, files); |
| 522 | if (files < 0) { |
| 523 | die("Failed to resparse '%s'\n", fname); |
| 524 | } |
| 525 | |
| 526 | return out_s; |
| 527 | } |
| 528 | |
| 529 | static int64_t get_target_sparse_limit(struct usb_handle *usb) |
| 530 | { |
| 531 | int64_t limit = 0; |
| 532 | char response[FB_RESPONSE_SZ + 1]; |
| 533 | int status = fb_getvar(usb, response, "max-download-size"); |
| 534 | |
| 535 | if (!status) { |
| 536 | limit = strtoul(response, NULL, 0); |
| 537 | if (limit > 0) { |
| 538 | fprintf(stderr, "target reported max download size of %lld bytes\n", |
| 539 | limit); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | return limit; |
| 544 | } |
| 545 | |
| 546 | static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size) |
| 547 | { |
| 548 | int64_t limit; |
| 549 | |
| 550 | if (sparse_limit == 0) { |
| 551 | return 0; |
| 552 | } else if (sparse_limit > 0) { |
| 553 | limit = sparse_limit; |
| 554 | } else { |
| 555 | if (target_sparse_limit == -1) { |
| 556 | target_sparse_limit = get_target_sparse_limit(usb); |
| 557 | } |
| 558 | if (target_sparse_limit > 0) { |
| 559 | limit = target_sparse_limit; |
| 560 | } else { |
Colin Cross | 0bbfb39 | 2012-07-24 18:05:21 -0700 | [diff] [blame] | 561 | return 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
| 565 | if (size > limit) { |
| 566 | return limit; |
| 567 | } |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 572 | /* Until we get lazy inode table init working in make_ext4fs, we need to |
| 573 | * erase partitions of type ext4 before flashing a filesystem so no stale |
| 574 | * inodes are left lying around. Otherwise, e2fsck gets very upset. |
| 575 | */ |
| 576 | static int needs_erase(const char *part) |
| 577 | { |
| 578 | /* The function fb_format_supported() currently returns the value |
| 579 | * we want, so just call it. |
| 580 | */ |
| 581 | return fb_format_supported(usb, part); |
| 582 | } |
| 583 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 584 | void do_flash(usb_handle *usb, const char *pname, const char *fname) |
| 585 | { |
| 586 | int64_t sz64; |
| 587 | void *data; |
| 588 | int64_t limit; |
| 589 | |
| 590 | sz64 = file_size(fname); |
| 591 | limit = get_sparse_limit(usb, sz64); |
| 592 | if (limit) { |
| 593 | struct sparse_file **s = load_sparse_files(fname, limit); |
| 594 | if (s == NULL) { |
| 595 | die("cannot sparse load '%s'\n", fname); |
| 596 | } |
| 597 | while (*s) { |
| 598 | sz64 = sparse_file_len(*s, true, false); |
| 599 | fb_queue_flash_sparse(pname, *s++, sz64); |
| 600 | } |
| 601 | } else { |
| 602 | unsigned int sz; |
| 603 | data = load_file(fname, &sz); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 604 | if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno)); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 605 | fb_queue_flash(pname, data, sz); |
| 606 | } |
| 607 | } |
| 608 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 609 | void do_update_signature(zipfile_t zip, char *fn) |
| 610 | { |
| 611 | void *data; |
| 612 | unsigned sz; |
| 613 | data = unzip_file(zip, fn, &sz); |
| 614 | if (data == 0) return; |
| 615 | fb_queue_download("signature", data, sz); |
| 616 | fb_queue_command("signature", "installing signature"); |
| 617 | } |
| 618 | |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 619 | void do_update(char *fn, int erase_first) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 620 | { |
| 621 | void *zdata; |
| 622 | unsigned zsize; |
| 623 | void *data; |
| 624 | unsigned sz; |
| 625 | zipfile_t zip; |
| 626 | |
| 627 | queue_info_dump(); |
| 628 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 629 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
| 630 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 631 | zdata = load_file(fn, &zsize); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 632 | if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 633 | |
| 634 | zip = init_zipfile(zdata, zsize); |
| 635 | if(zip == 0) die("failed to access zipdata in '%s'"); |
| 636 | |
| 637 | data = unzip_file(zip, "android-info.txt", &sz); |
| 638 | if (data == 0) { |
| 639 | char *tmp; |
| 640 | /* fallback for older zipfiles */ |
| 641 | data = unzip_file(zip, "android-product.txt", &sz); |
| 642 | if ((data == 0) || (sz < 1)) { |
| 643 | die("update package has no android-info.txt or android-product.txt"); |
| 644 | } |
| 645 | tmp = malloc(sz + 128); |
| 646 | if (tmp == 0) die("out of memory"); |
| 647 | sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data); |
| 648 | data = tmp; |
| 649 | sz = strlen(tmp); |
| 650 | } |
| 651 | |
| 652 | setup_requirements(data, sz); |
| 653 | |
| 654 | data = unzip_file(zip, "boot.img", &sz); |
| 655 | if (data == 0) die("update package missing boot.img"); |
| 656 | do_update_signature(zip, "boot.sig"); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 657 | if (erase_first && needs_erase("boot")) { |
| 658 | fb_queue_erase("boot"); |
| 659 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 660 | fb_queue_flash("boot", data, sz); |
| 661 | |
| 662 | data = unzip_file(zip, "recovery.img", &sz); |
| 663 | if (data != 0) { |
| 664 | do_update_signature(zip, "recovery.sig"); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 665 | if (erase_first && needs_erase("recovery")) { |
| 666 | fb_queue_erase("recovery"); |
| 667 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 668 | fb_queue_flash("recovery", data, sz); |
| 669 | } |
| 670 | |
| 671 | data = unzip_file(zip, "system.img", &sz); |
| 672 | if (data == 0) die("update package missing system.img"); |
| 673 | do_update_signature(zip, "system.sig"); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 674 | if (erase_first && needs_erase("system")) { |
| 675 | fb_queue_erase("system"); |
| 676 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 677 | fb_queue_flash("system", data, sz); |
| 678 | } |
| 679 | |
| 680 | void do_send_signature(char *fn) |
| 681 | { |
| 682 | void *data; |
| 683 | unsigned sz; |
| 684 | char *xtn; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 685 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 686 | xtn = strrchr(fn, '.'); |
| 687 | if (!xtn) return; |
| 688 | if (strcmp(xtn, ".img")) return; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 689 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 690 | strcpy(xtn,".sig"); |
| 691 | data = load_file(fn, &sz); |
| 692 | strcpy(xtn,".img"); |
| 693 | if (data == 0) return; |
| 694 | fb_queue_download("signature", data, sz); |
| 695 | fb_queue_command("signature", "installing signature"); |
| 696 | } |
| 697 | |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 698 | void do_flashall(int erase_first) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 699 | { |
| 700 | char *fname; |
| 701 | void *data; |
| 702 | unsigned sz; |
| 703 | |
| 704 | queue_info_dump(); |
| 705 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 706 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
| 707 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 708 | fname = find_item("info", product); |
| 709 | if (fname == 0) die("cannot find android-info.txt"); |
| 710 | data = load_file(fname, &sz); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 711 | if (data == 0) die("could not load android-info.txt: %s", strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 712 | setup_requirements(data, sz); |
| 713 | |
| 714 | fname = find_item("boot", product); |
| 715 | data = load_file(fname, &sz); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 716 | if (data == 0) die("could not load boot.img: %s", strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 717 | do_send_signature(fname); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 718 | if (erase_first && needs_erase("boot")) { |
| 719 | fb_queue_erase("boot"); |
| 720 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 721 | fb_queue_flash("boot", data, sz); |
| 722 | |
| 723 | fname = find_item("recovery", product); |
| 724 | data = load_file(fname, &sz); |
| 725 | if (data != 0) { |
| 726 | do_send_signature(fname); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 727 | if (erase_first && needs_erase("recovery")) { |
| 728 | fb_queue_erase("recovery"); |
| 729 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 730 | fb_queue_flash("recovery", data, sz); |
| 731 | } |
| 732 | |
| 733 | fname = find_item("system", product); |
| 734 | data = load_file(fname, &sz); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 735 | if (data == 0) die("could not load system.img: %s", strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 736 | do_send_signature(fname); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 737 | if (erase_first && needs_erase("system")) { |
| 738 | fb_queue_erase("system"); |
| 739 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 740 | fb_queue_flash("system", data, sz); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | #define skip(n) do { argc -= (n); argv += (n); } while (0) |
JP Abgrall | 2d13d14 | 2011-03-01 23:35:07 -0800 | [diff] [blame] | 744 | #define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 745 | |
| 746 | int do_oem_command(int argc, char **argv) |
| 747 | { |
| 748 | int i; |
| 749 | char command[256]; |
| 750 | if (argc <= 1) return 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 751 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 752 | command[0] = 0; |
| 753 | while(1) { |
| 754 | strcat(command,*argv); |
| 755 | skip(1); |
| 756 | if(argc == 0) break; |
| 757 | strcat(command," "); |
| 758 | } |
| 759 | |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 760 | fb_queue_command(command,""); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 761 | return 0; |
| 762 | } |
| 763 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 764 | static int64_t parse_num(const char *arg) |
| 765 | { |
| 766 | char *endptr; |
| 767 | unsigned long long num; |
| 768 | |
| 769 | num = strtoull(arg, &endptr, 0); |
| 770 | if (endptr == arg) { |
| 771 | return -1; |
| 772 | } |
| 773 | |
| 774 | if (*endptr == 'k' || *endptr == 'K') { |
| 775 | if (num >= (-1ULL) / 1024) { |
| 776 | return -1; |
| 777 | } |
| 778 | num *= 1024LL; |
| 779 | endptr++; |
| 780 | } else if (*endptr == 'm' || *endptr == 'M') { |
| 781 | if (num >= (-1ULL) / (1024 * 1024)) { |
| 782 | return -1; |
| 783 | } |
| 784 | num *= 1024LL * 1024LL; |
| 785 | endptr++; |
| 786 | } else if (*endptr == 'g' || *endptr == 'G') { |
| 787 | if (num >= (-1ULL) / (1024 * 1024 * 1024)) { |
| 788 | return -1; |
| 789 | } |
| 790 | num *= 1024LL * 1024LL * 1024LL; |
| 791 | endptr++; |
| 792 | } |
| 793 | |
| 794 | if (*endptr != '\0') { |
| 795 | return -1; |
| 796 | } |
| 797 | |
| 798 | if (num > INT64_MAX) { |
| 799 | return -1; |
| 800 | } |
| 801 | |
| 802 | return num; |
| 803 | } |
| 804 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 805 | int main(int argc, char **argv) |
| 806 | { |
| 807 | int wants_wipe = 0; |
| 808 | int wants_reboot = 0; |
| 809 | int wants_reboot_bootloader = 0; |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 810 | int erase_first = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 811 | void *data; |
| 812 | unsigned sz; |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 813 | int status; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 814 | int c; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 815 | int r; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 816 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 817 | const struct option longopts[] = { |
| 818 | {"base", required_argument, 0, 'b'}, |
| 819 | {"kernel_offset", required_argument, 0, 'k'}, |
| 820 | {"page_size", required_argument, 0, 'n'}, |
| 821 | {"ramdisk_offset", required_argument, 0, 'r'}, |
| 822 | {"help", 0, 0, 'h'}, |
| 823 | {0, 0, 0, 0} |
| 824 | }; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 825 | |
| 826 | serial = getenv("ANDROID_SERIAL"); |
| 827 | |
| 828 | while (1) { |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 829 | int option_index = 0; |
| 830 | c = getopt_long(argc, argv, "wub:k:n:r:s:S:lp:c:i:m:h", longopts, NULL); |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 831 | if (c < 0) { |
| 832 | break; |
| 833 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 834 | /* Alphabetical cases */ |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 835 | switch (c) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 836 | case 'b': |
| 837 | base_addr = strtoul(optarg, 0, 16); |
| 838 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 839 | case 'c': |
| 840 | cmdline = optarg; |
| 841 | break; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 842 | case 'h': |
| 843 | usage(); |
| 844 | return 1; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 845 | case 'i': { |
| 846 | char *endptr = NULL; |
| 847 | unsigned long val; |
| 848 | |
| 849 | val = strtoul(optarg, &endptr, 0); |
| 850 | if (!endptr || *endptr != '\0' || (val & ~0xffff)) |
| 851 | die("invalid vendor id '%s'", optarg); |
| 852 | vendor_id = (unsigned short)val; |
| 853 | break; |
| 854 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 855 | case 'k': |
| 856 | kernel_offset = strtoul(optarg, 0, 16); |
| 857 | break; |
| 858 | case 'l': |
| 859 | long_listing = 1; |
| 860 | break; |
| 861 | case 'n': |
| 862 | page_size = (unsigned)strtoul(optarg, NULL, 0); |
| 863 | if (!page_size) die("invalid page size"); |
| 864 | break; |
| 865 | case 'p': |
| 866 | product = optarg; |
| 867 | break; |
| 868 | case 'r': |
| 869 | ramdisk_offset = strtoul(optarg, 0, 16); |
| 870 | break; |
| 871 | case 's': |
| 872 | serial = optarg; |
| 873 | break; |
| 874 | case 'S': |
| 875 | sparse_limit = parse_num(optarg); |
| 876 | if (sparse_limit < 0) { |
| 877 | die("invalid sparse limit"); |
| 878 | } |
| 879 | break; |
| 880 | case 'u': |
| 881 | erase_first = 0; |
| 882 | break; |
| 883 | case 'w': |
| 884 | wants_wipe = 1; |
| 885 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 886 | case '?': |
| 887 | return 1; |
| 888 | default: |
| 889 | abort(); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | argc -= optind; |
| 894 | argv += optind; |
| 895 | |
| 896 | if (argc == 0 && !wants_wipe) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 897 | usage(); |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 898 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 899 | } |
| 900 | |
Colin Cross | 8fb6e06 | 2012-07-24 16:36:41 -0700 | [diff] [blame] | 901 | if (argc > 0 && !strcmp(*argv, "devices")) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 902 | skip(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 903 | list_devices(); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 904 | return 0; |
| 905 | } |
| 906 | |
Colin Cross | c7b75dc | 2012-08-29 18:17:06 -0700 | [diff] [blame] | 907 | if (argc > 0 && !strcmp(*argv, "help")) { |
| 908 | usage(); |
| 909 | return 0; |
| 910 | } |
| 911 | |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 912 | usb = open_device(); |
Elliott Hughes | 31dbed7 | 2009-10-07 15:38:53 -0700 | [diff] [blame] | 913 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 914 | while (argc > 0) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 915 | if(!strcmp(*argv, "getvar")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 916 | require(2); |
| 917 | fb_queue_display(argv[1], argv[1]); |
| 918 | skip(2); |
| 919 | } else if(!strcmp(*argv, "erase")) { |
| 920 | require(2); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 921 | |
| 922 | if (fb_format_supported(usb, argv[1])) { |
| 923 | fprintf(stderr, "******** Did you mean to fastboot format this partition?\n"); |
| 924 | } |
| 925 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 926 | fb_queue_erase(argv[1]); |
| 927 | skip(2); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 928 | } else if(!strcmp(*argv, "format")) { |
| 929 | require(2); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 930 | if (erase_first && needs_erase(argv[1])) { |
| 931 | fb_queue_erase(argv[1]); |
| 932 | } |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 933 | fb_queue_format(argv[1], 0); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 934 | skip(2); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 935 | } else if(!strcmp(*argv, "signature")) { |
| 936 | require(2); |
| 937 | data = load_file(argv[1], &sz); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 938 | if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 939 | if (sz != 256) die("signature must be 256 bytes"); |
| 940 | fb_queue_download("signature", data, sz); |
| 941 | fb_queue_command("signature", "installing signature"); |
| 942 | skip(2); |
| 943 | } else if(!strcmp(*argv, "reboot")) { |
| 944 | wants_reboot = 1; |
| 945 | skip(1); |
| 946 | } else if(!strcmp(*argv, "reboot-bootloader")) { |
| 947 | wants_reboot_bootloader = 1; |
| 948 | skip(1); |
| 949 | } else if (!strcmp(*argv, "continue")) { |
| 950 | fb_queue_command("continue", "resuming boot"); |
| 951 | skip(1); |
| 952 | } else if(!strcmp(*argv, "boot")) { |
| 953 | char *kname = 0; |
| 954 | char *rname = 0; |
| 955 | skip(1); |
| 956 | if (argc > 0) { |
| 957 | kname = argv[0]; |
| 958 | skip(1); |
| 959 | } |
| 960 | if (argc > 0) { |
| 961 | rname = argv[0]; |
| 962 | skip(1); |
| 963 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 964 | data = load_bootable_image(kname, rname, &sz, cmdline); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 965 | if (data == 0) return 1; |
| 966 | fb_queue_download("boot.img", data, sz); |
| 967 | fb_queue_command("boot", "booting"); |
| 968 | } else if(!strcmp(*argv, "flash")) { |
| 969 | char *pname = argv[1]; |
| 970 | char *fname = 0; |
| 971 | require(2); |
| 972 | if (argc > 2) { |
| 973 | fname = argv[2]; |
| 974 | skip(3); |
| 975 | } else { |
| 976 | fname = find_item(pname, product); |
| 977 | skip(2); |
| 978 | } |
| 979 | if (fname == 0) die("cannot determine image filename for '%s'", pname); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 980 | if (erase_first && needs_erase(pname)) { |
| 981 | fb_queue_erase(pname); |
| 982 | } |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 983 | do_flash(usb, pname, fname); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 984 | } else if(!strcmp(*argv, "flash:raw")) { |
| 985 | char *pname = argv[1]; |
| 986 | char *kname = argv[2]; |
| 987 | char *rname = 0; |
| 988 | require(3); |
| 989 | if(argc > 3) { |
| 990 | rname = argv[3]; |
| 991 | skip(4); |
| 992 | } else { |
| 993 | skip(3); |
| 994 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 995 | data = load_bootable_image(kname, rname, &sz, cmdline); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 996 | if (data == 0) die("cannot load bootable image"); |
| 997 | fb_queue_flash(pname, data, sz); |
| 998 | } else if(!strcmp(*argv, "flashall")) { |
| 999 | skip(1); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1000 | do_flashall(erase_first); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1001 | wants_reboot = 1; |
| 1002 | } else if(!strcmp(*argv, "update")) { |
| 1003 | if (argc > 1) { |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1004 | do_update(argv[1], erase_first); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1005 | skip(2); |
| 1006 | } else { |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1007 | do_update("update.zip", erase_first); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1008 | skip(1); |
| 1009 | } |
| 1010 | wants_reboot = 1; |
| 1011 | } else if(!strcmp(*argv, "oem")) { |
| 1012 | argc = do_oem_command(argc, argv); |
| 1013 | } else { |
| 1014 | usage(); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1015 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | if (wants_wipe) { |
| 1020 | fb_queue_erase("userdata"); |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 1021 | fb_queue_format("userdata", 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1022 | fb_queue_erase("cache"); |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 1023 | fb_queue_format("cache", 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1024 | } |
| 1025 | if (wants_reboot) { |
| 1026 | fb_queue_reboot(); |
| 1027 | } else if (wants_reboot_bootloader) { |
| 1028 | fb_queue_command("reboot-bootloader", "rebooting into bootloader"); |
| 1029 | } |
| 1030 | |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 1031 | if (fb_queue_is_empty()) |
| 1032 | return 0; |
| 1033 | |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 1034 | status = fb_execute_queue(usb); |
| 1035 | return (status) ? 1 : 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1036 | } |