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