Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, 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 <app.h> |
| 33 | #include <debug.h> |
| 34 | #include <arch/arm.h> |
| 35 | #include <dev/udc.h> |
| 36 | #include <string.h> |
| 37 | #include <kernel/thread.h> |
| 38 | #include <arch/ops.h> |
| 39 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 40 | #include <dev/flash.h> |
| 41 | #include <lib/ptable.h> |
Dima Zavin | b428360 | 2009-01-26 16:36:57 -0800 | [diff] [blame^] | 42 | #include <dev/keys.h> |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 43 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 44 | #include "bootimg.h" |
| 45 | #include "fastboot.h" |
| 46 | |
| 47 | #define TAGS_ADDR 0x10000100 |
| 48 | #define KERNEL_ADDR 0x10800000 |
| 49 | #define RAMDISK_ADDR 0x11000000 |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 50 | #define DEFAULT_CMDLINE "mem=50M console=null"; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 51 | |
| 52 | static struct udc_device surf_udc_device = { |
| 53 | .vendor_id = 0x18d1, |
| 54 | .product_id = 0x0001, |
| 55 | .version_id = 0x0100, |
| 56 | .manufacturer = "Google", |
| 57 | .product = "Android", |
| 58 | }; |
| 59 | |
| 60 | void platform_uninit_timer(void); |
| 61 | |
| 62 | |
| 63 | void boot_linux(void *kernel, unsigned *tags, |
| 64 | const char *cmdline, unsigned machtype, |
| 65 | void *ramdisk, unsigned ramdisk_size) |
| 66 | { |
| 67 | unsigned *ptr = tags; |
| 68 | void (*entry)(unsigned,unsigned,unsigned*) = kernel; |
| 69 | |
| 70 | /* CORE */ |
| 71 | *ptr++ = 2; |
| 72 | *ptr++ = 0x54410001; |
| 73 | |
| 74 | if (ramdisk_size) { |
| 75 | *ptr++ = 4; |
| 76 | *ptr++ = 0x54420005; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 77 | *ptr++ = (unsigned)ramdisk; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 78 | *ptr++ = ramdisk_size; |
| 79 | } |
| 80 | |
| 81 | if (cmdline && cmdline[0]) { |
| 82 | unsigned n; |
| 83 | /* include terminating 0 and round up to a word multiple */ |
| 84 | n = (strlen(cmdline) + 4) & (~3); |
| 85 | *ptr++ = (n / 4) + 2; |
| 86 | *ptr++ = 0x54410009; |
| 87 | memcpy(ptr, cmdline, n); |
| 88 | ptr += (n / 4); |
| 89 | } |
| 90 | |
| 91 | /* END */ |
| 92 | *ptr++ = 0; |
| 93 | *ptr++ = 0; |
| 94 | |
| 95 | dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n", |
| 96 | kernel, ramdisk, ramdisk_size); |
| 97 | if (cmdline) |
| 98 | dprintf(INFO, "cmdline: %s\n", cmdline); |
| 99 | |
| 100 | enter_critical_section(); |
| 101 | platform_uninit_timer(); |
| 102 | arch_disable_cache(UCACHE); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 103 | arch_disable_mmu(); |
| 104 | |
| 105 | entry(0, machtype, tags); |
| 106 | |
| 107 | } |
| 108 | |
| 109 | #define PAGE_MASK 2047 |
| 110 | |
| 111 | #define ROUND_TO_PAGE(x) (((x) + PAGE_MASK) & (~PAGE_MASK)) |
| 112 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 113 | static unsigned char buf[2048]; |
| 114 | |
| 115 | int boot_linux_from_flash(void) |
| 116 | { |
| 117 | struct boot_img_hdr *hdr = (void*) buf; |
| 118 | unsigned n; |
| 119 | struct ptentry *ptn; |
| 120 | struct ptable *ptable; |
| 121 | unsigned offset = 0; |
| 122 | const char *cmdline; |
| 123 | |
| 124 | ptable = flash_get_ptable(); |
| 125 | if (ptable == NULL) { |
| 126 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | ptn = ptable_find(ptable, "boot"); |
| 131 | if (ptn == NULL) { |
| 132 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | if (flash_read(ptn, offset, buf, 2048)) { |
| 137 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 138 | return -1; |
| 139 | } |
| 140 | offset += 2048; |
| 141 | |
| 142 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 143 | dprintf(CRITICAL, "ERROR: Invaled boot image heador\n"); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | n = ROUND_TO_PAGE(hdr->kernel_size); |
| 148 | if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) { |
| 149 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 150 | return -1; |
| 151 | } |
| 152 | offset += n; |
| 153 | |
| 154 | n = ROUND_TO_PAGE(hdr->ramdisk_size); |
| 155 | if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) { |
| 156 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 157 | return -1; |
| 158 | } |
| 159 | offset += n; |
| 160 | |
| 161 | dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr, |
| 162 | hdr->kernel_size); |
| 163 | dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr, |
| 164 | hdr->ramdisk_size); |
| 165 | |
| 166 | if(hdr->cmdline[0]) { |
| 167 | cmdline = (char*) hdr->cmdline; |
| 168 | } else { |
| 169 | cmdline = DEFAULT_CMDLINE; |
| 170 | } |
| 171 | dprintf(INFO, "cmdline = '%s'\n", cmdline); |
| 172 | |
| 173 | /* TODO: create/pass atags to kernel */ |
| 174 | |
| 175 | dprintf(INFO, "\nBooting Linux\n"); |
| 176 | boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR, |
| 177 | (const char *)cmdline, 1008000, |
| 178 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 179 | |
| 180 | return 0; |
| 181 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 182 | |
| 183 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 184 | { |
| 185 | unsigned kernel_actual; |
| 186 | unsigned ramdisk_actual; |
| 187 | static struct boot_img_hdr hdr; |
| 188 | char *ptr = ((char*) data); |
| 189 | |
| 190 | if (sz < sizeof(hdr)) { |
| 191 | fastboot_fail("invalid bootimage header"); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | memcpy(&hdr, data, sizeof(hdr)); |
| 196 | |
| 197 | /* ensure commandline is terminated */ |
| 198 | hdr.cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 199 | |
| 200 | kernel_actual = ROUND_TO_PAGE(hdr.kernel_size); |
| 201 | ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size); |
| 202 | |
| 203 | if (2048 + kernel_actual + ramdisk_actual < sz) { |
| 204 | fastboot_fail("incomplete bootimage"); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | memmove((void*) KERNEL_ADDR, ptr + 2048, hdr.kernel_size); |
| 209 | memmove((void*) RAMDISK_ADDR, ptr + 2048 + kernel_actual, hdr.ramdisk_size); |
| 210 | |
| 211 | fastboot_okay(""); |
| 212 | udc_stop(); |
| 213 | |
| 214 | |
| 215 | boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR, |
| 216 | (const char*) hdr.cmdline, 1008000, |
| 217 | (void*) RAMDISK_ADDR, hdr.ramdisk_size); |
| 218 | } |
| 219 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 220 | void cmd_erase(const char *arg, void *data, unsigned sz) |
| 221 | { |
| 222 | struct ptentry *ptn; |
| 223 | struct ptable *ptable; |
| 224 | |
| 225 | ptable = flash_get_ptable(); |
| 226 | if (ptable == NULL) { |
| 227 | fastboot_fail("partition table doesn't exist"); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | ptn = ptable_find(ptable, arg); |
| 232 | if (ptn == NULL) { |
| 233 | fastboot_fail("unknown partition name"); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | if (flash_erase(ptn)) { |
| 238 | fastboot_fail("failed to erase partition"); |
| 239 | return; |
| 240 | } |
| 241 | fastboot_okay(""); |
| 242 | } |
| 243 | |
| 244 | void cmd_flash(const char *arg, void *data, unsigned sz) |
| 245 | { |
| 246 | struct ptentry *ptn; |
| 247 | struct ptable *ptable; |
| 248 | unsigned extra = 0; |
| 249 | |
| 250 | ptable = flash_get_ptable(); |
| 251 | if (ptable == NULL) { |
| 252 | fastboot_fail("partition table doesn't exist"); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | ptn = ptable_find(ptable, arg); |
| 257 | if (ptn == NULL) { |
| 258 | fastboot_fail("unknown partition name"); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
| 263 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 264 | fastboot_fail("image is not a boot image"); |
| 265 | return; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")) |
| 270 | extra = 64; |
| 271 | else |
| 272 | sz = ROUND_TO_PAGE(sz); |
| 273 | |
| 274 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
| 275 | if (flash_write(ptn, extra, data, sz)) { |
| 276 | fastboot_fail("flash write failure"); |
| 277 | return; |
| 278 | } |
| 279 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 280 | fastboot_okay(""); |
| 281 | } |
| 282 | |
| 283 | void cmd_continue(const char *arg, void *data, unsigned sz) |
| 284 | { |
| 285 | fastboot_okay(""); |
| 286 | udc_stop(); |
| 287 | |
| 288 | boot_linux_from_flash(); |
| 289 | } |
| 290 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 291 | void aboot_init(const struct app_descriptor *app) |
| 292 | { |
Dima Zavin | b428360 | 2009-01-26 16:36:57 -0800 | [diff] [blame^] | 293 | if (keys_get_state(KEY_BACK) != 0) |
| 294 | goto fastboot; |
| 295 | |
| 296 | boot_linux_from_flash(); |
| 297 | dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting " |
| 298 | "to fastboot mode.\n"); |
| 299 | |
| 300 | fastboot: |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 301 | udc_init(&surf_udc_device); |
| 302 | |
| 303 | fastboot_register("boot", cmd_boot); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 304 | fastboot_register("erase:", cmd_erase); |
| 305 | fastboot_register("flash:", cmd_flash); |
| 306 | fastboot_register("continue", cmd_continue); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 307 | fastboot_publish("product", "swordfish"); |
| 308 | fastboot_publish("kernel", "lk"); |
| 309 | |
| 310 | fastboot_init((void*) 0x10100000, 100 * 1024 * 1024); |
| 311 | udc_start(); |
| 312 | } |
| 313 | |
| 314 | APP_START(aboot) |
| 315 | .init = aboot_init, |
| 316 | APP_END |
| 317 | |