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