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