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