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