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