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 | |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 31 | #include <ctype.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | #include <errno.h> |
| 33 | #include <fcntl.h> |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 34 | #include <getopt.h> |
Ying Wang | cf86e2f | 2014-05-15 20:06:40 -0700 | [diff] [blame] | 35 | #include <inttypes.h> |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 36 | #include <limits.h> |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 37 | #include <stdint.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <sys/stat.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | #include <sys/time.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 43 | #include <sys/types.h> |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 44 | #include <unistd.h> |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 45 | #include <functional> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 46 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 47 | #include <base/parseint.h> |
Elliott Hughes | 77c0e66 | 2015-11-02 15:51:12 -0800 | [diff] [blame] | 48 | #include <base/strings.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 49 | #include <sparse/sparse.h> |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 50 | #include <ziparchive/zip_archive.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 52 | #include <base/strings.h> |
| 53 | #include <base/parseint.h> |
| 54 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 55 | #include "bootimg_utils.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | #include "fastboot.h" |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 57 | #include "fs.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 59 | #ifndef O_BINARY |
| 60 | #define O_BINARY 0 |
| 61 | #endif |
| 62 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 63 | #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a))) |
| 64 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 65 | char cur_product[FB_RESPONSE_SZ + 1]; |
| 66 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | static const char *serial = 0; |
| 68 | static const char *product = 0; |
| 69 | static const char *cmdline = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | static unsigned short vendor_id = 0; |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 71 | static int long_listing = 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 72 | static int64_t sparse_limit = -1; |
| 73 | static int64_t target_sparse_limit = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 75 | static unsigned page_size = 2048; |
| 76 | static unsigned base_addr = 0x10000000; |
| 77 | static unsigned kernel_offset = 0x00008000; |
| 78 | static unsigned ramdisk_offset = 0x01000000; |
| 79 | static unsigned second_offset = 0x00f00000; |
| 80 | static unsigned tags_offset = 0x00000100; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 81 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 82 | enum fb_buffer_type { |
| 83 | FB_BUFFER, |
| 84 | FB_BUFFER_SPARSE, |
| 85 | }; |
| 86 | |
| 87 | struct fastboot_buffer { |
| 88 | enum fb_buffer_type type; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 89 | void* data; |
| 90 | int64_t sz; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static struct { |
| 94 | char img_name[13]; |
| 95 | char sig_name[13]; |
| 96 | char part_name[9]; |
| 97 | bool is_optional; |
Daniel Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 98 | } images[] = { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 99 | {"boot.img", "boot.sig", "boot", false}, |
| 100 | {"recovery.img", "recovery.sig", "recovery", true}, |
| 101 | {"system.img", "system.sig", "system", false}, |
Daniel Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 102 | {"vendor.img", "vendor.sig", "vendor", true}, |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 103 | }; |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 104 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 105 | static char* find_item(const char* item, const char* product) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 106 | char *dir; |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 107 | const char *fn; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 108 | char path[PATH_MAX + 128]; |
| 109 | |
| 110 | if(!strcmp(item,"boot")) { |
| 111 | fn = "boot.img"; |
| 112 | } else if(!strcmp(item,"recovery")) { |
| 113 | fn = "recovery.img"; |
| 114 | } else if(!strcmp(item,"system")) { |
| 115 | fn = "system.img"; |
Daniel Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 116 | } else if(!strcmp(item,"vendor")) { |
| 117 | fn = "vendor.img"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 | } else if(!strcmp(item,"userdata")) { |
| 119 | fn = "userdata.img"; |
Jean-Baptiste Queru | d7608a4 | 2011-09-30 14:39:25 -0700 | [diff] [blame] | 120 | } else if(!strcmp(item,"cache")) { |
| 121 | fn = "cache.img"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | } else if(!strcmp(item,"info")) { |
| 123 | fn = "android-info.txt"; |
| 124 | } else { |
| 125 | fprintf(stderr,"unknown partition '%s'\n", item); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | if(product) { |
| 130 | get_my_path(path); |
| 131 | sprintf(path + strlen(path), |
| 132 | "../../../target/product/%s/%s", product, fn); |
| 133 | return strdup(path); |
| 134 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 135 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 136 | dir = getenv("ANDROID_PRODUCT_OUT"); |
| 137 | if((dir == 0) || (dir[0] == 0)) { |
| 138 | die("neither -p product specified nor ANDROID_PRODUCT_OUT set"); |
| 139 | return 0; |
| 140 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 141 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 142 | sprintf(path, "%s/%s", dir, fn); |
| 143 | return strdup(path); |
| 144 | } |
| 145 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 146 | static int64_t get_file_size(int fd) { |
| 147 | struct stat sb; |
| 148 | return fstat(fd, &sb) == -1 ? -1 : sb.st_size; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 151 | static void* load_fd(int fd, int64_t* sz) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 152 | int errno_tmp; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 153 | char* data = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 155 | *sz = get_file_size(fd); |
| 156 | if (*sz < 0) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 157 | goto oops; |
| 158 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 160 | data = (char*) malloc(*sz); |
| 161 | if (data == nullptr) goto oops; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 162 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 163 | if(read(fd, data, *sz) != *sz) goto oops; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 164 | close(fd); |
| 165 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 166 | return data; |
| 167 | |
| 168 | oops: |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 169 | errno_tmp = errno; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 170 | close(fd); |
| 171 | if(data != 0) free(data); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 172 | errno = errno_tmp; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 | return 0; |
| 174 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 175 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 176 | static void* load_file(const char* fn, int64_t* sz) { |
| 177 | int fd = open(fn, O_RDONLY | O_BINARY); |
| 178 | if (fd == -1) return nullptr; |
| 179 | return load_fd(fd, sz); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 180 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 182 | static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) { |
Elliott Hughes | e1746fd | 2015-08-10 15:30:54 -0700 | [diff] [blame] | 183 | // Require a matching vendor id if the user specified one with -i. |
Elliott Hughes | b46964f | 2015-08-19 17:49:45 -0700 | [diff] [blame] | 184 | if (vendor_id != 0 && info->dev_vendor != vendor_id) { |
Elliott Hughes | e1746fd | 2015-08-10 15:30:54 -0700 | [diff] [blame] | 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) { |
| 189 | return -1; |
| 190 | } |
| 191 | |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 192 | // require matching serial number or device path if requested |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | // at the command line with the -s option. |
JP Abgrall | a032ded | 2012-06-06 11:53:33 -0700 | [diff] [blame] | 194 | if (local_serial && (strcmp(local_serial, info->serial_number) != 0 && |
| 195 | strcmp(local_serial, info->device_path) != 0)) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 199 | static int match_fastboot(usb_ifc_info* info) { |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 200 | return match_fastboot_with_serial(info, serial); |
| 201 | } |
| 202 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 203 | static int list_devices_callback(usb_ifc_info* info) { |
| 204 | if (match_fastboot_with_serial(info, nullptr) == 0) { |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 205 | const char* serial = info->serial_number; |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 206 | if (!info->writable) { |
| 207 | serial = "no permissions"; // like "adb devices" |
| 208 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 209 | if (!serial[0]) { |
| 210 | serial = "????????????"; |
| 211 | } |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 212 | // output compatible with "adb devices" |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 213 | if (!long_listing) { |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 214 | printf("%s\tfastboot\n", serial); |
Stephen Hines | 04f8953 | 2014-11-26 14:54:43 -0800 | [diff] [blame] | 215 | } else if (strcmp("", info->device_path) == 0) { |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 216 | printf("%-22s fastboot\n", serial); |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 217 | } else { |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 218 | printf("%-22s fastboot %s\n", serial, info->device_path); |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 219 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return -1; |
| 223 | } |
| 224 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 225 | static usb_handle* open_device() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 226 | static usb_handle *usb = 0; |
| 227 | int announce = 1; |
| 228 | |
| 229 | if(usb) return usb; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 230 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | for(;;) { |
| 232 | usb = usb_open(match_fastboot); |
| 233 | if(usb) return usb; |
| 234 | if(announce) { |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 235 | announce = 0; |
Elliott Hughes | b46964f | 2015-08-19 17:49:45 -0700 | [diff] [blame] | 236 | fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 237 | } |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 238 | usleep(1000); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 242 | static void list_devices() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | // We don't actually open a USB device here, |
| 244 | // just getting our callback called so we can |
| 245 | // list all the connected devices. |
| 246 | usb_open(list_devices_callback); |
| 247 | } |
| 248 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 249 | static void usage() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 250 | fprintf(stderr, |
| 251 | /* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */ |
| 252 | "usage: fastboot [ <option> ] <command>\n" |
| 253 | "\n" |
| 254 | "commands:\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 255 | " update <filename> Reflash device from update.zip.\n" |
| 256 | " flashall Flash boot, system, vendor, and --\n" |
| 257 | " if found -- recovery.\n" |
| 258 | " flash <partition> [ <filename> ] Write a file to a flash partition.\n" |
| 259 | " flashing lock Locks the device. Prevents flashing.\n" |
| 260 | " flashing unlock Unlocks the device. Allows flashing\n" |
| 261 | " any partition except\n" |
| 262 | " bootloader-related partitions.\n" |
| 263 | " flashing lock_critical Prevents flashing bootloader-related\n" |
| 264 | " partitions.\n" |
| 265 | " flashing unlock_critical Enables flashing bootloader-related\n" |
| 266 | " partitions.\n" |
| 267 | " flashing get_unlock_ability Queries bootloader to see if the\n" |
| 268 | " device is unlocked.\n" |
Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 269 | " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n" |
Elliott Hughes | 45be42e | 2015-09-02 20:15:48 -0700 | [diff] [blame] | 270 | " unlock nonce.\n" |
| 271 | " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n" |
Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 272 | " flashing lock_bootloader Locks the bootloader to prevent\n" |
Elliott Hughes | 45be42e | 2015-09-02 20:15:48 -0700 | [diff] [blame] | 273 | " bootloader version rollback.\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 274 | " erase <partition> Erase a flash partition.\n" |
| 275 | " format[:[<fs type>][:[<size>]] <partition>\n" |
| 276 | " Format a flash partition. Can\n" |
| 277 | " override the fs type and/or size\n" |
| 278 | " the bootloader reports.\n" |
| 279 | " getvar <variable> Display a bootloader variable.\n" |
| 280 | " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n" |
| 281 | " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n" |
| 282 | " Create bootimage and flash it.\n" |
| 283 | " devices [-l] List all connected devices [with\n" |
| 284 | " device paths].\n" |
| 285 | " continue Continue with autoboot.\n" |
| 286 | " reboot [bootloader] Reboot device [into bootloader].\n" |
| 287 | " reboot-bootloader Reboot device into bootloader.\n" |
| 288 | " help Show this help message.\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 289 | "\n" |
| 290 | "options:\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 291 | " -w Erase userdata and cache (and format\n" |
| 292 | " if supported by partition type).\n" |
| 293 | " -u Do not erase partition before\n" |
| 294 | " formatting.\n" |
| 295 | " -s <specific device> Specify device serial number\n" |
| 296 | " or path to device port.\n" |
| 297 | " -p <product> Specify product name.\n" |
| 298 | " -c <cmdline> Override kernel commandline.\n" |
| 299 | " -i <vendor id> Specify a custom USB vendor id.\n" |
| 300 | " -b <base_addr> Specify a custom kernel base\n" |
| 301 | " address (default: 0x10000000).\n" |
| 302 | " -n <page size> Specify the nand page size\n" |
| 303 | " (default: 2048).\n" |
| 304 | " -S <size>[K|M|G] Automatically sparse files greater\n" |
| 305 | " than 'size'. 0 to disable.\n" |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 306 | " --slot <suffix> Specify slot suffix to be used if the\n" |
| 307 | " device supports slots. This will be\n" |
| 308 | " added to all partition names that use\n" |
| 309 | " slots. 'all' can be given to refer\n" |
| 310 | " to all slots. If this is not given,\n" |
| 311 | " slotted partitions will default to\n" |
| 312 | " the current active slot.\n" |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 313 | " --set_active[=<suffix>] Sets the active slot. If no suffix is\n" |
| 314 | " provided, this will default to the value\n" |
| 315 | " given by --slot. If slots are not\n" |
| 316 | " supported, this does nothing.\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 317 | ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 318 | } |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 319 | static void* load_bootable_image(const char* kernel, const char* ramdisk, |
| 320 | const char* secondstage, int64_t* sz, |
| 321 | const char* cmdline) { |
| 322 | if (kernel == nullptr) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 323 | fprintf(stderr, "no image specified\n"); |
| 324 | return 0; |
| 325 | } |
| 326 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 327 | int64_t ksize; |
| 328 | void* kdata = load_file(kernel, &ksize); |
| 329 | if (kdata == nullptr) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 330 | 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] | 331 | return 0; |
| 332 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 333 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 334 | // Is this actually a boot image? |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 335 | if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 336 | if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 337 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 338 | if (ramdisk) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 339 | fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n"); |
| 340 | return 0; |
| 341 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 342 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 343 | *sz = ksize; |
| 344 | return kdata; |
| 345 | } |
| 346 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 347 | void* rdata = nullptr; |
| 348 | int64_t rsize = 0; |
| 349 | if (ramdisk) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 350 | rdata = load_file(ramdisk, &rsize); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 351 | if (rdata == nullptr) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 352 | 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] | 353 | return 0; |
| 354 | } |
| 355 | } |
| 356 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 357 | void* sdata = nullptr; |
| 358 | int64_t ssize = 0; |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 359 | if (secondstage) { |
| 360 | sdata = load_file(secondstage, &ssize); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 361 | if (sdata == nullptr) { |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 362 | fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno)); |
| 363 | return 0; |
| 364 | } |
| 365 | } |
| 366 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 367 | fprintf(stderr,"creating boot image...\n"); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 368 | int64_t bsize = 0; |
| 369 | void* bdata = mkbootimg(kdata, ksize, kernel_offset, |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 370 | rdata, rsize, ramdisk_offset, |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 371 | sdata, ssize, second_offset, |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 372 | page_size, base_addr, tags_offset, &bsize); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 373 | if (bdata == nullptr) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 374 | fprintf(stderr,"failed to create boot.img\n"); |
| 375 | return 0; |
| 376 | } |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 377 | if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline); |
| 378 | fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 379 | *sz = bsize; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 380 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 381 | return bdata; |
| 382 | } |
| 383 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 384 | static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 385 | { |
Yusuke Sato | 0744754 | 2015-06-25 14:39:19 -0700 | [diff] [blame] | 386 | ZipString zip_entry_name(entry_name); |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 387 | ZipEntry zip_entry; |
| 388 | if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) { |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 389 | fprintf(stderr, "archive does not contain '%s'\n", entry_name); |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 390 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 393 | *sz = zip_entry.uncompressed_length; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 394 | |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 395 | uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length)); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 396 | if (data == nullptr) { |
| 397 | fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name); |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 398 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 401 | int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length); |
| 402 | if (error != 0) { |
| 403 | fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 404 | free(data); |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 405 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 406 | } |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 407 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 408 | return data; |
| 409 | } |
| 410 | |
Elliott Hughes | a26fbee | 2015-06-03 15:22:11 -0700 | [diff] [blame] | 411 | #if defined(_WIN32) |
| 412 | |
| 413 | // TODO: move this to somewhere it can be shared. |
| 414 | |
| 415 | #include <windows.h> |
| 416 | |
| 417 | // Windows' tmpfile(3) requires administrator rights because |
| 418 | // it creates temporary files in the root directory. |
| 419 | static FILE* win32_tmpfile() { |
| 420 | char temp_path[PATH_MAX]; |
| 421 | DWORD nchars = GetTempPath(sizeof(temp_path), temp_path); |
| 422 | if (nchars == 0 || nchars >= sizeof(temp_path)) { |
| 423 | fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError()); |
| 424 | return nullptr; |
| 425 | } |
| 426 | |
| 427 | char filename[PATH_MAX]; |
| 428 | if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) { |
| 429 | fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError()); |
| 430 | return nullptr; |
| 431 | } |
| 432 | |
| 433 | return fopen(filename, "w+bTD"); |
| 434 | } |
| 435 | |
| 436 | #define tmpfile win32_tmpfile |
| 437 | |
| 438 | #endif |
| 439 | |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 440 | static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) { |
| 441 | FILE* fp = tmpfile(); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 442 | if (fp == nullptr) { |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 443 | fprintf(stderr, "failed to create temporary file for '%s': %s\n", |
| 444 | entry_name, strerror(errno)); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 445 | return -1; |
| 446 | } |
| 447 | |
Yusuke Sato | 0744754 | 2015-06-25 14:39:19 -0700 | [diff] [blame] | 448 | ZipString zip_entry_name(entry_name); |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 449 | ZipEntry zip_entry; |
| 450 | if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) { |
| 451 | fprintf(stderr, "archive does not contain '%s'\n", entry_name); |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 452 | return -1; |
| 453 | } |
| 454 | |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 455 | int fd = fileno(fp); |
| 456 | int error = ExtractEntryToFile(zip, &zip_entry, fd); |
| 457 | if (error != 0) { |
| 458 | fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error)); |
| 459 | return -1; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 462 | lseek(fd, 0, SEEK_SET); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 463 | return fd; |
| 464 | } |
| 465 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 466 | static char *strip(char *s) |
| 467 | { |
| 468 | int n; |
| 469 | while(*s && isspace(*s)) s++; |
| 470 | n = strlen(s); |
| 471 | while(n-- > 0) { |
| 472 | if(!isspace(s[n])) break; |
| 473 | s[n] = 0; |
| 474 | } |
| 475 | return s; |
| 476 | } |
| 477 | |
| 478 | #define MAX_OPTIONS 32 |
| 479 | static int setup_requirement_line(char *name) |
| 480 | { |
| 481 | char *val[MAX_OPTIONS]; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 482 | char *prod = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 483 | unsigned n, count; |
| 484 | char *x; |
| 485 | int invert = 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 486 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 487 | if (!strncmp(name, "reject ", 7)) { |
| 488 | name += 7; |
| 489 | invert = 1; |
| 490 | } else if (!strncmp(name, "require ", 8)) { |
| 491 | name += 8; |
| 492 | invert = 0; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 493 | } else if (!strncmp(name, "require-for-product:", 20)) { |
| 494 | // Get the product and point name past it |
| 495 | prod = name + 20; |
| 496 | name = strchr(name, ' '); |
| 497 | if (!name) return -1; |
| 498 | *name = 0; |
| 499 | name += 1; |
| 500 | invert = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | x = strchr(name, '='); |
| 504 | if (x == 0) return 0; |
| 505 | *x = 0; |
| 506 | val[0] = x + 1; |
| 507 | |
| 508 | for(count = 1; count < MAX_OPTIONS; count++) { |
| 509 | x = strchr(val[count - 1],'|'); |
| 510 | if (x == 0) break; |
| 511 | *x = 0; |
| 512 | val[count] = x + 1; |
| 513 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 514 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 515 | name = strip(name); |
| 516 | for(n = 0; n < count; n++) val[n] = strip(val[n]); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 517 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 518 | name = strip(name); |
| 519 | if (name == 0) return -1; |
| 520 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 521 | const char* var = name; |
| 522 | // Work around an unfortunate name mismatch. |
| 523 | if (!strcmp(name,"board")) var = "product"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 524 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 525 | const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 526 | if (out == 0) return -1; |
| 527 | |
| 528 | for(n = 0; n < count; n++) { |
| 529 | out[n] = strdup(strip(val[n])); |
Elliott Hughes | 14e28d3 | 2013-10-29 14:12:46 -0700 | [diff] [blame] | 530 | if (out[n] == 0) { |
| 531 | for(size_t i = 0; i < n; ++i) { |
| 532 | free((char*) out[i]); |
| 533 | } |
| 534 | free(out); |
| 535 | return -1; |
| 536 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 539 | fb_queue_require(prod, var, invert, n, out); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 540 | return 0; |
| 541 | } |
| 542 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 543 | static void setup_requirements(char* data, int64_t sz) { |
| 544 | char* s = data; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 545 | while (sz-- > 0) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 546 | if (*s == '\n') { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 547 | *s++ = 0; |
| 548 | if (setup_requirement_line(data)) { |
| 549 | die("out of memory"); |
| 550 | } |
| 551 | data = s; |
| 552 | } else { |
| 553 | s++; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 558 | static void queue_info_dump() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 559 | fb_queue_notice("--------------------------------------------"); |
| 560 | fb_queue_display("version-bootloader", "Bootloader Version..."); |
| 561 | fb_queue_display("version-baseband", "Baseband Version....."); |
| 562 | fb_queue_display("serialno", "Serial Number........"); |
| 563 | fb_queue_notice("--------------------------------------------"); |
| 564 | } |
| 565 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 566 | static struct sparse_file **load_sparse_files(int fd, int max_size) |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 567 | { |
Mohamad Ayyash | 80cc1f6 | 2015-03-31 12:09:29 -0700 | [diff] [blame] | 568 | struct sparse_file* s = sparse_file_import_auto(fd, false, true); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 569 | if (!s) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 570 | die("cannot sparse read file\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 573 | int files = sparse_file_resparse(s, max_size, nullptr, 0); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 574 | if (files < 0) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 575 | die("Failed to resparse\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 578 | sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1)); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 579 | if (!out_s) { |
| 580 | die("Failed to allocate sparse file array\n"); |
| 581 | } |
| 582 | |
| 583 | files = sparse_file_resparse(s, max_size, out_s, files); |
| 584 | if (files < 0) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 585 | die("Failed to resparse\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | return out_s; |
| 589 | } |
| 590 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 591 | static int64_t get_target_sparse_limit(usb_handle* usb) { |
| 592 | std::string max_download_size; |
Elliott Hughes | 2030bac | 2015-11-03 08:14:43 -0800 | [diff] [blame] | 593 | if (!fb_getvar(usb, "max-download-size", &max_download_size) || max_download_size.empty()) { |
Elliott Hughes | 3ab0586 | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 594 | fprintf(stderr, "target didn't report max-download-size\n"); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 595 | return 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Elliott Hughes | 77c0e66 | 2015-11-02 15:51:12 -0800 | [diff] [blame] | 598 | // Some bootloaders (angler, for example) send spurious whitespace too. |
| 599 | max_download_size = android::base::Trim(max_download_size); |
| 600 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 601 | uint64_t limit; |
| 602 | if (!android::base::ParseUint(max_download_size.c_str(), &limit)) { |
Elliott Hughes | 3ab0586 | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 603 | fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str()); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 604 | return 0; |
| 605 | } |
| 606 | if (limit > 0) { |
| 607 | fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit); |
| 608 | } |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 609 | return limit; |
| 610 | } |
| 611 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 612 | static int64_t get_sparse_limit(usb_handle* usb, int64_t size) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 613 | int64_t limit; |
| 614 | |
| 615 | if (sparse_limit == 0) { |
| 616 | return 0; |
| 617 | } else if (sparse_limit > 0) { |
| 618 | limit = sparse_limit; |
| 619 | } else { |
| 620 | if (target_sparse_limit == -1) { |
| 621 | target_sparse_limit = get_target_sparse_limit(usb); |
| 622 | } |
| 623 | if (target_sparse_limit > 0) { |
| 624 | limit = target_sparse_limit; |
| 625 | } else { |
Colin Cross | 0bbfb39 | 2012-07-24 18:05:21 -0700 | [diff] [blame] | 626 | return 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
| 630 | if (size > limit) { |
| 631 | return limit; |
| 632 | } |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 637 | // Until we get lazy inode table init working in make_ext4fs, we need to |
| 638 | // erase partitions of type ext4 before flashing a filesystem so no stale |
| 639 | // inodes are left lying around. Otherwise, e2fsck gets very upset. |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 640 | static bool needs_erase(usb_handle* usb, const char* partition) { |
| 641 | std::string partition_type; |
| 642 | if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) { |
| 643 | return false; |
| 644 | } |
| 645 | return partition_type == "ext4"; |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 648 | static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) { |
| 649 | int64_t sz = get_file_size(fd); |
| 650 | if (sz == -1) { |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 651 | return -1; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 652 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 653 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 654 | lseek64(fd, 0, SEEK_SET); |
| 655 | int64_t limit = get_sparse_limit(usb, sz); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 656 | if (limit) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 657 | sparse_file** s = load_sparse_files(fd, limit); |
| 658 | if (s == nullptr) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 659 | return -1; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 660 | } |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 661 | buf->type = FB_BUFFER_SPARSE; |
| 662 | buf->data = s; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 663 | } else { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 664 | void* data = load_fd(fd, &sz); |
| 665 | if (data == nullptr) return -1; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 666 | buf->type = FB_BUFFER; |
Sasha Levitskiy | 782111b | 2014-05-05 19:43:15 -0700 | [diff] [blame] | 667 | buf->data = data; |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 668 | buf->sz = sz; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 669 | } |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | static int load_buf(usb_handle *usb, const char *fname, |
| 675 | struct fastboot_buffer *buf) |
| 676 | { |
| 677 | int fd; |
| 678 | |
| 679 | fd = open(fname, O_RDONLY | O_BINARY); |
| 680 | if (fd < 0) { |
Daniel Rosenberg | 8228059 | 2014-04-29 13:45:05 -0700 | [diff] [blame] | 681 | return -1; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | return load_buf_fd(usb, fd, buf); |
| 685 | } |
| 686 | |
| 687 | static void flash_buf(const char *pname, struct fastboot_buffer *buf) |
| 688 | { |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 689 | sparse_file** s; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 690 | |
| 691 | switch (buf->type) { |
| 692 | case FB_BUFFER_SPARSE: |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 693 | s = reinterpret_cast<sparse_file**>(buf->data); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 694 | while (*s) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 695 | int64_t sz = sparse_file_len(*s, true, false); |
| 696 | fb_queue_flash_sparse(pname, *s++, sz); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 697 | } |
| 698 | break; |
| 699 | case FB_BUFFER: |
| 700 | fb_queue_flash(pname, buf->data, buf->sz); |
| 701 | break; |
| 702 | default: |
| 703 | die("unknown buffer type: %d", buf->type); |
| 704 | } |
| 705 | } |
| 706 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 707 | static std::vector<std::string> get_suffixes(usb_handle* usb) { |
| 708 | std::vector<std::string> suffixes; |
| 709 | std::string suffix_list; |
| 710 | if (!fb_getvar(usb, "slot-suffixes", &suffix_list)) { |
| 711 | die("Could not get suffixes.\n"); |
| 712 | } |
| 713 | return android::base::Split(suffix_list, ","); |
| 714 | } |
| 715 | |
| 716 | static std::string verify_slot(usb_handle* usb, const char *slot) { |
| 717 | if (strcmp(slot, "all") == 0) { |
| 718 | return "all"; |
| 719 | } |
| 720 | std::vector<std::string> suffixes = get_suffixes(usb); |
| 721 | for (const std::string &suffix : suffixes) { |
| 722 | if (suffix == slot) |
| 723 | return slot; |
| 724 | } |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 725 | fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 726 | for (const std::string &suffix : suffixes) { |
| 727 | fprintf(stderr, "%s\n", suffix.c_str()); |
| 728 | } |
| 729 | exit(1); |
| 730 | } |
| 731 | |
| 732 | static void do_for_partition(usb_handle* usb, const char *part, const char *slot, std::function<void(const std::string&)> func, bool force_slot) { |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 733 | std::string has_slot; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 734 | std::string current_slot; |
| 735 | |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 736 | if (!fb_getvar(usb, std::string("has-slot:")+part, &has_slot)) { |
| 737 | /* If has-slot is not supported, the answer is no. */ |
| 738 | has_slot = "no"; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 739 | } |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 740 | if (has_slot == "yes") { |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 741 | if (!slot || slot[0] == 0) { |
| 742 | if (!fb_getvar(usb, "current-slot", ¤t_slot)) { |
| 743 | die("Failed to identify current slot.\n"); |
| 744 | } |
| 745 | func(std::string(part) + '-' + current_slot); |
| 746 | } else { |
| 747 | func(std::string(part) + '-' + slot); |
| 748 | } |
| 749 | } else { |
| 750 | if (force_slot && slot && slot[0]) { |
| 751 | fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n", part, slot); |
| 752 | } |
| 753 | func(part); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | /* This function will find the real partition name given a base name, and a slot. If slot is NULL or empty, |
| 758 | * it will use the current slot. If slot is "all", it will return a list of all possible partition names. |
| 759 | * If force_slot is true, it will fail if a slot is specified, and the given partition does not support slots. |
| 760 | */ |
| 761 | static void do_for_partitions(usb_handle* usb, const char *part, const char *slot, std::function<void(const std::string&)> func, bool force_slot) { |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 762 | std::string has_slot; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 763 | |
| 764 | if (slot && strcmp(slot, "all") == 0) { |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 765 | if (!fb_getvar(usb, std::string("has-slot:") + part, &has_slot)) { |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 766 | die("Could not check if partition %s has slot.", part); |
| 767 | } |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 768 | if (has_slot == "yes") { |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 769 | std::vector<std::string> suffixes = get_suffixes(usb); |
| 770 | for (std::string &suffix : suffixes) { |
| 771 | do_for_partition(usb, part, suffix.c_str(), func, force_slot); |
| 772 | } |
| 773 | } else { |
| 774 | do_for_partition(usb, part, "", func, force_slot); |
| 775 | } |
| 776 | } else { |
| 777 | do_for_partition(usb, part, slot, func, force_slot); |
| 778 | } |
| 779 | } |
| 780 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 781 | static void do_flash(usb_handle* usb, const char* pname, const char* fname) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 782 | struct fastboot_buffer buf; |
| 783 | |
| 784 | if (load_buf(usb, fname, &buf)) { |
| 785 | die("cannot load '%s'", fname); |
| 786 | } |
| 787 | flash_buf(pname, &buf); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 790 | static void do_update_signature(ZipArchiveHandle zip, char* fn) { |
| 791 | int64_t sz; |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 792 | void* data = unzip_file(zip, fn, &sz); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 793 | if (data == nullptr) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 794 | fb_queue_download("signature", data, sz); |
| 795 | fb_queue_command("signature", "installing signature"); |
| 796 | } |
| 797 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 798 | static void do_update(usb_handle* usb, const char* filename, const char* slot_override, bool erase_first) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 799 | queue_info_dump(); |
| 800 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 801 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
| 802 | |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 803 | ZipArchiveHandle zip; |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 804 | int error = OpenArchive(filename, &zip); |
| 805 | if (error != 0) { |
Narayan Kamath | f6e9ffb | 2015-05-11 16:59:46 +0100 | [diff] [blame] | 806 | CloseArchive(zip); |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 807 | die("failed to open zip file '%s': %s", filename, ErrorCodeString(error)); |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 808 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 809 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 810 | int64_t sz; |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 811 | void* data = unzip_file(zip, "android-info.txt", &sz); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 812 | if (data == nullptr) { |
Narayan Kamath | f6e9ffb | 2015-05-11 16:59:46 +0100 | [diff] [blame] | 813 | CloseArchive(zip); |
Elliott Hughes | 7c6d884 | 2015-03-19 10:30:53 -0700 | [diff] [blame] | 814 | die("update package '%s' has no android-info.txt", filename); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 815 | } |
| 816 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 817 | setup_requirements(reinterpret_cast<char*>(data), sz); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 818 | |
Elliott Hughes | acdbe92 | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 819 | for (size_t i = 0; i < ARRAY_SIZE(images); ++i) { |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 820 | int fd = unzip_to_file(zip, images[i].img_name); |
Elliott Hughes | acdbe92 | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 821 | if (fd == -1) { |
| 822 | if (images[i].is_optional) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 823 | continue; |
Elliott Hughes | acdbe92 | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 824 | } |
Narayan Kamath | f6e9ffb | 2015-05-11 16:59:46 +0100 | [diff] [blame] | 825 | CloseArchive(zip); |
Elliott Hughes | acdbe92 | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 826 | exit(1); // unzip_to_file already explained why. |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 827 | } |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 828 | fastboot_buffer buf; |
| 829 | int rc = load_buf_fd(usb, fd, &buf); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 830 | if (rc) die("cannot load %s from flash", images[i].img_name); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 831 | |
| 832 | auto update = [&](const std::string &partition) { |
| 833 | do_update_signature(zip, images[i].sig_name); |
| 834 | if (erase_first && needs_erase(usb, partition.c_str())) { |
| 835 | fb_queue_erase(partition.c_str()); |
| 836 | } |
| 837 | flash_buf(partition.c_str(), &buf); |
| 838 | /* not closing the fd here since the sparse code keeps the fd around |
| 839 | * but hasn't mmaped data yet. The tmpfile will get cleaned up when the |
| 840 | * program exits. |
| 841 | */ |
| 842 | }; |
| 843 | do_for_partitions(usb, images[i].part_name, slot_override, update, false); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 844 | } |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 845 | |
| 846 | CloseArchive(zip); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 847 | } |
| 848 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 849 | static void do_send_signature(char* fn) { |
| 850 | char* xtn = strrchr(fn, '.'); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 851 | if (!xtn) return; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 852 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 853 | if (strcmp(xtn, ".img")) return; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 854 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 855 | strcpy(xtn, ".sig"); |
| 856 | |
| 857 | int64_t sz; |
| 858 | void* data = load_file(fn, &sz); |
| 859 | strcpy(xtn, ".img"); |
| 860 | if (data == nullptr) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 861 | fb_queue_download("signature", data, sz); |
| 862 | fb_queue_command("signature", "installing signature"); |
| 863 | } |
| 864 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 865 | static void do_flashall(usb_handle* usb, const char *slot_override, int erase_first) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 866 | queue_info_dump(); |
| 867 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 868 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
| 869 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 870 | char* fname = find_item("info", product); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 871 | if (fname == nullptr) die("cannot find android-info.txt"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 872 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 873 | int64_t sz; |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 874 | void* data = load_file(fname, &sz); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 875 | if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno)); |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 876 | |
| 877 | setup_requirements(reinterpret_cast<char*>(data), sz); |
| 878 | |
| 879 | for (size_t i = 0; i < ARRAY_SIZE(images); i++) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 880 | fname = find_item(images[i].part_name, product); |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 881 | fastboot_buffer buf; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 882 | if (load_buf(usb, fname, &buf)) { |
| 883 | if (images[i].is_optional) |
| 884 | continue; |
| 885 | die("could not load %s\n", images[i].img_name); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 886 | } |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 887 | |
| 888 | auto flashall = [&](const std::string &partition) { |
| 889 | do_send_signature(fname); |
| 890 | if (erase_first && needs_erase(usb, partition.c_str())) { |
| 891 | fb_queue_erase(partition.c_str()); |
| 892 | } |
| 893 | flash_buf(partition.c_str(), &buf); |
| 894 | }; |
| 895 | do_for_partitions(usb, images[i].part_name, slot_override, flashall, false); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 896 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | #define skip(n) do { argc -= (n); argv += (n); } while (0) |
JP Abgrall | 2d13d14 | 2011-03-01 23:35:07 -0800 | [diff] [blame] | 900 | #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] | 901 | |
Elliott Hughes | 45be42e | 2015-09-02 20:15:48 -0700 | [diff] [blame] | 902 | static int do_bypass_unlock_command(int argc, char **argv) |
Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 903 | { |
Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 904 | if (argc <= 2) return 0; |
| 905 | skip(2); |
| 906 | |
| 907 | /* |
| 908 | * Process unlock_bootloader, we have to load the message file |
| 909 | * and send that to the remote device. |
| 910 | */ |
| 911 | require(1); |
Elliott Hughes | 259ad4a | 2015-09-02 20:33:44 -0700 | [diff] [blame] | 912 | |
| 913 | int64_t sz; |
| 914 | void* data = load_file(*argv, &sz); |
| 915 | if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno)); |
Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 916 | fb_queue_download("unlock_message", data, sz); |
| 917 | fb_queue_command("flashing unlock_bootloader", "unlocking bootloader"); |
| 918 | skip(1); |
| 919 | return 0; |
| 920 | } |
| 921 | |
Elliott Hughes | 45be42e | 2015-09-02 20:15:48 -0700 | [diff] [blame] | 922 | static int do_oem_command(int argc, char **argv) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 923 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 924 | char command[256]; |
| 925 | if (argc <= 1) return 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 926 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 927 | command[0] = 0; |
| 928 | while(1) { |
| 929 | strcat(command,*argv); |
| 930 | skip(1); |
| 931 | if(argc == 0) break; |
| 932 | strcat(command," "); |
| 933 | } |
| 934 | |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 935 | fb_queue_command(command,""); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 936 | return 0; |
| 937 | } |
| 938 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 939 | static int64_t parse_num(const char *arg) |
| 940 | { |
| 941 | char *endptr; |
| 942 | unsigned long long num; |
| 943 | |
| 944 | num = strtoull(arg, &endptr, 0); |
| 945 | if (endptr == arg) { |
| 946 | return -1; |
| 947 | } |
| 948 | |
| 949 | if (*endptr == 'k' || *endptr == 'K') { |
| 950 | if (num >= (-1ULL) / 1024) { |
| 951 | return -1; |
| 952 | } |
| 953 | num *= 1024LL; |
| 954 | endptr++; |
| 955 | } else if (*endptr == 'm' || *endptr == 'M') { |
| 956 | if (num >= (-1ULL) / (1024 * 1024)) { |
| 957 | return -1; |
| 958 | } |
| 959 | num *= 1024LL * 1024LL; |
| 960 | endptr++; |
| 961 | } else if (*endptr == 'g' || *endptr == 'G') { |
| 962 | if (num >= (-1ULL) / (1024 * 1024 * 1024)) { |
| 963 | return -1; |
| 964 | } |
| 965 | num *= 1024LL * 1024LL * 1024LL; |
| 966 | endptr++; |
| 967 | } |
| 968 | |
| 969 | if (*endptr != '\0') { |
| 970 | return -1; |
| 971 | } |
| 972 | |
| 973 | if (num > INT64_MAX) { |
| 974 | return -1; |
| 975 | } |
| 976 | |
| 977 | return num; |
| 978 | } |
| 979 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 980 | static void fb_perform_format(usb_handle* usb, |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 981 | const char* partition, int skip_if_not_supported, |
| 982 | const char* type_override, const char* size_override) { |
| 983 | std::string partition_type, partition_size; |
| 984 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 985 | struct fastboot_buffer buf; |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 986 | const char* errMsg = nullptr; |
| 987 | const struct fs_generator* gen = nullptr; |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 988 | int fd; |
| 989 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 990 | unsigned int limit = INT_MAX; |
| 991 | if (target_sparse_limit > 0 && target_sparse_limit < limit) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 992 | limit = target_sparse_limit; |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 993 | } |
| 994 | if (sparse_limit > 0 && sparse_limit < limit) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 995 | limit = sparse_limit; |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 996 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 997 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 998 | if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 999 | errMsg = "Can't determine partition type.\n"; |
| 1000 | goto failed; |
| 1001 | } |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1002 | if (type_override) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1003 | if (partition_type != type_override) { |
| 1004 | fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n", |
| 1005 | partition, partition_type.c_str(), type_override); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1006 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1007 | partition_type = type_override; |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1008 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1009 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1010 | if (!fb_getvar(usb, std::string("partition-size:") + partition, &partition_size)) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1011 | errMsg = "Unable to get partition size\n"; |
| 1012 | goto failed; |
| 1013 | } |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1014 | if (size_override) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1015 | if (partition_size != size_override) { |
| 1016 | fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n", |
| 1017 | partition, partition_size.c_str(), size_override); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1018 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1019 | partition_size = size_override; |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1020 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1021 | |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 1022 | gen = fs_get_generator(partition_type); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1023 | if (!gen) { |
| 1024 | if (skip_if_not_supported) { |
| 1025 | fprintf(stderr, "Erase successful, but not automatically formatting.\n"); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1026 | fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str()); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1027 | return; |
| 1028 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1029 | fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n", |
| 1030 | partition_type.c_str()); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1031 | return; |
| 1032 | } |
| 1033 | |
Elliott Hughes | 2030bac | 2015-11-03 08:14:43 -0800 | [diff] [blame] | 1034 | // Some bootloaders (hammerhead, for example) use implicit hex. |
| 1035 | // This code used to use strtol with base 16. |
| 1036 | if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size; |
| 1037 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1038 | int64_t size; |
| 1039 | if (!android::base::ParseInt(partition_size.c_str(), &size)) { |
| 1040 | fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str()); |
| 1041 | return; |
| 1042 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1043 | |
| 1044 | fd = fileno(tmpfile()); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1045 | if (fs_generator_generate(gen, fd, size)) { |
| 1046 | fprintf(stderr, "Cannot generate image: %s\n", strerror(errno)); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1047 | close(fd); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1048 | return; |
| 1049 | } |
| 1050 | |
| 1051 | if (load_buf_fd(usb, fd, &buf)) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1052 | fprintf(stderr, "Cannot read image: %s\n", strerror(errno)); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1053 | close(fd); |
| 1054 | return; |
| 1055 | } |
| 1056 | flash_buf(partition, &buf); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1057 | return; |
| 1058 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1059 | failed: |
| 1060 | if (skip_if_not_supported) { |
| 1061 | fprintf(stderr, "Erase successful, but not automatically formatting.\n"); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1062 | if (errMsg) fprintf(stderr, "%s", errMsg); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1063 | } |
| 1064 | fprintf(stderr,"FAILED (%s)\n", fb_get_error()); |
| 1065 | } |
| 1066 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1067 | int main(int argc, char **argv) |
| 1068 | { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1069 | bool wants_wipe = false; |
| 1070 | bool wants_reboot = false; |
| 1071 | bool wants_reboot_bootloader = false; |
| 1072 | bool wants_set_active = false; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1073 | bool erase_first = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1074 | void *data; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1075 | int64_t sz; |
Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1076 | int longindex; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1077 | std::string slot_override; |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1078 | std::string next_active; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1079 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1080 | const struct option longopts[] = { |
| 1081 | {"base", required_argument, 0, 'b'}, |
| 1082 | {"kernel_offset", required_argument, 0, 'k'}, |
| 1083 | {"page_size", required_argument, 0, 'n'}, |
| 1084 | {"ramdisk_offset", required_argument, 0, 'r'}, |
Mohamad Ayyash | 29fd7b1 | 2014-01-14 18:27:25 -0800 | [diff] [blame] | 1085 | {"tags_offset", required_argument, 0, 't'}, |
Elliott Hughes | 379646b | 2015-06-02 13:50:00 -0700 | [diff] [blame] | 1086 | {"help", no_argument, 0, 'h'}, |
| 1087 | {"unbuffered", no_argument, 0, 0}, |
| 1088 | {"version", no_argument, 0, 0}, |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1089 | {"slot", required_argument, 0, 0}, |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1090 | {"set_active", optional_argument, 0, 'a'}, |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1091 | {0, 0, 0, 0} |
| 1092 | }; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1093 | |
| 1094 | serial = getenv("ANDROID_SERIAL"); |
| 1095 | |
| 1096 | while (1) { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1097 | int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:ha::", longopts, &longindex); |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1098 | if (c < 0) { |
| 1099 | break; |
| 1100 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1101 | /* Alphabetical cases */ |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1102 | switch (c) { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1103 | case 'a': |
| 1104 | wants_set_active = true; |
| 1105 | if (optarg) |
| 1106 | next_active = optarg; |
| 1107 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1108 | case 'b': |
| 1109 | base_addr = strtoul(optarg, 0, 16); |
| 1110 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1111 | case 'c': |
| 1112 | cmdline = optarg; |
| 1113 | break; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1114 | case 'h': |
| 1115 | usage(); |
| 1116 | return 1; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1117 | case 'i': { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1118 | char *endptr = nullptr; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1119 | unsigned long val; |
| 1120 | |
| 1121 | val = strtoul(optarg, &endptr, 0); |
| 1122 | if (!endptr || *endptr != '\0' || (val & ~0xffff)) |
| 1123 | die("invalid vendor id '%s'", optarg); |
| 1124 | vendor_id = (unsigned short)val; |
| 1125 | break; |
| 1126 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1127 | case 'k': |
| 1128 | kernel_offset = strtoul(optarg, 0, 16); |
| 1129 | break; |
| 1130 | case 'l': |
| 1131 | long_listing = 1; |
| 1132 | break; |
| 1133 | case 'n': |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1134 | page_size = (unsigned)strtoul(optarg, nullptr, 0); |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1135 | if (!page_size) die("invalid page size"); |
| 1136 | break; |
| 1137 | case 'p': |
| 1138 | product = optarg; |
| 1139 | break; |
| 1140 | case 'r': |
| 1141 | ramdisk_offset = strtoul(optarg, 0, 16); |
| 1142 | break; |
Mohamad Ayyash | 29fd7b1 | 2014-01-14 18:27:25 -0800 | [diff] [blame] | 1143 | case 't': |
| 1144 | tags_offset = strtoul(optarg, 0, 16); |
| 1145 | break; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1146 | case 's': |
| 1147 | serial = optarg; |
| 1148 | break; |
| 1149 | case 'S': |
| 1150 | sparse_limit = parse_num(optarg); |
| 1151 | if (sparse_limit < 0) { |
| 1152 | die("invalid sparse limit"); |
| 1153 | } |
| 1154 | break; |
| 1155 | case 'u': |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1156 | erase_first = false; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1157 | break; |
| 1158 | case 'w': |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1159 | wants_wipe = true; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1160 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1161 | case '?': |
| 1162 | return 1; |
Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1163 | case 0: |
| 1164 | if (strcmp("unbuffered", longopts[longindex].name) == 0) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1165 | setvbuf(stdout, nullptr, _IONBF, 0); |
| 1166 | setvbuf(stderr, nullptr, _IONBF, 0); |
Elliott Hughes | 379646b | 2015-06-02 13:50:00 -0700 | [diff] [blame] | 1167 | } else if (strcmp("version", longopts[longindex].name) == 0) { |
| 1168 | fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION); |
| 1169 | return 0; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1170 | } else if (strcmp("slot", longopts[longindex].name) == 0) { |
| 1171 | slot_override = std::string(optarg); |
Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1172 | } |
| 1173 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1174 | default: |
| 1175 | abort(); |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | argc -= optind; |
| 1180 | argv += optind; |
| 1181 | |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1182 | if (argc == 0 && !wants_wipe && !wants_set_active) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1183 | usage(); |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 1184 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1185 | } |
| 1186 | |
Colin Cross | 8fb6e06 | 2012-07-24 16:36:41 -0700 | [diff] [blame] | 1187 | if (argc > 0 && !strcmp(*argv, "devices")) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1188 | skip(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1189 | list_devices(); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1190 | return 0; |
| 1191 | } |
| 1192 | |
Colin Cross | c7b75dc | 2012-08-29 18:17:06 -0700 | [diff] [blame] | 1193 | if (argc > 0 && !strcmp(*argv, "help")) { |
| 1194 | usage(); |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
Elliott Hughes | c0ce65f | 2015-06-02 13:34:07 -0700 | [diff] [blame] | 1198 | usb_handle* usb = open_device(); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1199 | if (slot_override != "") |
| 1200 | slot_override = verify_slot(usb, slot_override.c_str()); |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1201 | if (next_active != "") |
| 1202 | next_active = verify_slot(usb, next_active.c_str()); |
| 1203 | |
| 1204 | if (wants_set_active) { |
| 1205 | if (next_active == "") { |
| 1206 | if (slot_override == "") { |
| 1207 | wants_set_active = false; |
| 1208 | } else { |
| 1209 | next_active = slot_override; |
| 1210 | } |
| 1211 | } |
| 1212 | } |
Elliott Hughes | 31dbed7 | 2009-10-07 15:38:53 -0700 | [diff] [blame] | 1213 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1214 | while (argc > 0) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1215 | if (!strcmp(*argv, "getvar")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1216 | require(2); |
| 1217 | fb_queue_display(argv[1], argv[1]); |
| 1218 | skip(2); |
| 1219 | } else if(!strcmp(*argv, "erase")) { |
| 1220 | require(2); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1221 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1222 | auto erase = [&](const std::string &partition) { |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 1223 | std::string partition_type; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1224 | if (fb_getvar(usb, std::string("partition-type:") + argv[1], &partition_type) && |
| 1225 | fs_get_generator(partition_type) != nullptr) { |
| 1226 | fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n", |
| 1227 | partition_type.c_str()); |
| 1228 | } |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1229 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1230 | fb_queue_erase(partition.c_str()); |
| 1231 | }; |
| 1232 | do_for_partitions(usb, argv[1], slot_override.c_str(), erase, true); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1233 | skip(2); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1234 | } else if(!strncmp(*argv, "format", strlen("format"))) { |
| 1235 | char *overrides; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1236 | char *type_override = nullptr; |
| 1237 | char *size_override = nullptr; |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 1238 | require(2); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1239 | /* |
| 1240 | * Parsing for: "format[:[type][:[size]]]" |
| 1241 | * Some valid things: |
| 1242 | * - select ontly the size, and leave default fs type: |
| 1243 | * format::0x4000000 userdata |
| 1244 | * - default fs type and size: |
| 1245 | * format userdata |
| 1246 | * format:: userdata |
| 1247 | */ |
| 1248 | overrides = strchr(*argv, ':'); |
| 1249 | if (overrides) { |
| 1250 | overrides++; |
| 1251 | size_override = strchr(overrides, ':'); |
| 1252 | if (size_override) { |
| 1253 | size_override[0] = '\0'; |
| 1254 | size_override++; |
| 1255 | } |
| 1256 | type_override = overrides; |
| 1257 | } |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1258 | if (type_override && !type_override[0]) type_override = nullptr; |
| 1259 | if (size_override && !size_override[0]) size_override = nullptr; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1260 | |
| 1261 | auto format = [&](const std::string &partition) { |
| 1262 | if (erase_first && needs_erase(usb, partition.c_str())) { |
| 1263 | fb_queue_erase(partition.c_str()); |
| 1264 | } |
| 1265 | fb_perform_format(usb, partition.c_str(), 0, type_override, size_override); |
| 1266 | }; |
| 1267 | do_for_partitions(usb, argv[1], slot_override.c_str(), format, true); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 1268 | skip(2); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1269 | } else if(!strcmp(*argv, "signature")) { |
| 1270 | require(2); |
| 1271 | data = load_file(argv[1], &sz); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1272 | if (data == nullptr) 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] | 1273 | if (sz != 256) die("signature must be 256 bytes"); |
| 1274 | fb_queue_download("signature", data, sz); |
| 1275 | fb_queue_command("signature", "installing signature"); |
| 1276 | skip(2); |
| 1277 | } else if(!strcmp(*argv, "reboot")) { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1278 | wants_reboot = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1279 | skip(1); |
Elliott Hughes | ca85df0 | 2015-02-25 10:02:00 -0800 | [diff] [blame] | 1280 | if (argc > 0) { |
| 1281 | if (!strcmp(*argv, "bootloader")) { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1282 | wants_reboot = false; |
| 1283 | wants_reboot_bootloader = true; |
Elliott Hughes | ca85df0 | 2015-02-25 10:02:00 -0800 | [diff] [blame] | 1284 | skip(1); |
| 1285 | } |
| 1286 | } |
| 1287 | require(0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1288 | } else if(!strcmp(*argv, "reboot-bootloader")) { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1289 | wants_reboot_bootloader = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1290 | skip(1); |
| 1291 | } else if (!strcmp(*argv, "continue")) { |
| 1292 | fb_queue_command("continue", "resuming boot"); |
| 1293 | skip(1); |
| 1294 | } else if(!strcmp(*argv, "boot")) { |
| 1295 | char *kname = 0; |
| 1296 | char *rname = 0; |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1297 | char *sname = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1298 | skip(1); |
| 1299 | if (argc > 0) { |
| 1300 | kname = argv[0]; |
| 1301 | skip(1); |
| 1302 | } |
| 1303 | if (argc > 0) { |
| 1304 | rname = argv[0]; |
| 1305 | skip(1); |
| 1306 | } |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1307 | if (argc > 0) { |
| 1308 | sname = argv[0]; |
| 1309 | skip(1); |
| 1310 | } |
| 1311 | data = load_bootable_image(kname, rname, sname, &sz, cmdline); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1312 | if (data == 0) return 1; |
| 1313 | fb_queue_download("boot.img", data, sz); |
| 1314 | fb_queue_command("boot", "booting"); |
| 1315 | } else if(!strcmp(*argv, "flash")) { |
| 1316 | char *pname = argv[1]; |
| 1317 | char *fname = 0; |
| 1318 | require(2); |
| 1319 | if (argc > 2) { |
| 1320 | fname = argv[2]; |
| 1321 | skip(3); |
| 1322 | } else { |
| 1323 | fname = find_item(pname, product); |
| 1324 | skip(2); |
| 1325 | } |
| 1326 | if (fname == 0) die("cannot determine image filename for '%s'", pname); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1327 | |
| 1328 | auto flash = [&](const std::string &partition) { |
| 1329 | if (erase_first && needs_erase(usb, partition.c_str())) { |
| 1330 | fb_queue_erase(partition.c_str()); |
| 1331 | } |
| 1332 | do_flash(usb, partition.c_str(), fname); |
| 1333 | }; |
| 1334 | do_for_partitions(usb, pname, slot_override.c_str(), flash, true); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1335 | } else if(!strcmp(*argv, "flash:raw")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1336 | char *kname = argv[2]; |
| 1337 | char *rname = 0; |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1338 | char *sname = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1339 | require(3); |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1340 | skip(3); |
| 1341 | if (argc > 0) { |
| 1342 | rname = argv[0]; |
| 1343 | skip(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1344 | } |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1345 | if (argc > 0) { |
| 1346 | sname = argv[0]; |
| 1347 | skip(1); |
| 1348 | } |
| 1349 | data = load_bootable_image(kname, rname, sname, &sz, cmdline); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1350 | if (data == 0) die("cannot load bootable image"); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1351 | auto flashraw = [&](const std::string &partition) { |
| 1352 | fb_queue_flash(partition.c_str(), data, sz); |
| 1353 | }; |
| 1354 | do_for_partitions(usb, argv[1], slot_override.c_str(), flashraw, true); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1355 | } else if(!strcmp(*argv, "flashall")) { |
| 1356 | skip(1); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1357 | do_flashall(usb, slot_override.c_str(), erase_first); |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1358 | wants_reboot = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1359 | } else if(!strcmp(*argv, "update")) { |
| 1360 | if (argc > 1) { |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1361 | do_update(usb, argv[1], slot_override.c_str(), erase_first); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1362 | skip(2); |
| 1363 | } else { |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1364 | do_update(usb, "update.zip", slot_override.c_str(), erase_first); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1365 | skip(1); |
| 1366 | } |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1367 | wants_reboot = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1368 | } else if(!strcmp(*argv, "oem")) { |
| 1369 | argc = do_oem_command(argc, argv); |
Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 1370 | } else if(!strcmp(*argv, "flashing")) { |
| 1371 | if (argc == 2 && (!strcmp(*(argv+1), "unlock") || |
| 1372 | !strcmp(*(argv+1), "lock") || |
| 1373 | !strcmp(*(argv+1), "unlock_critical") || |
| 1374 | !strcmp(*(argv+1), "lock_critical") || |
| 1375 | !strcmp(*(argv+1), "get_unlock_ability") || |
| 1376 | !strcmp(*(argv+1), "get_unlock_bootloader_nonce") || |
| 1377 | !strcmp(*(argv+1), "lock_bootloader"))) { |
| 1378 | argc = do_oem_command(argc, argv); |
| 1379 | } else |
| 1380 | if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) { |
| 1381 | argc = do_bypass_unlock_command(argc, argv); |
Badhri Jagan Sridharan | bf11095 | 2015-05-15 16:43:47 -0700 | [diff] [blame] | 1382 | } else { |
| 1383 | usage(); |
| 1384 | return 1; |
| 1385 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1386 | } else { |
| 1387 | usage(); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1388 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | if (wants_wipe) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1393 | fprintf(stderr, "wiping userdata...\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1394 | fb_queue_erase("userdata"); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1395 | fb_perform_format(usb, "userdata", 1, nullptr, nullptr); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1396 | |
| 1397 | std::string cache_type; |
| 1398 | if (fb_getvar(usb, "partition-type:cache", &cache_type) && !cache_type.empty()) { |
| 1399 | fprintf(stderr, "wiping cache...\n"); |
| 1400 | fb_queue_erase("cache"); |
| 1401 | fb_perform_format(usb, "cache", 1, nullptr, nullptr); |
| 1402 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1403 | } |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame^] | 1404 | if (wants_set_active) { |
| 1405 | fb_set_active(next_active.c_str()); |
| 1406 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1407 | if (wants_reboot) { |
| 1408 | fb_queue_reboot(); |
Mark Wachsler | ec25e7b | 2014-06-24 11:04:54 -0400 | [diff] [blame] | 1409 | fb_queue_wait_for_disconnect(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1410 | } else if (wants_reboot_bootloader) { |
| 1411 | fb_queue_command("reboot-bootloader", "rebooting into bootloader"); |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 1412 | fb_queue_wait_for_disconnect(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1413 | } |
| 1414 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1415 | return fb_execute_queue(usb) ? EXIT_FAILURE : EXIT_SUCCESS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1416 | } |