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 | { |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 55 | unsigned kernel_actual; |
| 56 | unsigned ramdisk_actual; |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 57 | unsigned second_actual; |
| 58 | void *kernel_ptr; |
| 59 | void *ramdisk_ptr; |
| 60 | void *second_ptr; |
| 61 | struct boot_img_hdr *hdr; |
| 62 | int sz, atags_sz, new_atags_sz; |
| 63 | int rv; |
| 64 | char *ptr = NULL, *atags_ptr = NULL, *new_atags = NULL; |
| 65 | D(DEBUG, "cmd_boot %s\n", arg); |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 66 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 67 | if (phandle->download_fd < 0) { |
| 68 | fastboot_fail(phandle, "no kernel file"); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | atags_ptr = read_atags(ATAGS_LOCATION, &atags_sz); |
| 73 | if (atags_ptr == NULL) { |
| 74 | fastboot_fail(phandle, "atags read error"); |
| 75 | goto error; |
| 76 | } |
| 77 | |
| 78 | sz = get_file_size(phandle->download_fd); |
| 79 | ptr = (char *) mmap(NULL, sz, PROT_READ, |
| 80 | MAP_POPULATE | MAP_PRIVATE, phandle->download_fd, 0); |
| 81 | hdr = (struct boot_img_hdr *) ptr; |
| 82 | |
| 83 | if (ptr == MAP_FAILED) { |
| 84 | fastboot_fail(phandle, "internal fastbootd error"); |
| 85 | goto error; |
| 86 | } |
| 87 | |
| 88 | if ((size_t) sz < sizeof(*hdr)) { |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 89 | fastboot_fail(phandle, "invalid bootimage header"); |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 90 | goto error; |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 93 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, hdr->page_size); |
| 94 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, hdr->page_size); |
| 95 | second_actual = ROUND_TO_PAGE(hdr->second_size, hdr->page_size); |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 96 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 97 | 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] | 98 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 99 | if (new_atags == NULL) { |
| 100 | fastboot_fail(phandle, "atags generate error"); |
| 101 | goto error; |
| 102 | } |
| 103 | if (new_atags_sz > 0x4000) { |
| 104 | fastboot_fail(phandle, "atags file to large"); |
| 105 | goto error; |
| 106 | } |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 107 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 108 | if ((int) (hdr->page_size + kernel_actual + ramdisk_actual) < sz) { |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 109 | fastboot_fail(phandle, "incomplete bootimage"); |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 110 | goto error; |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 113 | kernel_ptr = (void *)((unsigned) ptr + hdr->page_size); |
| 114 | ramdisk_ptr = (void *)((unsigned) kernel_ptr + kernel_actual); |
| 115 | second_ptr = (void *)((unsigned) ramdisk_ptr + ramdisk_actual); |
| 116 | |
| 117 | D(INFO, "preparing to boot"); |
| 118 | // Prepares boot physical addresses from header are ignored |
| 119 | rv = prepare_boot_linux(hdr->kernel_addr, kernel_ptr, kernel_actual, |
| 120 | hdr->ramdisk_addr, ramdisk_ptr, ramdisk_actual, |
| 121 | hdr->second_addr, second_ptr, second_actual, |
| 122 | hdr->tags_addr, new_atags, ROUND_TO_PAGE(new_atags_sz, hdr->page_size)); |
| 123 | if (rv < 0) { |
| 124 | fastboot_fail(phandle, "kexec prepare failed"); |
| 125 | goto error; |
| 126 | } |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 127 | |
| 128 | fastboot_okay(phandle, ""); |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 129 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 130 | free(atags_ptr); |
| 131 | munmap(ptr, sz); |
| 132 | free(new_atags); |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 133 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 134 | D(INFO, "Kexec going to reboot"); |
| 135 | reboot(LINUX_REBOOT_CMD_KEXEC); |
| 136 | |
| 137 | fastboot_fail(phandle, "reboot error"); |
| 138 | |
| 139 | return; |
| 140 | |
| 141 | error: |
| 142 | |
| 143 | if (atags_ptr != NULL) |
| 144 | free(atags_ptr); |
| 145 | if (ptr != NULL) |
| 146 | munmap(ptr, sz); |
| 147 | |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static void cmd_erase(struct protocol_handle *phandle, const char *arg) |
| 151 | { |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 152 | int partition_fd; |
| 153 | char path[PATH_MAX]; |
| 154 | D(DEBUG, "cmd_erase %s\n", arg); |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 155 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 156 | if (flash_find_entry(arg, path, PATH_MAX)) { |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 157 | fastboot_fail(phandle, "partition table doesn't exist"); |
| 158 | return; |
| 159 | } |
| 160 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 161 | if (path == NULL) { |
| 162 | fastboot_fail(phandle, "Couldn't find partition"); |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 166 | partition_fd = flash_get_partiton(path); |
| 167 | if (partition_fd < 0) { |
| 168 | fastboot_fail(phandle, "partiton file does not exists"); |
| 169 | } |
| 170 | |
| 171 | if (flash_erase(partition_fd)) { |
| 172 | fastboot_fail(phandle, "failed to erase partition"); |
| 173 | flash_close(partition_fd); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | if (flash_close(partition_fd) < 0) { |
| 178 | D(ERR, "could not close device %s", strerror(errno)); |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 179 | fastboot_fail(phandle, "failed to erase partition"); |
| 180 | return; |
| 181 | } |
| 182 | fastboot_okay(phandle, ""); |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 183 | } |
| 184 | |
| 185 | static int GPT_header_location() { |
| 186 | const char *location_str = fastboot_getvar("gpt_sector"); |
| 187 | char *str; |
| 188 | int location; |
| 189 | |
| 190 | if (!strcmp("", location_str)) { |
| 191 | D(INFO, "GPT location not specified using second sector"); |
| 192 | return 1; |
| 193 | } |
| 194 | else { |
| 195 | location = strtoul(location_str, &str, 10); |
| 196 | D(INFO, "GPT location specified as %d", location); |
| 197 | |
| 198 | if (*str != '\0') |
| 199 | return -1; |
| 200 | |
| 201 | return location - 1; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static void cmd_gpt_layout(struct protocol_handle *phandle, const char *arg) { |
| 206 | struct GPT_entry_table *oldtable; |
| 207 | int location; |
| 208 | struct GPT_content content; |
| 209 | const char *device; |
| 210 | device = fastboot_getvar("blockdev"); |
| 211 | |
| 212 | if (!strcmp(device, "")) { |
| 213 | fastboot_fail(phandle, "blockdev not defined in config file"); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | if (phandle->download_fd < 0) { |
| 218 | fastboot_fail(phandle, "no layout file"); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | location = GPT_header_location(); |
| 223 | oldtable = GPT_get_device(device, location); |
| 224 | |
| 225 | GPT_default_content(&content, oldtable); |
| 226 | if (oldtable == NULL) |
| 227 | D(WARN, "Could not get old gpt table"); |
| 228 | else |
| 229 | GPT_release_device(oldtable); |
| 230 | |
| 231 | if (!GPT_parse_file(phandle->download_fd, &content)) { |
| 232 | fastboot_fail(phandle, "Could not parse partition config file"); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | if (trigger_gpt_layout(&content)) { |
| 237 | fastboot_fail(phandle, "Vendor forbids this opperation"); |
| 238 | GPT_release_content(&content); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | if (!GPT_write_content(device, &content)) { |
| 243 | fastboot_fail(phandle, "Unable to write gpt file"); |
| 244 | GPT_release_content(&content); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | GPT_release_content(&content); |
| 249 | fastboot_okay(phandle, ""); |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static void cmd_flash(struct protocol_handle *phandle, const char *arg) |
| 253 | { |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 254 | int partition; |
| 255 | uint64_t sz; |
| 256 | char data[BOOT_MAGIC_SIZE]; |
| 257 | char path[PATH_MAX]; |
| 258 | ssize_t header_sz = 0; |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 259 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 260 | D(DEBUG, "cmd_flash %s\n", arg); |
| 261 | |
| 262 | if (try_handle_virtual_partition(phandle, arg)) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | if (phandle->download_fd < 0) { |
| 267 | fastboot_fail(phandle, "no kernel file"); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | if (flash_find_entry(arg, path, PATH_MAX)) { |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 272 | fastboot_fail(phandle, "partition table doesn't exist"); |
| 273 | return; |
| 274 | } |
| 275 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 276 | // TODO: Maybe its goot idea to check whether the partition is just bootable partition |
| 277 | if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) { |
| 278 | if (read_data_once(phandle->download_fd, data, BOOT_MAGIC_SIZE) < BOOT_MAGIC_SIZE) { |
| 279 | fastboot_fail(phandle, "incoming data read error, cannot read boot header"); |
| 280 | return; |
| 281 | } |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 282 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 283 | fastboot_fail(phandle, "image is not a boot image"); |
| 284 | return; |
| 285 | } |
| 286 | } |
| 287 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 288 | partition = flash_get_partiton(path); |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 289 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 290 | sz = get_file_size64(phandle->download_fd); |
| 291 | if (sz > get_file_size64(partition)) { |
| 292 | flash_close(partition); |
| 293 | D(WARN, "size of file too large"); |
| 294 | fastboot_fail(phandle, "size of file too large"); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | D(INFO, "writing %lld bytes to '%s'\n", sz, arg); |
| 299 | |
| 300 | if (flash_write(partition, phandle->download_fd, sz, header_sz)) { |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 301 | fastboot_fail(phandle, "flash write failure"); |
| 302 | return; |
| 303 | } |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 304 | D(INFO, "partition '%s' updated\n", arg); |
| 305 | |
| 306 | flash_close(partition); |
| 307 | |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 308 | fastboot_okay(phandle, ""); |
| 309 | } |
| 310 | |
| 311 | static void cmd_continue(struct protocol_handle *phandle, const char *arg) |
| 312 | { |
| 313 | fastboot_okay(phandle, ""); |
| 314 | #if 0 |
| 315 | udc_stop(); |
| 316 | |
| 317 | boot_linux_from_flash(); |
| 318 | #endif |
| 319 | } |
| 320 | |
| 321 | static void cmd_getvar(struct protocol_handle *phandle, const char *arg) |
| 322 | { |
| 323 | const char *value; |
| 324 | D(DEBUG, "cmd_getvar %s\n", arg); |
| 325 | |
| 326 | value = fastboot_getvar(arg); |
| 327 | |
| 328 | fastboot_okay(phandle, value); |
| 329 | } |
| 330 | |
| 331 | static void cmd_download(struct protocol_handle *phandle, const char *arg) |
| 332 | { |
| 333 | unsigned len = strtoul(arg, NULL, 16); |
| 334 | int old_fd; |
| 335 | |
| 336 | if (len > 256 * 1024 * 1024) { |
| 337 | fastboot_fail(phandle, "data too large"); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | fastboot_data(phandle, len); |
| 342 | |
| 343 | old_fd = protocol_get_download(phandle); |
| 344 | if (old_fd >= 0) { |
| 345 | off_t len = lseek(old_fd, 0, SEEK_END); |
| 346 | D(INFO, "disposing of unused fd %d, size %ld", old_fd, len); |
| 347 | close(old_fd); |
| 348 | } |
| 349 | |
| 350 | phandle->download_fd = protocol_handle_download(phandle, len); |
| 351 | if (phandle->download_fd < 0) { |
| 352 | //handle->state = STATE_ERROR; |
| 353 | fastboot_fail(phandle, "download failed"); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | fastboot_okay(phandle, ""); |
| 358 | } |
| 359 | |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 360 | static void cmd_oem(struct protocol_handle *phandle, const char *arg) { |
| 361 | const char *response = ""; |
| 362 | |
| 363 | if (trigger_oem_cmd(arg, &response)) |
| 364 | fastboot_fail(phandle, response); |
| 365 | else |
| 366 | fastboot_okay(phandle, response); |
| 367 | } |
| 368 | |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 369 | void commands_init() |
| 370 | { |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 371 | virtual_partition_register("partition-table", cmd_gpt_layout); |
| 372 | |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 373 | fastboot_register("boot", cmd_boot); |
| 374 | fastboot_register("erase:", cmd_erase); |
| 375 | fastboot_register("flash:", cmd_flash); |
| 376 | fastboot_register("continue", cmd_continue); |
| 377 | fastboot_register("getvar:", cmd_getvar); |
| 378 | fastboot_register("download:", cmd_download); |
Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame^] | 379 | fastboot_register("oem", cmd_oem); |
Colin Cross | a3d386e | 2013-02-06 21:03:34 -0800 | [diff] [blame] | 380 | //fastboot_publish("version", "0.5"); |
| 381 | //fastboot_publish("product", "swordfish"); |
| 382 | //fastboot_publish("kernel", "lk"); |
| 383 | } |