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