Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 5 | * Copyright (c) 2009-2010, 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> |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 44 | #include <dev/fbcon.h> |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 45 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 46 | #include "recovery.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 47 | #include "bootimg.h" |
| 48 | #include "fastboot.h" |
| 49 | |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 50 | #define EXPAND(NAME) #NAME |
| 51 | #define TARGET(NAME) EXPAND(NAME) |
Chandan Uddaraju | da91983 | 2009-11-17 01:06:11 -0800 | [diff] [blame] | 52 | #define DEFAULT_CMDLINE "mem=100M console=null"; |
Brian Swetland | 2defe16 | 2009-08-18 14:35:59 -0700 | [diff] [blame] | 53 | |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 54 | #ifdef MEMBASE |
| 55 | #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE)) |
| 56 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 57 | #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000 |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 58 | #endif |
| 59 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 60 | #define RECOVERY_MODE 0x77665502 |
| 61 | #define FASTBOOT_MODE 0x77665500 |
| 62 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 63 | static const char *emmc_cmdline = " androidboot.emmc=true"; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 64 | static const char *battchg_pause = " androidboot.battchg_pause=true"; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 65 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 66 | static struct udc_device surf_udc_device = { |
| 67 | .vendor_id = 0x18d1, |
Chandan Uddaraju | c53a1a1 | 2009-11-18 14:53:40 -0800 | [diff] [blame] | 68 | .product_id = 0xD00D, |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 69 | .version_id = 0x0100, |
| 70 | .manufacturer = "Google", |
| 71 | .product = "Android", |
| 72 | }; |
| 73 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 74 | struct atag_ptbl_entry |
| 75 | { |
| 76 | char name[16]; |
| 77 | unsigned offset; |
| 78 | unsigned size; |
| 79 | unsigned flags; |
| 80 | }; |
| 81 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 82 | void platform_uninit_timer(void); |
Chandan Uddaraju | c6860e1 | 2009-11-19 11:22:15 -0800 | [diff] [blame] | 83 | unsigned* target_atag_mem(unsigned* ptr); |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 84 | unsigned board_machtype(void); |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 85 | unsigned check_reboot_mode(void); |
David Ng | 3679bc5 | 2010-02-09 15:43:43 -0800 | [diff] [blame] | 86 | void *target_get_scratch_address(void); |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 87 | int target_is_emmc_boot(void); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 88 | void reboot_device(unsigned); |
Chandan Uddaraju | 7f5b901 | 2010-02-06 16:37:48 -0800 | [diff] [blame] | 89 | void target_battery_charging_enable(unsigned enable, unsigned disconnect); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 90 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 91 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 92 | static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn) |
| 93 | { |
| 94 | struct atag_ptbl_entry atag_ptn; |
| 95 | |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 96 | if (ptn->type == TYPE_MODEM_PARTITION) |
| 97 | return; |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 98 | memcpy(atag_ptn.name, ptn->name, 16); |
| 99 | atag_ptn.name[15] = '\0'; |
| 100 | atag_ptn.offset = ptn->start; |
| 101 | atag_ptn.size = ptn->length; |
| 102 | atag_ptn.flags = ptn->flags; |
| 103 | memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry)); |
| 104 | *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned); |
| 105 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 106 | |
| 107 | void boot_linux(void *kernel, unsigned *tags, |
| 108 | const char *cmdline, unsigned machtype, |
| 109 | void *ramdisk, unsigned ramdisk_size) |
| 110 | { |
| 111 | unsigned *ptr = tags; |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 112 | unsigned pcount = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 113 | void (*entry)(unsigned,unsigned,unsigned*) = kernel; |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 114 | struct ptable *ptable; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 115 | int cmdline_len = 0; |
| 116 | int have_cmdline = 0; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 117 | int pause_at_bootup = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 118 | |
| 119 | /* CORE */ |
| 120 | *ptr++ = 2; |
| 121 | *ptr++ = 0x54410001; |
| 122 | |
| 123 | if (ramdisk_size) { |
| 124 | *ptr++ = 4; |
| 125 | *ptr++ = 0x54420005; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 126 | *ptr++ = (unsigned)ramdisk; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 127 | *ptr++ = ramdisk_size; |
| 128 | } |
| 129 | |
Chandan Uddaraju | c6860e1 | 2009-11-19 11:22:15 -0800 | [diff] [blame] | 130 | ptr = target_atag_mem(ptr); |
| 131 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 132 | if (!target_is_emmc_boot()) { |
| 133 | /* Skip NAND partition ATAGS for eMMC boot */ |
| 134 | if ((ptable = flash_get_ptable()) && (ptable->count != 0)) { |
| 135 | int i; |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 136 | for(i=0; i < ptable->count; i++) { |
| 137 | struct ptentry *ptn; |
| 138 | ptn = ptable_get(ptable, i); |
| 139 | if (ptn->type == TYPE_APPS_PARTITION) |
| 140 | pcount++; |
| 141 | } |
| 142 | *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) / |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 143 | sizeof(unsigned))); |
| 144 | *ptr++ = 0x4d534d70; |
| 145 | for (i = 0; i < ptable->count; ++i) |
| 146 | ptentry_to_tag(&ptr, ptable_get(ptable, i)); |
| 147 | } |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 150 | if (cmdline && cmdline[0]) { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 151 | cmdline_len = strlen(cmdline); |
| 152 | have_cmdline = 1; |
| 153 | } |
| 154 | if (target_is_emmc_boot()) { |
| 155 | cmdline_len += strlen(emmc_cmdline); |
| 156 | } |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 157 | if (target_pause_for_battery_charge()) { |
| 158 | pause_at_bootup = 1; |
| 159 | cmdline_len += strlen(battchg_pause); |
| 160 | } |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 161 | if (cmdline_len > 0) { |
| 162 | const char *src; |
| 163 | char *dst; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 164 | unsigned n; |
| 165 | /* include terminating 0 and round up to a word multiple */ |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 166 | n = (cmdline_len + 4) & (~3); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 167 | *ptr++ = (n / 4) + 2; |
| 168 | *ptr++ = 0x54410009; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 169 | dst = (char *)ptr; |
| 170 | if (have_cmdline) { |
| 171 | src = cmdline; |
| 172 | while ((*dst++ = *src++)); |
| 173 | } |
| 174 | if (target_is_emmc_boot()) { |
| 175 | src = emmc_cmdline; |
| 176 | if (have_cmdline) --dst; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 177 | have_cmdline = 1; |
| 178 | while ((*dst++ = *src++)); |
| 179 | } |
| 180 | if (pause_at_bootup) { |
| 181 | src = battchg_pause; |
| 182 | if (have_cmdline) --dst; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 183 | while ((*dst++ = *src++)); |
| 184 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 185 | ptr += (n / 4); |
| 186 | } |
| 187 | |
| 188 | /* END */ |
| 189 | *ptr++ = 0; |
| 190 | *ptr++ = 0; |
| 191 | |
| 192 | dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n", |
| 193 | kernel, ramdisk, ramdisk_size); |
| 194 | if (cmdline) |
| 195 | dprintf(INFO, "cmdline: %s\n", cmdline); |
| 196 | |
| 197 | enter_critical_section(); |
| 198 | platform_uninit_timer(); |
| 199 | arch_disable_cache(UCACHE); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 200 | arch_disable_mmu(); |
| 201 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 202 | secondary_core((unsigned)kernel); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 203 | entry(0, machtype, tags); |
| 204 | |
| 205 | } |
| 206 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 207 | unsigned page_size = 0; |
| 208 | unsigned page_mask = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 209 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 210 | #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y))) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 211 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 212 | static unsigned char buf[4096]; //Equal to max-supported pagesize |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 213 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 214 | int boot_linux_from_mmc(void) |
| 215 | { |
| 216 | struct boot_img_hdr *hdr = (void*) buf; |
| 217 | struct boot_img_hdr *uhdr; |
| 218 | unsigned offset = 0; |
| 219 | unsigned long long ptn = 0; |
| 220 | unsigned n = 0; |
| 221 | const char *cmdline; |
| 222 | |
| 223 | uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 224 | if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 225 | dprintf(INFO, "Unified boot method!\n"); |
| 226 | hdr = uhdr; |
| 227 | goto unified_boot; |
| 228 | } |
| 229 | |
| 230 | ptn = mmc_ptn_offset("boot"); |
| 231 | if(ptn == 0) { |
| 232 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | if (mmc_read(ptn + offset, (unsigned int *)buf, page_size)) { |
| 237 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 238 | return -1; |
| 239 | } |
| 240 | offset += page_size; |
| 241 | |
| 242 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 243 | dprintf(CRITICAL, "ERROR: Invaled boot image header\n"); |
| 244 | return -1; |
| 245 | } |
| 246 | |
| 247 | if (hdr->page_size != page_size) { |
| 248 | dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size); |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 253 | if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) { |
| 254 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 255 | return -1; |
| 256 | } |
| 257 | offset += n; |
| 258 | |
| 259 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 260 | if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) { |
| 261 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 262 | return -1; |
| 263 | } |
| 264 | offset += n; |
| 265 | |
| 266 | unified_boot: |
| 267 | dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr, |
| 268 | hdr->kernel_size); |
| 269 | dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr, |
| 270 | hdr->ramdisk_size); |
| 271 | |
| 272 | if(hdr->cmdline[0]) { |
| 273 | cmdline = (char*) hdr->cmdline; |
| 274 | } else { |
| 275 | cmdline = DEFAULT_CMDLINE; |
| 276 | } |
| 277 | dprintf(INFO, "cmdline = '%s'\n", cmdline); |
| 278 | |
| 279 | dprintf(INFO, "\nBooting Linux\n"); |
| 280 | boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR, |
| 281 | (const char *)cmdline, board_machtype(), |
| 282 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 287 | int boot_linux_from_flash(void) |
| 288 | { |
| 289 | struct boot_img_hdr *hdr = (void*) buf; |
| 290 | unsigned n; |
| 291 | struct ptentry *ptn; |
| 292 | struct ptable *ptable; |
| 293 | unsigned offset = 0; |
| 294 | const char *cmdline; |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 295 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 296 | if (target_is_emmc_boot()) { |
| 297 | hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 298 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 299 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
| 300 | return -1; |
| 301 | } |
| 302 | goto continue_boot; |
| 303 | } |
| 304 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 305 | ptable = flash_get_ptable(); |
| 306 | if (ptable == NULL) { |
| 307 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 308 | return -1; |
| 309 | } |
| 310 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 311 | if(!boot_into_recovery) |
| 312 | { |
| 313 | ptn = ptable_find(ptable, "boot"); |
| 314 | if (ptn == NULL) { |
| 315 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 316 | return -1; |
| 317 | } |
| 318 | } |
| 319 | else |
| 320 | { |
| 321 | ptn = ptable_find(ptable, "recovery"); |
| 322 | if (ptn == NULL) { |
| 323 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 324 | return -1; |
| 325 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 328 | if (flash_read(ptn, offset, buf, page_size)) { |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 329 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 330 | return -1; |
| 331 | } |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 332 | offset += page_size; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 333 | |
| 334 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 335 | dprintf(CRITICAL, "ERROR: Invaled boot image heador\n"); |
| 336 | return -1; |
| 337 | } |
| 338 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 339 | if (hdr->page_size != page_size) { |
| 340 | dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size); |
| 341 | return -1; |
| 342 | } |
| 343 | |
| 344 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 345 | if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) { |
| 346 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 347 | return -1; |
| 348 | } |
| 349 | offset += n; |
| 350 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 351 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 352 | if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) { |
| 353 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 354 | return -1; |
| 355 | } |
| 356 | offset += n; |
| 357 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 358 | continue_boot: |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 359 | dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr, |
| 360 | hdr->kernel_size); |
| 361 | dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr, |
| 362 | hdr->ramdisk_size); |
| 363 | |
| 364 | if(hdr->cmdline[0]) { |
| 365 | cmdline = (char*) hdr->cmdline; |
| 366 | } else { |
| 367 | cmdline = DEFAULT_CMDLINE; |
| 368 | } |
| 369 | dprintf(INFO, "cmdline = '%s'\n", cmdline); |
| 370 | |
| 371 | /* TODO: create/pass atags to kernel */ |
| 372 | |
| 373 | dprintf(INFO, "\nBooting Linux\n"); |
| 374 | boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR, |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 375 | (const char *)cmdline, board_machtype(), |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 376 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 377 | |
| 378 | return 0; |
| 379 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 380 | |
| 381 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 382 | { |
| 383 | unsigned kernel_actual; |
| 384 | unsigned ramdisk_actual; |
| 385 | static struct boot_img_hdr hdr; |
| 386 | char *ptr = ((char*) data); |
| 387 | |
| 388 | if (sz < sizeof(hdr)) { |
| 389 | fastboot_fail("invalid bootimage header"); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | memcpy(&hdr, data, sizeof(hdr)); |
| 394 | |
| 395 | /* ensure commandline is terminated */ |
| 396 | hdr.cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 397 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 398 | kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask); |
| 399 | ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 400 | |
Chandan Uddaraju | 1f6030f | 2010-03-19 13:26:38 -0700 | [diff] [blame] | 401 | if (page_size + kernel_actual + ramdisk_actual < sz) { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 402 | fastboot_fail("incomplete bootimage"); |
| 403 | return; |
| 404 | } |
| 405 | |
Chandan Uddaraju | 1f6030f | 2010-03-19 13:26:38 -0700 | [diff] [blame] | 406 | memmove((void*) KERNEL_ADDR, ptr + page_size, hdr.kernel_size); |
| 407 | memmove((void*) RAMDISK_ADDR, ptr + page_size + kernel_actual, hdr.ramdisk_size); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 408 | |
| 409 | fastboot_okay(""); |
Chandan Uddaraju | 7f5b901 | 2010-02-06 16:37:48 -0800 | [diff] [blame] | 410 | target_battery_charging_enable(0, 1); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 411 | udc_stop(); |
| 412 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 413 | boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR, |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 414 | (const char*) hdr.cmdline, board_machtype(), |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 415 | (void*) RAMDISK_ADDR, hdr.ramdisk_size); |
| 416 | } |
| 417 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 418 | void cmd_erase(const char *arg, void *data, unsigned sz) |
| 419 | { |
| 420 | struct ptentry *ptn; |
| 421 | struct ptable *ptable; |
| 422 | |
| 423 | ptable = flash_get_ptable(); |
| 424 | if (ptable == NULL) { |
| 425 | fastboot_fail("partition table doesn't exist"); |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | ptn = ptable_find(ptable, arg); |
| 430 | if (ptn == NULL) { |
| 431 | fastboot_fail("unknown partition name"); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | if (flash_erase(ptn)) { |
| 436 | fastboot_fail("failed to erase partition"); |
| 437 | return; |
| 438 | } |
| 439 | fastboot_okay(""); |
| 440 | } |
| 441 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 442 | void cmd_flash_mmc(const char *arg, void *data, unsigned sz) |
| 443 | { |
| 444 | unsigned long long ptn = 0; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 445 | unsigned long long size = 0; |
| 446 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 447 | ptn = mmc_ptn_offset(arg); |
| 448 | if(ptn == 0) { |
| 449 | fastboot_fail("partition table doesn't exist"); |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) { |
| 454 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 455 | fastboot_fail("image is not a boot image"); |
| 456 | return; |
| 457 | } |
| 458 | } |
| 459 | |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 460 | size = mmc_ptn_size(arg); |
| 461 | if (ROUND_TO_PAGE(sz,511) > size) { |
| 462 | fastboot_fail("size too large"); |
| 463 | return; |
| 464 | } |
| 465 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 466 | if (mmc_write(ptn , sz, (unsigned int *)data)) { |
| 467 | fastboot_fail("flash write failure"); |
| 468 | return; |
| 469 | } |
| 470 | fastboot_okay(""); |
| 471 | return; |
| 472 | } |
| 473 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 474 | void cmd_flash(const char *arg, void *data, unsigned sz) |
| 475 | { |
| 476 | struct ptentry *ptn; |
| 477 | struct ptable *ptable; |
| 478 | unsigned extra = 0; |
| 479 | |
| 480 | ptable = flash_get_ptable(); |
| 481 | if (ptable == NULL) { |
| 482 | fastboot_fail("partition table doesn't exist"); |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | ptn = ptable_find(ptable, arg); |
| 487 | if (ptn == NULL) { |
| 488 | fastboot_fail("unknown partition name"); |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
| 493 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 494 | fastboot_fail("image is not a boot image"); |
| 495 | return; |
| 496 | } |
| 497 | } |
| 498 | |
Chandan Uddaraju | d6d4504 | 2010-02-24 21:12:45 -0800 | [diff] [blame] | 499 | if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata") |
| 500 | || !strcmp(ptn->name, "persist")) |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 501 | extra = ((page_size >> 9) * 16); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 502 | else |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 503 | sz = ROUND_TO_PAGE(sz, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 504 | |
| 505 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
| 506 | if (flash_write(ptn, extra, data, sz)) { |
| 507 | fastboot_fail("flash write failure"); |
| 508 | return; |
| 509 | } |
| 510 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 511 | fastboot_okay(""); |
| 512 | } |
| 513 | |
| 514 | void cmd_continue(const char *arg, void *data, unsigned sz) |
| 515 | { |
| 516 | fastboot_okay(""); |
Chandan Uddaraju | 7f5b901 | 2010-02-06 16:37:48 -0800 | [diff] [blame] | 517 | target_battery_charging_enable(0, 1); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 518 | udc_stop(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 519 | if (target_is_emmc_boot()) |
| 520 | { |
| 521 | boot_linux_from_mmc(); |
| 522 | } |
| 523 | else |
| 524 | { |
| 525 | boot_linux_from_flash(); |
| 526 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 529 | void cmd_reboot(const char *arg, void *data, unsigned sz) |
| 530 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 531 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 532 | fastboot_okay(""); |
| 533 | reboot_device(0); |
| 534 | } |
| 535 | |
| 536 | void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz) |
| 537 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 538 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 539 | fastboot_okay(""); |
| 540 | reboot_device(FASTBOOT_MODE); |
| 541 | } |
| 542 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 543 | void splash_screen () |
| 544 | { |
| 545 | struct ptentry *ptn; |
| 546 | struct ptable *ptable; |
| 547 | struct fbcon_config *fb_display = NULL; |
| 548 | |
| 549 | if (!target_is_emmc_boot()) |
| 550 | { |
| 551 | ptable = flash_get_ptable(); |
| 552 | if (ptable == NULL) { |
| 553 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 554 | return -1; |
| 555 | } |
| 556 | |
| 557 | ptn = ptable_find(ptable, "splash"); |
| 558 | if (ptn == NULL) { |
| 559 | dprintf(CRITICAL, "ERROR: No splash partition found\n"); |
| 560 | } else { |
| 561 | fb_display = fbcon_display(); |
| 562 | if (fb_display) { |
| 563 | if (flash_read(ptn, 0, fb_display->base, |
| 564 | (fb_display->width * fb_display->height * fb_display->bpp/8))) { |
| 565 | fbcon_clear(); |
| 566 | dprintf(CRITICAL, "ERROR: Cannot read splash image\n"); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 573 | void aboot_init(const struct app_descriptor *app) |
| 574 | { |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 575 | unsigned reboot_mode = 0; |
| 576 | unsigned disp_init = 0; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 577 | unsigned usb_init = 0; |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 578 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 579 | /* Setup page size information for nand/emmc reads */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 580 | if (target_is_emmc_boot()) |
| 581 | { |
| 582 | page_size = 2048; |
| 583 | page_mask = page_size - 1; |
| 584 | } |
| 585 | else |
| 586 | { |
| 587 | page_size = flash_page_size(); |
| 588 | page_mask = page_size - 1; |
| 589 | } |
| 590 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 591 | /* Display splash screen if enabled */ |
| 592 | #if DISPLAY_SPLASH_SCREEN |
| 593 | display_init(); |
| 594 | dprintf(INFO, "Diplay initialized\n"); |
| 595 | disp_init = 1; |
Chandan Uddaraju | 40b227d | 2010-08-03 19:25:41 -0700 | [diff] [blame] | 596 | diplay_image_on_screen(); |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 597 | #endif |
| 598 | |
| 599 | /* Check if we should do something other than booting up */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 600 | if (keys_get_state(KEY_HOME) != 0) |
| 601 | boot_into_recovery = 1; |
| 602 | if (keys_get_state(KEY_BACK) != 0) |
| 603 | goto fastboot; |
| 604 | if (keys_get_state(KEY_CLEAR) != 0) |
| 605 | goto fastboot; |
| 606 | |
| 607 | #if NO_KEYPAD_DRIVER |
| 608 | /* With no keypad implementation, check the status of USB connection. */ |
| 609 | /* If USB is connected then go into fastboot mode. */ |
| 610 | usb_init = 1; |
| 611 | udc_init(&surf_udc_device); |
| 612 | if (usb_cable_status()) |
| 613 | goto fastboot; |
| 614 | #endif |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 615 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 616 | if (target_is_emmc_boot()) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 617 | { |
| 618 | boot_linux_from_mmc(); |
| 619 | } |
| 620 | else |
| 621 | { |
| 622 | reboot_mode = check_reboot_mode(); |
| 623 | if (reboot_mode == RECOVERY_MODE) { |
| 624 | boot_into_recovery = 1; |
| 625 | } else if(reboot_mode == FASTBOOT_MODE) { |
| 626 | goto fastboot; |
| 627 | } |
| 628 | recovery_init(); |
| 629 | boot_linux_from_flash(); |
| 630 | } |
Dima Zavin | b428360 | 2009-01-26 16:36:57 -0800 | [diff] [blame] | 631 | dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting " |
| 632 | "to fastboot mode.\n"); |
| 633 | |
| 634 | fastboot: |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 635 | |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 636 | if(!usb_init) |
| 637 | udc_init(&surf_udc_device); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 638 | |
| 639 | fastboot_register("boot", cmd_boot); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 640 | fastboot_register("erase:", cmd_erase); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 641 | if (target_is_emmc_boot()) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 642 | { |
| 643 | fastboot_register("flash:", cmd_flash_mmc); |
| 644 | } |
| 645 | else |
| 646 | { |
| 647 | fastboot_register("flash:", cmd_flash); |
| 648 | } |
| 649 | |
| 650 | fastboot_register("continue", cmd_continue); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 651 | fastboot_register("reboot", cmd_reboot); |
| 652 | fastboot_register("reboot-bootloader", cmd_reboot_bootloader); |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 653 | fastboot_publish("product", TARGET(BOARD)); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 654 | fastboot_publish("kernel", "lk"); |
| 655 | |
Ajay Dudani | d43419e | 2010-02-10 13:17:39 -0800 | [diff] [blame] | 656 | fastboot_init(target_get_scratch_address(), 120 * 1024 * 1024); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 657 | udc_start(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 658 | target_battery_charging_enable(1, 0); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | APP_START(aboot) |
| 662 | .init = aboot_init, |
| 663 | APP_END |
| 664 | |