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