| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (c) 2009-2013, Google Inc. | 
|  | 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 | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | *  * Neither the name of Google, Inc. nor the names of its contributors | 
|  | 15 | *    may be used to endorse or promote products derived from this | 
|  | 16 | *    software without specific prior written permission. | 
|  | 17 | * | 
|  | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 22 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 25 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 26 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 28 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 29 | * SUCH DAMAGE. | 
|  | 30 | */ | 
|  | 31 |  | 
|  | 32 | #include <stdlib.h> | 
|  | 33 | #include <unistd.h> | 
|  | 34 | #include <sys/types.h> | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 35 | #include <sys/mman.h> | 
|  | 36 | #include <sys/stat.h> | 
|  | 37 | #include <unistd.h> | 
|  | 38 | #include <sys/reboot.h> | 
|  | 39 | #include <fcntl.h> | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 40 |  | 
|  | 41 | #include "bootimg.h" | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 42 | #include "commands/boot.h" | 
|  | 43 | #include "commands/flash.h" | 
|  | 44 | #include "commands/partitions.h" | 
|  | 45 | #include "commands/virtual_partitions.h" | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 46 | #include "debug.h" | 
|  | 47 | #include "protocol.h" | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 48 | #include "trigger.h" | 
|  | 49 | #include "utils.h" | 
|  | 50 |  | 
|  | 51 | #define ATAGS_LOCATION "/proc/atags" | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 52 |  | 
|  | 53 | static void cmd_boot(struct protocol_handle *phandle, const char *arg) | 
|  | 54 | { | 
| Szymon Starzycki | 4662a11 | 2013-10-02 17:21:41 -0700 | [diff] [blame] | 55 | int sz, atags_sz, new_atags_sz; | 
|  | 56 | int rv; | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 57 | unsigned kernel_actual; | 
|  | 58 | unsigned ramdisk_actual; | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 59 | unsigned second_actual; | 
|  | 60 | void *kernel_ptr; | 
|  | 61 | void *ramdisk_ptr; | 
|  | 62 | void *second_ptr; | 
|  | 63 | struct boot_img_hdr *hdr; | 
| Szymon Starzycki | 4662a11 | 2013-10-02 17:21:41 -0700 | [diff] [blame] | 64 | char *ptr = NULL; | 
|  | 65 | char *atags_ptr = NULL; | 
|  | 66 | char *new_atags = NULL; | 
|  | 67 | int data_fd = 0; | 
|  | 68 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 69 | D(DEBUG, "cmd_boot %s\n", arg); | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 70 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 71 | if (phandle->download_fd < 0) { | 
|  | 72 | fastboot_fail(phandle, "no kernel file"); | 
|  | 73 | return; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | atags_ptr = read_atags(ATAGS_LOCATION, &atags_sz); | 
|  | 77 | if (atags_ptr == NULL) { | 
|  | 78 | fastboot_fail(phandle, "atags read error"); | 
|  | 79 | goto error; | 
|  | 80 | } | 
|  | 81 |  | 
| Szymon Starzycki | 4662a11 | 2013-10-02 17:21:41 -0700 | [diff] [blame] | 82 | // TODO: With cms we can also verify partition name included as | 
|  | 83 | // cms signed attribute | 
|  | 84 | if (flash_validate_certificate(phandle->download_fd, &data_fd) != 1) { | 
|  | 85 | fastboot_fail(phandle, "Access forbiden you need the certificate"); | 
|  | 86 | return; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | sz = get_file_size(data_fd); | 
|  | 90 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 91 | ptr = (char *) mmap(NULL, sz, PROT_READ, | 
| Szymon Starzycki | 4662a11 | 2013-10-02 17:21:41 -0700 | [diff] [blame] | 92 | MAP_POPULATE | MAP_PRIVATE, data_fd, 0); | 
|  | 93 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 94 | hdr = (struct boot_img_hdr *) ptr; | 
|  | 95 |  | 
|  | 96 | if (ptr == MAP_FAILED) { | 
|  | 97 | fastboot_fail(phandle, "internal fastbootd error"); | 
|  | 98 | goto error; | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | if ((size_t) sz < sizeof(*hdr)) { | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 102 | fastboot_fail(phandle, "invalid bootimage header"); | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 103 | goto error; | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 106 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, hdr->page_size); | 
|  | 107 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, hdr->page_size); | 
|  | 108 | second_actual = ROUND_TO_PAGE(hdr->second_size, hdr->page_size); | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 109 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 110 | new_atags = (char *) create_atags((unsigned *) atags_ptr, atags_sz, hdr, &new_atags_sz); | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 111 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 112 | if (new_atags == NULL) { | 
|  | 113 | fastboot_fail(phandle, "atags generate error"); | 
|  | 114 | goto error; | 
|  | 115 | } | 
|  | 116 | if (new_atags_sz > 0x4000) { | 
|  | 117 | fastboot_fail(phandle, "atags file to large"); | 
|  | 118 | goto error; | 
|  | 119 | } | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 120 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 121 | if ((int) (hdr->page_size + kernel_actual + ramdisk_actual) < sz) { | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 122 | fastboot_fail(phandle, "incomplete bootimage"); | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 123 | goto error; | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 124 | } | 
|  | 125 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 126 | kernel_ptr = (void *)((unsigned) ptr + hdr->page_size); | 
|  | 127 | ramdisk_ptr = (void *)((unsigned) kernel_ptr + kernel_actual); | 
|  | 128 | second_ptr = (void *)((unsigned) ramdisk_ptr + ramdisk_actual); | 
|  | 129 |  | 
|  | 130 | D(INFO, "preparing to boot"); | 
| Szymon Starzycki | baf4c4b | 2013-10-04 10:28:03 -0700 | [diff] [blame] | 131 | // Prepares boot physical address. Addresses from header are ignored | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 132 | rv = prepare_boot_linux(hdr->kernel_addr, kernel_ptr, kernel_actual, | 
|  | 133 | hdr->ramdisk_addr, ramdisk_ptr, ramdisk_actual, | 
|  | 134 | hdr->second_addr, second_ptr, second_actual, | 
|  | 135 | hdr->tags_addr, new_atags, ROUND_TO_PAGE(new_atags_sz, hdr->page_size)); | 
|  | 136 | if (rv < 0) { | 
|  | 137 | fastboot_fail(phandle, "kexec prepare failed"); | 
|  | 138 | goto error; | 
|  | 139 | } | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 140 |  | 
|  | 141 | fastboot_okay(phandle, ""); | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 142 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 143 | free(atags_ptr); | 
|  | 144 | munmap(ptr, sz); | 
|  | 145 | free(new_atags); | 
| Szymon Starzycki | 4662a11 | 2013-10-02 17:21:41 -0700 | [diff] [blame] | 146 | close(data_fd); | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 147 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 148 | D(INFO, "Kexec going to reboot"); | 
|  | 149 | reboot(LINUX_REBOOT_CMD_KEXEC); | 
|  | 150 |  | 
|  | 151 | fastboot_fail(phandle, "reboot error"); | 
|  | 152 |  | 
|  | 153 | return; | 
|  | 154 |  | 
|  | 155 | error: | 
|  | 156 |  | 
|  | 157 | if (atags_ptr != NULL) | 
|  | 158 | free(atags_ptr); | 
|  | 159 | if (ptr != NULL) | 
|  | 160 | munmap(ptr, sz); | 
|  | 161 |  | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 162 | } | 
|  | 163 |  | 
|  | 164 | static void cmd_erase(struct protocol_handle *phandle, const char *arg) | 
|  | 165 | { | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 166 | int partition_fd; | 
|  | 167 | char path[PATH_MAX]; | 
|  | 168 | D(DEBUG, "cmd_erase %s\n", arg); | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 169 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 170 | if (flash_find_entry(arg, path, PATH_MAX)) { | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 171 | fastboot_fail(phandle, "partition table doesn't exist"); | 
|  | 172 | return; | 
|  | 173 | } | 
|  | 174 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 175 | if (path == NULL) { | 
|  | 176 | fastboot_fail(phandle, "Couldn't find partition"); | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 177 | return; | 
|  | 178 | } | 
|  | 179 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 180 | partition_fd = flash_get_partiton(path); | 
|  | 181 | if (partition_fd < 0) { | 
|  | 182 | fastboot_fail(phandle, "partiton file does not exists"); | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | if (flash_erase(partition_fd)) { | 
|  | 186 | fastboot_fail(phandle, "failed to erase partition"); | 
|  | 187 | flash_close(partition_fd); | 
|  | 188 | return; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | if (flash_close(partition_fd) < 0) { | 
|  | 192 | D(ERR, "could not close device %s", strerror(errno)); | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 193 | fastboot_fail(phandle, "failed to erase partition"); | 
|  | 194 | return; | 
|  | 195 | } | 
|  | 196 | fastboot_okay(phandle, ""); | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
|  | 199 | static int GPT_header_location() { | 
|  | 200 | const char *location_str = fastboot_getvar("gpt_sector"); | 
|  | 201 | char *str; | 
|  | 202 | int location; | 
|  | 203 |  | 
|  | 204 | if (!strcmp("", location_str)) { | 
|  | 205 | D(INFO, "GPT location not specified using second sector"); | 
|  | 206 | return 1; | 
|  | 207 | } | 
|  | 208 | else { | 
|  | 209 | location = strtoul(location_str, &str, 10); | 
|  | 210 | D(INFO, "GPT location specified as %d", location); | 
|  | 211 |  | 
|  | 212 | if (*str != '\0') | 
|  | 213 | return -1; | 
|  | 214 |  | 
|  | 215 | return location - 1; | 
|  | 216 | } | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | static void cmd_gpt_layout(struct protocol_handle *phandle, const char *arg) { | 
|  | 220 | struct GPT_entry_table *oldtable; | 
|  | 221 | int location; | 
|  | 222 | struct GPT_content content; | 
|  | 223 | const char *device; | 
|  | 224 | device = fastboot_getvar("blockdev"); | 
|  | 225 |  | 
|  | 226 | if (!strcmp(device, "")) { | 
|  | 227 | fastboot_fail(phandle, "blockdev not defined in config file"); | 
|  | 228 | return; | 
|  | 229 | } | 
|  | 230 |  | 
| Szymon Starzycki | 55b61f4 | 2013-10-03 11:06:45 -0700 | [diff] [blame] | 231 | //TODO: add same verification as in cmd_flash | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 232 | if (phandle->download_fd < 0) { | 
|  | 233 | fastboot_fail(phandle, "no layout file"); | 
|  | 234 | return; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | location = GPT_header_location(); | 
|  | 238 | oldtable = GPT_get_device(device, location); | 
|  | 239 |  | 
|  | 240 | GPT_default_content(&content, oldtable); | 
|  | 241 | if (oldtable == NULL) | 
|  | 242 | D(WARN, "Could not get old gpt table"); | 
|  | 243 | else | 
|  | 244 | GPT_release_device(oldtable); | 
|  | 245 |  | 
|  | 246 | if (!GPT_parse_file(phandle->download_fd, &content)) { | 
|  | 247 | fastboot_fail(phandle, "Could not parse partition config file"); | 
|  | 248 | return; | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | if (trigger_gpt_layout(&content)) { | 
|  | 252 | fastboot_fail(phandle, "Vendor forbids this opperation"); | 
|  | 253 | GPT_release_content(&content); | 
|  | 254 | return; | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | if (!GPT_write_content(device, &content)) { | 
|  | 258 | fastboot_fail(phandle, "Unable to write gpt file"); | 
|  | 259 | GPT_release_content(&content); | 
|  | 260 | return; | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | GPT_release_content(&content); | 
|  | 264 | fastboot_okay(phandle, ""); | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 265 | } | 
|  | 266 |  | 
|  | 267 | static void cmd_flash(struct protocol_handle *phandle, const char *arg) | 
|  | 268 | { | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 269 | int partition; | 
|  | 270 | uint64_t sz; | 
|  | 271 | char data[BOOT_MAGIC_SIZE]; | 
|  | 272 | char path[PATH_MAX]; | 
|  | 273 | ssize_t header_sz = 0; | 
| Szymon Starzycki | 4662a11 | 2013-10-02 17:21:41 -0700 | [diff] [blame] | 274 | int data_fd = 0; | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 275 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 276 | D(DEBUG, "cmd_flash %s\n", arg); | 
|  | 277 |  | 
|  | 278 | if (try_handle_virtual_partition(phandle, arg)) { | 
|  | 279 | return; | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | if (phandle->download_fd < 0) { | 
|  | 283 | fastboot_fail(phandle, "no kernel file"); | 
|  | 284 | return; | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | if (flash_find_entry(arg, path, PATH_MAX)) { | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 288 | fastboot_fail(phandle, "partition table doesn't exist"); | 
|  | 289 | return; | 
|  | 290 | } | 
|  | 291 |  | 
| Szymon Starzycki | 4662a11 | 2013-10-02 17:21:41 -0700 | [diff] [blame] | 292 | if (flash_validate_certificate(phandle->download_fd, &data_fd) != 1) { | 
|  | 293 | fastboot_fail(phandle, "Access forbiden you need certificate"); | 
|  | 294 | return; | 
|  | 295 | } | 
|  | 296 |  | 
| Szymon Starzycki | baf4c4b | 2013-10-04 10:28:03 -0700 | [diff] [blame] | 297 | // TODO: Maybe its goot idea to check whether the partition is bootable | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 298 | if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) { | 
| Szymon Starzycki | 4662a11 | 2013-10-02 17:21:41 -0700 | [diff] [blame] | 299 | if (read_data_once(data_fd, data, BOOT_MAGIC_SIZE) < BOOT_MAGIC_SIZE) { | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 300 | fastboot_fail(phandle, "incoming data read error, cannot read boot header"); | 
|  | 301 | return; | 
|  | 302 | } | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 303 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { | 
|  | 304 | fastboot_fail(phandle, "image is not a boot image"); | 
|  | 305 | return; | 
|  | 306 | } | 
|  | 307 | } | 
|  | 308 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 309 | partition = flash_get_partiton(path); | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 310 |  | 
| Szymon Starzycki | 4662a11 | 2013-10-02 17:21:41 -0700 | [diff] [blame] | 311 | sz = get_file_size64(data_fd); | 
|  | 312 |  | 
|  | 313 | sz -= header_sz; | 
|  | 314 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 315 | if (sz > get_file_size64(partition)) { | 
|  | 316 | flash_close(partition); | 
|  | 317 | D(WARN, "size of file too large"); | 
|  | 318 | fastboot_fail(phandle, "size of file too large"); | 
|  | 319 | return; | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | D(INFO, "writing %lld bytes to '%s'\n", sz, arg); | 
|  | 323 |  | 
|  | 324 | if (flash_write(partition, phandle->download_fd, sz, header_sz)) { | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 325 | fastboot_fail(phandle, "flash write failure"); | 
|  | 326 | return; | 
|  | 327 | } | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 328 | D(INFO, "partition '%s' updated\n", arg); | 
|  | 329 |  | 
|  | 330 | flash_close(partition); | 
| Szymon Starzycki | 4662a11 | 2013-10-02 17:21:41 -0700 | [diff] [blame] | 331 | close(data_fd); | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 332 |  | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 333 | fastboot_okay(phandle, ""); | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | static void cmd_continue(struct protocol_handle *phandle, const char *arg) | 
|  | 337 | { | 
|  | 338 | fastboot_okay(phandle, ""); | 
|  | 339 | #if 0 | 
|  | 340 | udc_stop(); | 
|  | 341 |  | 
|  | 342 | boot_linux_from_flash(); | 
|  | 343 | #endif | 
|  | 344 | } | 
|  | 345 |  | 
|  | 346 | static void cmd_getvar(struct protocol_handle *phandle, const char *arg) | 
|  | 347 | { | 
|  | 348 | const char *value; | 
|  | 349 | D(DEBUG, "cmd_getvar %s\n", arg); | 
|  | 350 |  | 
|  | 351 | value = fastboot_getvar(arg); | 
|  | 352 |  | 
|  | 353 | fastboot_okay(phandle, value); | 
|  | 354 | } | 
|  | 355 |  | 
|  | 356 | static void cmd_download(struct protocol_handle *phandle, const char *arg) | 
|  | 357 | { | 
|  | 358 | unsigned len = strtoul(arg, NULL, 16); | 
|  | 359 | int old_fd; | 
|  | 360 |  | 
|  | 361 | if (len > 256 * 1024 * 1024) { | 
|  | 362 | fastboot_fail(phandle, "data too large"); | 
|  | 363 | return; | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | fastboot_data(phandle, len); | 
|  | 367 |  | 
|  | 368 | old_fd = protocol_get_download(phandle); | 
|  | 369 | if (old_fd >= 0) { | 
|  | 370 | off_t len = lseek(old_fd, 0, SEEK_END); | 
|  | 371 | D(INFO, "disposing of unused fd %d, size %ld", old_fd, len); | 
|  | 372 | close(old_fd); | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | phandle->download_fd = protocol_handle_download(phandle, len); | 
|  | 376 | if (phandle->download_fd < 0) { | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 377 | fastboot_fail(phandle, "download failed"); | 
|  | 378 | return; | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | fastboot_okay(phandle, ""); | 
|  | 382 | } | 
|  | 383 |  | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 384 | static void cmd_oem(struct protocol_handle *phandle, const char *arg) { | 
|  | 385 | const char *response = ""; | 
|  | 386 |  | 
| Szymon Starzycki | baf4c4b | 2013-10-04 10:28:03 -0700 | [diff] [blame] | 387 | //TODO: Maybe it should get download descriptor also | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 388 | if (trigger_oem_cmd(arg, &response)) | 
|  | 389 | fastboot_fail(phandle, response); | 
|  | 390 | else | 
|  | 391 | fastboot_okay(phandle, response); | 
|  | 392 | } | 
|  | 393 |  | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 394 | void commands_init() | 
|  | 395 | { | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 396 | virtual_partition_register("partition-table", cmd_gpt_layout); | 
|  | 397 |  | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 398 | fastboot_register("boot", cmd_boot); | 
|  | 399 | fastboot_register("erase:", cmd_erase); | 
|  | 400 | fastboot_register("flash:", cmd_flash); | 
|  | 401 | fastboot_register("continue", cmd_continue); | 
|  | 402 | fastboot_register("getvar:", cmd_getvar); | 
|  | 403 | fastboot_register("download:", cmd_download); | 
| Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 404 | fastboot_register("oem", cmd_oem); | 
| Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 405 | //fastboot_publish("version", "0.5"); | 
|  | 406 | //fastboot_publish("product", "swordfish"); | 
|  | 407 | //fastboot_publish("kernel", "lk"); | 
|  | 408 | } |