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