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