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