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> |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 48 | #include <partition_parser.h> |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 49 | #include <platform.h> |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 50 | #include <crypto_hash.h> |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 51 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 52 | #include "image_verify.h" |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 53 | #include "recovery.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 54 | #include "bootimg.h" |
| 55 | #include "fastboot.h" |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 56 | #include "sparse_format.h" |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 57 | #include "mmc.h" |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 58 | #include "devinfo.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 59 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 60 | #include "scm.h" |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 61 | |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 62 | #define EXPAND(NAME) #NAME |
| 63 | #define TARGET(NAME) EXPAND(NAME) |
Chandan Uddaraju | da91983 | 2009-11-17 01:06:11 -0800 | [diff] [blame] | 64 | #define DEFAULT_CMDLINE "mem=100M console=null"; |
Brian Swetland | 2defe16 | 2009-08-18 14:35:59 -0700 | [diff] [blame] | 65 | |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 66 | #ifdef MEMBASE |
| 67 | #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE)) |
| 68 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 69 | #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000 |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 70 | #endif |
| 71 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 72 | #define RECOVERY_MODE 0x77665502 |
| 73 | #define FASTBOOT_MODE 0x77665500 |
| 74 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 75 | static const char *emmc_cmdline = " androidboot.emmc=true"; |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 76 | static const char *usb_sn_cmdline = " androidboot.serialno="; |
Ajay Dudani | ca3a33c | 2011-11-18 08:31:40 -0800 | [diff] [blame] | 77 | static const char *battchg_pause = " androidboot.mode=charger"; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 78 | static const char *auth_kernel = " androidboot.authorized_kernel=true"; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 79 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 80 | static const char *baseband_apq = " androidboot.baseband=apq"; |
| 81 | static const char *baseband_msm = " androidboot.baseband=msm"; |
| 82 | static const char *baseband_csfb = " androidboot.baseband=csfb"; |
| 83 | static const char *baseband_svlte2a = " androidboot.baseband=svlte2a"; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 84 | static const char *baseband_mdm = " androidboot.baseband=mdm"; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 85 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 86 | /* Assuming unauthorized kernel image by default */ |
| 87 | static int auth_kernel_img = 0; |
| 88 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 89 | static device_info device = {DEVICE_MAGIC, 0, 0}; |
| 90 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 91 | static struct udc_device surf_udc_device = { |
| 92 | .vendor_id = 0x18d1, |
Chandan Uddaraju | c53a1a1 | 2009-11-18 14:53:40 -0800 | [diff] [blame] | 93 | .product_id = 0xD00D, |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 94 | .version_id = 0x0100, |
| 95 | .manufacturer = "Google", |
| 96 | .product = "Android", |
| 97 | }; |
| 98 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 99 | struct atag_ptbl_entry |
| 100 | { |
| 101 | char name[16]; |
| 102 | unsigned offset; |
| 103 | unsigned size; |
| 104 | unsigned flags; |
| 105 | }; |
| 106 | |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 107 | char sn_buf[13]; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 108 | |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 109 | extern int emmc_recovery_init(void); |
| 110 | |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 111 | #if NO_KEYPAD_DRIVER |
| 112 | extern int fastboot_trigger(void); |
| 113 | #endif |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 114 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 115 | static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn) |
| 116 | { |
| 117 | struct atag_ptbl_entry atag_ptn; |
| 118 | |
| 119 | memcpy(atag_ptn.name, ptn->name, 16); |
| 120 | atag_ptn.name[15] = '\0'; |
| 121 | atag_ptn.offset = ptn->start; |
| 122 | atag_ptn.size = ptn->length; |
| 123 | atag_ptn.flags = ptn->flags; |
| 124 | memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry)); |
| 125 | *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned); |
| 126 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 127 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 128 | void boot_linux(void *kernel, unsigned *tags, |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 129 | const char *cmdline, unsigned machtype, |
| 130 | void *ramdisk, unsigned ramdisk_size) |
| 131 | { |
| 132 | unsigned *ptr = tags; |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 133 | unsigned pcount = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 134 | void (*entry)(unsigned,unsigned,unsigned*) = kernel; |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 135 | struct ptable *ptable; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 136 | int cmdline_len = 0; |
| 137 | int have_cmdline = 0; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 138 | int pause_at_bootup = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 139 | |
| 140 | /* CORE */ |
| 141 | *ptr++ = 2; |
| 142 | *ptr++ = 0x54410001; |
| 143 | |
| 144 | if (ramdisk_size) { |
| 145 | *ptr++ = 4; |
| 146 | *ptr++ = 0x54420005; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 147 | *ptr++ = (unsigned)ramdisk; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 148 | *ptr++ = ramdisk_size; |
| 149 | } |
| 150 | |
Chandan Uddaraju | c6860e1 | 2009-11-19 11:22:15 -0800 | [diff] [blame] | 151 | ptr = target_atag_mem(ptr); |
| 152 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 153 | if (!target_is_emmc_boot()) { |
| 154 | /* Skip NAND partition ATAGS for eMMC boot */ |
| 155 | if ((ptable = flash_get_ptable()) && (ptable->count != 0)) { |
| 156 | int i; |
Mekala Natarajan | c43d884 | 2011-10-26 15:09:10 -0700 | [diff] [blame] | 157 | *ptr++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) / |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 158 | sizeof(unsigned))); |
| 159 | *ptr++ = 0x4d534d70; |
| 160 | for (i = 0; i < ptable->count; ++i) |
| 161 | ptentry_to_tag(&ptr, ptable_get(ptable, i)); |
| 162 | } |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 165 | if (cmdline && cmdline[0]) { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 166 | cmdline_len = strlen(cmdline); |
| 167 | have_cmdline = 1; |
| 168 | } |
| 169 | if (target_is_emmc_boot()) { |
| 170 | cmdline_len += strlen(emmc_cmdline); |
| 171 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 172 | |
| 173 | cmdline_len += strlen(usb_sn_cmdline); |
| 174 | cmdline_len += strlen(sn_buf); |
| 175 | |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 176 | if (target_pause_for_battery_charge()) { |
| 177 | pause_at_bootup = 1; |
| 178 | cmdline_len += strlen(battchg_pause); |
| 179 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 180 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 181 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 182 | cmdline_len += strlen(auth_kernel); |
| 183 | } |
| 184 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 185 | /* Determine correct androidboot.baseband to use */ |
| 186 | switch(target_baseband()) |
| 187 | { |
| 188 | case BASEBAND_APQ: |
| 189 | cmdline_len += strlen(baseband_apq); |
| 190 | break; |
| 191 | |
| 192 | case BASEBAND_MSM: |
| 193 | cmdline_len += strlen(baseband_msm); |
| 194 | break; |
| 195 | |
| 196 | case BASEBAND_CSFB: |
| 197 | cmdline_len += strlen(baseband_csfb); |
| 198 | break; |
| 199 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 200 | case BASEBAND_SVLTE2A: |
| 201 | cmdline_len += strlen(baseband_svlte2a); |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 202 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 203 | |
| 204 | case BASEBAND_MDM: |
| 205 | cmdline_len += strlen(baseband_mdm); |
| 206 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 207 | } |
| 208 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 209 | if (cmdline_len > 0) { |
| 210 | const char *src; |
| 211 | char *dst; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 212 | unsigned n; |
| 213 | /* include terminating 0 and round up to a word multiple */ |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 214 | n = (cmdline_len + 4) & (~3); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 215 | *ptr++ = (n / 4) + 2; |
| 216 | *ptr++ = 0x54410009; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 217 | dst = (char *)ptr; |
| 218 | if (have_cmdline) { |
| 219 | src = cmdline; |
| 220 | while ((*dst++ = *src++)); |
| 221 | } |
| 222 | if (target_is_emmc_boot()) { |
| 223 | src = emmc_cmdline; |
| 224 | if (have_cmdline) --dst; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 225 | have_cmdline = 1; |
| 226 | while ((*dst++ = *src++)); |
| 227 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 228 | |
| 229 | src = usb_sn_cmdline; |
| 230 | if (have_cmdline) --dst; |
| 231 | have_cmdline = 1; |
| 232 | while ((*dst++ = *src++)); |
| 233 | src = sn_buf; |
| 234 | if (have_cmdline) --dst; |
| 235 | have_cmdline = 1; |
| 236 | while ((*dst++ = *src++)); |
| 237 | |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 238 | if (pause_at_bootup) { |
| 239 | src = battchg_pause; |
| 240 | if (have_cmdline) --dst; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 241 | while ((*dst++ = *src++)); |
| 242 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 243 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 244 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 245 | src = auth_kernel; |
| 246 | if (have_cmdline) --dst; |
| 247 | while ((*dst++ = *src++)); |
| 248 | } |
| 249 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 250 | switch(target_baseband()) |
| 251 | { |
| 252 | case BASEBAND_APQ: |
| 253 | src = baseband_apq; |
| 254 | if (have_cmdline) --dst; |
| 255 | while ((*dst++ = *src++)); |
| 256 | break; |
| 257 | |
| 258 | case BASEBAND_MSM: |
| 259 | src = baseband_msm; |
| 260 | if (have_cmdline) --dst; |
| 261 | while ((*dst++ = *src++)); |
| 262 | break; |
| 263 | |
| 264 | case BASEBAND_CSFB: |
| 265 | src = baseband_csfb; |
| 266 | if (have_cmdline) --dst; |
| 267 | while ((*dst++ = *src++)); |
| 268 | break; |
| 269 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 270 | case BASEBAND_SVLTE2A: |
| 271 | src = baseband_svlte2a; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 272 | if (have_cmdline) --dst; |
| 273 | while ((*dst++ = *src++)); |
| 274 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 275 | |
| 276 | case BASEBAND_MDM: |
| 277 | src = baseband_mdm; |
| 278 | if (have_cmdline) --dst; |
| 279 | while ((*dst++ = *src++)); |
| 280 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 281 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 282 | ptr += (n / 4); |
| 283 | } |
| 284 | |
| 285 | /* END */ |
| 286 | *ptr++ = 0; |
| 287 | *ptr++ = 0; |
| 288 | |
| 289 | dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n", |
| 290 | kernel, ramdisk, ramdisk_size); |
| 291 | if (cmdline) |
| 292 | dprintf(INFO, "cmdline: %s\n", cmdline); |
| 293 | |
| 294 | enter_critical_section(); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 295 | /* do any platform specific cleanup before kernel entry */ |
| 296 | platform_uninit(); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 297 | arch_disable_cache(UCACHE); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 298 | arch_disable_mmu(); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 299 | entry(0, machtype, tags); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 302 | unsigned page_size = 0; |
| 303 | unsigned page_mask = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 304 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 305 | #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y))) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 306 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 307 | static unsigned char buf[4096]; //Equal to max-supported pagesize |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 308 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 309 | int boot_linux_from_mmc(void) |
| 310 | { |
| 311 | struct boot_img_hdr *hdr = (void*) buf; |
| 312 | struct boot_img_hdr *uhdr; |
| 313 | unsigned offset = 0; |
| 314 | unsigned long long ptn = 0; |
| 315 | unsigned n = 0; |
| 316 | const char *cmdline; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 317 | int index = INVALID_PTN; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 318 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 319 | unsigned char *image_addr = 0; |
| 320 | unsigned kernel_actual; |
| 321 | unsigned ramdisk_actual; |
| 322 | unsigned imagesize_actual; |
| 323 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 324 | uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 325 | if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 326 | dprintf(INFO, "Unified boot method!\n"); |
| 327 | hdr = uhdr; |
| 328 | goto unified_boot; |
| 329 | } |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 330 | if (!boot_into_recovery) { |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 331 | index = partition_get_index("boot"); |
| 332 | ptn = partition_get_offset(index); |
| 333 | if(ptn == 0) { |
Shashank Mittal | 85b91f6 | 2010-10-30 10:12:38 -0700 | [diff] [blame] | 334 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 335 | return -1; |
| 336 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 337 | } |
| 338 | else { |
| 339 | index = partition_get_index("recovery"); |
| 340 | ptn = partition_get_offset(index); |
| 341 | if(ptn == 0) { |
Shashank Mittal | 85b91f6 | 2010-10-30 10:12:38 -0700 | [diff] [blame] | 342 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 343 | return -1; |
| 344 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 347 | if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) { |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 348 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 349 | return -1; |
| 350 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 351 | |
| 352 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 353 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 354 | return -1; |
| 355 | } |
| 356 | |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 357 | if (hdr->page_size && (hdr->page_size != page_size)) { |
| 358 | page_size = hdr->page_size; |
| 359 | page_mask = page_size - 1; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 362 | /* Authenticate Kernel */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 363 | if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered)) |
Subbaraman Narayanamurthy | 958fa24 | 2011-01-27 17:42:38 -0800 | [diff] [blame] | 364 | { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 365 | image_addr = (unsigned char *)target_get_scratch_address(); |
| 366 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 367 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 368 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual); |
| 369 | |
| 370 | offset = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 371 | |
| 372 | /* Assuming device rooted at this time */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 373 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 374 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 375 | /* Read image without signature */ |
| 376 | if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) |
| 377 | { |
| 378 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 379 | return -1; |
Subbaraman Narayanamurthy | 958fa24 | 2011-01-27 17:42:38 -0800 | [diff] [blame] | 380 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 381 | |
| 382 | offset = imagesize_actual; |
| 383 | /* Read signature */ |
| 384 | if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size)) |
| 385 | { |
| 386 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | auth_kernel_img = image_verify((unsigned char *)image_addr, |
| 391 | (unsigned char *)(image_addr + imagesize_actual), |
| 392 | imagesize_actual, |
| 393 | CRYPTO_AUTH_ALG_SHA256); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 394 | |
| 395 | if(auth_kernel_img) |
| 396 | { |
| 397 | /* Authorized kernel */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 398 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 399 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* Move kernel and ramdisk to correct address */ |
| 403 | memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size); |
| 404 | memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 405 | |
| 406 | /* Make sure everything from scratch address is read before next step!*/ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 407 | if(device.is_tampered) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 408 | { |
| 409 | write_device_info_mmc(&device); |
| 410 | #ifdef TZ_TAMPER_FUSE |
| 411 | set_tamper_fuse_cmd(); |
| 412 | #endif |
| 413 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 414 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 415 | else |
| 416 | { |
| 417 | offset += page_size; |
| 418 | |
| 419 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 420 | if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) { |
| 421 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 422 | return -1; |
| 423 | } |
| 424 | offset += n; |
| 425 | |
| 426 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 427 | if(n != 0) |
| 428 | { |
| 429 | if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) { |
| 430 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 431 | return -1; |
| 432 | } |
| 433 | } |
| 434 | offset += n; |
| 435 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 436 | |
| 437 | unified_boot: |
| 438 | dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr, |
| 439 | hdr->kernel_size); |
| 440 | dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr, |
| 441 | hdr->ramdisk_size); |
| 442 | |
| 443 | if(hdr->cmdline[0]) { |
| 444 | cmdline = (char*) hdr->cmdline; |
| 445 | } else { |
| 446 | cmdline = DEFAULT_CMDLINE; |
| 447 | } |
| 448 | dprintf(INFO, "cmdline = '%s'\n", cmdline); |
| 449 | |
| 450 | dprintf(INFO, "\nBooting Linux\n"); |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 451 | boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr, |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 452 | (const char *)cmdline, board_machtype(), |
| 453 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 458 | int boot_linux_from_flash(void) |
| 459 | { |
| 460 | struct boot_img_hdr *hdr = (void*) buf; |
| 461 | unsigned n; |
| 462 | struct ptentry *ptn; |
| 463 | struct ptable *ptable; |
| 464 | unsigned offset = 0; |
| 465 | const char *cmdline; |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 466 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 467 | unsigned char *image_addr = 0; |
| 468 | unsigned kernel_actual; |
| 469 | unsigned ramdisk_actual; |
| 470 | unsigned imagesize_actual; |
| 471 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 472 | if (target_is_emmc_boot()) { |
| 473 | hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 474 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 475 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
| 476 | return -1; |
| 477 | } |
| 478 | goto continue_boot; |
| 479 | } |
| 480 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 481 | ptable = flash_get_ptable(); |
| 482 | if (ptable == NULL) { |
| 483 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 484 | return -1; |
| 485 | } |
| 486 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 487 | if(!boot_into_recovery) |
| 488 | { |
| 489 | ptn = ptable_find(ptable, "boot"); |
| 490 | if (ptn == NULL) { |
| 491 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 492 | return -1; |
| 493 | } |
| 494 | } |
| 495 | else |
| 496 | { |
| 497 | ptn = ptable_find(ptable, "recovery"); |
| 498 | if (ptn == NULL) { |
| 499 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 500 | return -1; |
| 501 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 504 | if (flash_read(ptn, offset, buf, page_size)) { |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 505 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 506 | return -1; |
| 507 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 508 | |
| 509 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 510 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 511 | return -1; |
| 512 | } |
| 513 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 514 | if (hdr->page_size != page_size) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 515 | 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] | 516 | return -1; |
| 517 | } |
| 518 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 519 | /* Authenticate Kernel */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 520 | if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered)) |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 521 | { |
| 522 | image_addr = (unsigned char *)target_get_scratch_address(); |
| 523 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 524 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 525 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 526 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 527 | offset = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 528 | |
| 529 | /* Assuming device rooted at this time */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 530 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 531 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 532 | /* Read image without signature */ |
| 533 | if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual)) |
| 534 | { |
| 535 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 536 | return -1; |
| 537 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 538 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 539 | offset = imagesize_actual; |
| 540 | /* Read signature */ |
| 541 | if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size)) |
| 542 | { |
| 543 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 544 | } |
| 545 | else |
| 546 | { |
| 547 | |
| 548 | /* Verify signature */ |
| 549 | auth_kernel_img = image_verify((unsigned char *)image_addr, |
| 550 | (unsigned char *)(image_addr + imagesize_actual), |
| 551 | imagesize_actual, |
| 552 | CRYPTO_AUTH_ALG_SHA256); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 553 | |
| 554 | if(auth_kernel_img) |
| 555 | { |
| 556 | /* Authorized kernel */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 557 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 558 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* Move kernel and ramdisk to correct address */ |
| 562 | memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size); |
| 563 | memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 564 | |
| 565 | /* Make sure everything from scratch address is read before next step!*/ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 566 | if(device.is_tampered) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 567 | { |
| 568 | write_device_info_flash(&device); |
| 569 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 570 | } |
| 571 | else |
| 572 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 573 | offset = page_size; |
| 574 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 575 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 576 | if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) { |
| 577 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 578 | return -1; |
| 579 | } |
| 580 | offset += n; |
| 581 | |
| 582 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 583 | if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) { |
| 584 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 585 | return -1; |
| 586 | } |
| 587 | offset += n; |
| 588 | } |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 589 | continue_boot: |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 590 | dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr, |
| 591 | hdr->kernel_size); |
| 592 | dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr, |
| 593 | hdr->ramdisk_size); |
| 594 | |
| 595 | if(hdr->cmdline[0]) { |
| 596 | cmdline = (char*) hdr->cmdline; |
| 597 | } else { |
| 598 | cmdline = DEFAULT_CMDLINE; |
| 599 | } |
| 600 | dprintf(INFO, "cmdline = '%s'\n", cmdline); |
| 601 | |
| 602 | /* TODO: create/pass atags to kernel */ |
| 603 | |
| 604 | dprintf(INFO, "\nBooting Linux\n"); |
Ajay Dudani | e28a607 | 2011-07-01 13:59:46 -0700 | [diff] [blame] | 605 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 606 | (const char *)cmdline, board_machtype(), |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 607 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 608 | |
| 609 | return 0; |
| 610 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 611 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 612 | unsigned char info_buf[4096]; |
| 613 | void write_device_info_mmc(device_info *dev) |
| 614 | { |
| 615 | struct device_info *info = (void*) info_buf; |
| 616 | unsigned long long ptn = 0; |
| 617 | unsigned long long size; |
| 618 | int index = INVALID_PTN; |
| 619 | |
| 620 | index = partition_get_index("aboot"); |
| 621 | ptn = partition_get_offset(index); |
| 622 | if(ptn == 0) |
| 623 | { |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | size = partition_get_size(index); |
| 628 | |
| 629 | memcpy(info, dev, sizeof(device_info)); |
| 630 | |
| 631 | if(mmc_write((ptn + size - 512), 512, (void *)info_buf)) |
| 632 | { |
| 633 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 634 | return; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | void read_device_info_mmc(device_info *dev) |
| 639 | { |
| 640 | struct device_info *info = (void*) info_buf; |
| 641 | unsigned long long ptn = 0; |
| 642 | unsigned long long size; |
| 643 | int index = INVALID_PTN; |
| 644 | |
| 645 | index = partition_get_index("aboot"); |
| 646 | ptn = partition_get_offset(index); |
| 647 | if(ptn == 0) |
| 648 | { |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | size = partition_get_size(index); |
| 653 | |
| 654 | if(mmc_read((ptn + size - 512), (void *)info_buf, 512)) |
| 655 | { |
| 656 | dprintf(CRITICAL, "ERROR: Cannot read device info\n"); |
| 657 | return; |
| 658 | } |
| 659 | |
| 660 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 661 | { |
| 662 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
| 663 | info->is_unlocked = 0; |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 664 | info->is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 665 | |
| 666 | write_device_info_mmc(info); |
| 667 | } |
| 668 | memcpy(dev, info, sizeof(device_info)); |
| 669 | } |
| 670 | |
| 671 | void write_device_info_flash(device_info *dev) |
| 672 | { |
| 673 | struct device_info *info = (void *) info_buf; |
| 674 | struct ptentry *ptn; |
| 675 | struct ptable *ptable; |
| 676 | |
| 677 | ptable = flash_get_ptable(); |
| 678 | if (ptable == NULL) |
| 679 | { |
| 680 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | ptn = ptable_find(ptable, "devinfo"); |
| 685 | if (ptn == NULL) |
| 686 | { |
| 687 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | memcpy(info, dev, sizeof(device_info)); |
| 692 | |
| 693 | if (flash_write(ptn, 0, (void *)info_buf, page_size)) |
| 694 | { |
| 695 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 696 | return; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | void read_device_info_flash(device_info *dev) |
| 701 | { |
| 702 | struct device_info *info = (void*) info_buf; |
| 703 | struct ptentry *ptn; |
| 704 | struct ptable *ptable; |
| 705 | |
| 706 | ptable = flash_get_ptable(); |
| 707 | if (ptable == NULL) |
| 708 | { |
| 709 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 710 | return; |
| 711 | } |
| 712 | |
| 713 | ptn = ptable_find(ptable, "devinfo"); |
| 714 | if (ptn == NULL) |
| 715 | { |
| 716 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 717 | return; |
| 718 | } |
| 719 | |
| 720 | if (flash_read(ptn, 0, (void *)info_buf, page_size)) |
| 721 | { |
| 722 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 727 | { |
| 728 | while(1); |
| 729 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
| 730 | info->is_unlocked = 0; |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 731 | info->is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 732 | write_device_info_flash(info); |
| 733 | } |
| 734 | memcpy(dev, info, sizeof(device_info)); |
| 735 | } |
| 736 | |
| 737 | void write_device_info(device_info *dev) |
| 738 | { |
| 739 | if(target_is_emmc_boot()) |
| 740 | { |
| 741 | write_device_info_mmc(dev); |
| 742 | } |
| 743 | else |
| 744 | { |
| 745 | write_device_info_flash(dev); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | void read_device_info(device_info *dev) |
| 750 | { |
| 751 | if(target_is_emmc_boot()) |
| 752 | { |
| 753 | read_device_info_mmc(dev); |
| 754 | } |
| 755 | else |
| 756 | { |
| 757 | read_device_info_flash(dev); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | void reset_device_info() |
| 762 | { |
| 763 | dprintf(ALWAYS, "reset_device_info called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 764 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 765 | write_device_info(&device); |
| 766 | } |
| 767 | |
| 768 | void set_device_root() |
| 769 | { |
| 770 | dprintf(ALWAYS, "set_device_root called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 771 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 772 | write_device_info(&device); |
| 773 | } |
| 774 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 775 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 776 | { |
| 777 | unsigned kernel_actual; |
| 778 | unsigned ramdisk_actual; |
| 779 | static struct boot_img_hdr hdr; |
| 780 | char *ptr = ((char*) data); |
| 781 | |
| 782 | if (sz < sizeof(hdr)) { |
| 783 | fastboot_fail("invalid bootimage header"); |
| 784 | return; |
| 785 | } |
| 786 | |
| 787 | memcpy(&hdr, data, sizeof(hdr)); |
| 788 | |
| 789 | /* ensure commandline is terminated */ |
| 790 | hdr.cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 791 | |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 792 | if(target_is_emmc_boot() && hdr.page_size) { |
| 793 | page_size = hdr.page_size; |
| 794 | page_mask = page_size - 1; |
| 795 | } |
| 796 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 797 | kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask); |
| 798 | ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 799 | |
Shashank Mittal | 1f0e266 | 2011-09-01 15:06:00 -0700 | [diff] [blame] | 800 | /* sz should have atleast raw boot image */ |
| 801 | if (page_size + kernel_actual + ramdisk_actual > sz) { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 802 | fastboot_fail("incomplete bootimage"); |
| 803 | return; |
| 804 | } |
| 805 | |
Ajay Dudani | e28a607 | 2011-07-01 13:59:46 -0700 | [diff] [blame] | 806 | memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size); |
| 807 | 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] | 808 | |
| 809 | fastboot_okay(""); |
| 810 | udc_stop(); |
| 811 | |
Amol Jadi | e67872e | 2011-06-27 14:14:11 -0700 | [diff] [blame] | 812 | boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr, |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 813 | (const char*) hdr.cmdline, board_machtype(), |
Ajay Dudani | e28a607 | 2011-07-01 13:59:46 -0700 | [diff] [blame] | 814 | (void*) hdr.ramdisk_addr, hdr.ramdisk_size); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 815 | } |
| 816 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 817 | void cmd_erase(const char *arg, void *data, unsigned sz) |
| 818 | { |
| 819 | struct ptentry *ptn; |
| 820 | struct ptable *ptable; |
| 821 | |
| 822 | ptable = flash_get_ptable(); |
| 823 | if (ptable == NULL) { |
| 824 | fastboot_fail("partition table doesn't exist"); |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | ptn = ptable_find(ptable, arg); |
| 829 | if (ptn == NULL) { |
| 830 | fastboot_fail("unknown partition name"); |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | if (flash_erase(ptn)) { |
| 835 | fastboot_fail("failed to erase partition"); |
| 836 | return; |
| 837 | } |
| 838 | fastboot_okay(""); |
| 839 | } |
| 840 | |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 841 | |
| 842 | void cmd_erase_mmc(const char *arg, void *data, unsigned sz) |
| 843 | { |
| 844 | unsigned long long ptn = 0; |
neetid | b4b24d6 | 2012-01-20 12:13:09 -0800 | [diff] [blame^] | 845 | unsigned int out[512] = {0}; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 846 | int index = INVALID_PTN; |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 847 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 848 | index = partition_get_index(arg); |
| 849 | ptn = partition_get_offset(index); |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 850 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 851 | if(ptn == 0) { |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 852 | fastboot_fail("Partition table doesn't exist\n"); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 853 | return; |
| 854 | } |
neetid | b4b24d6 | 2012-01-20 12:13:09 -0800 | [diff] [blame^] | 855 | /* Simple inefficient version of erase. Just writing |
| 856 | 0 in first block */ |
| 857 | if (mmc_write(ptn , 512, (unsigned int *)out)) { |
| 858 | fastboot_fail("failed to erase partition"); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 859 | return; |
| 860 | } |
| 861 | fastboot_okay(""); |
| 862 | } |
| 863 | |
| 864 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 865 | void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz) |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 866 | { |
| 867 | unsigned long long ptn = 0; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 868 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 869 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 870 | |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 871 | if (!strcmp(arg, "partition")) |
| 872 | { |
| 873 | dprintf(INFO, "Attempt to write partition image.\n"); |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 874 | if (write_partition(sz, (unsigned char *) data)) { |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 875 | fastboot_fail("failed to write partition"); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 876 | return; |
| 877 | } |
| 878 | } |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 879 | else |
| 880 | { |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 881 | index = partition_get_index(arg); |
| 882 | ptn = partition_get_offset(index); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 883 | if(ptn == 0) { |
| 884 | fastboot_fail("partition table doesn't exist"); |
| 885 | return; |
| 886 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 887 | |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 888 | if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) { |
| 889 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 890 | fastboot_fail("image is not a boot image"); |
| 891 | return; |
| 892 | } |
| 893 | } |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 894 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 895 | size = partition_get_size(index); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 896 | if (ROUND_TO_PAGE(sz,511) > size) { |
| 897 | fastboot_fail("size too large"); |
| 898 | return; |
| 899 | } |
| 900 | else if (mmc_write(ptn , sz, (unsigned int *)data)) { |
| 901 | fastboot_fail("flash write failure"); |
| 902 | return; |
| 903 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 904 | } |
| 905 | fastboot_okay(""); |
| 906 | return; |
| 907 | } |
| 908 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 909 | void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz) |
| 910 | { |
| 911 | unsigned int chunk; |
| 912 | unsigned int chunk_data_sz; |
| 913 | sparse_header_t *sparse_header; |
| 914 | chunk_header_t *chunk_header; |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 915 | uint32_t total_blocks = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 916 | unsigned long long ptn = 0; |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 917 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 918 | int index = INVALID_PTN; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 919 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 920 | index = partition_get_index(arg); |
| 921 | ptn = partition_get_offset(index); |
| 922 | if(ptn == 0) { |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 923 | fastboot_fail("partition table doesn't exist"); |
| 924 | return; |
| 925 | } |
| 926 | |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 927 | size = partition_get_size(index); |
| 928 | if (ROUND_TO_PAGE(sz,511) > size) { |
| 929 | fastboot_fail("size too large"); |
| 930 | return; |
| 931 | } |
| 932 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 933 | /* Read and skip over sparse image header */ |
| 934 | sparse_header = (sparse_header_t *) data; |
| 935 | data += sparse_header->file_hdr_sz; |
| 936 | if(sparse_header->file_hdr_sz > sizeof(sparse_header_t)) |
| 937 | { |
| 938 | /* Skip the remaining bytes in a header that is longer than |
| 939 | * we expected. |
| 940 | */ |
| 941 | data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t)); |
| 942 | } |
| 943 | |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 944 | dprintf (SPEW, "=== Sparse Image Header ===\n"); |
| 945 | dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic); |
| 946 | dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version); |
| 947 | dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version); |
| 948 | dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz); |
| 949 | dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz); |
| 950 | dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz); |
| 951 | dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks); |
| 952 | dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 953 | |
| 954 | /* Start processing chunks */ |
| 955 | for (chunk=0; chunk<sparse_header->total_chunks; chunk++) |
| 956 | { |
| 957 | /* Read and skip over chunk header */ |
| 958 | chunk_header = (chunk_header_t *) data; |
| 959 | data += sizeof(chunk_header_t); |
| 960 | |
| 961 | dprintf (SPEW, "=== Chunk Header ===\n"); |
| 962 | dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type); |
| 963 | dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz); |
| 964 | dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz); |
| 965 | |
| 966 | if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) |
| 967 | { |
| 968 | /* Skip the remaining bytes in a header that is longer than |
| 969 | * we expected. |
| 970 | */ |
| 971 | data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t)); |
| 972 | } |
| 973 | |
| 974 | chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz; |
| 975 | switch (chunk_header->chunk_type) |
| 976 | { |
| 977 | case CHUNK_TYPE_RAW: |
| 978 | if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz + |
| 979 | chunk_data_sz)) |
| 980 | { |
| 981 | fastboot_fail("Bogus chunk size for chunk type Raw"); |
| 982 | return; |
| 983 | } |
| 984 | |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 985 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
| 986 | chunk_data_sz, |
| 987 | (unsigned int*)data)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 988 | { |
| 989 | fastboot_fail("flash write failure"); |
| 990 | return; |
| 991 | } |
| 992 | total_blocks += chunk_header->chunk_sz; |
| 993 | data += chunk_data_sz; |
| 994 | break; |
| 995 | |
| 996 | case CHUNK_TYPE_DONT_CARE: |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 997 | total_blocks += chunk_header->chunk_sz; |
| 998 | break; |
| 999 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1000 | case CHUNK_TYPE_CRC: |
| 1001 | if(chunk_header->total_sz != sparse_header->chunk_hdr_sz) |
| 1002 | { |
| 1003 | fastboot_fail("Bogus chunk size for chunk type Dont Care"); |
| 1004 | return; |
| 1005 | } |
| 1006 | total_blocks += chunk_header->chunk_sz; |
| 1007 | data += chunk_data_sz; |
| 1008 | break; |
| 1009 | |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 1010 | default: |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1011 | fastboot_fail("Unknown chunk type"); |
| 1012 | return; |
| 1013 | } |
| 1014 | } |
| 1015 | |
Ajay Dudani | 0c6927b | 2011-05-18 11:12:16 -0700 | [diff] [blame] | 1016 | dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n", |
| 1017 | total_blocks, sparse_header->total_blks); |
| 1018 | |
| 1019 | if(total_blocks != sparse_header->total_blks) |
| 1020 | { |
| 1021 | fastboot_fail("sparse image write failure"); |
| 1022 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1023 | |
| 1024 | fastboot_okay(""); |
| 1025 | return; |
| 1026 | } |
| 1027 | |
| 1028 | void cmd_flash_mmc(const char *arg, void *data, unsigned sz) |
| 1029 | { |
| 1030 | sparse_header_t *sparse_header; |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1031 | /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */ |
| 1032 | unsigned int *magic_number = (unsigned int *) data; |
| 1033 | int ret=0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1034 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1035 | if (magic_number[0] == SSD_HEADER_MAGIC_0 && |
| 1036 | magic_number[1] == SSD_HEADER_MAGIC_1) |
| 1037 | { |
| 1038 | #ifdef SSD_ENABLE |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 1039 | ret = decrypt_img_scm((uint32 **) &data, &sz); |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1040 | #endif |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 1041 | if (ret != 0) { |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1042 | dprintf(CRITICAL, "ERROR: Invalid secure image\n"); |
| 1043 | return; |
| 1044 | } |
| 1045 | } |
| 1046 | sparse_header = (sparse_header_t *) data; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1047 | if (sparse_header->magic != SPARSE_HEADER_MAGIC) |
| 1048 | cmd_flash_mmc_img(arg, data, sz); |
| 1049 | else |
| 1050 | cmd_flash_mmc_sparse_img(arg, data, sz); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1051 | return; |
| 1052 | } |
| 1053 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1054 | void cmd_flash(const char *arg, void *data, unsigned sz) |
| 1055 | { |
| 1056 | struct ptentry *ptn; |
| 1057 | struct ptable *ptable; |
| 1058 | unsigned extra = 0; |
| 1059 | |
| 1060 | ptable = flash_get_ptable(); |
| 1061 | if (ptable == NULL) { |
| 1062 | fastboot_fail("partition table doesn't exist"); |
| 1063 | return; |
| 1064 | } |
| 1065 | |
| 1066 | ptn = ptable_find(ptable, arg); |
| 1067 | if (ptn == NULL) { |
| 1068 | fastboot_fail("unknown partition name"); |
| 1069 | return; |
| 1070 | } |
| 1071 | |
| 1072 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
| 1073 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 1074 | fastboot_fail("image is not a boot image"); |
| 1075 | return; |
| 1076 | } |
| 1077 | } |
| 1078 | |
Chandan Uddaraju | d6d4504 | 2010-02-24 21:12:45 -0800 | [diff] [blame] | 1079 | if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata") |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 1080 | || !strcmp(ptn->name, "persist")) { |
| 1081 | if (flash_ecc_bch_enabled()) |
| 1082 | /* Spare data bytes for 8 bit ECC increased by 4 */ |
| 1083 | extra = ((page_size >> 9) * 20); |
| 1084 | else |
| 1085 | extra = ((page_size >> 9) * 16); |
| 1086 | } else |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 1087 | sz = ROUND_TO_PAGE(sz, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1088 | |
| 1089 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
| 1090 | if (flash_write(ptn, extra, data, sz)) { |
| 1091 | fastboot_fail("flash write failure"); |
| 1092 | return; |
| 1093 | } |
| 1094 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 1095 | fastboot_okay(""); |
| 1096 | } |
| 1097 | |
| 1098 | void cmd_continue(const char *arg, void *data, unsigned sz) |
| 1099 | { |
| 1100 | fastboot_okay(""); |
| 1101 | udc_stop(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1102 | if (target_is_emmc_boot()) |
| 1103 | { |
| 1104 | boot_linux_from_mmc(); |
| 1105 | } |
| 1106 | else |
| 1107 | { |
| 1108 | boot_linux_from_flash(); |
| 1109 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1112 | void cmd_reboot(const char *arg, void *data, unsigned sz) |
| 1113 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1114 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1115 | fastboot_okay(""); |
| 1116 | reboot_device(0); |
| 1117 | } |
| 1118 | |
| 1119 | void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz) |
| 1120 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1121 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1122 | fastboot_okay(""); |
| 1123 | reboot_device(FASTBOOT_MODE); |
| 1124 | } |
| 1125 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1126 | void cmd_oem_unlock(const char *arg, void *data, unsigned sz) |
| 1127 | { |
| 1128 | if(!device.is_unlocked) |
| 1129 | { |
| 1130 | device.is_unlocked = 1; |
| 1131 | write_device_info(&device); |
| 1132 | } |
| 1133 | fastboot_okay(""); |
| 1134 | } |
| 1135 | |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1136 | void cmd_oem_devinfo(const char *arg, void *data, unsigned sz) |
| 1137 | { |
| 1138 | char response[64]; |
| 1139 | snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false")); |
| 1140 | fastboot_info(response); |
| 1141 | snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false")); |
| 1142 | fastboot_info(response); |
| 1143 | fastboot_okay(""); |
| 1144 | } |
| 1145 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1146 | void splash_screen () |
| 1147 | { |
| 1148 | struct ptentry *ptn; |
| 1149 | struct ptable *ptable; |
| 1150 | struct fbcon_config *fb_display = NULL; |
| 1151 | |
| 1152 | if (!target_is_emmc_boot()) |
| 1153 | { |
| 1154 | ptable = flash_get_ptable(); |
| 1155 | if (ptable == NULL) { |
| 1156 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 1157 | return; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | ptn = ptable_find(ptable, "splash"); |
| 1161 | if (ptn == NULL) { |
| 1162 | dprintf(CRITICAL, "ERROR: No splash partition found\n"); |
| 1163 | } else { |
| 1164 | fb_display = fbcon_display(); |
| 1165 | if (fb_display) { |
| 1166 | if (flash_read(ptn, 0, fb_display->base, |
| 1167 | (fb_display->width * fb_display->height * fb_display->bpp/8))) { |
| 1168 | fbcon_clear(); |
| 1169 | dprintf(CRITICAL, "ERROR: Cannot read splash image\n"); |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | } |
| 1175 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1176 | void aboot_init(const struct app_descriptor *app) |
| 1177 | { |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 1178 | unsigned reboot_mode = 0; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1179 | unsigned usb_init = 0; |
Vivek Mehta | 5f1c9d4 | 2011-04-01 20:11:59 -0700 | [diff] [blame] | 1180 | unsigned sz = 0; |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 1181 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1182 | /* Setup page size information for nand/emmc reads */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1183 | if (target_is_emmc_boot()) |
| 1184 | { |
| 1185 | page_size = 2048; |
| 1186 | page_mask = page_size - 1; |
| 1187 | } |
| 1188 | else |
| 1189 | { |
| 1190 | page_size = flash_page_size(); |
| 1191 | page_mask = page_size - 1; |
| 1192 | } |
| 1193 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1194 | if(target_use_signed_kernel()) |
| 1195 | { |
| 1196 | read_device_info(&device); |
| 1197 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 1200 | target_serialno((unsigned char *) sn_buf); |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 1201 | dprintf(SPEW,"serial number: %s\n",sn_buf); |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 1202 | surf_udc_device.serialno = sn_buf; |
| 1203 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1204 | /* Check if we should do something other than booting up */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1205 | if (keys_get_state(KEY_HOME) != 0) |
| 1206 | boot_into_recovery = 1; |
Wentao Xu | 153902c | 2010-12-20 16:20:52 -0500 | [diff] [blame] | 1207 | if (keys_get_state(KEY_VOLUMEUP) != 0) |
| 1208 | boot_into_recovery = 1; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 1209 | if(!boot_into_recovery) |
| 1210 | { |
| 1211 | if (keys_get_state(KEY_BACK) != 0) |
| 1212 | goto fastboot; |
| 1213 | if (keys_get_state(KEY_VOLUMEDOWN) != 0) |
| 1214 | goto fastboot; |
| 1215 | } |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1216 | |
| 1217 | #if NO_KEYPAD_DRIVER |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 1218 | if (fastboot_trigger()) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1219 | goto fastboot; |
| 1220 | #endif |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 1221 | |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 1222 | reboot_mode = check_reboot_mode(); |
| 1223 | if (reboot_mode == RECOVERY_MODE) { |
| 1224 | boot_into_recovery = 1; |
| 1225 | } else if(reboot_mode == FASTBOOT_MODE) { |
| 1226 | goto fastboot; |
| 1227 | } |
| 1228 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1229 | if (target_is_emmc_boot()) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1230 | { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 1231 | if(emmc_recovery_init()) |
| 1232 | dprintf(ALWAYS,"error in emmc_recovery_init\n"); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1233 | if(target_use_signed_kernel()) |
| 1234 | { |
| 1235 | if((device.is_unlocked) || (device.is_tampered)) |
| 1236 | { |
| 1237 | #ifdef TZ_TAMPER_FUSE |
| 1238 | set_tamper_fuse_cmd(); |
| 1239 | #endif |
| 1240 | } |
| 1241 | } |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1242 | boot_linux_from_mmc(); |
| 1243 | } |
| 1244 | else |
| 1245 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1246 | recovery_init(); |
| 1247 | boot_linux_from_flash(); |
| 1248 | } |
Dima Zavin | b428360 | 2009-01-26 16:36:57 -0800 | [diff] [blame] | 1249 | dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting " |
| 1250 | "to fastboot mode.\n"); |
| 1251 | |
| 1252 | fastboot: |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1253 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1254 | target_fastboot_init(); |
Amol Jadi | 57abe4c | 2011-05-24 15:47:27 -0700 | [diff] [blame] | 1255 | |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1256 | if(!usb_init) |
| 1257 | udc_init(&surf_udc_device); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1258 | |
| 1259 | fastboot_register("boot", cmd_boot); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1260 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1261 | if (target_is_emmc_boot()) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1262 | { |
| 1263 | fastboot_register("flash:", cmd_flash_mmc); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1264 | fastboot_register("erase:", cmd_erase_mmc); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1265 | } |
| 1266 | else |
| 1267 | { |
| 1268 | fastboot_register("flash:", cmd_flash); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1269 | fastboot_register("erase:", cmd_erase); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | fastboot_register("continue", cmd_continue); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1273 | fastboot_register("reboot", cmd_reboot); |
| 1274 | fastboot_register("reboot-bootloader", cmd_reboot_bootloader); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1275 | fastboot_register("oem unlock", cmd_oem_unlock); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1276 | fastboot_register("oem device-info", cmd_oem_devinfo); |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 1277 | fastboot_publish("product", TARGET(BOARD)); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1278 | fastboot_publish("kernel", "lk"); |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1279 | partition_dump(); |
Vivek Mehta | 5f1c9d4 | 2011-04-01 20:11:59 -0700 | [diff] [blame] | 1280 | sz = target_get_max_flash_size(); |
| 1281 | fastboot_init(target_get_scratch_address(), sz); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1282 | udc_start(); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | APP_START(aboot) |
| 1286 | .init = aboot_init, |
| 1287 | APP_END |
| 1288 | |