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