Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 5 | * Copyright (c) 2009, Code Aurora Forum. All rights reserved. |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 6 | * |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are met: |
| 9 | * * Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * * Neither the name of Code Aurora nor |
| 15 | * the names of its contributors may be used to endorse or promote |
| 16 | * products derived from this software without specific prior written |
| 17 | * permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 26 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 28 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 29 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | * |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | #include <app.h> |
| 34 | #include <debug.h> |
| 35 | #include <arch/arm.h> |
| 36 | #include <dev/udc.h> |
| 37 | #include <string.h> |
| 38 | #include <kernel/thread.h> |
| 39 | #include <arch/ops.h> |
| 40 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 41 | #include <dev/flash.h> |
| 42 | #include <lib/ptable.h> |
Dima Zavin | b428360 | 2009-01-26 16:36:57 -0800 | [diff] [blame] | 43 | #include <dev/keys.h> |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 44 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 45 | #include "bootimg.h" |
| 46 | #include "fastboot.h" |
| 47 | |
Chandan Uddaraju | da91983 | 2009-11-17 01:06:11 -0800 | [diff] [blame] | 48 | #define DEFAULT_CMDLINE "mem=100M console=null"; |
Brian Swetland | 2defe16 | 2009-08-18 14:35:59 -0700 | [diff] [blame] | 49 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 50 | #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000 |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame^] | 51 | #define RECOVERY_MODE 0x77665502 |
| 52 | #define FASTBOOT_MODE 0x77665500 |
| 53 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 54 | static const char *emmc_cmdline = " androidboot.emmc=true"; |
| 55 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 56 | static struct udc_device surf_udc_device = { |
| 57 | .vendor_id = 0x18d1, |
Chandan Uddaraju | c53a1a1 | 2009-11-18 14:53:40 -0800 | [diff] [blame] | 58 | .product_id = 0xD00D, |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 59 | .version_id = 0x0100, |
| 60 | .manufacturer = "Google", |
| 61 | .product = "Android", |
| 62 | }; |
| 63 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 64 | struct atag_ptbl_entry |
| 65 | { |
| 66 | char name[16]; |
| 67 | unsigned offset; |
| 68 | unsigned size; |
| 69 | unsigned flags; |
| 70 | }; |
| 71 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 72 | void platform_uninit_timer(void); |
Chandan Uddaraju | c6860e1 | 2009-11-19 11:22:15 -0800 | [diff] [blame] | 73 | unsigned* target_atag_mem(unsigned* ptr); |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 74 | unsigned board_machtype(void); |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame^] | 75 | unsigned check_reboot_mode(void); |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 76 | int target_is_emmc_boot(void); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 77 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame^] | 78 | static int boot_into_recovery = 0; |
| 79 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 80 | static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn) |
| 81 | { |
| 82 | struct atag_ptbl_entry atag_ptn; |
| 83 | |
| 84 | memcpy(atag_ptn.name, ptn->name, 16); |
| 85 | atag_ptn.name[15] = '\0'; |
| 86 | atag_ptn.offset = ptn->start; |
| 87 | atag_ptn.size = ptn->length; |
| 88 | atag_ptn.flags = ptn->flags; |
| 89 | memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry)); |
| 90 | *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned); |
| 91 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 92 | |
| 93 | void boot_linux(void *kernel, unsigned *tags, |
| 94 | const char *cmdline, unsigned machtype, |
| 95 | void *ramdisk, unsigned ramdisk_size) |
| 96 | { |
| 97 | unsigned *ptr = tags; |
| 98 | void (*entry)(unsigned,unsigned,unsigned*) = kernel; |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 99 | struct ptable *ptable; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 100 | int cmdline_len = 0; |
| 101 | int have_cmdline = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 102 | |
| 103 | /* CORE */ |
| 104 | *ptr++ = 2; |
| 105 | *ptr++ = 0x54410001; |
| 106 | |
| 107 | if (ramdisk_size) { |
| 108 | *ptr++ = 4; |
| 109 | *ptr++ = 0x54420005; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 110 | *ptr++ = (unsigned)ramdisk; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 111 | *ptr++ = ramdisk_size; |
| 112 | } |
| 113 | |
Chandan Uddaraju | c6860e1 | 2009-11-19 11:22:15 -0800 | [diff] [blame] | 114 | ptr = target_atag_mem(ptr); |
| 115 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 116 | if (!target_is_emmc_boot()) { |
| 117 | /* Skip NAND partition ATAGS for eMMC boot */ |
| 118 | if ((ptable = flash_get_ptable()) && (ptable->count != 0)) { |
| 119 | int i; |
| 120 | *ptr++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) / |
| 121 | sizeof(unsigned))); |
| 122 | *ptr++ = 0x4d534d70; |
| 123 | for (i = 0; i < ptable->count; ++i) |
| 124 | ptentry_to_tag(&ptr, ptable_get(ptable, i)); |
| 125 | } |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 128 | if (cmdline && cmdline[0]) { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 129 | cmdline_len = strlen(cmdline); |
| 130 | have_cmdline = 1; |
| 131 | } |
| 132 | if (target_is_emmc_boot()) { |
| 133 | cmdline_len += strlen(emmc_cmdline); |
| 134 | } |
| 135 | if (cmdline_len > 0) { |
| 136 | const char *src; |
| 137 | char *dst; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 138 | unsigned n; |
| 139 | /* include terminating 0 and round up to a word multiple */ |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 140 | n = (cmdline_len + 4) & (~3); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 141 | *ptr++ = (n / 4) + 2; |
| 142 | *ptr++ = 0x54410009; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 143 | dst = (char *)ptr; |
| 144 | if (have_cmdline) { |
| 145 | src = cmdline; |
| 146 | while ((*dst++ = *src++)); |
| 147 | } |
| 148 | if (target_is_emmc_boot()) { |
| 149 | src = emmc_cmdline; |
| 150 | if (have_cmdline) --dst; |
| 151 | while ((*dst++ = *src++)); |
| 152 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 153 | ptr += (n / 4); |
| 154 | } |
| 155 | |
| 156 | /* END */ |
| 157 | *ptr++ = 0; |
| 158 | *ptr++ = 0; |
| 159 | |
| 160 | dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n", |
| 161 | kernel, ramdisk, ramdisk_size); |
| 162 | if (cmdline) |
| 163 | dprintf(INFO, "cmdline: %s\n", cmdline); |
| 164 | |
| 165 | enter_critical_section(); |
| 166 | platform_uninit_timer(); |
| 167 | arch_disable_cache(UCACHE); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 168 | arch_disable_mmu(); |
| 169 | |
| 170 | entry(0, machtype, tags); |
| 171 | |
| 172 | } |
| 173 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 174 | unsigned page_size = 0; |
| 175 | unsigned page_mask = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 176 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 177 | #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y))) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 178 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 179 | static unsigned char buf[4096]; //Equal to max-supported pagesize |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 180 | |
| 181 | int boot_linux_from_flash(void) |
| 182 | { |
| 183 | struct boot_img_hdr *hdr = (void*) buf; |
| 184 | unsigned n; |
| 185 | struct ptentry *ptn; |
| 186 | struct ptable *ptable; |
| 187 | unsigned offset = 0; |
| 188 | const char *cmdline; |
| 189 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame^] | 190 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 191 | if (target_is_emmc_boot()) { |
| 192 | hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 193 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 194 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
| 195 | return -1; |
| 196 | } |
| 197 | goto continue_boot; |
| 198 | } |
| 199 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 200 | ptable = flash_get_ptable(); |
| 201 | if (ptable == NULL) { |
| 202 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 203 | return -1; |
| 204 | } |
| 205 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame^] | 206 | if(!boot_into_recovery) |
| 207 | { |
| 208 | ptn = ptable_find(ptable, "boot"); |
| 209 | if (ptn == NULL) { |
| 210 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 211 | return -1; |
| 212 | } |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | ptn = ptable_find(ptable, "recovery"); |
| 217 | if (ptn == NULL) { |
| 218 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 219 | return -1; |
| 220 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 223 | if (flash_read(ptn, offset, buf, page_size)) { |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 224 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 225 | return -1; |
| 226 | } |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 227 | offset += page_size; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 228 | |
| 229 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 230 | dprintf(CRITICAL, "ERROR: Invaled boot image heador\n"); |
| 231 | return -1; |
| 232 | } |
| 233 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 234 | if (hdr->page_size != page_size) { |
| 235 | dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size); |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 240 | if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) { |
| 241 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 242 | return -1; |
| 243 | } |
| 244 | offset += n; |
| 245 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 246 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 247 | if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) { |
| 248 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 249 | return -1; |
| 250 | } |
| 251 | offset += n; |
| 252 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 253 | continue_boot: |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 254 | dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr, |
| 255 | hdr->kernel_size); |
| 256 | dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr, |
| 257 | hdr->ramdisk_size); |
| 258 | |
| 259 | if(hdr->cmdline[0]) { |
| 260 | cmdline = (char*) hdr->cmdline; |
| 261 | } else { |
| 262 | cmdline = DEFAULT_CMDLINE; |
| 263 | } |
| 264 | dprintf(INFO, "cmdline = '%s'\n", cmdline); |
| 265 | |
| 266 | /* TODO: create/pass atags to kernel */ |
| 267 | |
| 268 | dprintf(INFO, "\nBooting Linux\n"); |
| 269 | boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR, |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 270 | (const char *)cmdline, board_machtype(), |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 271 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 272 | |
| 273 | return 0; |
| 274 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 275 | |
| 276 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 277 | { |
| 278 | unsigned kernel_actual; |
| 279 | unsigned ramdisk_actual; |
| 280 | static struct boot_img_hdr hdr; |
| 281 | char *ptr = ((char*) data); |
| 282 | |
| 283 | if (sz < sizeof(hdr)) { |
| 284 | fastboot_fail("invalid bootimage header"); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | memcpy(&hdr, data, sizeof(hdr)); |
| 289 | |
| 290 | /* ensure commandline is terminated */ |
| 291 | hdr.cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 292 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 293 | kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask); |
| 294 | ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 295 | |
| 296 | if (2048 + kernel_actual + ramdisk_actual < sz) { |
| 297 | fastboot_fail("incomplete bootimage"); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | memmove((void*) KERNEL_ADDR, ptr + 2048, hdr.kernel_size); |
| 302 | memmove((void*) RAMDISK_ADDR, ptr + 2048 + kernel_actual, hdr.ramdisk_size); |
| 303 | |
| 304 | fastboot_okay(""); |
| 305 | udc_stop(); |
| 306 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 307 | boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR, |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 308 | (const char*) hdr.cmdline, board_machtype(), |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 309 | (void*) RAMDISK_ADDR, hdr.ramdisk_size); |
| 310 | } |
| 311 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 312 | void cmd_erase(const char *arg, void *data, unsigned sz) |
| 313 | { |
| 314 | struct ptentry *ptn; |
| 315 | struct ptable *ptable; |
| 316 | |
| 317 | ptable = flash_get_ptable(); |
| 318 | if (ptable == NULL) { |
| 319 | fastboot_fail("partition table doesn't exist"); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | ptn = ptable_find(ptable, arg); |
| 324 | if (ptn == NULL) { |
| 325 | fastboot_fail("unknown partition name"); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | if (flash_erase(ptn)) { |
| 330 | fastboot_fail("failed to erase partition"); |
| 331 | return; |
| 332 | } |
| 333 | fastboot_okay(""); |
| 334 | } |
| 335 | |
| 336 | void cmd_flash(const char *arg, void *data, unsigned sz) |
| 337 | { |
| 338 | struct ptentry *ptn; |
| 339 | struct ptable *ptable; |
| 340 | unsigned extra = 0; |
| 341 | |
| 342 | ptable = flash_get_ptable(); |
| 343 | if (ptable == NULL) { |
| 344 | fastboot_fail("partition table doesn't exist"); |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | ptn = ptable_find(ptable, arg); |
| 349 | if (ptn == NULL) { |
| 350 | fastboot_fail("unknown partition name"); |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
| 355 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 356 | fastboot_fail("image is not a boot image"); |
| 357 | return; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")) |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 362 | extra = ((page_size >> 9) * 16); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 363 | else |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 364 | sz = ROUND_TO_PAGE(sz, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 365 | |
| 366 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
| 367 | if (flash_write(ptn, extra, data, sz)) { |
| 368 | fastboot_fail("flash write failure"); |
| 369 | return; |
| 370 | } |
| 371 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 372 | fastboot_okay(""); |
| 373 | } |
| 374 | |
| 375 | void cmd_continue(const char *arg, void *data, unsigned sz) |
| 376 | { |
| 377 | fastboot_okay(""); |
| 378 | udc_stop(); |
| 379 | |
| 380 | boot_linux_from_flash(); |
| 381 | } |
| 382 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 383 | void aboot_init(const struct app_descriptor *app) |
| 384 | { |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame^] | 385 | unsigned reboot_mode = 0; |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 386 | page_size = flash_page_size(); |
| 387 | page_mask = page_size - 1; |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame^] | 388 | if (keys_get_state(KEY_HOME) != 0) |
| 389 | boot_into_recovery = 1; |
Dima Zavin | b428360 | 2009-01-26 16:36:57 -0800 | [diff] [blame] | 390 | if (keys_get_state(KEY_BACK) != 0) |
| 391 | goto fastboot; |
| 392 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame^] | 393 | reboot_mode = check_reboot_mode(); |
| 394 | if (reboot_mode == RECOVERY_MODE){ |
| 395 | boot_into_recovery = 1; |
| 396 | }else if(reboot_mode == FASTBOOT_MODE){ |
| 397 | goto fastboot; |
| 398 | } |
| 399 | |
Dima Zavin | b428360 | 2009-01-26 16:36:57 -0800 | [diff] [blame] | 400 | boot_linux_from_flash(); |
| 401 | dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting " |
| 402 | "to fastboot mode.\n"); |
| 403 | |
| 404 | fastboot: |
Chandan Uddaraju | 2412050 | 2009-12-12 19:17:04 -0800 | [diff] [blame] | 405 | display_init(); |
| 406 | dprintf(INFO, "Diplay initialized\n"); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 407 | udc_init(&surf_udc_device); |
| 408 | |
| 409 | fastboot_register("boot", cmd_boot); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 410 | fastboot_register("erase:", cmd_erase); |
| 411 | fastboot_register("flash:", cmd_flash); |
| 412 | fastboot_register("continue", cmd_continue); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 413 | fastboot_publish("product", "swordfish"); |
| 414 | fastboot_publish("kernel", "lk"); |
| 415 | |
Brian Swetland | 2defe16 | 2009-08-18 14:35:59 -0700 | [diff] [blame] | 416 | fastboot_init((void*) SCRATCH_ADDR, 100 * 1024 * 1024); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 417 | udc_start(); |
| 418 | } |
| 419 | |
| 420 | APP_START(aboot) |
| 421 | .init = aboot_init, |
| 422 | APP_END |
| 423 | |