Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 5 | * Copyright (c) 2009-2011, 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> |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 45 | #include <baseband.h> |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 46 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 47 | #include "recovery.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 48 | #include "bootimg.h" |
| 49 | #include "fastboot.h" |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame^] | 50 | #include "sparse_format.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 51 | |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 52 | #define EXPAND(NAME) #NAME |
| 53 | #define TARGET(NAME) EXPAND(NAME) |
Chandan Uddaraju | da91983 | 2009-11-17 01:06:11 -0800 | [diff] [blame] | 54 | #define DEFAULT_CMDLINE "mem=100M console=null"; |
Brian Swetland | 2defe16 | 2009-08-18 14:35:59 -0700 | [diff] [blame] | 55 | |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 56 | #ifdef MEMBASE |
| 57 | #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE)) |
| 58 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 59 | #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000 |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 60 | #endif |
| 61 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 62 | #define RECOVERY_MODE 0x77665502 |
| 63 | #define FASTBOOT_MODE 0x77665500 |
| 64 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 65 | static const char *emmc_cmdline = " androidboot.emmc=true"; |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 66 | static const char *usb_sn_cmdline = " androidboot.serialno="; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 67 | static const char *battchg_pause = " androidboot.battchg_pause=true"; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 68 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 69 | static const char *baseband_apq = " androidboot.baseband=apq"; |
| 70 | static const char *baseband_msm = " androidboot.baseband=msm"; |
| 71 | static const char *baseband_csfb = " androidboot.baseband=csfb"; |
| 72 | static const char *baseband_svlte2a = " androidboot.baseband=svlte2a"; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 73 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 74 | static struct udc_device surf_udc_device = { |
| 75 | .vendor_id = 0x18d1, |
Chandan Uddaraju | c53a1a1 | 2009-11-18 14:53:40 -0800 | [diff] [blame] | 76 | .product_id = 0xD00D, |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 77 | .version_id = 0x0100, |
| 78 | .manufacturer = "Google", |
| 79 | .product = "Android", |
| 80 | }; |
| 81 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 82 | struct atag_ptbl_entry |
| 83 | { |
| 84 | char name[16]; |
| 85 | unsigned offset; |
| 86 | unsigned size; |
| 87 | unsigned flags; |
| 88 | }; |
| 89 | |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 90 | char sn_buf[13]; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 91 | void platform_uninit_timer(void); |
Chandan Uddaraju | c6860e1 | 2009-11-19 11:22:15 -0800 | [diff] [blame] | 92 | unsigned* target_atag_mem(unsigned* ptr); |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 93 | unsigned board_machtype(void); |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 94 | unsigned check_reboot_mode(void); |
David Ng | 3679bc5 | 2010-02-09 15:43:43 -0800 | [diff] [blame] | 95 | void *target_get_scratch_address(void); |
Vivek Mehta | 5f1c9d4 | 2011-04-01 20:11:59 -0700 | [diff] [blame] | 96 | unsigned target_get_max_flash_size(void); |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 97 | int target_is_emmc_boot(void); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 98 | void reboot_device(unsigned); |
Chandan Uddaraju | 7f5b901 | 2010-02-06 16:37:48 -0800 | [diff] [blame] | 99 | void target_battery_charging_enable(unsigned enable, unsigned disconnect); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 100 | unsigned int mmc_write (unsigned long long data_addr, |
| 101 | unsigned int data_len, unsigned int* in); |
Shashank Mittal | 920798e | 2010-10-04 17:17:37 -0700 | [diff] [blame] | 102 | unsigned long long mmc_ptn_offset (unsigned char * name); |
| 103 | unsigned long long mmc_ptn_size (unsigned char * name); |
Shashank Mittal | c648e71 | 2010-10-06 18:37:42 -0700 | [diff] [blame] | 104 | void display_shutdown(void); |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 105 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 106 | static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn) |
| 107 | { |
| 108 | struct atag_ptbl_entry atag_ptn; |
| 109 | |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 110 | if (ptn->type == TYPE_MODEM_PARTITION) |
| 111 | return; |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 112 | memcpy(atag_ptn.name, ptn->name, 16); |
| 113 | atag_ptn.name[15] = '\0'; |
| 114 | atag_ptn.offset = ptn->start; |
| 115 | atag_ptn.size = ptn->length; |
| 116 | atag_ptn.flags = ptn->flags; |
| 117 | memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry)); |
| 118 | *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned); |
| 119 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 120 | |
| 121 | void boot_linux(void *kernel, unsigned *tags, |
| 122 | const char *cmdline, unsigned machtype, |
| 123 | void *ramdisk, unsigned ramdisk_size) |
| 124 | { |
| 125 | unsigned *ptr = tags; |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 126 | unsigned pcount = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 127 | void (*entry)(unsigned,unsigned,unsigned*) = kernel; |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 128 | struct ptable *ptable; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 129 | int cmdline_len = 0; |
| 130 | int have_cmdline = 0; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 131 | int pause_at_bootup = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 132 | |
| 133 | /* CORE */ |
| 134 | *ptr++ = 2; |
| 135 | *ptr++ = 0x54410001; |
| 136 | |
| 137 | if (ramdisk_size) { |
| 138 | *ptr++ = 4; |
| 139 | *ptr++ = 0x54420005; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 140 | *ptr++ = (unsigned)ramdisk; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 141 | *ptr++ = ramdisk_size; |
| 142 | } |
| 143 | |
Chandan Uddaraju | c6860e1 | 2009-11-19 11:22:15 -0800 | [diff] [blame] | 144 | ptr = target_atag_mem(ptr); |
| 145 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 146 | if (!target_is_emmc_boot()) { |
| 147 | /* Skip NAND partition ATAGS for eMMC boot */ |
| 148 | if ((ptable = flash_get_ptable()) && (ptable->count != 0)) { |
| 149 | int i; |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 150 | for(i=0; i < ptable->count; i++) { |
| 151 | struct ptentry *ptn; |
| 152 | ptn = ptable_get(ptable, i); |
| 153 | if (ptn->type == TYPE_APPS_PARTITION) |
| 154 | pcount++; |
| 155 | } |
| 156 | *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) / |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 157 | sizeof(unsigned))); |
| 158 | *ptr++ = 0x4d534d70; |
| 159 | for (i = 0; i < ptable->count; ++i) |
| 160 | ptentry_to_tag(&ptr, ptable_get(ptable, i)); |
| 161 | } |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 164 | if (cmdline && cmdline[0]) { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 165 | cmdline_len = strlen(cmdline); |
| 166 | have_cmdline = 1; |
| 167 | } |
| 168 | if (target_is_emmc_boot()) { |
| 169 | cmdline_len += strlen(emmc_cmdline); |
| 170 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 171 | |
| 172 | cmdline_len += strlen(usb_sn_cmdline); |
| 173 | cmdline_len += strlen(sn_buf); |
| 174 | |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 175 | if (target_pause_for_battery_charge()) { |
| 176 | pause_at_bootup = 1; |
| 177 | cmdline_len += strlen(battchg_pause); |
| 178 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 179 | |
| 180 | /* Determine correct androidboot.baseband to use */ |
| 181 | switch(target_baseband()) |
| 182 | { |
| 183 | case BASEBAND_APQ: |
| 184 | cmdline_len += strlen(baseband_apq); |
| 185 | break; |
| 186 | |
| 187 | case BASEBAND_MSM: |
| 188 | cmdline_len += strlen(baseband_msm); |
| 189 | break; |
| 190 | |
| 191 | case BASEBAND_CSFB: |
| 192 | cmdline_len += strlen(baseband_csfb); |
| 193 | break; |
| 194 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 195 | case BASEBAND_SVLTE2A: |
| 196 | cmdline_len += strlen(baseband_svlte2a); |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 197 | break; |
| 198 | } |
| 199 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 200 | if (cmdline_len > 0) { |
| 201 | const char *src; |
| 202 | char *dst; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 203 | unsigned n; |
| 204 | /* include terminating 0 and round up to a word multiple */ |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 205 | n = (cmdline_len + 4) & (~3); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 206 | *ptr++ = (n / 4) + 2; |
| 207 | *ptr++ = 0x54410009; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 208 | dst = (char *)ptr; |
| 209 | if (have_cmdline) { |
| 210 | src = cmdline; |
| 211 | while ((*dst++ = *src++)); |
| 212 | } |
| 213 | if (target_is_emmc_boot()) { |
| 214 | src = emmc_cmdline; |
| 215 | if (have_cmdline) --dst; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 216 | have_cmdline = 1; |
| 217 | while ((*dst++ = *src++)); |
| 218 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 219 | |
| 220 | src = usb_sn_cmdline; |
| 221 | if (have_cmdline) --dst; |
| 222 | have_cmdline = 1; |
| 223 | while ((*dst++ = *src++)); |
| 224 | src = sn_buf; |
| 225 | if (have_cmdline) --dst; |
| 226 | have_cmdline = 1; |
| 227 | while ((*dst++ = *src++)); |
| 228 | |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 229 | if (pause_at_bootup) { |
| 230 | src = battchg_pause; |
| 231 | if (have_cmdline) --dst; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 232 | while ((*dst++ = *src++)); |
| 233 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 234 | |
| 235 | switch(target_baseband()) |
| 236 | { |
| 237 | case BASEBAND_APQ: |
| 238 | src = baseband_apq; |
| 239 | if (have_cmdline) --dst; |
| 240 | while ((*dst++ = *src++)); |
| 241 | break; |
| 242 | |
| 243 | case BASEBAND_MSM: |
| 244 | src = baseband_msm; |
| 245 | if (have_cmdline) --dst; |
| 246 | while ((*dst++ = *src++)); |
| 247 | break; |
| 248 | |
| 249 | case BASEBAND_CSFB: |
| 250 | src = baseband_csfb; |
| 251 | if (have_cmdline) --dst; |
| 252 | while ((*dst++ = *src++)); |
| 253 | break; |
| 254 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 255 | case BASEBAND_SVLTE2A: |
| 256 | src = baseband_svlte2a; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 257 | if (have_cmdline) --dst; |
| 258 | while ((*dst++ = *src++)); |
| 259 | break; |
| 260 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 261 | ptr += (n / 4); |
| 262 | } |
| 263 | |
| 264 | /* END */ |
| 265 | *ptr++ = 0; |
| 266 | *ptr++ = 0; |
| 267 | |
| 268 | dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n", |
| 269 | kernel, ramdisk, ramdisk_size); |
| 270 | if (cmdline) |
| 271 | dprintf(INFO, "cmdline: %s\n", cmdline); |
| 272 | |
| 273 | enter_critical_section(); |
| 274 | platform_uninit_timer(); |
| 275 | arch_disable_cache(UCACHE); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 276 | arch_disable_mmu(); |
Shashank Mittal | c648e71 | 2010-10-06 18:37:42 -0700 | [diff] [blame] | 277 | #if DISPLAY_SPLASH_SCREEN |
| 278 | display_shutdown(); |
| 279 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 280 | entry(0, machtype, tags); |
| 281 | |
| 282 | } |
| 283 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 284 | unsigned page_size = 0; |
| 285 | unsigned page_mask = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 286 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 287 | #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y))) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 288 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 289 | static unsigned char buf[4096]; //Equal to max-supported pagesize |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 290 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 291 | int boot_linux_from_mmc(void) |
| 292 | { |
| 293 | struct boot_img_hdr *hdr = (void*) buf; |
| 294 | struct boot_img_hdr *uhdr; |
| 295 | unsigned offset = 0; |
| 296 | unsigned long long ptn = 0; |
| 297 | unsigned n = 0; |
| 298 | const char *cmdline; |
| 299 | |
| 300 | uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 301 | if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 302 | dprintf(INFO, "Unified boot method!\n"); |
| 303 | hdr = uhdr; |
| 304 | goto unified_boot; |
| 305 | } |
Shashank Mittal | 85b91f6 | 2010-10-30 10:12:38 -0700 | [diff] [blame] | 306 | if(!boot_into_recovery) |
| 307 | { |
| 308 | ptn = mmc_ptn_offset("boot"); |
| 309 | if(ptn == 0) { |
| 310 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 311 | return -1; |
| 312 | } |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | ptn = mmc_ptn_offset("recovery"); |
| 317 | if(ptn == 0) { |
| 318 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 319 | return -1; |
| 320 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | if (mmc_read(ptn + offset, (unsigned int *)buf, page_size)) { |
| 324 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 325 | return -1; |
| 326 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 327 | |
| 328 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 329 | dprintf(CRITICAL, "ERROR: Invaled boot image header\n"); |
| 330 | return -1; |
| 331 | } |
| 332 | |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 333 | if (hdr->page_size && (hdr->page_size != page_size)) { |
| 334 | page_size = hdr->page_size; |
| 335 | page_mask = page_size - 1; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 336 | } |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 337 | offset += page_size; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 338 | |
| 339 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 340 | if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) { |
| 341 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 342 | return -1; |
| 343 | } |
| 344 | offset += n; |
| 345 | |
| 346 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Subbaraman Narayanamurthy | 958fa24 | 2011-01-27 17:42:38 -0800 | [diff] [blame] | 347 | if(n != 0) |
| 348 | { |
| 349 | if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) { |
| 350 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 351 | return -1; |
| 352 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 353 | } |
| 354 | offset += n; |
| 355 | |
| 356 | unified_boot: |
| 357 | dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr, |
| 358 | hdr->kernel_size); |
| 359 | dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr, |
| 360 | hdr->ramdisk_size); |
| 361 | |
| 362 | if(hdr->cmdline[0]) { |
| 363 | cmdline = (char*) hdr->cmdline; |
| 364 | } else { |
| 365 | cmdline = DEFAULT_CMDLINE; |
| 366 | } |
| 367 | dprintf(INFO, "cmdline = '%s'\n", cmdline); |
| 368 | |
| 369 | dprintf(INFO, "\nBooting Linux\n"); |
| 370 | boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR, |
| 371 | (const char *)cmdline, board_machtype(), |
| 372 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 377 | int boot_linux_from_flash(void) |
| 378 | { |
| 379 | struct boot_img_hdr *hdr = (void*) buf; |
| 380 | unsigned n; |
| 381 | struct ptentry *ptn; |
| 382 | struct ptable *ptable; |
| 383 | unsigned offset = 0; |
| 384 | const char *cmdline; |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 385 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 386 | if (target_is_emmc_boot()) { |
| 387 | hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 388 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 389 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
| 390 | return -1; |
| 391 | } |
| 392 | goto continue_boot; |
| 393 | } |
| 394 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 395 | ptable = flash_get_ptable(); |
| 396 | if (ptable == NULL) { |
| 397 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 398 | return -1; |
| 399 | } |
| 400 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 401 | if(!boot_into_recovery) |
| 402 | { |
| 403 | ptn = ptable_find(ptable, "boot"); |
| 404 | if (ptn == NULL) { |
| 405 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 406 | return -1; |
| 407 | } |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | ptn = ptable_find(ptable, "recovery"); |
| 412 | if (ptn == NULL) { |
| 413 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 414 | return -1; |
| 415 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 418 | if (flash_read(ptn, offset, buf, page_size)) { |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 419 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 420 | return -1; |
| 421 | } |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 422 | offset += page_size; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 423 | |
| 424 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 425 | dprintf(CRITICAL, "ERROR: Invaled boot image heador\n"); |
| 426 | return -1; |
| 427 | } |
| 428 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 429 | if (hdr->page_size != page_size) { |
| 430 | dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size); |
| 431 | return -1; |
| 432 | } |
| 433 | |
| 434 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 435 | if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) { |
| 436 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 437 | return -1; |
| 438 | } |
| 439 | offset += n; |
| 440 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 441 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 442 | if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) { |
| 443 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 444 | return -1; |
| 445 | } |
| 446 | offset += n; |
| 447 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 448 | continue_boot: |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 449 | dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr, |
| 450 | hdr->kernel_size); |
| 451 | dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr, |
| 452 | hdr->ramdisk_size); |
| 453 | |
| 454 | if(hdr->cmdline[0]) { |
| 455 | cmdline = (char*) hdr->cmdline; |
| 456 | } else { |
| 457 | cmdline = DEFAULT_CMDLINE; |
| 458 | } |
| 459 | dprintf(INFO, "cmdline = '%s'\n", cmdline); |
| 460 | |
| 461 | /* TODO: create/pass atags to kernel */ |
| 462 | |
| 463 | dprintf(INFO, "\nBooting Linux\n"); |
| 464 | boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR, |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 465 | (const char *)cmdline, board_machtype(), |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 466 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 467 | |
| 468 | return 0; |
| 469 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 470 | |
| 471 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 472 | { |
| 473 | unsigned kernel_actual; |
| 474 | unsigned ramdisk_actual; |
| 475 | static struct boot_img_hdr hdr; |
| 476 | char *ptr = ((char*) data); |
| 477 | |
| 478 | if (sz < sizeof(hdr)) { |
| 479 | fastboot_fail("invalid bootimage header"); |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | memcpy(&hdr, data, sizeof(hdr)); |
| 484 | |
| 485 | /* ensure commandline is terminated */ |
| 486 | hdr.cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 487 | |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 488 | if(target_is_emmc_boot() && hdr.page_size) { |
| 489 | page_size = hdr.page_size; |
| 490 | page_mask = page_size - 1; |
| 491 | } |
| 492 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 493 | kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask); |
| 494 | ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 495 | |
Chandan Uddaraju | 1f6030f | 2010-03-19 13:26:38 -0700 | [diff] [blame] | 496 | if (page_size + kernel_actual + ramdisk_actual < sz) { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 497 | fastboot_fail("incomplete bootimage"); |
| 498 | return; |
| 499 | } |
| 500 | |
Chandan Uddaraju | 1f6030f | 2010-03-19 13:26:38 -0700 | [diff] [blame] | 501 | memmove((void*) KERNEL_ADDR, ptr + page_size, hdr.kernel_size); |
| 502 | memmove((void*) RAMDISK_ADDR, ptr + page_size + kernel_actual, hdr.ramdisk_size); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 503 | |
| 504 | fastboot_okay(""); |
Chandan Uddaraju | 7f5b901 | 2010-02-06 16:37:48 -0800 | [diff] [blame] | 505 | target_battery_charging_enable(0, 1); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 506 | udc_stop(); |
| 507 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 508 | boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR, |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 509 | (const char*) hdr.cmdline, board_machtype(), |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 510 | (void*) RAMDISK_ADDR, hdr.ramdisk_size); |
| 511 | } |
| 512 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 513 | void cmd_erase(const char *arg, void *data, unsigned sz) |
| 514 | { |
| 515 | struct ptentry *ptn; |
| 516 | struct ptable *ptable; |
| 517 | |
| 518 | ptable = flash_get_ptable(); |
| 519 | if (ptable == NULL) { |
| 520 | fastboot_fail("partition table doesn't exist"); |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | ptn = ptable_find(ptable, arg); |
| 525 | if (ptn == NULL) { |
| 526 | fastboot_fail("unknown partition name"); |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | if (flash_erase(ptn)) { |
| 531 | fastboot_fail("failed to erase partition"); |
| 532 | return; |
| 533 | } |
| 534 | fastboot_okay(""); |
| 535 | } |
| 536 | |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 537 | |
| 538 | void cmd_erase_mmc(const char *arg, void *data, unsigned sz) |
| 539 | { |
| 540 | unsigned long long ptn = 0; |
| 541 | unsigned int out[512] = {0}; |
| 542 | |
| 543 | ptn = mmc_ptn_offset(arg); |
| 544 | if(ptn == 0) { |
| 545 | fastboot_fail("partition table doesn't exist"); |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | |
| 550 | /* Simple inefficient version of erase. Just writing |
| 551 | 0 in first block */ |
| 552 | if (mmc_write(ptn , 512, (unsigned int *)out)) { |
| 553 | fastboot_fail("failed to erase partition"); |
| 554 | return; |
| 555 | } |
| 556 | fastboot_okay(""); |
| 557 | } |
| 558 | |
| 559 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame^] | 560 | void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz) |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 561 | { |
| 562 | unsigned long long ptn = 0; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 563 | unsigned long long size = 0; |
| 564 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 565 | ptn = mmc_ptn_offset(arg); |
| 566 | if(ptn == 0) { |
| 567 | fastboot_fail("partition table doesn't exist"); |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) { |
| 572 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 573 | fastboot_fail("image is not a boot image"); |
| 574 | return; |
| 575 | } |
| 576 | } |
| 577 | |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 578 | size = mmc_ptn_size(arg); |
| 579 | if (ROUND_TO_PAGE(sz,511) > size) { |
| 580 | fastboot_fail("size too large"); |
| 581 | return; |
| 582 | } |
| 583 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 584 | if (mmc_write(ptn , sz, (unsigned int *)data)) { |
| 585 | fastboot_fail("flash write failure"); |
| 586 | return; |
| 587 | } |
| 588 | fastboot_okay(""); |
| 589 | return; |
| 590 | } |
| 591 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame^] | 592 | void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz) |
| 593 | { |
| 594 | unsigned int chunk; |
| 595 | unsigned int chunk_data_sz; |
| 596 | sparse_header_t *sparse_header; |
| 597 | chunk_header_t *chunk_header; |
| 598 | uint32_t crc32 = 0; |
| 599 | uint32_t total_blocks = 0; |
| 600 | unsigned long long ptn = 0; |
| 601 | unsigned long long size = 0; |
| 602 | |
| 603 | ptn = mmc_ptn_offset(arg); |
| 604 | if(ptn == 0) { |
| 605 | fastboot_fail("partition table doesn't exist"); |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | /* Read and skip over sparse image header */ |
| 610 | sparse_header = (sparse_header_t *) data; |
| 611 | data += sparse_header->file_hdr_sz; |
| 612 | if(sparse_header->file_hdr_sz > sizeof(sparse_header_t)) |
| 613 | { |
| 614 | /* Skip the remaining bytes in a header that is longer than |
| 615 | * we expected. |
| 616 | */ |
| 617 | data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t)); |
| 618 | } |
| 619 | |
| 620 | dprintf (INFO, "=== Sparse Image Header ===\n"); |
| 621 | dprintf (INFO, "magic: 0x%x\n", sparse_header->magic); |
| 622 | dprintf (INFO, "major_version: 0x%x\n", sparse_header->major_version); |
| 623 | dprintf (INFO, "minor_version: 0x%x\n", sparse_header->minor_version); |
| 624 | dprintf (INFO, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz); |
| 625 | dprintf (INFO, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz); |
| 626 | dprintf (INFO, "blk_sz: %d\n", sparse_header->blk_sz); |
| 627 | dprintf (INFO, "total_blks: %d\n", sparse_header->total_blks); |
| 628 | dprintf (INFO, "total_chunks: %d\n", sparse_header->total_chunks); |
| 629 | |
| 630 | /* Start processing chunks */ |
| 631 | for (chunk=0; chunk<sparse_header->total_chunks; chunk++) |
| 632 | { |
| 633 | /* Read and skip over chunk header */ |
| 634 | chunk_header = (chunk_header_t *) data; |
| 635 | data += sizeof(chunk_header_t); |
| 636 | |
| 637 | dprintf (SPEW, "=== Chunk Header ===\n"); |
| 638 | dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type); |
| 639 | dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz); |
| 640 | dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz); |
| 641 | |
| 642 | if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) |
| 643 | { |
| 644 | /* Skip the remaining bytes in a header that is longer than |
| 645 | * we expected. |
| 646 | */ |
| 647 | data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t)); |
| 648 | } |
| 649 | |
| 650 | chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz; |
| 651 | switch (chunk_header->chunk_type) |
| 652 | { |
| 653 | case CHUNK_TYPE_RAW: |
| 654 | if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz + |
| 655 | chunk_data_sz)) |
| 656 | { |
| 657 | fastboot_fail("Bogus chunk size for chunk type Raw"); |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | if(mmc_write(ptn + (total_blocks*sparse_header->blk_sz), |
| 662 | chunk_data_sz, |
| 663 | (unsigned int*)data)) |
| 664 | { |
| 665 | fastboot_fail("flash write failure"); |
| 666 | return; |
| 667 | } |
| 668 | total_blocks += chunk_header->chunk_sz; |
| 669 | data += chunk_data_sz; |
| 670 | break; |
| 671 | |
| 672 | case CHUNK_TYPE_DONT_CARE: |
| 673 | case CHUNK_TYPE_CRC: |
| 674 | if(chunk_header->total_sz != sparse_header->chunk_hdr_sz) |
| 675 | { |
| 676 | fastboot_fail("Bogus chunk size for chunk type Dont Care"); |
| 677 | return; |
| 678 | } |
| 679 | total_blocks += chunk_header->chunk_sz; |
| 680 | data += chunk_data_sz; |
| 681 | break; |
| 682 | |
| 683 | fastboot_fail("Unknown chunk type"); |
| 684 | return; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n", |
| 689 | total_blocks, sparse_header->total_blks); |
| 690 | |
| 691 | fastboot_okay(""); |
| 692 | return; |
| 693 | } |
| 694 | |
| 695 | void cmd_flash_mmc(const char *arg, void *data, unsigned sz) |
| 696 | { |
| 697 | sparse_header_t *sparse_header; |
| 698 | sparse_header = (sparse_header_t *) data; |
| 699 | |
| 700 | if (sparse_header->magic != SPARSE_HEADER_MAGIC) |
| 701 | cmd_flash_mmc_img(arg, data, sz); |
| 702 | else |
| 703 | cmd_flash_mmc_sparse_img(arg, data, sz); |
| 704 | |
| 705 | return; |
| 706 | } |
| 707 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 708 | void cmd_flash(const char *arg, void *data, unsigned sz) |
| 709 | { |
| 710 | struct ptentry *ptn; |
| 711 | struct ptable *ptable; |
| 712 | unsigned extra = 0; |
| 713 | |
| 714 | ptable = flash_get_ptable(); |
| 715 | if (ptable == NULL) { |
| 716 | fastboot_fail("partition table doesn't exist"); |
| 717 | return; |
| 718 | } |
| 719 | |
| 720 | ptn = ptable_find(ptable, arg); |
| 721 | if (ptn == NULL) { |
| 722 | fastboot_fail("unknown partition name"); |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
| 727 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 728 | fastboot_fail("image is not a boot image"); |
| 729 | return; |
| 730 | } |
| 731 | } |
| 732 | |
Chandan Uddaraju | d6d4504 | 2010-02-24 21:12:45 -0800 | [diff] [blame] | 733 | if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata") |
| 734 | || !strcmp(ptn->name, "persist")) |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 735 | extra = ((page_size >> 9) * 16); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 736 | else |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 737 | sz = ROUND_TO_PAGE(sz, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 738 | |
| 739 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
| 740 | if (flash_write(ptn, extra, data, sz)) { |
| 741 | fastboot_fail("flash write failure"); |
| 742 | return; |
| 743 | } |
| 744 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 745 | fastboot_okay(""); |
| 746 | } |
| 747 | |
| 748 | void cmd_continue(const char *arg, void *data, unsigned sz) |
| 749 | { |
| 750 | fastboot_okay(""); |
Chandan Uddaraju | 7f5b901 | 2010-02-06 16:37:48 -0800 | [diff] [blame] | 751 | target_battery_charging_enable(0, 1); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 752 | udc_stop(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 753 | if (target_is_emmc_boot()) |
| 754 | { |
| 755 | boot_linux_from_mmc(); |
| 756 | } |
| 757 | else |
| 758 | { |
| 759 | boot_linux_from_flash(); |
| 760 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 763 | void cmd_reboot(const char *arg, void *data, unsigned sz) |
| 764 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 765 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 766 | fastboot_okay(""); |
| 767 | reboot_device(0); |
| 768 | } |
| 769 | |
| 770 | void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz) |
| 771 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 772 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 773 | fastboot_okay(""); |
| 774 | reboot_device(FASTBOOT_MODE); |
| 775 | } |
| 776 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 777 | void splash_screen () |
| 778 | { |
| 779 | struct ptentry *ptn; |
| 780 | struct ptable *ptable; |
| 781 | struct fbcon_config *fb_display = NULL; |
| 782 | |
| 783 | if (!target_is_emmc_boot()) |
| 784 | { |
| 785 | ptable = flash_get_ptable(); |
| 786 | if (ptable == NULL) { |
| 787 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 788 | return -1; |
| 789 | } |
| 790 | |
| 791 | ptn = ptable_find(ptable, "splash"); |
| 792 | if (ptn == NULL) { |
| 793 | dprintf(CRITICAL, "ERROR: No splash partition found\n"); |
| 794 | } else { |
| 795 | fb_display = fbcon_display(); |
| 796 | if (fb_display) { |
| 797 | if (flash_read(ptn, 0, fb_display->base, |
| 798 | (fb_display->width * fb_display->height * fb_display->bpp/8))) { |
| 799 | fbcon_clear(); |
| 800 | dprintf(CRITICAL, "ERROR: Cannot read splash image\n"); |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 807 | void aboot_init(const struct app_descriptor *app) |
| 808 | { |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 809 | unsigned reboot_mode = 0; |
| 810 | unsigned disp_init = 0; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 811 | unsigned usb_init = 0; |
Vivek Mehta | 5f1c9d4 | 2011-04-01 20:11:59 -0700 | [diff] [blame] | 812 | unsigned sz = 0; |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 813 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 814 | /* Setup page size information for nand/emmc reads */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 815 | if (target_is_emmc_boot()) |
| 816 | { |
| 817 | page_size = 2048; |
| 818 | page_mask = page_size - 1; |
| 819 | } |
| 820 | else |
| 821 | { |
| 822 | page_size = flash_page_size(); |
| 823 | page_mask = page_size - 1; |
| 824 | } |
| 825 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 826 | /* Display splash screen if enabled */ |
| 827 | #if DISPLAY_SPLASH_SCREEN |
| 828 | display_init(); |
| 829 | dprintf(INFO, "Diplay initialized\n"); |
| 830 | disp_init = 1; |
Chandan Uddaraju | 40b227d | 2010-08-03 19:25:41 -0700 | [diff] [blame] | 831 | diplay_image_on_screen(); |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 832 | #endif |
| 833 | |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 834 | target_serialno(sn_buf); |
| 835 | dprintf(INFO,"sn %s\n",sn_buf); |
| 836 | surf_udc_device.serialno = sn_buf; |
| 837 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 838 | /* Check if we should do something other than booting up */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 839 | if (keys_get_state(KEY_HOME) != 0) |
| 840 | boot_into_recovery = 1; |
Wentao Xu | 153902c | 2010-12-20 16:20:52 -0500 | [diff] [blame] | 841 | if (keys_get_state(KEY_VOLUMEUP) != 0) |
| 842 | boot_into_recovery = 1; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 843 | if (keys_get_state(KEY_BACK) != 0) |
| 844 | goto fastboot; |
Wentao Xu | 153902c | 2010-12-20 16:20:52 -0500 | [diff] [blame] | 845 | if (keys_get_state(KEY_VOLUMEDOWN) != 0) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 846 | goto fastboot; |
| 847 | |
| 848 | #if NO_KEYPAD_DRIVER |
| 849 | /* With no keypad implementation, check the status of USB connection. */ |
| 850 | /* If USB is connected then go into fastboot mode. */ |
| 851 | usb_init = 1; |
| 852 | udc_init(&surf_udc_device); |
| 853 | if (usb_cable_status()) |
| 854 | goto fastboot; |
| 855 | #endif |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 856 | |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 857 | reboot_mode = check_reboot_mode(); |
| 858 | if (reboot_mode == RECOVERY_MODE) { |
| 859 | boot_into_recovery = 1; |
| 860 | } else if(reboot_mode == FASTBOOT_MODE) { |
| 861 | goto fastboot; |
| 862 | } |
| 863 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 864 | if (target_is_emmc_boot()) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 865 | { |
| 866 | boot_linux_from_mmc(); |
| 867 | } |
| 868 | else |
| 869 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 870 | recovery_init(); |
| 871 | boot_linux_from_flash(); |
| 872 | } |
Dima Zavin | b428360 | 2009-01-26 16:36:57 -0800 | [diff] [blame] | 873 | dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting " |
| 874 | "to fastboot mode.\n"); |
| 875 | |
| 876 | fastboot: |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 877 | |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 878 | if(!usb_init) |
| 879 | udc_init(&surf_udc_device); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 880 | |
| 881 | fastboot_register("boot", cmd_boot); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 882 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 883 | if (target_is_emmc_boot()) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 884 | { |
| 885 | fastboot_register("flash:", cmd_flash_mmc); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 886 | fastboot_register("erase:", cmd_erase_mmc); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 887 | } |
| 888 | else |
| 889 | { |
| 890 | fastboot_register("flash:", cmd_flash); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 891 | fastboot_register("erase:", cmd_erase); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | fastboot_register("continue", cmd_continue); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 895 | fastboot_register("reboot", cmd_reboot); |
| 896 | fastboot_register("reboot-bootloader", cmd_reboot_bootloader); |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 897 | fastboot_publish("product", TARGET(BOARD)); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 898 | fastboot_publish("kernel", "lk"); |
Vivek Mehta | 5f1c9d4 | 2011-04-01 20:11:59 -0700 | [diff] [blame] | 899 | sz = target_get_max_flash_size(); |
| 900 | fastboot_init(target_get_scratch_address(), sz); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 901 | udc_start(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 902 | target_battery_charging_enable(1, 0); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | APP_START(aboot) |
| 906 | .init = aboot_init, |
| 907 | APP_END |
| 908 | |