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