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