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