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