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 | |
| 40 | #include "bootimg.h" |
| 41 | #include "fastboot.h" |
| 42 | |
| 43 | #define TAGS_ADDR 0x10000100 |
| 44 | #define KERNEL_ADDR 0x10800000 |
| 45 | #define RAMDISK_ADDR 0x11000000 |
| 46 | |
| 47 | static struct udc_device surf_udc_device = { |
| 48 | .vendor_id = 0x18d1, |
| 49 | .product_id = 0x0001, |
| 50 | .version_id = 0x0100, |
| 51 | .manufacturer = "Google", |
| 52 | .product = "Android", |
| 53 | }; |
| 54 | |
| 55 | void platform_uninit_timer(void); |
| 56 | |
| 57 | |
| 58 | void boot_linux(void *kernel, unsigned *tags, |
| 59 | const char *cmdline, unsigned machtype, |
| 60 | void *ramdisk, unsigned ramdisk_size) |
| 61 | { |
| 62 | unsigned *ptr = tags; |
| 63 | void (*entry)(unsigned,unsigned,unsigned*) = kernel; |
| 64 | |
| 65 | /* CORE */ |
| 66 | *ptr++ = 2; |
| 67 | *ptr++ = 0x54410001; |
| 68 | |
| 69 | if (ramdisk_size) { |
| 70 | *ptr++ = 4; |
| 71 | *ptr++ = 0x54420005; |
| 72 | *ptr++ = RAMDISK_ADDR; |
| 73 | *ptr++ = ramdisk_size; |
| 74 | } |
| 75 | |
| 76 | if (cmdline && cmdline[0]) { |
| 77 | unsigned n; |
| 78 | /* include terminating 0 and round up to a word multiple */ |
| 79 | n = (strlen(cmdline) + 4) & (~3); |
| 80 | *ptr++ = (n / 4) + 2; |
| 81 | *ptr++ = 0x54410009; |
| 82 | memcpy(ptr, cmdline, n); |
| 83 | ptr += (n / 4); |
| 84 | } |
| 85 | |
| 86 | /* END */ |
| 87 | *ptr++ = 0; |
| 88 | *ptr++ = 0; |
| 89 | |
| 90 | dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n", |
| 91 | kernel, ramdisk, ramdisk_size); |
| 92 | if (cmdline) |
| 93 | dprintf(INFO, "cmdline: %s\n", cmdline); |
| 94 | |
| 95 | enter_critical_section(); |
| 96 | platform_uninit_timer(); |
| 97 | arch_disable_cache(UCACHE); |
| 98 | arch_enable_cache(ICACHE); |
| 99 | arch_disable_mmu(); |
| 100 | |
| 101 | entry(0, machtype, tags); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | #define PAGE_MASK 2047 |
| 106 | |
| 107 | #define ROUND_TO_PAGE(x) (((x) + PAGE_MASK) & (~PAGE_MASK)) |
| 108 | |
| 109 | |
| 110 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 111 | { |
| 112 | unsigned kernel_actual; |
| 113 | unsigned ramdisk_actual; |
| 114 | static struct boot_img_hdr hdr; |
| 115 | char *ptr = ((char*) data); |
| 116 | |
| 117 | if (sz < sizeof(hdr)) { |
| 118 | fastboot_fail("invalid bootimage header"); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | memcpy(&hdr, data, sizeof(hdr)); |
| 123 | |
| 124 | /* ensure commandline is terminated */ |
| 125 | hdr.cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 126 | |
| 127 | kernel_actual = ROUND_TO_PAGE(hdr.kernel_size); |
| 128 | ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size); |
| 129 | |
| 130 | if (2048 + kernel_actual + ramdisk_actual < sz) { |
| 131 | fastboot_fail("incomplete bootimage"); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | memmove((void*) KERNEL_ADDR, ptr + 2048, hdr.kernel_size); |
| 136 | memmove((void*) RAMDISK_ADDR, ptr + 2048 + kernel_actual, hdr.ramdisk_size); |
| 137 | |
| 138 | fastboot_okay(""); |
| 139 | udc_stop(); |
| 140 | |
| 141 | |
| 142 | boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR, |
| 143 | (const char*) hdr.cmdline, 1008000, |
| 144 | (void*) RAMDISK_ADDR, hdr.ramdisk_size); |
| 145 | } |
| 146 | |
| 147 | void aboot_init(const struct app_descriptor *app) |
| 148 | { |
| 149 | udc_init(&surf_udc_device); |
| 150 | |
| 151 | fastboot_register("boot", cmd_boot); |
| 152 | fastboot_publish("product", "swordfish"); |
| 153 | fastboot_publish("kernel", "lk"); |
| 154 | |
| 155 | fastboot_init((void*) 0x10100000, 100 * 1024 * 1024); |
| 156 | udc_start(); |
| 157 | } |
| 158 | |
| 159 | APP_START(aboot) |
| 160 | .init = aboot_init, |
| 161 | APP_END |
| 162 | |