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> |
| 35 | |
| 36 | #include "bootimg.h" |
| 37 | #include "debug.h" |
| 38 | #include "protocol.h" |
| 39 | |
| 40 | static void cmd_boot(struct protocol_handle *phandle, const char *arg) |
| 41 | { |
| 42 | #if 0 |
| 43 | unsigned kernel_actual; |
| 44 | unsigned ramdisk_actual; |
| 45 | static struct boot_img_hdr hdr; |
| 46 | char *ptr = ((char*) data); |
| 47 | |
| 48 | if (sz < sizeof(hdr)) { |
| 49 | fastboot_fail(phandle, "invalid bootimage header"); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | memcpy(&hdr, data, sizeof(hdr)); |
| 54 | |
| 55 | /* ensure commandline is terminated */ |
| 56 | hdr.cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 57 | |
| 58 | kernel_actual = ROUND_TO_PAGE(hdr.kernel_size); |
| 59 | ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size); |
| 60 | |
| 61 | if (2048 + kernel_actual + ramdisk_actual < sz) { |
| 62 | fastboot_fail(phandle, "incomplete bootimage"); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | /*memmove((void*) KERNEL_ADDR, ptr + 2048, hdr.kernel_size); |
| 67 | memmove((void*) RAMDISK_ADDR, ptr + 2048 + kernel_actual, hdr.ramdisk_size);*/ |
| 68 | |
| 69 | fastboot_okay(phandle, ""); |
| 70 | udc_stop(); |
| 71 | |
| 72 | |
| 73 | /*boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR, |
| 74 | (const char*) hdr.cmdline, LINUX_MACHTYPE, |
| 75 | (void*) RAMDISK_ADDR, hdr.ramdisk_size);*/ |
| 76 | #endif |
| 77 | } |
| 78 | |
| 79 | static void cmd_erase(struct protocol_handle *phandle, const char *arg) |
| 80 | { |
| 81 | #if 0 |
| 82 | struct ptentry *ptn; |
| 83 | struct ptable *ptable; |
| 84 | |
| 85 | ptable = flash_get_ptable(); |
| 86 | if (ptable == NULL) { |
| 87 | fastboot_fail(phandle, "partition table doesn't exist"); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | ptn = ptable_find(ptable, arg); |
| 92 | if (ptn == NULL) { |
| 93 | fastboot_fail(phandle, "unknown partition name"); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if (flash_erase(ptn)) { |
| 98 | fastboot_fail(phandle, "failed to erase partition"); |
| 99 | return; |
| 100 | } |
| 101 | fastboot_okay(phandle, ""); |
| 102 | #endif |
| 103 | } |
| 104 | |
| 105 | static void cmd_flash(struct protocol_handle *phandle, const char *arg) |
| 106 | { |
| 107 | #if 0 |
| 108 | struct ptentry *ptn; |
| 109 | struct ptable *ptable; |
| 110 | unsigned extra = 0; |
| 111 | |
| 112 | ptable = flash_get_ptable(); |
| 113 | if (ptable == NULL) { |
| 114 | fastboot_fail(phandle, "partition table doesn't exist"); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | ptn = ptable_find(ptable, arg); |
| 119 | if (ptn == NULL) { |
| 120 | fastboot_fail(phandle, "unknown partition name"); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
| 125 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 126 | fastboot_fail(phandle, "image is not a boot image"); |
| 127 | return; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")) |
| 132 | extra = 64; |
| 133 | else |
| 134 | sz = ROUND_TO_PAGE(sz); |
| 135 | |
| 136 | D(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
| 137 | if (flash_write(ptn, extra, data, sz)) { |
| 138 | fastboot_fail(phandle, "flash write failure"); |
| 139 | return; |
| 140 | } |
| 141 | D(INFO, "partition '%s' updated\n", ptn->name); |
| 142 | #endif |
| 143 | fastboot_okay(phandle, ""); |
| 144 | } |
| 145 | |
| 146 | static void cmd_continue(struct protocol_handle *phandle, const char *arg) |
| 147 | { |
| 148 | fastboot_okay(phandle, ""); |
| 149 | #if 0 |
| 150 | udc_stop(); |
| 151 | |
| 152 | boot_linux_from_flash(); |
| 153 | #endif |
| 154 | } |
| 155 | |
| 156 | static void cmd_getvar(struct protocol_handle *phandle, const char *arg) |
| 157 | { |
| 158 | const char *value; |
| 159 | D(DEBUG, "cmd_getvar %s\n", arg); |
| 160 | |
| 161 | value = fastboot_getvar(arg); |
| 162 | |
| 163 | fastboot_okay(phandle, value); |
| 164 | } |
| 165 | |
| 166 | static void cmd_download(struct protocol_handle *phandle, const char *arg) |
| 167 | { |
| 168 | unsigned len = strtoul(arg, NULL, 16); |
| 169 | int old_fd; |
| 170 | |
| 171 | if (len > 256 * 1024 * 1024) { |
| 172 | fastboot_fail(phandle, "data too large"); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | fastboot_data(phandle, len); |
| 177 | |
| 178 | old_fd = protocol_get_download(phandle); |
| 179 | if (old_fd >= 0) { |
| 180 | off_t len = lseek(old_fd, 0, SEEK_END); |
| 181 | D(INFO, "disposing of unused fd %d, size %ld", old_fd, len); |
| 182 | close(old_fd); |
| 183 | } |
| 184 | |
| 185 | phandle->download_fd = protocol_handle_download(phandle, len); |
| 186 | if (phandle->download_fd < 0) { |
| 187 | //handle->state = STATE_ERROR; |
| 188 | fastboot_fail(phandle, "download failed"); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | fastboot_okay(phandle, ""); |
| 193 | } |
| 194 | |
| 195 | void commands_init() |
| 196 | { |
| 197 | fastboot_register("boot", cmd_boot); |
| 198 | fastboot_register("erase:", cmd_erase); |
| 199 | fastboot_register("flash:", cmd_flash); |
| 200 | fastboot_register("continue", cmd_continue); |
| 201 | fastboot_register("getvar:", cmd_getvar); |
| 202 | fastboot_register("download:", cmd_download); |
| 203 | //fastboot_publish("version", "0.5"); |
| 204 | //fastboot_publish("product", "swordfish"); |
| 205 | //fastboot_publish("kernel", "lk"); |
| 206 | } |