Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 5 | * Copyright (c) 2009-2012, The Linux Foundation. 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. |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 14 | * * Neither the name of The Linux Foundation nor |
Chandan Uddaraju | 5fa471a | 2009-12-02 17:31:34 -0800 | [diff] [blame] | 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> |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 51 | #include <malloc.h> |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 52 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 53 | #if DEVICE_TREE |
| 54 | #include <libfdt.h> |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 55 | #include <dev_tree.h> |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 56 | #endif |
| 57 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 58 | #include "image_verify.h" |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 59 | #include "recovery.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 60 | #include "bootimg.h" |
| 61 | #include "fastboot.h" |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 62 | #include "sparse_format.h" |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 63 | #include "mmc.h" |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 64 | #include "devinfo.h" |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 65 | #include "board.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 66 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 67 | #include "scm.h" |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 68 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 69 | extern bool target_use_signed_kernel(void); |
| 70 | extern void dsb(); |
| 71 | extern void isb(); |
| 72 | extern void platform_uninit(void); |
| 73 | |
| 74 | void write_device_info_mmc(device_info *dev); |
| 75 | void write_device_info_flash(device_info *dev); |
| 76 | |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 77 | #define EXPAND(NAME) #NAME |
| 78 | #define TARGET(NAME) EXPAND(NAME) |
Chandan Uddaraju | da91983 | 2009-11-17 01:06:11 -0800 | [diff] [blame] | 79 | #define DEFAULT_CMDLINE "mem=100M console=null"; |
Brian Swetland | 2defe16 | 2009-08-18 14:35:59 -0700 | [diff] [blame] | 80 | |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 81 | #ifdef MEMBASE |
| 82 | #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE)) |
| 83 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 84 | #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000 |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 85 | #endif |
| 86 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 87 | #define RECOVERY_MODE 0x77665502 |
| 88 | #define FASTBOOT_MODE 0x77665500 |
| 89 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 90 | static const char *emmc_cmdline = " androidboot.emmc=true"; |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 91 | static const char *usb_sn_cmdline = " androidboot.serialno="; |
Ajay Dudani | ca3a33c | 2011-11-18 08:31:40 -0800 | [diff] [blame] | 92 | static const char *battchg_pause = " androidboot.mode=charger"; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 93 | static const char *auth_kernel = " androidboot.authorized_kernel=true"; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 94 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 95 | static const char *baseband_apq = " androidboot.baseband=apq"; |
| 96 | static const char *baseband_msm = " androidboot.baseband=msm"; |
| 97 | static const char *baseband_csfb = " androidboot.baseband=csfb"; |
| 98 | static const char *baseband_svlte2a = " androidboot.baseband=svlte2a"; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 99 | static const char *baseband_mdm = " androidboot.baseband=mdm"; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 100 | static const char *baseband_sglte = " androidboot.baseband=sglte"; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 101 | static const char *baseband_dsda = " androidboot.baseband=dsda"; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 102 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 103 | /* Assuming unauthorized kernel image by default */ |
| 104 | static int auth_kernel_img = 0; |
| 105 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 106 | static device_info device = {DEVICE_MAGIC, 0, 0}; |
| 107 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 108 | static struct udc_device surf_udc_device = { |
| 109 | .vendor_id = 0x18d1, |
Chandan Uddaraju | c53a1a1 | 2009-11-18 14:53:40 -0800 | [diff] [blame] | 110 | .product_id = 0xD00D, |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 111 | .version_id = 0x0100, |
| 112 | .manufacturer = "Google", |
| 113 | .product = "Android", |
| 114 | }; |
| 115 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 116 | struct atag_ptbl_entry |
| 117 | { |
| 118 | char name[16]; |
| 119 | unsigned offset; |
| 120 | unsigned size; |
| 121 | unsigned flags; |
| 122 | }; |
| 123 | |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 124 | char sn_buf[13]; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 125 | |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 126 | extern int emmc_recovery_init(void); |
| 127 | |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 128 | #if NO_KEYPAD_DRIVER |
| 129 | extern int fastboot_trigger(void); |
| 130 | #endif |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 131 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 132 | static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn) |
| 133 | { |
| 134 | struct atag_ptbl_entry atag_ptn; |
| 135 | |
| 136 | memcpy(atag_ptn.name, ptn->name, 16); |
| 137 | atag_ptn.name[15] = '\0'; |
| 138 | atag_ptn.offset = ptn->start; |
| 139 | atag_ptn.size = ptn->length; |
| 140 | atag_ptn.flags = ptn->flags; |
| 141 | memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry)); |
| 142 | *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned); |
| 143 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 144 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 145 | unsigned char *update_cmdline(const char * cmdline) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 146 | { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 147 | int cmdline_len = 0; |
| 148 | int have_cmdline = 0; |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 149 | unsigned char *cmdline_final = NULL; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 150 | int pause_at_bootup = 0; |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 151 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 152 | if (cmdline && cmdline[0]) { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 153 | cmdline_len = strlen(cmdline); |
| 154 | have_cmdline = 1; |
| 155 | } |
| 156 | if (target_is_emmc_boot()) { |
| 157 | cmdline_len += strlen(emmc_cmdline); |
| 158 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 159 | |
| 160 | cmdline_len += strlen(usb_sn_cmdline); |
| 161 | cmdline_len += strlen(sn_buf); |
| 162 | |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 163 | if (target_pause_for_battery_charge()) { |
| 164 | pause_at_bootup = 1; |
| 165 | cmdline_len += strlen(battchg_pause); |
| 166 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 167 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 168 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 169 | cmdline_len += strlen(auth_kernel); |
| 170 | } |
| 171 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 172 | /* Determine correct androidboot.baseband to use */ |
| 173 | switch(target_baseband()) |
| 174 | { |
| 175 | case BASEBAND_APQ: |
| 176 | cmdline_len += strlen(baseband_apq); |
| 177 | break; |
| 178 | |
| 179 | case BASEBAND_MSM: |
| 180 | cmdline_len += strlen(baseband_msm); |
| 181 | break; |
| 182 | |
| 183 | case BASEBAND_CSFB: |
| 184 | cmdline_len += strlen(baseband_csfb); |
| 185 | break; |
| 186 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 187 | case BASEBAND_SVLTE2A: |
| 188 | cmdline_len += strlen(baseband_svlte2a); |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 189 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 190 | |
| 191 | case BASEBAND_MDM: |
| 192 | cmdline_len += strlen(baseband_mdm); |
| 193 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 194 | |
| 195 | case BASEBAND_SGLTE: |
| 196 | cmdline_len += strlen(baseband_sglte); |
| 197 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 198 | |
| 199 | case BASEBAND_DSDA: |
| 200 | cmdline_len += strlen(baseband_dsda); |
| 201 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 202 | } |
| 203 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 204 | if (cmdline_len > 0) { |
| 205 | const char *src; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 206 | unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3)); |
| 207 | ASSERT(dst != NULL); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 208 | |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 209 | /* Save start ptr for debug print */ |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 210 | cmdline_final = dst; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 211 | if (have_cmdline) { |
| 212 | src = cmdline; |
| 213 | while ((*dst++ = *src++)); |
| 214 | } |
| 215 | if (target_is_emmc_boot()) { |
| 216 | src = emmc_cmdline; |
| 217 | if (have_cmdline) --dst; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 218 | have_cmdline = 1; |
| 219 | while ((*dst++ = *src++)); |
| 220 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 221 | |
| 222 | src = usb_sn_cmdline; |
| 223 | if (have_cmdline) --dst; |
| 224 | have_cmdline = 1; |
| 225 | while ((*dst++ = *src++)); |
| 226 | src = sn_buf; |
| 227 | if (have_cmdline) --dst; |
| 228 | have_cmdline = 1; |
| 229 | while ((*dst++ = *src++)); |
| 230 | |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 231 | if (pause_at_bootup) { |
| 232 | src = battchg_pause; |
| 233 | if (have_cmdline) --dst; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 234 | while ((*dst++ = *src++)); |
| 235 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 236 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 237 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 238 | src = auth_kernel; |
| 239 | if (have_cmdline) --dst; |
| 240 | while ((*dst++ = *src++)); |
| 241 | } |
| 242 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 243 | switch(target_baseband()) |
| 244 | { |
| 245 | case BASEBAND_APQ: |
| 246 | src = baseband_apq; |
| 247 | if (have_cmdline) --dst; |
| 248 | while ((*dst++ = *src++)); |
| 249 | break; |
| 250 | |
| 251 | case BASEBAND_MSM: |
| 252 | src = baseband_msm; |
| 253 | if (have_cmdline) --dst; |
| 254 | while ((*dst++ = *src++)); |
| 255 | break; |
| 256 | |
| 257 | case BASEBAND_CSFB: |
| 258 | src = baseband_csfb; |
| 259 | if (have_cmdline) --dst; |
| 260 | while ((*dst++ = *src++)); |
| 261 | break; |
| 262 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 263 | case BASEBAND_SVLTE2A: |
| 264 | src = baseband_svlte2a; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 265 | if (have_cmdline) --dst; |
| 266 | while ((*dst++ = *src++)); |
| 267 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 268 | |
| 269 | case BASEBAND_MDM: |
| 270 | src = baseband_mdm; |
| 271 | if (have_cmdline) --dst; |
| 272 | while ((*dst++ = *src++)); |
| 273 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 274 | |
| 275 | case BASEBAND_SGLTE: |
| 276 | src = baseband_sglte; |
| 277 | if (have_cmdline) --dst; |
| 278 | while ((*dst++ = *src++)); |
| 279 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 280 | |
| 281 | case BASEBAND_DSDA: |
| 282 | src = baseband_dsda; |
| 283 | if (have_cmdline) --dst; |
| 284 | while ((*dst++ = *src++)); |
| 285 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 286 | } |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 287 | } |
| 288 | return cmdline_final; |
| 289 | } |
| 290 | |
| 291 | unsigned *atag_core(unsigned *ptr) |
| 292 | { |
| 293 | /* CORE */ |
| 294 | *ptr++ = 2; |
| 295 | *ptr++ = 0x54410001; |
| 296 | |
| 297 | return ptr; |
| 298 | |
| 299 | } |
| 300 | |
| 301 | unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk, |
| 302 | unsigned ramdisk_size) |
| 303 | { |
| 304 | if (ramdisk_size) { |
| 305 | *ptr++ = 4; |
| 306 | *ptr++ = 0x54420005; |
| 307 | *ptr++ = (unsigned)ramdisk; |
| 308 | *ptr++ = ramdisk_size; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 311 | return ptr; |
| 312 | } |
| 313 | |
| 314 | unsigned *atag_ptable(unsigned **ptr_addr) |
| 315 | { |
| 316 | int i; |
| 317 | struct ptable *ptable; |
| 318 | |
| 319 | if ((ptable = flash_get_ptable()) && (ptable->count != 0)) { |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 320 | *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) / |
| 321 | sizeof(unsigned))); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 322 | *(*ptr_addr)++ = 0x4d534d70; |
| 323 | for (i = 0; i < ptable->count; ++i) |
| 324 | ptentry_to_tag(ptr_addr, ptable_get(ptable, i)); |
| 325 | } |
| 326 | |
| 327 | return (*ptr_addr); |
| 328 | } |
| 329 | |
| 330 | unsigned *atag_cmdline(unsigned *ptr, const char *cmdline) |
| 331 | { |
| 332 | int cmdline_length = 0; |
| 333 | int n; |
| 334 | unsigned char *cmdline_final = NULL; |
| 335 | char *dest; |
| 336 | |
| 337 | cmdline_final = update_cmdline(cmdline); |
| 338 | if (cmdline_final){ |
| 339 | dprintf(INFO, "cmdline: %s\n", cmdline_final); |
| 340 | } |
| 341 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 342 | cmdline_length =strlen((const char*)cmdline_final); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 343 | n = (cmdline_length + 4) & (~3); |
| 344 | |
| 345 | *ptr++ = (n / 4) + 2; |
| 346 | *ptr++ = 0x54410009; |
| 347 | dest = (char *) ptr; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 348 | while ((*dest++ = *cmdline_final++)); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 349 | ptr += (n / 4); |
| 350 | |
| 351 | return ptr; |
| 352 | } |
| 353 | |
| 354 | unsigned *atag_end(unsigned *ptr) |
| 355 | { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 356 | /* END */ |
| 357 | *ptr++ = 0; |
| 358 | *ptr++ = 0; |
| 359 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 360 | return ptr; |
| 361 | } |
| 362 | |
| 363 | void generate_atags(unsigned *ptr, const char *cmdline, |
| 364 | void *ramdisk, unsigned ramdisk_size) |
| 365 | { |
| 366 | |
| 367 | ptr = atag_core(ptr); |
| 368 | ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size); |
| 369 | ptr = target_atag_mem(ptr); |
| 370 | |
| 371 | /* Skip NAND partition ATAGS for eMMC boot */ |
| 372 | if (!target_is_emmc_boot()){ |
| 373 | ptr = atag_ptable(&ptr); |
| 374 | } |
| 375 | |
| 376 | ptr = atag_cmdline(ptr, cmdline); |
| 377 | ptr = atag_end(ptr); |
| 378 | } |
| 379 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 380 | typedef void entry_func_ptr(unsigned, unsigned, unsigned*); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 381 | void boot_linux(void *kernel, unsigned *tags, |
| 382 | const char *cmdline, unsigned machtype, |
| 383 | void *ramdisk, unsigned ramdisk_size) |
| 384 | { |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 385 | int ret = 0; |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 386 | void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel)); |
Deepa Dinamani | 25a9f76 | 2012-11-30 15:57:15 -0800 | [diff] [blame^] | 387 | uint32_t tags_phys = PA((addr_t)tags); |
| 388 | |
| 389 | ramdisk = PA(ramdisk); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 390 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 391 | #if DEVICE_TREE |
| 392 | /* Update the Device Tree */ |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 393 | ret = update_device_tree((void *)tags, cmdline, ramdisk, ramdisk_size); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 394 | if(ret) |
| 395 | { |
| 396 | dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n"); |
| 397 | ASSERT(0); |
| 398 | } |
| 399 | #else |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 400 | /* Generating the Atags */ |
| 401 | generate_atags(tags, cmdline, ramdisk, ramdisk_size); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 402 | #endif |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 403 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 404 | dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n", |
| 405 | kernel, ramdisk, ramdisk_size); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 406 | |
| 407 | enter_critical_section(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 408 | |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 409 | /* do any platform specific cleanup before kernel entry */ |
| 410 | platform_uninit(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 411 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 412 | arch_disable_cache(UCACHE); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 413 | |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 414 | #if ARM_WITH_MMU |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 415 | arch_disable_mmu(); |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 416 | #endif |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 417 | |
| 418 | entry(0, machtype, (unsigned*)tags_phys); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 421 | unsigned page_size = 0; |
| 422 | unsigned page_mask = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 423 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 424 | #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y))) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 425 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 426 | static unsigned char buf[4096]; //Equal to max-supported pagesize |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 427 | static unsigned char dt_buf[4096]; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 428 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 429 | int boot_linux_from_mmc(void) |
| 430 | { |
| 431 | struct boot_img_hdr *hdr = (void*) buf; |
| 432 | struct boot_img_hdr *uhdr; |
| 433 | unsigned offset = 0; |
| 434 | unsigned long long ptn = 0; |
| 435 | unsigned n = 0; |
| 436 | const char *cmdline; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 437 | int index = INVALID_PTN; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 438 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 439 | unsigned char *image_addr = 0; |
| 440 | unsigned kernel_actual; |
| 441 | unsigned ramdisk_actual; |
| 442 | unsigned imagesize_actual; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 443 | unsigned second_actual = 0; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 444 | |
| 445 | #if DEVICE_TREE |
| 446 | struct dt_table *table; |
| 447 | struct dt_entry *dt_entry_ptr; |
| 448 | unsigned dt_table_offset; |
| 449 | #endif |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 450 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 451 | uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 452 | if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 453 | dprintf(INFO, "Unified boot method!\n"); |
| 454 | hdr = uhdr; |
| 455 | goto unified_boot; |
| 456 | } |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 457 | if (!boot_into_recovery) { |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 458 | index = partition_get_index("boot"); |
| 459 | ptn = partition_get_offset(index); |
| 460 | if(ptn == 0) { |
Shashank Mittal | 85b91f6 | 2010-10-30 10:12:38 -0700 | [diff] [blame] | 461 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 462 | return -1; |
| 463 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 464 | } |
| 465 | else { |
| 466 | index = partition_get_index("recovery"); |
| 467 | ptn = partition_get_offset(index); |
| 468 | if(ptn == 0) { |
Shashank Mittal | 85b91f6 | 2010-10-30 10:12:38 -0700 | [diff] [blame] | 469 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 470 | return -1; |
| 471 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 474 | if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) { |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 475 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 476 | return -1; |
| 477 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 478 | |
| 479 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 480 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 481 | return -1; |
| 482 | } |
| 483 | |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 484 | if (hdr->page_size && (hdr->page_size != page_size)) { |
| 485 | page_size = hdr->page_size; |
| 486 | page_mask = page_size - 1; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 489 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 490 | hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr)); |
| 491 | hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr)); |
| 492 | hdr->tags_addr = VA((addr_t)(hdr->tags_addr)); |
| 493 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 494 | /* Authenticate Kernel */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 495 | if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered)) |
Subbaraman Narayanamurthy | 958fa24 | 2011-01-27 17:42:38 -0800 | [diff] [blame] | 496 | { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 497 | image_addr = (unsigned char *)target_get_scratch_address(); |
| 498 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 499 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Channagoud Kadabi | 4b27651 | 2012-08-28 15:16:30 +0530 | [diff] [blame] | 500 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual); |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 501 | |
| 502 | offset = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 503 | |
| 504 | /* Assuming device rooted at this time */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 505 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 506 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 507 | /* Read image without signature */ |
| 508 | if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) |
| 509 | { |
| 510 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 511 | return -1; |
Subbaraman Narayanamurthy | 958fa24 | 2011-01-27 17:42:38 -0800 | [diff] [blame] | 512 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 513 | |
| 514 | offset = imagesize_actual; |
| 515 | /* Read signature */ |
| 516 | if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size)) |
| 517 | { |
| 518 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 519 | } |
| 520 | else |
| 521 | { |
| 522 | auth_kernel_img = image_verify((unsigned char *)image_addr, |
| 523 | (unsigned char *)(image_addr + imagesize_actual), |
| 524 | imagesize_actual, |
| 525 | CRYPTO_AUTH_ALG_SHA256); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 526 | |
| 527 | if(auth_kernel_img) |
| 528 | { |
| 529 | /* Authorized kernel */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 530 | device.is_tampered = 0; |
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 | } |
| 533 | |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 534 | /* Move kernel, ramdisk and device tree to correct address */ |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 535 | memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size); |
| 536 | 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] | 537 | |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 538 | #if DEVICE_TREE |
| 539 | if(hdr->dt_size) { |
| 540 | table = (struct dt_table*) dt_buf; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 541 | dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual); |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 542 | |
| 543 | memmove((void *) dt_buf, (char *)dt_table_offset, page_size); |
| 544 | |
| 545 | /* Restriction that the device tree entry table should be less than a page*/ |
| 546 | ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size); |
| 547 | |
| 548 | /* Validate the device tree table header */ |
| 549 | if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) { |
| 550 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 551 | return -1; |
| 552 | } |
| 553 | |
| 554 | /* Find index of device tree within device tree table */ |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 555 | if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){ |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 556 | dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n"); |
| 557 | return -1; |
| 558 | } |
| 559 | |
| 560 | /* Read device device tree in the "tags_add */ |
| 561 | memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size); |
| 562 | } |
| 563 | #endif |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 564 | /* Make sure everything from scratch address is read before next step!*/ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 565 | if(device.is_tampered) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 566 | { |
| 567 | write_device_info_mmc(&device); |
| 568 | #ifdef TZ_TAMPER_FUSE |
| 569 | set_tamper_fuse_cmd(); |
| 570 | #endif |
| 571 | } |
Channagoud Kadabi | bf695c6 | 2012-04-10 13:31:56 +0530 | [diff] [blame] | 572 | #if USE_PCOM_SECBOOT |
| 573 | set_tamper_flag(device.is_tampered); |
| 574 | #endif |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 575 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 576 | else |
| 577 | { |
| 578 | offset += page_size; |
| 579 | |
| 580 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 581 | if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) { |
| 582 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 583 | return -1; |
| 584 | } |
| 585 | offset += n; |
| 586 | |
| 587 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 588 | if(n != 0) |
| 589 | { |
| 590 | if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) { |
| 591 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 592 | return -1; |
| 593 | } |
| 594 | } |
| 595 | offset += n; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 596 | |
| 597 | if(hdr->second_size != 0) { |
| 598 | n = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 599 | offset += n; |
| 600 | } |
| 601 | |
| 602 | #if DEVICE_TREE |
| 603 | if(hdr->dt_size != 0) { |
| 604 | |
| 605 | /* Read the device tree table into buffer */ |
| 606 | if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) { |
| 607 | dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n"); |
| 608 | return -1; |
| 609 | } |
| 610 | table = (struct dt_table*) dt_buf; |
| 611 | |
| 612 | /* Restriction that the device tree entry table should be less than a page*/ |
| 613 | ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size); |
| 614 | |
| 615 | /* Validate the device tree table header */ |
| 616 | if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) { |
| 617 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 618 | return -1; |
| 619 | } |
| 620 | |
| 621 | /* Calculate the offset of device tree within device tree table */ |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 622 | if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){ |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 623 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 624 | return -1; |
| 625 | } |
| 626 | |
| 627 | /* Read device device tree in the "tags_add */ |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 628 | if(mmc_read(ptn + offset + dt_entry_ptr->offset, |
| 629 | (void *)hdr->tags_addr, dt_entry_ptr->size)) { |
| 630 | dprintf(CRITICAL, "ERROR: Cannot read device tree\n"); |
| 631 | return -1; |
| 632 | } |
| 633 | } |
| 634 | #endif |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 635 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 636 | |
| 637 | unified_boot: |
| 638 | dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr, |
| 639 | hdr->kernel_size); |
| 640 | dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr, |
| 641 | hdr->ramdisk_size); |
| 642 | |
| 643 | if(hdr->cmdline[0]) { |
| 644 | cmdline = (char*) hdr->cmdline; |
| 645 | } else { |
| 646 | cmdline = DEFAULT_CMDLINE; |
| 647 | } |
| 648 | dprintf(INFO, "cmdline = '%s'\n", cmdline); |
| 649 | |
| 650 | dprintf(INFO, "\nBooting Linux\n"); |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 651 | boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr, |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 652 | (const char *)cmdline, board_machtype(), |
| 653 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 658 | int boot_linux_from_flash(void) |
| 659 | { |
| 660 | struct boot_img_hdr *hdr = (void*) buf; |
| 661 | unsigned n; |
| 662 | struct ptentry *ptn; |
| 663 | struct ptable *ptable; |
| 664 | unsigned offset = 0; |
| 665 | const char *cmdline; |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 666 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 667 | unsigned char *image_addr = 0; |
| 668 | unsigned kernel_actual; |
| 669 | unsigned ramdisk_actual; |
| 670 | unsigned imagesize_actual; |
| 671 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 672 | #if DEVICE_TREE |
| 673 | struct dt_table *table; |
| 674 | struct dt_entry *dt_entry_ptr; |
| 675 | #endif |
| 676 | |
| 677 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 678 | if (target_is_emmc_boot()) { |
| 679 | hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 680 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 681 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
| 682 | return -1; |
| 683 | } |
| 684 | goto continue_boot; |
| 685 | } |
| 686 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 687 | ptable = flash_get_ptable(); |
| 688 | if (ptable == NULL) { |
| 689 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 690 | return -1; |
| 691 | } |
| 692 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 693 | if(!boot_into_recovery) |
| 694 | { |
| 695 | ptn = ptable_find(ptable, "boot"); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 696 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 697 | if (ptn == NULL) { |
| 698 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 699 | return -1; |
| 700 | } |
| 701 | } |
| 702 | else |
| 703 | { |
| 704 | ptn = ptable_find(ptable, "recovery"); |
| 705 | if (ptn == NULL) { |
| 706 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 707 | return -1; |
| 708 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 709 | } |
| 710 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 711 | if (flash_read(ptn, offset, buf, page_size)) { |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 712 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 713 | return -1; |
| 714 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 715 | |
| 716 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 717 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 718 | return -1; |
| 719 | } |
| 720 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 721 | if (hdr->page_size != page_size) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 722 | 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] | 723 | return -1; |
| 724 | } |
| 725 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 726 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 727 | hdr->kernel_addr = VA(hdr->kernel_addr); |
| 728 | hdr->ramdisk_addr = VA(hdr->ramdisk_addr); |
| 729 | hdr->tags_addr = VA(hdr->tags_addr); |
| 730 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 731 | /* Authenticate Kernel */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 732 | if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered)) |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 733 | { |
| 734 | image_addr = (unsigned char *)target_get_scratch_address(); |
| 735 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 736 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 737 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 738 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 739 | offset = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 740 | |
| 741 | /* Assuming device rooted at this time */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 742 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 743 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 744 | /* Read image without signature */ |
| 745 | if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual)) |
| 746 | { |
| 747 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 748 | return -1; |
| 749 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 750 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 751 | offset = imagesize_actual; |
| 752 | /* Read signature */ |
| 753 | if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size)) |
| 754 | { |
| 755 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 756 | } |
| 757 | else |
| 758 | { |
| 759 | |
| 760 | /* Verify signature */ |
| 761 | auth_kernel_img = image_verify((unsigned char *)image_addr, |
| 762 | (unsigned char *)(image_addr + imagesize_actual), |
| 763 | imagesize_actual, |
| 764 | CRYPTO_AUTH_ALG_SHA256); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 765 | |
| 766 | if(auth_kernel_img) |
| 767 | { |
| 768 | /* Authorized kernel */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 769 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 770 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /* Move kernel and ramdisk to correct address */ |
| 774 | memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size); |
| 775 | 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] | 776 | |
| 777 | /* Make sure everything from scratch address is read before next step!*/ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 778 | if(device.is_tampered) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 779 | { |
| 780 | write_device_info_flash(&device); |
| 781 | } |
Channagoud Kadabi | 5c86fe3 | 2012-02-16 10:58:48 +0530 | [diff] [blame] | 782 | #if USE_PCOM_SECBOOT |
| 783 | set_tamper_flag(device.is_tampered); |
| 784 | #endif |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 785 | } |
| 786 | else |
| 787 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 788 | offset = page_size; |
| 789 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 790 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 791 | if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) { |
| 792 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 793 | return -1; |
| 794 | } |
| 795 | offset += n; |
| 796 | |
| 797 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 798 | if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) { |
| 799 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 800 | return -1; |
| 801 | } |
| 802 | offset += n; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 803 | |
| 804 | if(hdr->second_size != 0) { |
| 805 | n = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 806 | offset += n; |
| 807 | } |
| 808 | |
| 809 | #if DEVICE_TREE |
| 810 | if(hdr->dt_size != 0) { |
| 811 | |
| 812 | /* Read the device tree table into buffer */ |
| 813 | if(flash_read(ptn, offset, (void *) dt_buf, page_size)) { |
| 814 | dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n"); |
| 815 | return -1; |
| 816 | } |
| 817 | |
| 818 | table = (struct dt_table*) dt_buf; |
| 819 | |
| 820 | /* Restriction that the device tree entry table should be less than a page*/ |
| 821 | ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size); |
| 822 | |
| 823 | /* Validate the device tree table header */ |
| 824 | if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) { |
| 825 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 826 | return -1; |
| 827 | } |
| 828 | |
| 829 | /* Calculate the offset of device tree within device tree table */ |
| 830 | if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){ |
| 831 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 832 | return -1; |
| 833 | } |
| 834 | |
| 835 | /* Read device device tree in the "tags_add */ |
| 836 | if(flash_read(ptn, offset + dt_entry_ptr->offset, |
| 837 | (void *)hdr->tags_addr, dt_entry_ptr->size)) { |
| 838 | dprintf(CRITICAL, "ERROR: Cannot read device tree\n"); |
| 839 | return -1; |
| 840 | } |
| 841 | } |
| 842 | #endif |
| 843 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 844 | } |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 845 | continue_boot: |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 846 | dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr, |
| 847 | hdr->kernel_size); |
| 848 | dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr, |
| 849 | hdr->ramdisk_size); |
| 850 | |
| 851 | if(hdr->cmdline[0]) { |
| 852 | cmdline = (char*) hdr->cmdline; |
| 853 | } else { |
| 854 | cmdline = DEFAULT_CMDLINE; |
| 855 | } |
| 856 | dprintf(INFO, "cmdline = '%s'\n", cmdline); |
| 857 | |
| 858 | /* TODO: create/pass atags to kernel */ |
| 859 | |
| 860 | dprintf(INFO, "\nBooting Linux\n"); |
Ajay Dudani | e28a607 | 2011-07-01 13:59:46 -0700 | [diff] [blame] | 861 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 862 | (const char *)cmdline, board_machtype(), |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 863 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 864 | |
| 865 | return 0; |
| 866 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 867 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 868 | unsigned char info_buf[4096]; |
| 869 | void write_device_info_mmc(device_info *dev) |
| 870 | { |
| 871 | struct device_info *info = (void*) info_buf; |
| 872 | unsigned long long ptn = 0; |
| 873 | unsigned long long size; |
| 874 | int index = INVALID_PTN; |
| 875 | |
| 876 | index = partition_get_index("aboot"); |
| 877 | ptn = partition_get_offset(index); |
| 878 | if(ptn == 0) |
| 879 | { |
| 880 | return; |
| 881 | } |
| 882 | |
| 883 | size = partition_get_size(index); |
| 884 | |
| 885 | memcpy(info, dev, sizeof(device_info)); |
| 886 | |
| 887 | if(mmc_write((ptn + size - 512), 512, (void *)info_buf)) |
| 888 | { |
| 889 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 890 | return; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | void read_device_info_mmc(device_info *dev) |
| 895 | { |
| 896 | struct device_info *info = (void*) info_buf; |
| 897 | unsigned long long ptn = 0; |
| 898 | unsigned long long size; |
| 899 | int index = INVALID_PTN; |
| 900 | |
| 901 | index = partition_get_index("aboot"); |
| 902 | ptn = partition_get_offset(index); |
| 903 | if(ptn == 0) |
| 904 | { |
| 905 | return; |
| 906 | } |
| 907 | |
| 908 | size = partition_get_size(index); |
| 909 | |
| 910 | if(mmc_read((ptn + size - 512), (void *)info_buf, 512)) |
| 911 | { |
| 912 | dprintf(CRITICAL, "ERROR: Cannot read device info\n"); |
| 913 | return; |
| 914 | } |
| 915 | |
| 916 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 917 | { |
| 918 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
| 919 | info->is_unlocked = 0; |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 920 | info->is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 921 | |
| 922 | write_device_info_mmc(info); |
| 923 | } |
| 924 | memcpy(dev, info, sizeof(device_info)); |
| 925 | } |
| 926 | |
| 927 | void write_device_info_flash(device_info *dev) |
| 928 | { |
| 929 | struct device_info *info = (void *) info_buf; |
| 930 | struct ptentry *ptn; |
| 931 | struct ptable *ptable; |
| 932 | |
| 933 | ptable = flash_get_ptable(); |
| 934 | if (ptable == NULL) |
| 935 | { |
| 936 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 937 | return; |
| 938 | } |
| 939 | |
| 940 | ptn = ptable_find(ptable, "devinfo"); |
| 941 | if (ptn == NULL) |
| 942 | { |
| 943 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 944 | return; |
| 945 | } |
| 946 | |
| 947 | memcpy(info, dev, sizeof(device_info)); |
| 948 | |
| 949 | if (flash_write(ptn, 0, (void *)info_buf, page_size)) |
| 950 | { |
| 951 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 952 | return; |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | void read_device_info_flash(device_info *dev) |
| 957 | { |
| 958 | struct device_info *info = (void*) info_buf; |
| 959 | struct ptentry *ptn; |
| 960 | struct ptable *ptable; |
| 961 | |
| 962 | ptable = flash_get_ptable(); |
| 963 | if (ptable == NULL) |
| 964 | { |
| 965 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | ptn = ptable_find(ptable, "devinfo"); |
| 970 | if (ptn == NULL) |
| 971 | { |
| 972 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 973 | return; |
| 974 | } |
| 975 | |
| 976 | if (flash_read(ptn, 0, (void *)info_buf, page_size)) |
| 977 | { |
| 978 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 983 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 984 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
| 985 | info->is_unlocked = 0; |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 986 | info->is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 987 | write_device_info_flash(info); |
| 988 | } |
| 989 | memcpy(dev, info, sizeof(device_info)); |
| 990 | } |
| 991 | |
| 992 | void write_device_info(device_info *dev) |
| 993 | { |
| 994 | if(target_is_emmc_boot()) |
| 995 | { |
| 996 | write_device_info_mmc(dev); |
| 997 | } |
| 998 | else |
| 999 | { |
| 1000 | write_device_info_flash(dev); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | void read_device_info(device_info *dev) |
| 1005 | { |
| 1006 | if(target_is_emmc_boot()) |
| 1007 | { |
| 1008 | read_device_info_mmc(dev); |
| 1009 | } |
| 1010 | else |
| 1011 | { |
| 1012 | read_device_info_flash(dev); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | void reset_device_info() |
| 1017 | { |
| 1018 | dprintf(ALWAYS, "reset_device_info called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1019 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1020 | write_device_info(&device); |
| 1021 | } |
| 1022 | |
| 1023 | void set_device_root() |
| 1024 | { |
| 1025 | dprintf(ALWAYS, "set_device_root called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1026 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1027 | write_device_info(&device); |
| 1028 | } |
| 1029 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1030 | #if DEVICE_TREE |
| 1031 | int copy_dtb(uint8_t *boot_image_start) |
| 1032 | { |
| 1033 | uint32 dt_image_offset = 0; |
| 1034 | uint32_t n; |
| 1035 | struct dt_table *table; |
| 1036 | struct dt_entry *dt_entry_ptr; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1037 | |
| 1038 | struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start); |
| 1039 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1040 | if(hdr->dt_size != 0) { |
| 1041 | |
| 1042 | /* add kernel offset */ |
| 1043 | dt_image_offset += page_size; |
| 1044 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 1045 | dt_image_offset += n; |
| 1046 | |
| 1047 | /* add ramdisk offset */ |
| 1048 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 1049 | dt_image_offset += n; |
| 1050 | |
| 1051 | /* add second offset */ |
| 1052 | if(hdr->second_size != 0) { |
| 1053 | n = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 1054 | dt_image_offset += n; |
| 1055 | } |
| 1056 | |
| 1057 | /* offset now point to start of dt.img */ |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1058 | table = (struct dt_table*)(boot_image_start + dt_image_offset); |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1059 | |
| 1060 | /* Restriction that the device tree entry table should be less than a page*/ |
| 1061 | ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size); |
| 1062 | |
| 1063 | /* Validate the device tree table header */ |
| 1064 | if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) { |
| 1065 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 1066 | return -1; |
| 1067 | } |
| 1068 | |
| 1069 | /* Calculate the offset of device tree within device tree table */ |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1070 | if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){ |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1071 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 1072 | return -1; |
| 1073 | } |
| 1074 | |
| 1075 | /* Read device device tree in the "tags_add */ |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1076 | memmove((void*) hdr->tags_addr, |
| 1077 | boot_image_start + dt_image_offset + dt_entry_ptr->offset, |
| 1078 | dt_entry_ptr->size); |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /* Everything looks fine. Return success. */ |
| 1082 | return 0; |
| 1083 | } |
| 1084 | #endif |
| 1085 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1086 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 1087 | { |
| 1088 | unsigned kernel_actual; |
| 1089 | unsigned ramdisk_actual; |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1090 | struct boot_img_hdr *hdr; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1091 | char *ptr = ((char*) data); |
| 1092 | |
| 1093 | if (sz < sizeof(hdr)) { |
| 1094 | fastboot_fail("invalid bootimage header"); |
| 1095 | return; |
| 1096 | } |
| 1097 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1098 | hdr = (struct boot_img_hdr *)data; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1099 | |
| 1100 | /* ensure commandline is terminated */ |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1101 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1102 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1103 | if(target_is_emmc_boot() && hdr->page_size) { |
| 1104 | page_size = hdr->page_size; |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 1105 | page_mask = page_size - 1; |
| 1106 | } |
| 1107 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1108 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 1109 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 1110 | |
| 1111 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 1112 | hdr->kernel_addr = VA(hdr->kernel_addr); |
| 1113 | hdr->ramdisk_addr = VA(hdr->ramdisk_addr); |
| 1114 | hdr->tags_addr = VA(hdr->tags_addr); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1115 | |
Shashank Mittal | 1f0e266 | 2011-09-01 15:06:00 -0700 | [diff] [blame] | 1116 | /* sz should have atleast raw boot image */ |
| 1117 | if (page_size + kernel_actual + ramdisk_actual > sz) { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1118 | fastboot_fail("incomplete bootimage"); |
| 1119 | return; |
| 1120 | } |
| 1121 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1122 | #if DEVICE_TREE |
| 1123 | /* find correct dtb and copy it to right location */ |
| 1124 | if(copy_dtb(data)) |
| 1125 | { |
| 1126 | fastboot_fail("dtb not found"); |
| 1127 | return; |
| 1128 | } |
| 1129 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1130 | |
| 1131 | fastboot_okay(""); |
| 1132 | udc_stop(); |
| 1133 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1134 | memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size); |
| 1135 | memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size); |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1136 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1137 | boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr, |
| 1138 | (const char*) hdr->cmdline, board_machtype(), |
| 1139 | (void*) hdr->ramdisk_addr, hdr->ramdisk_size); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1140 | } |
| 1141 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1142 | void cmd_erase(const char *arg, void *data, unsigned sz) |
| 1143 | { |
| 1144 | struct ptentry *ptn; |
| 1145 | struct ptable *ptable; |
| 1146 | |
| 1147 | ptable = flash_get_ptable(); |
| 1148 | if (ptable == NULL) { |
| 1149 | fastboot_fail("partition table doesn't exist"); |
| 1150 | return; |
| 1151 | } |
| 1152 | |
| 1153 | ptn = ptable_find(ptable, arg); |
| 1154 | if (ptn == NULL) { |
| 1155 | fastboot_fail("unknown partition name"); |
| 1156 | return; |
| 1157 | } |
| 1158 | |
| 1159 | if (flash_erase(ptn)) { |
| 1160 | fastboot_fail("failed to erase partition"); |
| 1161 | return; |
| 1162 | } |
| 1163 | fastboot_okay(""); |
| 1164 | } |
| 1165 | |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1166 | |
| 1167 | void cmd_erase_mmc(const char *arg, void *data, unsigned sz) |
| 1168 | { |
| 1169 | unsigned long long ptn = 0; |
neetid | b4b24d6 | 2012-01-20 12:13:09 -0800 | [diff] [blame] | 1170 | unsigned int out[512] = {0}; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1171 | int index = INVALID_PTN; |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1172 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1173 | index = partition_get_index(arg); |
| 1174 | ptn = partition_get_offset(index); |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 1175 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1176 | if(ptn == 0) { |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 1177 | fastboot_fail("Partition table doesn't exist\n"); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1178 | return; |
| 1179 | } |
neetid | b4b24d6 | 2012-01-20 12:13:09 -0800 | [diff] [blame] | 1180 | /* Simple inefficient version of erase. Just writing |
| 1181 | 0 in first block */ |
| 1182 | if (mmc_write(ptn , 512, (unsigned int *)out)) { |
| 1183 | fastboot_fail("failed to erase partition"); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1184 | return; |
| 1185 | } |
| 1186 | fastboot_okay(""); |
| 1187 | } |
| 1188 | |
| 1189 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1190 | void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz) |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1191 | { |
| 1192 | unsigned long long ptn = 0; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 1193 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1194 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 1195 | |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 1196 | if (!strcmp(arg, "partition")) |
| 1197 | { |
| 1198 | dprintf(INFO, "Attempt to write partition image.\n"); |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 1199 | if (write_partition(sz, (unsigned char *) data)) { |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 1200 | fastboot_fail("failed to write partition"); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1201 | return; |
| 1202 | } |
| 1203 | } |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 1204 | else |
| 1205 | { |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1206 | index = partition_get_index(arg); |
| 1207 | ptn = partition_get_offset(index); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 1208 | if(ptn == 0) { |
| 1209 | fastboot_fail("partition table doesn't exist"); |
| 1210 | return; |
| 1211 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1212 | |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 1213 | if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) { |
| 1214 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 1215 | fastboot_fail("image is not a boot image"); |
| 1216 | return; |
| 1217 | } |
| 1218 | } |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 1219 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1220 | size = partition_get_size(index); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 1221 | if (ROUND_TO_PAGE(sz,511) > size) { |
| 1222 | fastboot_fail("size too large"); |
| 1223 | return; |
| 1224 | } |
| 1225 | else if (mmc_write(ptn , sz, (unsigned int *)data)) { |
| 1226 | fastboot_fail("flash write failure"); |
| 1227 | return; |
| 1228 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1229 | } |
| 1230 | fastboot_okay(""); |
| 1231 | return; |
| 1232 | } |
| 1233 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1234 | void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz) |
| 1235 | { |
| 1236 | unsigned int chunk; |
| 1237 | unsigned int chunk_data_sz; |
| 1238 | sparse_header_t *sparse_header; |
| 1239 | chunk_header_t *chunk_header; |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 1240 | uint32_t total_blocks = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1241 | unsigned long long ptn = 0; |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 1242 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1243 | int index = INVALID_PTN; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1244 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1245 | index = partition_get_index(arg); |
| 1246 | ptn = partition_get_offset(index); |
| 1247 | if(ptn == 0) { |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1248 | fastboot_fail("partition table doesn't exist"); |
| 1249 | return; |
| 1250 | } |
| 1251 | |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 1252 | size = partition_get_size(index); |
| 1253 | if (ROUND_TO_PAGE(sz,511) > size) { |
| 1254 | fastboot_fail("size too large"); |
| 1255 | return; |
| 1256 | } |
| 1257 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1258 | /* Read and skip over sparse image header */ |
| 1259 | sparse_header = (sparse_header_t *) data; |
| 1260 | data += sparse_header->file_hdr_sz; |
| 1261 | if(sparse_header->file_hdr_sz > sizeof(sparse_header_t)) |
| 1262 | { |
| 1263 | /* Skip the remaining bytes in a header that is longer than |
| 1264 | * we expected. |
| 1265 | */ |
| 1266 | data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t)); |
| 1267 | } |
| 1268 | |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 1269 | dprintf (SPEW, "=== Sparse Image Header ===\n"); |
| 1270 | dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic); |
| 1271 | dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version); |
| 1272 | dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version); |
| 1273 | dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz); |
| 1274 | dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz); |
| 1275 | dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz); |
| 1276 | dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks); |
| 1277 | dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1278 | |
| 1279 | /* Start processing chunks */ |
| 1280 | for (chunk=0; chunk<sparse_header->total_chunks; chunk++) |
| 1281 | { |
| 1282 | /* Read and skip over chunk header */ |
| 1283 | chunk_header = (chunk_header_t *) data; |
| 1284 | data += sizeof(chunk_header_t); |
| 1285 | |
| 1286 | dprintf (SPEW, "=== Chunk Header ===\n"); |
| 1287 | dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type); |
| 1288 | dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz); |
| 1289 | dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz); |
| 1290 | |
| 1291 | if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) |
| 1292 | { |
| 1293 | /* Skip the remaining bytes in a header that is longer than |
| 1294 | * we expected. |
| 1295 | */ |
| 1296 | data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t)); |
| 1297 | } |
| 1298 | |
| 1299 | chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz; |
| 1300 | switch (chunk_header->chunk_type) |
| 1301 | { |
| 1302 | case CHUNK_TYPE_RAW: |
| 1303 | if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz + |
| 1304 | chunk_data_sz)) |
| 1305 | { |
| 1306 | fastboot_fail("Bogus chunk size for chunk type Raw"); |
| 1307 | return; |
| 1308 | } |
| 1309 | |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 1310 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
| 1311 | chunk_data_sz, |
| 1312 | (unsigned int*)data)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1313 | { |
| 1314 | fastboot_fail("flash write failure"); |
| 1315 | return; |
| 1316 | } |
| 1317 | total_blocks += chunk_header->chunk_sz; |
| 1318 | data += chunk_data_sz; |
| 1319 | break; |
| 1320 | |
| 1321 | case CHUNK_TYPE_DONT_CARE: |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 1322 | total_blocks += chunk_header->chunk_sz; |
| 1323 | break; |
| 1324 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1325 | case CHUNK_TYPE_CRC: |
| 1326 | if(chunk_header->total_sz != sparse_header->chunk_hdr_sz) |
| 1327 | { |
| 1328 | fastboot_fail("Bogus chunk size for chunk type Dont Care"); |
| 1329 | return; |
| 1330 | } |
| 1331 | total_blocks += chunk_header->chunk_sz; |
| 1332 | data += chunk_data_sz; |
| 1333 | break; |
| 1334 | |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 1335 | default: |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1336 | fastboot_fail("Unknown chunk type"); |
| 1337 | return; |
| 1338 | } |
| 1339 | } |
| 1340 | |
Ajay Dudani | 0c6927b | 2011-05-18 11:12:16 -0700 | [diff] [blame] | 1341 | dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n", |
| 1342 | total_blocks, sparse_header->total_blks); |
| 1343 | |
| 1344 | if(total_blocks != sparse_header->total_blks) |
| 1345 | { |
| 1346 | fastboot_fail("sparse image write failure"); |
| 1347 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1348 | |
| 1349 | fastboot_okay(""); |
| 1350 | return; |
| 1351 | } |
| 1352 | |
| 1353 | void cmd_flash_mmc(const char *arg, void *data, unsigned sz) |
| 1354 | { |
| 1355 | sparse_header_t *sparse_header; |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1356 | /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */ |
| 1357 | unsigned int *magic_number = (unsigned int *) data; |
| 1358 | int ret=0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1359 | |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 1360 | if (magic_number[0] == DECRYPT_MAGIC_0 && |
| 1361 | magic_number[1] == DECRYPT_MAGIC_1) |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1362 | { |
| 1363 | #ifdef SSD_ENABLE |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 1364 | ret = decrypt_scm((uint32 **) &data, &sz); |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1365 | #endif |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 1366 | if (ret != 0) { |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1367 | dprintf(CRITICAL, "ERROR: Invalid secure image\n"); |
| 1368 | return; |
| 1369 | } |
| 1370 | } |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 1371 | else if (magic_number[0] == ENCRYPT_MAGIC_0 && |
| 1372 | magic_number[1] == ENCRYPT_MAGIC_1) |
| 1373 | { |
| 1374 | #ifdef SSD_ENABLE |
| 1375 | ret = encrypt_scm((uint32 **) &data, &sz); |
| 1376 | #endif |
| 1377 | if (ret != 0) { |
| 1378 | dprintf(CRITICAL, "ERROR: Encryption Failure\n"); |
| 1379 | return; |
| 1380 | } |
| 1381 | } |
| 1382 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1383 | sparse_header = (sparse_header_t *) data; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1384 | if (sparse_header->magic != SPARSE_HEADER_MAGIC) |
| 1385 | cmd_flash_mmc_img(arg, data, sz); |
| 1386 | else |
| 1387 | cmd_flash_mmc_sparse_img(arg, data, sz); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1388 | return; |
| 1389 | } |
| 1390 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1391 | void cmd_flash(const char *arg, void *data, unsigned sz) |
| 1392 | { |
| 1393 | struct ptentry *ptn; |
| 1394 | struct ptable *ptable; |
| 1395 | unsigned extra = 0; |
| 1396 | |
| 1397 | ptable = flash_get_ptable(); |
| 1398 | if (ptable == NULL) { |
| 1399 | fastboot_fail("partition table doesn't exist"); |
| 1400 | return; |
| 1401 | } |
| 1402 | |
| 1403 | ptn = ptable_find(ptable, arg); |
| 1404 | if (ptn == NULL) { |
| 1405 | fastboot_fail("unknown partition name"); |
| 1406 | return; |
| 1407 | } |
| 1408 | |
| 1409 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
| 1410 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 1411 | fastboot_fail("image is not a boot image"); |
| 1412 | return; |
| 1413 | } |
| 1414 | } |
| 1415 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 1416 | if (!strcmp(ptn->name, "system") |
Deepa Dinamani | 13e32c4 | 2012-03-12 14:34:17 -0700 | [diff] [blame] | 1417 | || !strcmp(ptn->name, "userdata") |
| 1418 | || !strcmp(ptn->name, "persist") |
| 1419 | || !strcmp(ptn->name, "recoveryfs")) { |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 1420 | if (flash_ecc_bch_enabled()) |
| 1421 | /* Spare data bytes for 8 bit ECC increased by 4 */ |
| 1422 | extra = ((page_size >> 9) * 20); |
| 1423 | else |
| 1424 | extra = ((page_size >> 9) * 16); |
| 1425 | } else |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 1426 | sz = ROUND_TO_PAGE(sz, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1427 | |
| 1428 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
| 1429 | if (flash_write(ptn, extra, data, sz)) { |
| 1430 | fastboot_fail("flash write failure"); |
| 1431 | return; |
| 1432 | } |
| 1433 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 1434 | fastboot_okay(""); |
| 1435 | } |
| 1436 | |
| 1437 | void cmd_continue(const char *arg, void *data, unsigned sz) |
| 1438 | { |
| 1439 | fastboot_okay(""); |
| 1440 | udc_stop(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1441 | if (target_is_emmc_boot()) |
| 1442 | { |
| 1443 | boot_linux_from_mmc(); |
| 1444 | } |
| 1445 | else |
| 1446 | { |
| 1447 | boot_linux_from_flash(); |
| 1448 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1449 | } |
| 1450 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1451 | void cmd_reboot(const char *arg, void *data, unsigned sz) |
| 1452 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1453 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1454 | fastboot_okay(""); |
| 1455 | reboot_device(0); |
| 1456 | } |
| 1457 | |
| 1458 | void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz) |
| 1459 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1460 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1461 | fastboot_okay(""); |
| 1462 | reboot_device(FASTBOOT_MODE); |
| 1463 | } |
| 1464 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1465 | void cmd_oem_unlock(const char *arg, void *data, unsigned sz) |
| 1466 | { |
| 1467 | if(!device.is_unlocked) |
| 1468 | { |
| 1469 | device.is_unlocked = 1; |
| 1470 | write_device_info(&device); |
| 1471 | } |
| 1472 | fastboot_okay(""); |
| 1473 | } |
| 1474 | |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1475 | void cmd_oem_devinfo(const char *arg, void *data, unsigned sz) |
| 1476 | { |
| 1477 | char response[64]; |
| 1478 | snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false")); |
| 1479 | fastboot_info(response); |
| 1480 | snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false")); |
| 1481 | fastboot_info(response); |
| 1482 | fastboot_okay(""); |
| 1483 | } |
| 1484 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1485 | void splash_screen () |
| 1486 | { |
| 1487 | struct ptentry *ptn; |
| 1488 | struct ptable *ptable; |
| 1489 | struct fbcon_config *fb_display = NULL; |
| 1490 | |
| 1491 | if (!target_is_emmc_boot()) |
| 1492 | { |
| 1493 | ptable = flash_get_ptable(); |
| 1494 | if (ptable == NULL) { |
| 1495 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 1496 | return; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | ptn = ptable_find(ptable, "splash"); |
| 1500 | if (ptn == NULL) { |
| 1501 | dprintf(CRITICAL, "ERROR: No splash partition found\n"); |
| 1502 | } else { |
| 1503 | fb_display = fbcon_display(); |
| 1504 | if (fb_display) { |
| 1505 | if (flash_read(ptn, 0, fb_display->base, |
| 1506 | (fb_display->width * fb_display->height * fb_display->bpp/8))) { |
| 1507 | fbcon_clear(); |
| 1508 | dprintf(CRITICAL, "ERROR: Cannot read splash image\n"); |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1515 | void aboot_init(const struct app_descriptor *app) |
| 1516 | { |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 1517 | unsigned reboot_mode = 0; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1518 | unsigned usb_init = 0; |
Vivek Mehta | 5f1c9d4 | 2011-04-01 20:11:59 -0700 | [diff] [blame] | 1519 | unsigned sz = 0; |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 1520 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1521 | /* Setup page size information for nand/emmc reads */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1522 | if (target_is_emmc_boot()) |
| 1523 | { |
| 1524 | page_size = 2048; |
| 1525 | page_mask = page_size - 1; |
| 1526 | } |
| 1527 | else |
| 1528 | { |
| 1529 | page_size = flash_page_size(); |
| 1530 | page_mask = page_size - 1; |
| 1531 | } |
| 1532 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1533 | if(target_use_signed_kernel()) |
| 1534 | { |
| 1535 | read_device_info(&device); |
| 1536 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 1539 | target_serialno((unsigned char *) sn_buf); |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 1540 | dprintf(SPEW,"serial number: %s\n",sn_buf); |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 1541 | surf_udc_device.serialno = sn_buf; |
| 1542 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1543 | /* Check if we should do something other than booting up */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1544 | if (keys_get_state(KEY_HOME) != 0) |
| 1545 | boot_into_recovery = 1; |
Wentao Xu | 153902c | 2010-12-20 16:20:52 -0500 | [diff] [blame] | 1546 | if (keys_get_state(KEY_VOLUMEUP) != 0) |
| 1547 | boot_into_recovery = 1; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 1548 | if(!boot_into_recovery) |
| 1549 | { |
| 1550 | if (keys_get_state(KEY_BACK) != 0) |
| 1551 | goto fastboot; |
| 1552 | if (keys_get_state(KEY_VOLUMEDOWN) != 0) |
| 1553 | goto fastboot; |
| 1554 | } |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1555 | |
| 1556 | #if NO_KEYPAD_DRIVER |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 1557 | if (fastboot_trigger()) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1558 | goto fastboot; |
| 1559 | #endif |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 1560 | |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 1561 | reboot_mode = check_reboot_mode(); |
| 1562 | if (reboot_mode == RECOVERY_MODE) { |
| 1563 | boot_into_recovery = 1; |
| 1564 | } else if(reboot_mode == FASTBOOT_MODE) { |
| 1565 | goto fastboot; |
| 1566 | } |
| 1567 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1568 | if (target_is_emmc_boot()) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1569 | { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 1570 | if(emmc_recovery_init()) |
| 1571 | dprintf(ALWAYS,"error in emmc_recovery_init\n"); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1572 | if(target_use_signed_kernel()) |
| 1573 | { |
| 1574 | if((device.is_unlocked) || (device.is_tampered)) |
| 1575 | { |
| 1576 | #ifdef TZ_TAMPER_FUSE |
| 1577 | set_tamper_fuse_cmd(); |
| 1578 | #endif |
Channagoud Kadabi | bf695c6 | 2012-04-10 13:31:56 +0530 | [diff] [blame] | 1579 | #if USE_PCOM_SECBOOT |
| 1580 | set_tamper_flag(device.is_tampered); |
| 1581 | #endif |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1582 | } |
| 1583 | } |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1584 | boot_linux_from_mmc(); |
| 1585 | } |
| 1586 | else |
| 1587 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1588 | recovery_init(); |
Channagoud Kadabi | e7b6670 | 2012-03-22 15:54:30 +0530 | [diff] [blame] | 1589 | #if USE_PCOM_SECBOOT |
| 1590 | if((device.is_unlocked) || (device.is_tampered)) |
| 1591 | set_tamper_flag(device.is_tampered); |
| 1592 | #endif |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1593 | boot_linux_from_flash(); |
| 1594 | } |
Dima Zavin | b428360 | 2009-01-26 16:36:57 -0800 | [diff] [blame] | 1595 | dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting " |
| 1596 | "to fastboot mode.\n"); |
| 1597 | |
| 1598 | fastboot: |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1599 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1600 | target_fastboot_init(); |
Amol Jadi | 57abe4c | 2011-05-24 15:47:27 -0700 | [diff] [blame] | 1601 | |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1602 | if(!usb_init) |
| 1603 | udc_init(&surf_udc_device); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1604 | |
| 1605 | fastboot_register("boot", cmd_boot); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1606 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1607 | if (target_is_emmc_boot()) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1608 | { |
| 1609 | fastboot_register("flash:", cmd_flash_mmc); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1610 | fastboot_register("erase:", cmd_erase_mmc); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1611 | } |
| 1612 | else |
| 1613 | { |
| 1614 | fastboot_register("flash:", cmd_flash); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1615 | fastboot_register("erase:", cmd_erase); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | fastboot_register("continue", cmd_continue); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1619 | fastboot_register("reboot", cmd_reboot); |
| 1620 | fastboot_register("reboot-bootloader", cmd_reboot_bootloader); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1621 | fastboot_register("oem unlock", cmd_oem_unlock); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1622 | fastboot_register("oem device-info", cmd_oem_devinfo); |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 1623 | fastboot_publish("product", TARGET(BOARD)); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1624 | fastboot_publish("kernel", "lk"); |
Trevor Bourget | 59b25d5 | 2012-01-13 18:43:36 -0800 | [diff] [blame] | 1625 | fastboot_publish("serialno", sn_buf); |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1626 | partition_dump(); |
Vivek Mehta | 5f1c9d4 | 2011-04-01 20:11:59 -0700 | [diff] [blame] | 1627 | sz = target_get_max_flash_size(); |
| 1628 | fastboot_init(target_get_scratch_address(), sz); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1629 | udc_start(); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | APP_START(aboot) |
| 1633 | .init = aboot_init, |
| 1634 | APP_END |