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> |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 36 | #include <string.h> |
Channagoud Kadabi | 132ff55 | 2013-04-19 14:34:44 -0700 | [diff] [blame] | 37 | #include <stdlib.h> |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 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> |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 53 | #include <sha.h> |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 54 | #include <platform/iomap.h> |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 55 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 56 | #if DEVICE_TREE |
| 57 | #include <libfdt.h> |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 58 | #include <dev_tree.h> |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 59 | #endif |
| 60 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 61 | #include "image_verify.h" |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 62 | #include "recovery.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 63 | #include "bootimg.h" |
| 64 | #include "fastboot.h" |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 65 | #include "sparse_format.h" |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 66 | #include "mmc.h" |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 67 | #include "devinfo.h" |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 68 | #include "board.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 69 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 70 | #include "scm.h" |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 71 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 72 | extern bool target_use_signed_kernel(void); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 73 | extern void platform_uninit(void); |
Channagoud Kadabi | 33defe2 | 2013-06-18 18:35:40 -0700 | [diff] [blame] | 74 | extern void target_uninit(void); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 75 | |
| 76 | void write_device_info_mmc(device_info *dev); |
| 77 | void write_device_info_flash(device_info *dev); |
| 78 | |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 79 | #define EXPAND(NAME) #NAME |
| 80 | #define TARGET(NAME) EXPAND(NAME) |
Brian Swetland | 2defe16 | 2009-08-18 14:35:59 -0700 | [diff] [blame] | 81 | |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 82 | #ifdef MEMBASE |
| 83 | #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE)) |
| 84 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 85 | #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000 |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 86 | #endif |
| 87 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 88 | #ifndef MEMSIZE |
| 89 | #define MEMSIZE 1024*1024 |
| 90 | #endif |
| 91 | |
| 92 | #define MAX_TAGS_SIZE 1024 |
| 93 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 94 | #define RECOVERY_MODE 0x77665502 |
| 95 | #define FASTBOOT_MODE 0x77665500 |
| 96 | |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 97 | /* make 4096 as default size to ensure EFS,EXT4's erasing */ |
| 98 | #define DEFAULT_ERASE_SIZE 4096 |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 99 | #define MAX_PANEL_BUF_SIZE 64 |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 100 | |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 101 | #if UFS_SUPPORT |
| 102 | static const char *emmc_cmdline = " androidboot.bootdevice=msm_sdcc.1"; |
| 103 | static const char *ufs_cmdline = " androidboot.bootdevice=msm_ufs.1"; |
| 104 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 105 | static const char *emmc_cmdline = " androidboot.emmc=true"; |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 106 | #endif |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 107 | static const char *usb_sn_cmdline = " androidboot.serialno="; |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 108 | static const char *androidboot_mode = " androidboot.mode="; |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 109 | static const char *display_cmdline = " mdss_mdp.panel="; |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 110 | static const char *loglevel = " quiet"; |
Ajay Dudani | ca3a33c | 2011-11-18 08:31:40 -0800 | [diff] [blame] | 111 | static const char *battchg_pause = " androidboot.mode=charger"; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 112 | static const char *auth_kernel = " androidboot.authorized_kernel=true"; |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 113 | static const char *secondary_gpt_enable = " gpt"; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 114 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 115 | static const char *baseband_apq = " androidboot.baseband=apq"; |
| 116 | static const char *baseband_msm = " androidboot.baseband=msm"; |
| 117 | static const char *baseband_csfb = " androidboot.baseband=csfb"; |
| 118 | static const char *baseband_svlte2a = " androidboot.baseband=svlte2a"; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 119 | static const char *baseband_mdm = " androidboot.baseband=mdm"; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 120 | static const char *baseband_sglte = " androidboot.baseband=sglte"; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 121 | static const char *baseband_dsda = " androidboot.baseband=dsda"; |
| 122 | static const char *baseband_dsda2 = " androidboot.baseband=dsda2"; |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 123 | static const char *baseband_sglte2 = " androidboot.baseband=sglte2"; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 124 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 125 | static unsigned page_size = 0; |
| 126 | static unsigned page_mask = 0; |
| 127 | static char ffbm_mode_string[FFBM_MODE_BUF_SIZE]; |
| 128 | static bool boot_into_ffbm; |
| 129 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 130 | /* Assuming unauthorized kernel image by default */ |
| 131 | static int auth_kernel_img = 0; |
| 132 | |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 133 | static device_info device = {DEVICE_MAGIC, 0, 0, 0}; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 134 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 135 | struct atag_ptbl_entry |
| 136 | { |
| 137 | char name[16]; |
| 138 | unsigned offset; |
| 139 | unsigned size; |
| 140 | unsigned flags; |
| 141 | }; |
| 142 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 143 | /* |
| 144 | * Partition info, required to be published |
| 145 | * for fastboot |
| 146 | */ |
| 147 | struct getvar_partition_info { |
| 148 | const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */ |
| 149 | char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */ |
| 150 | char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */ |
| 151 | char size_response[MAX_RSP_SIZE]; /* fastboot response for size */ |
| 152 | char type_response[MAX_RSP_SIZE]; /* fastboot response for type */ |
| 153 | }; |
| 154 | |
| 155 | /* |
| 156 | * Right now, we are publishing the info for only |
| 157 | * three partitions |
| 158 | */ |
| 159 | struct getvar_partition_info part_info[] = |
| 160 | { |
| 161 | { "system" , "partition-size:", "partition-type:", "", "ext4" }, |
| 162 | { "userdata", "partition-size:", "partition-type:", "", "ext4" }, |
| 163 | { "cache" , "partition-size:", "partition-type:", "", "ext4" }, |
| 164 | }; |
| 165 | |
| 166 | char max_download_size[MAX_RSP_SIZE]; |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 167 | char charger_screen_enabled[MAX_RSP_SIZE]; |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 168 | char sn_buf[13]; |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 169 | char display_panel_buf[MAX_PANEL_BUF_SIZE]; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 170 | |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 171 | extern int emmc_recovery_init(void); |
| 172 | |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 173 | #if NO_KEYPAD_DRIVER |
| 174 | extern int fastboot_trigger(void); |
| 175 | #endif |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 176 | |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 177 | static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr) |
| 178 | { |
| 179 | /* overwrite the destination of specified for the project */ |
Channagoud Kadabi | 7042fa3 | 2013-04-26 16:44:14 -0700 | [diff] [blame] | 180 | #ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS |
| 181 | hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR; |
| 182 | hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR; |
| 183 | hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR; |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 184 | #endif |
| 185 | } |
| 186 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 187 | static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn) |
| 188 | { |
| 189 | struct atag_ptbl_entry atag_ptn; |
| 190 | |
| 191 | memcpy(atag_ptn.name, ptn->name, 16); |
| 192 | atag_ptn.name[15] = '\0'; |
| 193 | atag_ptn.offset = ptn->start; |
| 194 | atag_ptn.size = ptn->length; |
| 195 | atag_ptn.flags = ptn->flags; |
| 196 | memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry)); |
| 197 | *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned); |
| 198 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 199 | |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 200 | #if UFS_SUPPORT |
| 201 | char* get_boot_dev_cmdline() |
| 202 | { |
| 203 | const char *boot_device; |
| 204 | uint32_t val = 0; |
| 205 | |
| 206 | val = target_get_boot_device(); |
| 207 | switch(val) |
| 208 | { |
| 209 | case BOOT_DEFAULT: |
| 210 | case BOOT_EMMC: |
| 211 | boot_device = emmc_cmdline; |
| 212 | break; |
| 213 | case BOOT_UFS: |
| 214 | boot_device = ufs_cmdline; |
| 215 | break; |
| 216 | default: |
| 217 | dprintf(CRITICAL,"ERROR: Unexpected boot_device val=%x",val); |
| 218 | ASSERT(0); |
| 219 | }; |
| 220 | |
| 221 | return boot_device; |
| 222 | } |
| 223 | #endif |
| 224 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 225 | unsigned char *update_cmdline(const char * cmdline) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 226 | { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 227 | int cmdline_len = 0; |
| 228 | int have_cmdline = 0; |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 229 | unsigned char *cmdline_final = NULL; |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 230 | #if UFS_SUPPORT |
| 231 | const char *boot_dev_cmdline = NULL; |
| 232 | #endif |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 233 | int pause_at_bootup = 0; |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 234 | bool gpt_exists = partition_gpt_exists(); |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 235 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 236 | if (cmdline && cmdline[0]) { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 237 | cmdline_len = strlen(cmdline); |
| 238 | have_cmdline = 1; |
| 239 | } |
| 240 | if (target_is_emmc_boot()) { |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 241 | #if UFS_SUPPORT |
| 242 | boot_dev_cmdline = get_boot_dev_cmdline(); |
| 243 | cmdline_len += strlen(boot_dev_cmdline); |
| 244 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 245 | cmdline_len += strlen(emmc_cmdline); |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 246 | #endif |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 247 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 248 | |
| 249 | cmdline_len += strlen(usb_sn_cmdline); |
| 250 | cmdline_len += strlen(sn_buf); |
| 251 | |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 252 | if (boot_into_recovery && gpt_exists) |
| 253 | cmdline_len += strlen(secondary_gpt_enable); |
| 254 | |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 255 | if (boot_into_ffbm) { |
| 256 | cmdline_len += strlen(androidboot_mode); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 257 | cmdline_len += strlen(ffbm_mode_string); |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 258 | /* reduce kernel console messages to speed-up boot */ |
| 259 | cmdline_len += strlen(loglevel); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 260 | } else if (device.charger_screen_enabled && |
| 261 | target_pause_for_battery_charge()) { |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 262 | pause_at_bootup = 1; |
| 263 | cmdline_len += strlen(battchg_pause); |
| 264 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 265 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 266 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 267 | cmdline_len += strlen(auth_kernel); |
| 268 | } |
| 269 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 270 | /* Determine correct androidboot.baseband to use */ |
| 271 | switch(target_baseband()) |
| 272 | { |
| 273 | case BASEBAND_APQ: |
| 274 | cmdline_len += strlen(baseband_apq); |
| 275 | break; |
| 276 | |
| 277 | case BASEBAND_MSM: |
| 278 | cmdline_len += strlen(baseband_msm); |
| 279 | break; |
| 280 | |
| 281 | case BASEBAND_CSFB: |
| 282 | cmdline_len += strlen(baseband_csfb); |
| 283 | break; |
| 284 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 285 | case BASEBAND_SVLTE2A: |
| 286 | cmdline_len += strlen(baseband_svlte2a); |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 287 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 288 | |
| 289 | case BASEBAND_MDM: |
| 290 | cmdline_len += strlen(baseband_mdm); |
| 291 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 292 | |
| 293 | case BASEBAND_SGLTE: |
| 294 | cmdline_len += strlen(baseband_sglte); |
| 295 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 296 | |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 297 | case BASEBAND_SGLTE2: |
| 298 | cmdline_len += strlen(baseband_sglte2); |
| 299 | break; |
| 300 | |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 301 | case BASEBAND_DSDA: |
| 302 | cmdline_len += strlen(baseband_dsda); |
| 303 | break; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 304 | |
| 305 | case BASEBAND_DSDA2: |
| 306 | cmdline_len += strlen(baseband_dsda2); |
| 307 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 310 | if (target_display_panel_node(display_panel_buf, MAX_PANEL_BUF_SIZE) && |
| 311 | strlen(display_panel_buf)) |
| 312 | { |
| 313 | cmdline_len += strlen(display_cmdline); |
| 314 | cmdline_len += strlen(display_panel_buf); |
| 315 | } |
| 316 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 317 | if (cmdline_len > 0) { |
| 318 | const char *src; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 319 | unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3)); |
| 320 | ASSERT(dst != NULL); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 321 | |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 322 | /* Save start ptr for debug print */ |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 323 | cmdline_final = dst; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 324 | if (have_cmdline) { |
| 325 | src = cmdline; |
| 326 | while ((*dst++ = *src++)); |
| 327 | } |
| 328 | if (target_is_emmc_boot()) { |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 329 | #if UFS_SUPPORT |
| 330 | src = boot_dev_cmdline; |
| 331 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 332 | src = emmc_cmdline; |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 333 | #endif |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 334 | if (have_cmdline) --dst; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 335 | have_cmdline = 1; |
| 336 | while ((*dst++ = *src++)); |
| 337 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 338 | |
| 339 | src = usb_sn_cmdline; |
| 340 | if (have_cmdline) --dst; |
| 341 | have_cmdline = 1; |
| 342 | while ((*dst++ = *src++)); |
| 343 | src = sn_buf; |
| 344 | if (have_cmdline) --dst; |
| 345 | have_cmdline = 1; |
| 346 | while ((*dst++ = *src++)); |
| 347 | |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 348 | if (boot_into_recovery && gpt_exists) { |
| 349 | src = secondary_gpt_enable; |
| 350 | if (have_cmdline) --dst; |
| 351 | while ((*dst++ = *src++)); |
| 352 | } |
| 353 | |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 354 | if (boot_into_ffbm) { |
| 355 | src = androidboot_mode; |
| 356 | if (have_cmdline) --dst; |
| 357 | while ((*dst++ = *src++)); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 358 | src = ffbm_mode_string; |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 359 | if (have_cmdline) --dst; |
| 360 | while ((*dst++ = *src++)); |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 361 | src = loglevel; |
| 362 | if (have_cmdline) --dst; |
| 363 | while ((*dst++ = *src++)); |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 364 | } else if (pause_at_bootup) { |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 365 | src = battchg_pause; |
| 366 | if (have_cmdline) --dst; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 367 | while ((*dst++ = *src++)); |
| 368 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 369 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 370 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 371 | src = auth_kernel; |
| 372 | if (have_cmdline) --dst; |
| 373 | while ((*dst++ = *src++)); |
| 374 | } |
| 375 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 376 | switch(target_baseband()) |
| 377 | { |
| 378 | case BASEBAND_APQ: |
| 379 | src = baseband_apq; |
| 380 | if (have_cmdline) --dst; |
| 381 | while ((*dst++ = *src++)); |
| 382 | break; |
| 383 | |
| 384 | case BASEBAND_MSM: |
| 385 | src = baseband_msm; |
| 386 | if (have_cmdline) --dst; |
| 387 | while ((*dst++ = *src++)); |
| 388 | break; |
| 389 | |
| 390 | case BASEBAND_CSFB: |
| 391 | src = baseband_csfb; |
| 392 | if (have_cmdline) --dst; |
| 393 | while ((*dst++ = *src++)); |
| 394 | break; |
| 395 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 396 | case BASEBAND_SVLTE2A: |
| 397 | src = baseband_svlte2a; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 398 | if (have_cmdline) --dst; |
| 399 | while ((*dst++ = *src++)); |
| 400 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 401 | |
| 402 | case BASEBAND_MDM: |
| 403 | src = baseband_mdm; |
| 404 | if (have_cmdline) --dst; |
| 405 | while ((*dst++ = *src++)); |
| 406 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 407 | |
| 408 | case BASEBAND_SGLTE: |
| 409 | src = baseband_sglte; |
| 410 | if (have_cmdline) --dst; |
| 411 | while ((*dst++ = *src++)); |
| 412 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 413 | |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 414 | case BASEBAND_SGLTE2: |
| 415 | src = baseband_sglte2; |
| 416 | if (have_cmdline) --dst; |
| 417 | while ((*dst++ = *src++)); |
| 418 | break; |
| 419 | |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 420 | case BASEBAND_DSDA: |
| 421 | src = baseband_dsda; |
| 422 | if (have_cmdline) --dst; |
| 423 | while ((*dst++ = *src++)); |
| 424 | break; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 425 | |
| 426 | case BASEBAND_DSDA2: |
| 427 | src = baseband_dsda2; |
| 428 | if (have_cmdline) --dst; |
| 429 | while ((*dst++ = *src++)); |
| 430 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 431 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 432 | |
| 433 | if (strlen(display_panel_buf)) { |
| 434 | src = display_cmdline; |
| 435 | if (have_cmdline) --dst; |
| 436 | while ((*dst++ = *src++)); |
| 437 | src = display_panel_buf; |
| 438 | if (have_cmdline) --dst; |
| 439 | while ((*dst++ = *src++)); |
| 440 | } |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 441 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 442 | |
| 443 | |
Deepa Dinamani | 8b8736d | 2012-12-19 15:00:56 -0800 | [diff] [blame] | 444 | dprintf(INFO, "cmdline: %s\n", cmdline_final); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 445 | return cmdline_final; |
| 446 | } |
| 447 | |
| 448 | unsigned *atag_core(unsigned *ptr) |
| 449 | { |
| 450 | /* CORE */ |
| 451 | *ptr++ = 2; |
| 452 | *ptr++ = 0x54410001; |
| 453 | |
| 454 | return ptr; |
| 455 | |
| 456 | } |
| 457 | |
| 458 | unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk, |
| 459 | unsigned ramdisk_size) |
| 460 | { |
| 461 | if (ramdisk_size) { |
| 462 | *ptr++ = 4; |
| 463 | *ptr++ = 0x54420005; |
| 464 | *ptr++ = (unsigned)ramdisk; |
| 465 | *ptr++ = ramdisk_size; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 468 | return ptr; |
| 469 | } |
| 470 | |
| 471 | unsigned *atag_ptable(unsigned **ptr_addr) |
| 472 | { |
| 473 | int i; |
| 474 | struct ptable *ptable; |
| 475 | |
| 476 | if ((ptable = flash_get_ptable()) && (ptable->count != 0)) { |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 477 | *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) / |
| 478 | sizeof(unsigned))); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 479 | *(*ptr_addr)++ = 0x4d534d70; |
| 480 | for (i = 0; i < ptable->count; ++i) |
| 481 | ptentry_to_tag(ptr_addr, ptable_get(ptable, i)); |
| 482 | } |
| 483 | |
| 484 | return (*ptr_addr); |
| 485 | } |
| 486 | |
| 487 | unsigned *atag_cmdline(unsigned *ptr, const char *cmdline) |
| 488 | { |
| 489 | int cmdline_length = 0; |
| 490 | int n; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 491 | char *dest; |
| 492 | |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 493 | cmdline_length = strlen((const char*)cmdline); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 494 | n = (cmdline_length + 4) & (~3); |
| 495 | |
| 496 | *ptr++ = (n / 4) + 2; |
| 497 | *ptr++ = 0x54410009; |
| 498 | dest = (char *) ptr; |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 499 | while ((*dest++ = *cmdline++)); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 500 | ptr += (n / 4); |
| 501 | |
| 502 | return ptr; |
| 503 | } |
| 504 | |
| 505 | unsigned *atag_end(unsigned *ptr) |
| 506 | { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 507 | /* END */ |
| 508 | *ptr++ = 0; |
| 509 | *ptr++ = 0; |
| 510 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 511 | return ptr; |
| 512 | } |
| 513 | |
| 514 | void generate_atags(unsigned *ptr, const char *cmdline, |
| 515 | void *ramdisk, unsigned ramdisk_size) |
| 516 | { |
| 517 | |
| 518 | ptr = atag_core(ptr); |
| 519 | ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size); |
| 520 | ptr = target_atag_mem(ptr); |
| 521 | |
| 522 | /* Skip NAND partition ATAGS for eMMC boot */ |
| 523 | if (!target_is_emmc_boot()){ |
| 524 | ptr = atag_ptable(&ptr); |
| 525 | } |
| 526 | |
| 527 | ptr = atag_cmdline(ptr, cmdline); |
| 528 | ptr = atag_end(ptr); |
| 529 | } |
| 530 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 531 | typedef void entry_func_ptr(unsigned, unsigned, unsigned*); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 532 | void boot_linux(void *kernel, unsigned *tags, |
| 533 | const char *cmdline, unsigned machtype, |
| 534 | void *ramdisk, unsigned ramdisk_size) |
| 535 | { |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 536 | unsigned char *final_cmdline; |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 537 | #if DEVICE_TREE |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 538 | int ret = 0; |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 539 | #endif |
| 540 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 541 | void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel)); |
Deepa Dinamani | 25a9f76 | 2012-11-30 15:57:15 -0800 | [diff] [blame] | 542 | uint32_t tags_phys = PA((addr_t)tags); |
| 543 | |
| 544 | ramdisk = PA(ramdisk); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 545 | |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 546 | final_cmdline = update_cmdline((const char*)cmdline); |
| 547 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 548 | #if DEVICE_TREE |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 549 | dprintf(INFO, "Updating device tree: start\n"); |
| 550 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 551 | /* Update the Device Tree */ |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 552 | ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 553 | if(ret) |
| 554 | { |
| 555 | dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n"); |
| 556 | ASSERT(0); |
| 557 | } |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 558 | dprintf(INFO, "Updating device tree: done\n"); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 559 | #else |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 560 | /* Generating the Atags */ |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 561 | generate_atags(tags, final_cmdline, ramdisk, ramdisk_size); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 562 | #endif |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 563 | |
Channagoud Kadabi | 33defe2 | 2013-06-18 18:35:40 -0700 | [diff] [blame] | 564 | /* Perform target specific cleanup */ |
| 565 | target_uninit(); |
| 566 | |
Deepa Dinamani | 33734bc | 2013-03-06 12:16:06 -0800 | [diff] [blame] | 567 | dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n", |
| 568 | entry, ramdisk, ramdisk_size, tags_phys); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 569 | |
| 570 | enter_critical_section(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 571 | |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 572 | /* do any platform specific cleanup before kernel entry */ |
| 573 | platform_uninit(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 574 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 575 | arch_disable_cache(UCACHE); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 576 | |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 577 | #if ARM_WITH_MMU |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 578 | arch_disable_mmu(); |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 579 | #endif |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 580 | bs_set_timestamp(BS_KERNEL_ENTRY); |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 581 | entry(0, machtype, (unsigned*)tags_phys); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 584 | /* Function to check if the memory address range falls within the aboot |
| 585 | * boundaries. |
| 586 | * start: Start of the memory region |
| 587 | * size: Size of the memory region |
| 588 | */ |
| 589 | int check_aboot_addr_range_overlap(uint32_t start, uint32_t size) |
| 590 | { |
| 591 | /* Check for boundary conditions. */ |
| 592 | if ((start + size) < start) |
| 593 | return -1; |
| 594 | |
| 595 | /* Check for memory overlap. */ |
| 596 | if ((start < MEMBASE) && ((start + size) <= MEMBASE)) |
| 597 | return 0; |
Channagoud Kadabi | 9414391 | 2013-10-15 12:53:52 -0700 | [diff] [blame^] | 598 | else if (start >= (MEMBASE + MEMSIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 599 | return 0; |
| 600 | else |
| 601 | return -1; |
| 602 | } |
| 603 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 604 | #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y))) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 605 | |
Channagoud Kadabi | 132ff55 | 2013-04-19 14:34:44 -0700 | [diff] [blame] | 606 | BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 607 | #if DEVICE_TREE |
Channagoud Kadabi | 132ff55 | 2013-04-19 14:34:44 -0700 | [diff] [blame] | 608 | BUF_DMA_ALIGN(dt_buf, 4096); |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 609 | #endif |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 610 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 611 | static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size) |
| 612 | { |
| 613 | int ret; |
| 614 | |
| 615 | /* Assume device is rooted at this time. */ |
| 616 | device.is_tampered = 1; |
| 617 | |
| 618 | dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size); |
| 619 | |
| 620 | ret = image_verify((unsigned char *)bootimg_addr, |
| 621 | (unsigned char *)(bootimg_addr + bootimg_size), |
| 622 | bootimg_size, |
| 623 | CRYPTO_AUTH_ALG_SHA256); |
| 624 | |
| 625 | dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret); |
| 626 | |
| 627 | if (ret) |
| 628 | { |
| 629 | /* Authorized kernel */ |
| 630 | device.is_tampered = 0; |
Sundarajan Srinivasan | 3fb21f1 | 2013-09-16 18:36:15 -0700 | [diff] [blame] | 631 | auth_kernel_img = 1; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | #if USE_PCOM_SECBOOT |
| 635 | set_tamper_flag(device.is_tampered); |
| 636 | #endif |
| 637 | |
| 638 | if(device.is_tampered) |
| 639 | { |
| 640 | write_device_info_mmc(&device); |
| 641 | #ifdef TZ_TAMPER_FUSE |
| 642 | set_tamper_fuse_cmd(); |
| 643 | #endif |
| 644 | #ifdef ASSERT_ON_TAMPER |
| 645 | dprintf(CRITICAL, "Device is tampered. Asserting..\n"); |
| 646 | ASSERT(0); |
| 647 | #endif |
| 648 | } |
| 649 | } |
| 650 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 651 | int boot_linux_from_mmc(void) |
| 652 | { |
| 653 | struct boot_img_hdr *hdr = (void*) buf; |
| 654 | struct boot_img_hdr *uhdr; |
| 655 | unsigned offset = 0; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 656 | int rcode; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 657 | unsigned long long ptn = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 658 | int index = INVALID_PTN; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 659 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 660 | unsigned char *image_addr = 0; |
| 661 | unsigned kernel_actual; |
| 662 | unsigned ramdisk_actual; |
| 663 | unsigned imagesize_actual; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 664 | unsigned second_actual = 0; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 665 | |
| 666 | #if DEVICE_TREE |
| 667 | struct dt_table *table; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 668 | struct dt_entry dt_entry; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 669 | unsigned dt_table_offset; |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 670 | uint32_t dt_actual; |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 671 | uint32_t dt_hdr_size; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 672 | #endif |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 673 | if (!boot_into_recovery) { |
| 674 | memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string)); |
| 675 | rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string)); |
| 676 | if (rcode <= 0) { |
| 677 | boot_into_ffbm = false; |
| 678 | if (rcode < 0) |
| 679 | dprintf(CRITICAL,"failed to get ffbm cookie"); |
| 680 | } else |
| 681 | boot_into_ffbm = true; |
| 682 | } else |
| 683 | boot_into_ffbm = false; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 684 | uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 685 | if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 686 | dprintf(INFO, "Unified boot method!\n"); |
| 687 | hdr = uhdr; |
| 688 | goto unified_boot; |
| 689 | } |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 690 | if (!boot_into_recovery) { |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 691 | index = partition_get_index("boot"); |
| 692 | ptn = partition_get_offset(index); |
| 693 | if(ptn == 0) { |
Shashank Mittal | 85b91f6 | 2010-10-30 10:12:38 -0700 | [diff] [blame] | 694 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 695 | return -1; |
| 696 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 697 | } |
| 698 | else { |
| 699 | index = partition_get_index("recovery"); |
| 700 | ptn = partition_get_offset(index); |
| 701 | if(ptn == 0) { |
Shashank Mittal | 85b91f6 | 2010-10-30 10:12:38 -0700 | [diff] [blame] | 702 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 703 | return -1; |
| 704 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 707 | if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) { |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 708 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 709 | return -1; |
| 710 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 711 | |
| 712 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 713 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 714 | return -1; |
| 715 | } |
| 716 | |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 717 | if (hdr->page_size && (hdr->page_size != page_size)) { |
| 718 | page_size = hdr->page_size; |
| 719 | page_mask = page_size - 1; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 722 | /* |
| 723 | * Update the kernel/ramdisk/tags address if the boot image header |
| 724 | * has default values, these default values come from mkbootimg when |
| 725 | * the boot image is flashed using fastboot flash:raw |
| 726 | */ |
| 727 | update_ker_tags_rdisk_addr(hdr); |
| 728 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 729 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 730 | hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr)); |
| 731 | hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr)); |
| 732 | hdr->tags_addr = VA((addr_t)(hdr->tags_addr)); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 733 | |
| 734 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 735 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 736 | |
| 737 | /* Check if the addresses in the header are valid. */ |
| 738 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) || |
| 739 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual)) |
| 740 | { |
| 741 | dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n"); |
| 742 | return -1; |
| 743 | } |
| 744 | |
| 745 | #ifndef DEVICE_TREE |
| 746 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE)) |
| 747 | { |
| 748 | dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n"); |
| 749 | return -1; |
| 750 | } |
| 751 | #endif |
| 752 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 753 | /* Authenticate Kernel */ |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 754 | dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n", |
| 755 | (int) target_use_signed_kernel(), |
| 756 | device.is_unlocked, |
| 757 | device.is_tampered); |
| 758 | |
Deepa Dinamani | 23b60d4 | 2013-06-24 18:10:52 -0700 | [diff] [blame] | 759 | if(target_use_signed_kernel() && (!device.is_unlocked)) |
Subbaraman Narayanamurthy | 958fa24 | 2011-01-27 17:42:38 -0800 | [diff] [blame] | 760 | { |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 761 | offset = 0; |
| 762 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 763 | image_addr = (unsigned char *)target_get_scratch_address(); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 764 | |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 765 | #if DEVICE_TREE |
| 766 | dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask); |
| 767 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 768 | |
Deepa Dinamani | d14bb4d | 2013-07-12 17:28:29 -0700 | [diff] [blame] | 769 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 770 | { |
| 771 | dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n"); |
| 772 | return -1; |
| 773 | } |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 774 | #else |
Channagoud Kadabi | 4b27651 | 2012-08-28 15:16:30 +0530 | [diff] [blame] | 775 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 776 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 777 | #endif |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 778 | |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 779 | dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual); |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 780 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 781 | |
Deepa Dinamani | d14bb4d | 2013-07-12 17:28:29 -0700 | [diff] [blame] | 782 | if (check_aboot_addr_range_overlap(image_addr, imagesize_actual)) |
| 783 | { |
| 784 | dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n"); |
| 785 | return -1; |
| 786 | } |
| 787 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 788 | /* Read image without signature */ |
| 789 | if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) |
| 790 | { |
| 791 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 792 | return -1; |
Subbaraman Narayanamurthy | 958fa24 | 2011-01-27 17:42:38 -0800 | [diff] [blame] | 793 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 794 | |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 795 | dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual); |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 796 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 797 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 798 | offset = imagesize_actual; |
Deepa Dinamani | d14bb4d | 2013-07-12 17:28:29 -0700 | [diff] [blame] | 799 | |
| 800 | if (check_aboot_addr_range_overlap(image_addr + offset, page_size)) |
| 801 | { |
| 802 | dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n"); |
| 803 | return -1; |
| 804 | } |
| 805 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 806 | /* Read signature */ |
| 807 | if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size)) |
| 808 | { |
| 809 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 810 | return -1; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 811 | } |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 812 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 813 | verify_signed_bootimg(image_addr, imagesize_actual); |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 814 | |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 815 | /* Move kernel, ramdisk and device tree to correct address */ |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 816 | memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size); |
| 817 | memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 818 | |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 819 | #if DEVICE_TREE |
| 820 | if(hdr->dt_size) { |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 821 | dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual); |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 822 | table = (struct dt_table*) dt_table_offset; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 823 | |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 824 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 825 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 826 | return -1; |
| 827 | } |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 828 | |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 829 | /* Find index of device tree within device tree table */ |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 830 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 831 | dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n"); |
| 832 | return -1; |
| 833 | } |
| 834 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 835 | /* Validate and Read device device tree in the "tags_add */ |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 836 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 837 | { |
| 838 | dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n"); |
| 839 | return -1; |
| 840 | } |
| 841 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 842 | memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size); |
Channagoud Kadabi | 3509562 | 2013-03-01 13:53:05 -0800 | [diff] [blame] | 843 | } else { |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 844 | /* |
| 845 | * If appended dev tree is found, update the atags with |
| 846 | * memory address to the DTB appended location on RAM. |
| 847 | * Else update with the atags address in the kernel header |
| 848 | */ |
| 849 | void *dtb; |
| 850 | dtb = dev_tree_appended((void*) hdr->kernel_addr, |
Dima Zavin | e63e557 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 851 | hdr->kernel_size, |
| 852 | (void *)hdr->tags_addr); |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 853 | if (!dtb) { |
| 854 | dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n"); |
| 855 | return -1; |
| 856 | } |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 857 | } |
| 858 | #endif |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 859 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 860 | else |
| 861 | { |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 862 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 863 | |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 864 | dprintf(INFO, "Loading boot image (%d): start\n", |
| 865 | kernel_actual + ramdisk_actual); |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 866 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 867 | |
| 868 | offset = page_size; |
| 869 | |
| 870 | /* Load kernel */ |
| 871 | if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 872 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 873 | return -1; |
| 874 | } |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 875 | offset += kernel_actual; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 876 | |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 877 | /* Load ramdisk */ |
| 878 | if(ramdisk_actual != 0) |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 879 | { |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 880 | if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 881 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 882 | return -1; |
| 883 | } |
| 884 | } |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 885 | offset += ramdisk_actual; |
| 886 | |
| 887 | dprintf(INFO, "Loading boot image (%d): done\n", |
| 888 | kernel_actual + ramdisk_actual); |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 889 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 890 | |
| 891 | if(hdr->second_size != 0) { |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 892 | offset += second_actual; |
| 893 | /* Second image loading not implemented. */ |
| 894 | ASSERT(0); |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | #if DEVICE_TREE |
| 898 | if(hdr->dt_size != 0) { |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 899 | /* Read the first page of device tree table into buffer */ |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 900 | if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) { |
| 901 | dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n"); |
| 902 | return -1; |
| 903 | } |
| 904 | table = (struct dt_table*) dt_buf; |
| 905 | |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 906 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 907 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 908 | return -1; |
| 909 | } |
| 910 | |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 911 | table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size); |
| 912 | if (!table) |
| 913 | return -1; |
| 914 | |
| 915 | /* Read the entire device tree table into buffer */ |
| 916 | if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) { |
| 917 | dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n"); |
| 918 | return -1; |
| 919 | } |
| 920 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 921 | /* Find index of device tree within device tree table */ |
| 922 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 923 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 924 | return -1; |
| 925 | } |
| 926 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 927 | /* Validate and Read device device tree in the "tags_add */ |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 928 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 929 | { |
| 930 | dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n"); |
| 931 | return -1; |
| 932 | } |
| 933 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 934 | if(mmc_read(ptn + offset + dt_entry.offset, |
| 935 | (void *)hdr->tags_addr, dt_entry.size)) { |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 936 | dprintf(CRITICAL, "ERROR: Cannot read device tree\n"); |
| 937 | return -1; |
| 938 | } |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 939 | #ifdef TZ_SAVE_KERNEL_HASH |
| 940 | aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual, |
| 941 | hdr->ramdisk_addr, ramdisk_actual, |
| 942 | ptn, offset, hdr->dt_size); |
| 943 | #endif /* TZ_SAVE_KERNEL_HASH */ |
| 944 | |
Channagoud Kadabi | 3509562 | 2013-03-01 13:53:05 -0800 | [diff] [blame] | 945 | } else { |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 946 | /* |
| 947 | * If appended dev tree is found, update the atags with |
| 948 | * memory address to the DTB appended location on RAM. |
| 949 | * Else update with the atags address in the kernel header |
| 950 | */ |
| 951 | void *dtb; |
| 952 | dtb = dev_tree_appended((void*) hdr->kernel_addr, |
Dima Zavin | e63e557 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 953 | kernel_actual, |
| 954 | (void *)hdr->tags_addr); |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 955 | if (!dtb) { |
| 956 | dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n"); |
| 957 | return -1; |
| 958 | } |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 959 | } |
| 960 | #endif |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 961 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 962 | |
Stanimir Varbanov | 69ec546 | 2013-07-18 18:17:42 +0300 | [diff] [blame] | 963 | if (boot_into_recovery && !device.is_unlocked && !device.is_tampered) |
| 964 | target_load_ssd_keystore(); |
| 965 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 966 | unified_boot: |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 967 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 968 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Dima Zavin | bd3daa0 | 2013-03-26 11:06:44 -0700 | [diff] [blame] | 969 | (const char *)hdr->cmdline, board_machtype(), |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 970 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 971 | |
| 972 | return 0; |
| 973 | } |
| 974 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 975 | int boot_linux_from_flash(void) |
| 976 | { |
| 977 | struct boot_img_hdr *hdr = (void*) buf; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 978 | struct ptentry *ptn; |
| 979 | struct ptable *ptable; |
| 980 | unsigned offset = 0; |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 981 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 982 | unsigned char *image_addr = 0; |
| 983 | unsigned kernel_actual; |
| 984 | unsigned ramdisk_actual; |
| 985 | unsigned imagesize_actual; |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 986 | unsigned second_actual; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 987 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 988 | #if DEVICE_TREE |
| 989 | struct dt_table *table; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 990 | struct dt_entry dt_entry; |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 991 | uint32_t dt_actual; |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 992 | uint32_t dt_hdr_size; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 993 | #endif |
| 994 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 995 | if (target_is_emmc_boot()) { |
| 996 | hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 997 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 998 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
| 999 | return -1; |
| 1000 | } |
| 1001 | goto continue_boot; |
| 1002 | } |
| 1003 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1004 | ptable = flash_get_ptable(); |
| 1005 | if (ptable == NULL) { |
| 1006 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 1007 | return -1; |
| 1008 | } |
| 1009 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 1010 | if(!boot_into_recovery) |
| 1011 | { |
| 1012 | ptn = ptable_find(ptable, "boot"); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1013 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 1014 | if (ptn == NULL) { |
| 1015 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 1016 | return -1; |
| 1017 | } |
| 1018 | } |
| 1019 | else |
| 1020 | { |
| 1021 | ptn = ptable_find(ptable, "recovery"); |
| 1022 | if (ptn == NULL) { |
| 1023 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 1024 | return -1; |
| 1025 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 1028 | if (flash_read(ptn, offset, buf, page_size)) { |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1029 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 1030 | return -1; |
| 1031 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1032 | |
| 1033 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 1034 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1035 | return -1; |
| 1036 | } |
| 1037 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 1038 | if (hdr->page_size != page_size) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 1039 | 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] | 1040 | return -1; |
| 1041 | } |
| 1042 | |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 1043 | /* |
| 1044 | * Update the kernel/ramdisk/tags address if the boot image header |
| 1045 | * has default values, these default values come from mkbootimg when |
| 1046 | * the boot image is flashed using fastboot flash:raw |
| 1047 | */ |
| 1048 | update_ker_tags_rdisk_addr(hdr); |
| 1049 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1050 | /* Get virtual addresses since the hdr saves physical addresses. */ |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1051 | hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr)); |
| 1052 | hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr)); |
| 1053 | hdr->tags_addr = VA((addr_t)(hdr->tags_addr)); |
| 1054 | |
| 1055 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 1056 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 1057 | |
| 1058 | /* Check if the addresses in the header are valid. */ |
| 1059 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) || |
| 1060 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual)) |
| 1061 | { |
| 1062 | dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n"); |
| 1063 | return -1; |
| 1064 | } |
| 1065 | |
| 1066 | #ifndef DEVICE_TREE |
| 1067 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE)) |
| 1068 | { |
| 1069 | dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n"); |
| 1070 | return -1; |
| 1071 | } |
| 1072 | #endif |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1073 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1074 | /* Authenticate Kernel */ |
Deepa Dinamani | 23b60d4 | 2013-06-24 18:10:52 -0700 | [diff] [blame] | 1075 | if(target_use_signed_kernel() && (!device.is_unlocked)) |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1076 | { |
| 1077 | image_addr = (unsigned char *)target_get_scratch_address(); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1078 | offset = 0; |
| 1079 | |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1080 | #if DEVICE_TREE |
| 1081 | dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask); |
| 1082 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1083 | |
| 1084 | if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size)) |
| 1085 | { |
| 1086 | dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n"); |
| 1087 | return -1; |
| 1088 | } |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1089 | #else |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1090 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual); |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1091 | #endif |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1092 | |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1093 | dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual); |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 1094 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1095 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1096 | /* Read image without signature */ |
| 1097 | if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual)) |
| 1098 | { |
| 1099 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 1100 | return -1; |
| 1101 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1102 | |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1103 | dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual); |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 1104 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1105 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1106 | offset = imagesize_actual; |
| 1107 | /* Read signature */ |
| 1108 | if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size)) |
| 1109 | { |
| 1110 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1111 | return -1; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1112 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1113 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1114 | verify_signed_bootimg(image_addr, imagesize_actual); |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1115 | |
| 1116 | /* Move kernel and ramdisk to correct address */ |
| 1117 | memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size); |
| 1118 | 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] | 1119 | #if DEVICE_TREE |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1120 | /* Validate and Read device device tree in the "tags_add */ |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1121 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1122 | { |
| 1123 | dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n"); |
| 1124 | return -1; |
| 1125 | } |
| 1126 | |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1127 | memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size); |
| 1128 | #endif |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1129 | |
| 1130 | /* Make sure everything from scratch address is read before next step!*/ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1131 | if(device.is_tampered) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1132 | { |
| 1133 | write_device_info_flash(&device); |
| 1134 | } |
Channagoud Kadabi | 5c86fe3 | 2012-02-16 10:58:48 +0530 | [diff] [blame] | 1135 | #if USE_PCOM_SECBOOT |
| 1136 | set_tamper_flag(device.is_tampered); |
| 1137 | #endif |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1138 | } |
| 1139 | else |
| 1140 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1141 | offset = page_size; |
| 1142 | |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1143 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 1144 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 1145 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 1146 | |
| 1147 | dprintf(INFO, "Loading boot image (%d): start\n", |
| 1148 | kernel_actual + ramdisk_actual); |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 1149 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1150 | |
| 1151 | if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1152 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 1153 | return -1; |
| 1154 | } |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1155 | offset += kernel_actual; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1156 | |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1157 | if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1158 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 1159 | return -1; |
| 1160 | } |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1161 | offset += ramdisk_actual; |
| 1162 | |
| 1163 | dprintf(INFO, "Loading boot image (%d): done\n", |
| 1164 | kernel_actual + ramdisk_actual); |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 1165 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1166 | |
| 1167 | if(hdr->second_size != 0) { |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1168 | offset += second_actual; |
| 1169 | /* Second image loading not implemented. */ |
| 1170 | ASSERT(0); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | #if DEVICE_TREE |
| 1174 | if(hdr->dt_size != 0) { |
| 1175 | |
| 1176 | /* Read the device tree table into buffer */ |
| 1177 | if(flash_read(ptn, offset, (void *) dt_buf, page_size)) { |
| 1178 | dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n"); |
| 1179 | return -1; |
| 1180 | } |
| 1181 | |
| 1182 | table = (struct dt_table*) dt_buf; |
| 1183 | |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 1184 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1185 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 1186 | return -1; |
| 1187 | } |
| 1188 | |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 1189 | table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size); |
| 1190 | if (!table) |
| 1191 | return -1; |
| 1192 | |
| 1193 | /* Read the entire device tree table into buffer */ |
| 1194 | if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) { |
| 1195 | dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n"); |
| 1196 | return -1; |
| 1197 | } |
| 1198 | |
| 1199 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1200 | /* Find index of device tree within device tree table */ |
| 1201 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1202 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 1203 | return -1; |
| 1204 | } |
| 1205 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1206 | /* Validate and Read device device tree in the "tags_add */ |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1207 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1208 | { |
| 1209 | dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n"); |
| 1210 | return -1; |
| 1211 | } |
| 1212 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1213 | /* Read device device tree in the "tags_add */ |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1214 | if(flash_read(ptn, offset + dt_entry.offset, |
| 1215 | (void *)hdr->tags_addr, dt_entry.size)) { |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1216 | dprintf(CRITICAL, "ERROR: Cannot read device tree\n"); |
| 1217 | return -1; |
| 1218 | } |
| 1219 | } |
| 1220 | #endif |
| 1221 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1222 | } |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 1223 | continue_boot: |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1224 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1225 | /* TODO: create/pass atags to kernel */ |
| 1226 | |
Ajay Dudani | e28a607 | 2011-07-01 13:59:46 -0700 | [diff] [blame] | 1227 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Dima Zavin | bd3daa0 | 2013-03-26 11:06:44 -0700 | [diff] [blame] | 1228 | (const char *)hdr->cmdline, board_machtype(), |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1229 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 1230 | |
| 1231 | return 0; |
| 1232 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1233 | |
Channagoud Kadabi | 132ff55 | 2013-04-19 14:34:44 -0700 | [diff] [blame] | 1234 | BUF_DMA_ALIGN(info_buf, 4096); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1235 | void write_device_info_mmc(device_info *dev) |
| 1236 | { |
| 1237 | struct device_info *info = (void*) info_buf; |
| 1238 | unsigned long long ptn = 0; |
| 1239 | unsigned long long size; |
| 1240 | int index = INVALID_PTN; |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 1241 | uint32_t blocksize; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1242 | |
| 1243 | index = partition_get_index("aboot"); |
| 1244 | ptn = partition_get_offset(index); |
| 1245 | if(ptn == 0) |
| 1246 | { |
| 1247 | return; |
| 1248 | } |
| 1249 | |
| 1250 | size = partition_get_size(index); |
| 1251 | |
| 1252 | memcpy(info, dev, sizeof(device_info)); |
| 1253 | |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 1254 | blocksize = mmc_get_device_blocksize(); |
| 1255 | |
| 1256 | if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf)) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1257 | { |
| 1258 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 1259 | return; |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | void read_device_info_mmc(device_info *dev) |
| 1264 | { |
| 1265 | struct device_info *info = (void*) info_buf; |
| 1266 | unsigned long long ptn = 0; |
| 1267 | unsigned long long size; |
| 1268 | int index = INVALID_PTN; |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 1269 | uint32_t blocksize; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1270 | |
| 1271 | index = partition_get_index("aboot"); |
| 1272 | ptn = partition_get_offset(index); |
| 1273 | if(ptn == 0) |
| 1274 | { |
| 1275 | return; |
| 1276 | } |
| 1277 | |
| 1278 | size = partition_get_size(index); |
| 1279 | |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 1280 | blocksize = mmc_get_device_blocksize(); |
| 1281 | |
| 1282 | if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize)) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1283 | { |
| 1284 | dprintf(CRITICAL, "ERROR: Cannot read device info\n"); |
| 1285 | return; |
| 1286 | } |
| 1287 | |
| 1288 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 1289 | { |
| 1290 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
| 1291 | info->is_unlocked = 0; |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1292 | info->is_tampered = 0; |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 1293 | info->charger_screen_enabled = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1294 | |
| 1295 | write_device_info_mmc(info); |
| 1296 | } |
| 1297 | memcpy(dev, info, sizeof(device_info)); |
| 1298 | } |
| 1299 | |
| 1300 | void write_device_info_flash(device_info *dev) |
| 1301 | { |
| 1302 | struct device_info *info = (void *) info_buf; |
| 1303 | struct ptentry *ptn; |
| 1304 | struct ptable *ptable; |
| 1305 | |
| 1306 | ptable = flash_get_ptable(); |
| 1307 | if (ptable == NULL) |
| 1308 | { |
| 1309 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 1310 | return; |
| 1311 | } |
| 1312 | |
| 1313 | ptn = ptable_find(ptable, "devinfo"); |
| 1314 | if (ptn == NULL) |
| 1315 | { |
| 1316 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 1317 | return; |
| 1318 | } |
| 1319 | |
| 1320 | memcpy(info, dev, sizeof(device_info)); |
| 1321 | |
| 1322 | if (flash_write(ptn, 0, (void *)info_buf, page_size)) |
| 1323 | { |
| 1324 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 1325 | return; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | void read_device_info_flash(device_info *dev) |
| 1330 | { |
| 1331 | struct device_info *info = (void*) info_buf; |
| 1332 | struct ptentry *ptn; |
| 1333 | struct ptable *ptable; |
| 1334 | |
| 1335 | ptable = flash_get_ptable(); |
| 1336 | if (ptable == NULL) |
| 1337 | { |
| 1338 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 1339 | return; |
| 1340 | } |
| 1341 | |
| 1342 | ptn = ptable_find(ptable, "devinfo"); |
| 1343 | if (ptn == NULL) |
| 1344 | { |
| 1345 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 1346 | return; |
| 1347 | } |
| 1348 | |
| 1349 | if (flash_read(ptn, 0, (void *)info_buf, page_size)) |
| 1350 | { |
| 1351 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 1352 | return; |
| 1353 | } |
| 1354 | |
| 1355 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 1356 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1357 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
| 1358 | info->is_unlocked = 0; |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1359 | info->is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1360 | write_device_info_flash(info); |
| 1361 | } |
| 1362 | memcpy(dev, info, sizeof(device_info)); |
| 1363 | } |
| 1364 | |
| 1365 | void write_device_info(device_info *dev) |
| 1366 | { |
| 1367 | if(target_is_emmc_boot()) |
| 1368 | { |
| 1369 | write_device_info_mmc(dev); |
| 1370 | } |
| 1371 | else |
| 1372 | { |
| 1373 | write_device_info_flash(dev); |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | void read_device_info(device_info *dev) |
| 1378 | { |
| 1379 | if(target_is_emmc_boot()) |
| 1380 | { |
| 1381 | read_device_info_mmc(dev); |
| 1382 | } |
| 1383 | else |
| 1384 | { |
| 1385 | read_device_info_flash(dev); |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | void reset_device_info() |
| 1390 | { |
| 1391 | dprintf(ALWAYS, "reset_device_info called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1392 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1393 | write_device_info(&device); |
| 1394 | } |
| 1395 | |
| 1396 | void set_device_root() |
| 1397 | { |
| 1398 | dprintf(ALWAYS, "set_device_root called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1399 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1400 | write_device_info(&device); |
| 1401 | } |
| 1402 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1403 | #if DEVICE_TREE |
| 1404 | int copy_dtb(uint8_t *boot_image_start) |
| 1405 | { |
| 1406 | uint32 dt_image_offset = 0; |
| 1407 | uint32_t n; |
| 1408 | struct dt_table *table; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1409 | struct dt_entry dt_entry; |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 1410 | uint32_t dt_hdr_size; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1411 | |
| 1412 | struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start); |
| 1413 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1414 | if(hdr->dt_size != 0) { |
| 1415 | |
| 1416 | /* add kernel offset */ |
| 1417 | dt_image_offset += page_size; |
| 1418 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 1419 | dt_image_offset += n; |
| 1420 | |
| 1421 | /* add ramdisk offset */ |
| 1422 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 1423 | dt_image_offset += n; |
| 1424 | |
| 1425 | /* add second offset */ |
| 1426 | if(hdr->second_size != 0) { |
| 1427 | n = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 1428 | dt_image_offset += n; |
| 1429 | } |
| 1430 | |
| 1431 | /* offset now point to start of dt.img */ |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1432 | table = (struct dt_table*)(boot_image_start + dt_image_offset); |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1433 | |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 1434 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1435 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 1436 | return -1; |
| 1437 | } |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1438 | /* Find index of device tree within device tree table */ |
| 1439 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1440 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 1441 | return -1; |
| 1442 | } |
| 1443 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1444 | /* Validate and Read device device tree in the "tags_add */ |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1445 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1446 | { |
| 1447 | dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n"); |
| 1448 | return -1; |
| 1449 | } |
| 1450 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1451 | /* Read device device tree in the "tags_add */ |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1452 | memmove((void*) hdr->tags_addr, |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1453 | boot_image_start + dt_image_offset + dt_entry.offset, |
| 1454 | dt_entry.size); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 1455 | } else |
| 1456 | return -1; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1457 | |
| 1458 | /* Everything looks fine. Return success. */ |
| 1459 | return 0; |
| 1460 | } |
| 1461 | #endif |
| 1462 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1463 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 1464 | { |
| 1465 | unsigned kernel_actual; |
| 1466 | unsigned ramdisk_actual; |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1467 | struct boot_img_hdr *hdr; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1468 | char *ptr = ((char*) data); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 1469 | int ret = 0; |
| 1470 | uint8_t dtb_copied = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1471 | |
| 1472 | if (sz < sizeof(hdr)) { |
| 1473 | fastboot_fail("invalid bootimage header"); |
| 1474 | return; |
| 1475 | } |
| 1476 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1477 | hdr = (struct boot_img_hdr *)data; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1478 | |
| 1479 | /* ensure commandline is terminated */ |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1480 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1481 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1482 | if(target_is_emmc_boot() && hdr->page_size) { |
| 1483 | page_size = hdr->page_size; |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 1484 | page_mask = page_size - 1; |
| 1485 | } |
| 1486 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1487 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 1488 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 1489 | |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 1490 | /* |
| 1491 | * Update the kernel/ramdisk/tags address if the boot image header |
| 1492 | * has default values, these default values come from mkbootimg when |
| 1493 | * the boot image is flashed using fastboot flash:raw |
| 1494 | */ |
| 1495 | update_ker_tags_rdisk_addr(hdr); |
Dima Zavin | 3cadfff | 2013-03-21 14:30:48 -0700 | [diff] [blame] | 1496 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1497 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 1498 | hdr->kernel_addr = VA(hdr->kernel_addr); |
| 1499 | hdr->ramdisk_addr = VA(hdr->ramdisk_addr); |
| 1500 | hdr->tags_addr = VA(hdr->tags_addr); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1501 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1502 | /* Check if the addresses in the header are valid. */ |
| 1503 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) || |
| 1504 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual)) |
| 1505 | { |
| 1506 | dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n"); |
| 1507 | return -1; |
| 1508 | } |
| 1509 | |
Shashank Mittal | 1f0e266 | 2011-09-01 15:06:00 -0700 | [diff] [blame] | 1510 | /* sz should have atleast raw boot image */ |
| 1511 | if (page_size + kernel_actual + ramdisk_actual > sz) { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1512 | fastboot_fail("incomplete bootimage"); |
| 1513 | return; |
| 1514 | } |
| 1515 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1516 | #if DEVICE_TREE |
| 1517 | /* find correct dtb and copy it to right location */ |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 1518 | ret = copy_dtb(data); |
| 1519 | |
| 1520 | dtb_copied = !ret ? 1 : 0; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1521 | #else |
| 1522 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE)) |
| 1523 | { |
| 1524 | dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n"); |
| 1525 | return -1; |
| 1526 | } |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 1527 | #endif |
| 1528 | |
| 1529 | /* Load ramdisk & kernel */ |
| 1530 | memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size); |
| 1531 | memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size); |
| 1532 | |
| 1533 | #if DEVICE_TREE |
| 1534 | /* |
| 1535 | * If dtb is not found look for appended DTB in the kernel. |
| 1536 | * If appended dev tree is found, update the atags with |
| 1537 | * memory address to the DTB appended location on RAM. |
| 1538 | * Else update with the atags address in the kernel header |
| 1539 | */ |
| 1540 | if (!dtb_copied) { |
| 1541 | void *dtb; |
Dima Zavin | e63e557 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 1542 | dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size, |
| 1543 | (void *)hdr->tags_addr); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 1544 | if (!dtb) { |
| 1545 | fastboot_fail("dtb not found"); |
| 1546 | return; |
| 1547 | } |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 1548 | } |
| 1549 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1550 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1551 | #ifndef DEVICE_TREE |
| 1552 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE)) |
| 1553 | { |
| 1554 | dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n"); |
| 1555 | return -1; |
| 1556 | } |
| 1557 | #endif |
| 1558 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1559 | fastboot_okay(""); |
Amol Jadi | 7c4316c | 2013-10-07 14:19:26 -0700 | [diff] [blame] | 1560 | fastboot_stop(); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1561 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 1562 | boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr, |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1563 | (const char*) hdr->cmdline, board_machtype(), |
| 1564 | (void*) hdr->ramdisk_addr, hdr->ramdisk_size); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1565 | } |
| 1566 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1567 | void cmd_erase(const char *arg, void *data, unsigned sz) |
| 1568 | { |
| 1569 | struct ptentry *ptn; |
| 1570 | struct ptable *ptable; |
| 1571 | |
| 1572 | ptable = flash_get_ptable(); |
| 1573 | if (ptable == NULL) { |
| 1574 | fastboot_fail("partition table doesn't exist"); |
| 1575 | return; |
| 1576 | } |
| 1577 | |
| 1578 | ptn = ptable_find(ptable, arg); |
| 1579 | if (ptn == NULL) { |
| 1580 | fastboot_fail("unknown partition name"); |
| 1581 | return; |
| 1582 | } |
| 1583 | |
| 1584 | if (flash_erase(ptn)) { |
| 1585 | fastboot_fail("failed to erase partition"); |
| 1586 | return; |
| 1587 | } |
| 1588 | fastboot_okay(""); |
| 1589 | } |
| 1590 | |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1591 | |
| 1592 | void cmd_erase_mmc(const char *arg, void *data, unsigned sz) |
| 1593 | { |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 1594 | BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1595 | unsigned long long ptn = 0; |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 1596 | unsigned long long size; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1597 | int index = INVALID_PTN; |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1598 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1599 | index = partition_get_index(arg); |
| 1600 | ptn = partition_get_offset(index); |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 1601 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1602 | if(ptn == 0) { |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 1603 | fastboot_fail("Partition table doesn't exist\n"); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1604 | return; |
| 1605 | } |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 1606 | |
| 1607 | size = partition_get_size(index); |
| 1608 | if (size > DEFAULT_ERASE_SIZE) |
| 1609 | size = DEFAULT_ERASE_SIZE; |
| 1610 | |
neetid | b4b24d6 | 2012-01-20 12:13:09 -0800 | [diff] [blame] | 1611 | /* Simple inefficient version of erase. Just writing |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 1612 | 0 in first several blocks */ |
| 1613 | if (mmc_write(ptn , size, (unsigned int *)out)) { |
neetid | b4b24d6 | 2012-01-20 12:13:09 -0800 | [diff] [blame] | 1614 | fastboot_fail("failed to erase partition"); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1615 | return; |
| 1616 | } |
| 1617 | fastboot_okay(""); |
| 1618 | } |
| 1619 | |
| 1620 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1621 | void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz) |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1622 | { |
| 1623 | unsigned long long ptn = 0; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 1624 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1625 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 1626 | |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 1627 | if (!strcmp(arg, "partition")) |
| 1628 | { |
| 1629 | dprintf(INFO, "Attempt to write partition image.\n"); |
Neeti Desai | 5f26aff | 2011-09-30 10:27:40 -0700 | [diff] [blame] | 1630 | if (write_partition(sz, (unsigned char *) data)) { |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 1631 | fastboot_fail("failed to write partition"); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1632 | return; |
| 1633 | } |
| 1634 | } |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 1635 | else |
| 1636 | { |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1637 | index = partition_get_index(arg); |
| 1638 | ptn = partition_get_offset(index); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 1639 | if(ptn == 0) { |
| 1640 | fastboot_fail("partition table doesn't exist"); |
| 1641 | return; |
| 1642 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1643 | |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 1644 | if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) { |
| 1645 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 1646 | fastboot_fail("image is not a boot image"); |
| 1647 | return; |
| 1648 | } |
| 1649 | } |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 1650 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1651 | size = partition_get_size(index); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 1652 | if (ROUND_TO_PAGE(sz,511) > size) { |
| 1653 | fastboot_fail("size too large"); |
| 1654 | return; |
| 1655 | } |
| 1656 | else if (mmc_write(ptn , sz, (unsigned int *)data)) { |
| 1657 | fastboot_fail("flash write failure"); |
| 1658 | return; |
| 1659 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1660 | } |
| 1661 | fastboot_okay(""); |
| 1662 | return; |
| 1663 | } |
| 1664 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1665 | void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz) |
| 1666 | { |
| 1667 | unsigned int chunk; |
| 1668 | unsigned int chunk_data_sz; |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 1669 | uint32_t *fill_buf = NULL; |
| 1670 | uint32_t fill_val; |
| 1671 | uint32_t chunk_blk_cnt = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1672 | sparse_header_t *sparse_header; |
| 1673 | chunk_header_t *chunk_header; |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 1674 | uint32_t total_blocks = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1675 | unsigned long long ptn = 0; |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 1676 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1677 | int index = INVALID_PTN; |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 1678 | int i; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1679 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1680 | index = partition_get_index(arg); |
| 1681 | ptn = partition_get_offset(index); |
| 1682 | if(ptn == 0) { |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1683 | fastboot_fail("partition table doesn't exist"); |
| 1684 | return; |
| 1685 | } |
| 1686 | |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 1687 | size = partition_get_size(index); |
| 1688 | if (ROUND_TO_PAGE(sz,511) > size) { |
| 1689 | fastboot_fail("size too large"); |
| 1690 | return; |
| 1691 | } |
| 1692 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1693 | /* Read and skip over sparse image header */ |
| 1694 | sparse_header = (sparse_header_t *) data; |
Ajay Dudani | 876b328 | 2012-12-21 14:12:17 -0800 | [diff] [blame] | 1695 | if ((sparse_header->total_blks * sparse_header->blk_sz) > size) { |
| 1696 | fastboot_fail("size too large"); |
| 1697 | return; |
| 1698 | } |
| 1699 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1700 | data += sparse_header->file_hdr_sz; |
| 1701 | if(sparse_header->file_hdr_sz > sizeof(sparse_header_t)) |
| 1702 | { |
| 1703 | /* Skip the remaining bytes in a header that is longer than |
| 1704 | * we expected. |
| 1705 | */ |
| 1706 | data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t)); |
| 1707 | } |
| 1708 | |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 1709 | dprintf (SPEW, "=== Sparse Image Header ===\n"); |
| 1710 | dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic); |
| 1711 | dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version); |
| 1712 | dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version); |
| 1713 | dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz); |
| 1714 | dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz); |
| 1715 | dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz); |
| 1716 | dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks); |
| 1717 | dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1718 | |
| 1719 | /* Start processing chunks */ |
| 1720 | for (chunk=0; chunk<sparse_header->total_chunks; chunk++) |
| 1721 | { |
| 1722 | /* Read and skip over chunk header */ |
| 1723 | chunk_header = (chunk_header_t *) data; |
| 1724 | data += sizeof(chunk_header_t); |
| 1725 | |
| 1726 | dprintf (SPEW, "=== Chunk Header ===\n"); |
| 1727 | dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type); |
| 1728 | dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz); |
| 1729 | dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz); |
| 1730 | |
| 1731 | if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) |
| 1732 | { |
| 1733 | /* Skip the remaining bytes in a header that is longer than |
| 1734 | * we expected. |
| 1735 | */ |
| 1736 | data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t)); |
| 1737 | } |
| 1738 | |
| 1739 | chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz; |
| 1740 | switch (chunk_header->chunk_type) |
| 1741 | { |
| 1742 | case CHUNK_TYPE_RAW: |
| 1743 | if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz + |
| 1744 | chunk_data_sz)) |
| 1745 | { |
| 1746 | fastboot_fail("Bogus chunk size for chunk type Raw"); |
| 1747 | return; |
| 1748 | } |
| 1749 | |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 1750 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
| 1751 | chunk_data_sz, |
| 1752 | (unsigned int*)data)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1753 | { |
| 1754 | fastboot_fail("flash write failure"); |
| 1755 | return; |
| 1756 | } |
| 1757 | total_blocks += chunk_header->chunk_sz; |
| 1758 | data += chunk_data_sz; |
| 1759 | break; |
| 1760 | |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 1761 | case CHUNK_TYPE_FILL: |
| 1762 | if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz + |
| 1763 | sizeof(uint32_t))) |
| 1764 | { |
| 1765 | fastboot_fail("Bogus chunk size for chunk type FILL"); |
| 1766 | return; |
| 1767 | } |
| 1768 | |
| 1769 | fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE)); |
| 1770 | if (!fill_buf) |
| 1771 | { |
| 1772 | fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL"); |
| 1773 | return; |
| 1774 | } |
| 1775 | |
| 1776 | fill_val = *(uint32_t *)data; |
| 1777 | data = (char *) data + sizeof(uint32_t); |
| 1778 | chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz; |
| 1779 | |
| 1780 | for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++) |
| 1781 | { |
| 1782 | fill_buf[i] = fill_val; |
| 1783 | } |
| 1784 | |
| 1785 | for (i = 0; i < chunk_blk_cnt; i++) |
| 1786 | { |
| 1787 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
| 1788 | sparse_header->blk_sz, |
| 1789 | fill_buf)) |
| 1790 | { |
| 1791 | fastboot_fail("flash write failure"); |
| 1792 | free(fill_buf); |
| 1793 | return; |
| 1794 | } |
| 1795 | |
| 1796 | total_blocks++; |
| 1797 | } |
| 1798 | |
| 1799 | free(fill_buf); |
| 1800 | break; |
| 1801 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1802 | case CHUNK_TYPE_DONT_CARE: |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 1803 | total_blocks += chunk_header->chunk_sz; |
| 1804 | break; |
| 1805 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1806 | case CHUNK_TYPE_CRC: |
| 1807 | if(chunk_header->total_sz != sparse_header->chunk_hdr_sz) |
| 1808 | { |
| 1809 | fastboot_fail("Bogus chunk size for chunk type Dont Care"); |
| 1810 | return; |
| 1811 | } |
| 1812 | total_blocks += chunk_header->chunk_sz; |
| 1813 | data += chunk_data_sz; |
| 1814 | break; |
| 1815 | |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 1816 | default: |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 1817 | dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1818 | fastboot_fail("Unknown chunk type"); |
| 1819 | return; |
| 1820 | } |
| 1821 | } |
| 1822 | |
Ajay Dudani | 0c6927b | 2011-05-18 11:12:16 -0700 | [diff] [blame] | 1823 | dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n", |
| 1824 | total_blocks, sparse_header->total_blks); |
| 1825 | |
| 1826 | if(total_blocks != sparse_header->total_blks) |
| 1827 | { |
| 1828 | fastboot_fail("sparse image write failure"); |
| 1829 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1830 | |
| 1831 | fastboot_okay(""); |
| 1832 | return; |
| 1833 | } |
| 1834 | |
| 1835 | void cmd_flash_mmc(const char *arg, void *data, unsigned sz) |
| 1836 | { |
| 1837 | sparse_header_t *sparse_header; |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1838 | /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */ |
| 1839 | unsigned int *magic_number = (unsigned int *) data; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1840 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1841 | #ifdef SSD_ENABLE |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 1842 | int ret=0; |
| 1843 | uint32 major_version=0; |
| 1844 | uint32 minor_version=0; |
| 1845 | |
| 1846 | ret = scm_svc_version(&major_version,&minor_version); |
| 1847 | if(!ret) |
| 1848 | { |
| 1849 | if(major_version >= 2) |
| 1850 | { |
Amir Samuelov | bb65ce0 | 2013-05-05 12:20:18 +0300 | [diff] [blame] | 1851 | if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") ) |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 1852 | { |
| 1853 | ret = encrypt_scm((uint32 **) &data, &sz); |
| 1854 | if (ret != 0) { |
| 1855 | dprintf(CRITICAL, "ERROR: Encryption Failure\n"); |
| 1856 | return; |
| 1857 | } |
| 1858 | |
Amir Samuelov | bb65ce0 | 2013-05-05 12:20:18 +0300 | [diff] [blame] | 1859 | /* Protect only for SSD */ |
| 1860 | if (!strcmp(arg, "ssd")) { |
| 1861 | ret = scm_protect_keystore((uint32 *) data, sz); |
| 1862 | if (ret != 0) { |
| 1863 | dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n"); |
| 1864 | return; |
| 1865 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 1866 | } |
| 1867 | } |
| 1868 | else |
| 1869 | { |
| 1870 | ret = decrypt_scm_v2((uint32 **) &data, &sz); |
| 1871 | if(ret != 0) |
| 1872 | { |
| 1873 | dprintf(CRITICAL,"ERROR: Decryption Failure\n"); |
| 1874 | return; |
| 1875 | } |
| 1876 | } |
| 1877 | } |
| 1878 | else |
| 1879 | { |
| 1880 | if (magic_number[0] == DECRYPT_MAGIC_0 && |
| 1881 | magic_number[1] == DECRYPT_MAGIC_1) |
| 1882 | { |
| 1883 | ret = decrypt_scm((uint32 **) &data, &sz); |
| 1884 | if (ret != 0) { |
| 1885 | dprintf(CRITICAL, "ERROR: Invalid secure image\n"); |
| 1886 | return; |
| 1887 | } |
| 1888 | } |
| 1889 | else if (magic_number[0] == ENCRYPT_MAGIC_0 && |
| 1890 | magic_number[1] == ENCRYPT_MAGIC_1) |
| 1891 | { |
| 1892 | ret = encrypt_scm((uint32 **) &data, &sz); |
| 1893 | if (ret != 0) { |
| 1894 | dprintf(CRITICAL, "ERROR: Encryption Failure\n"); |
| 1895 | return; |
| 1896 | } |
| 1897 | } |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1898 | } |
| 1899 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 1900 | else |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 1901 | { |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 1902 | dprintf(CRITICAL,"INVALID SVC Version\n"); |
| 1903 | return; |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 1904 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 1905 | #endif /* SSD_ENABLE */ |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 1906 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1907 | sparse_header = (sparse_header_t *) data; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1908 | if (sparse_header->magic != SPARSE_HEADER_MAGIC) |
| 1909 | cmd_flash_mmc_img(arg, data, sz); |
| 1910 | else |
| 1911 | cmd_flash_mmc_sparse_img(arg, data, sz); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1912 | return; |
| 1913 | } |
| 1914 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1915 | void cmd_flash(const char *arg, void *data, unsigned sz) |
| 1916 | { |
| 1917 | struct ptentry *ptn; |
| 1918 | struct ptable *ptable; |
| 1919 | unsigned extra = 0; |
| 1920 | |
| 1921 | ptable = flash_get_ptable(); |
| 1922 | if (ptable == NULL) { |
| 1923 | fastboot_fail("partition table doesn't exist"); |
| 1924 | return; |
| 1925 | } |
| 1926 | |
| 1927 | ptn = ptable_find(ptable, arg); |
| 1928 | if (ptn == NULL) { |
| 1929 | fastboot_fail("unknown partition name"); |
| 1930 | return; |
| 1931 | } |
| 1932 | |
| 1933 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
| 1934 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 1935 | fastboot_fail("image is not a boot image"); |
| 1936 | return; |
| 1937 | } |
| 1938 | } |
| 1939 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 1940 | if (!strcmp(ptn->name, "system") |
Deepa Dinamani | 13e32c4 | 2012-03-12 14:34:17 -0700 | [diff] [blame] | 1941 | || !strcmp(ptn->name, "userdata") |
| 1942 | || !strcmp(ptn->name, "persist") |
| 1943 | || !strcmp(ptn->name, "recoveryfs")) { |
Deepa Dinamani | ea17791 | 2013-04-30 15:51:10 -0700 | [diff] [blame] | 1944 | extra = 1; |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 1945 | } else |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 1946 | sz = ROUND_TO_PAGE(sz, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1947 | |
| 1948 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
| 1949 | if (flash_write(ptn, extra, data, sz)) { |
| 1950 | fastboot_fail("flash write failure"); |
| 1951 | return; |
| 1952 | } |
| 1953 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 1954 | fastboot_okay(""); |
| 1955 | } |
| 1956 | |
| 1957 | void cmd_continue(const char *arg, void *data, unsigned sz) |
| 1958 | { |
| 1959 | fastboot_okay(""); |
Amol Jadi | 7c4316c | 2013-10-07 14:19:26 -0700 | [diff] [blame] | 1960 | fastboot_stop(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1961 | if (target_is_emmc_boot()) |
| 1962 | { |
| 1963 | boot_linux_from_mmc(); |
| 1964 | } |
| 1965 | else |
| 1966 | { |
| 1967 | boot_linux_from_flash(); |
| 1968 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1969 | } |
| 1970 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1971 | void cmd_reboot(const char *arg, void *data, unsigned sz) |
| 1972 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1973 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1974 | fastboot_okay(""); |
| 1975 | reboot_device(0); |
| 1976 | } |
| 1977 | |
| 1978 | void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz) |
| 1979 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1980 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1981 | fastboot_okay(""); |
| 1982 | reboot_device(FASTBOOT_MODE); |
| 1983 | } |
| 1984 | |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 1985 | void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size) |
| 1986 | { |
| 1987 | dprintf(INFO, "Enabling charger screen check\n"); |
| 1988 | device.charger_screen_enabled = 1; |
| 1989 | write_device_info(&device); |
| 1990 | fastboot_okay(""); |
| 1991 | } |
| 1992 | |
| 1993 | void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size) |
| 1994 | { |
| 1995 | dprintf(INFO, "Disabling charger screen check\n"); |
| 1996 | device.charger_screen_enabled = 0; |
| 1997 | write_device_info(&device); |
| 1998 | fastboot_okay(""); |
| 1999 | } |
| 2000 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2001 | void cmd_oem_unlock(const char *arg, void *data, unsigned sz) |
| 2002 | { |
| 2003 | if(!device.is_unlocked) |
| 2004 | { |
| 2005 | device.is_unlocked = 1; |
| 2006 | write_device_info(&device); |
| 2007 | } |
| 2008 | fastboot_okay(""); |
| 2009 | } |
| 2010 | |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2011 | void cmd_oem_devinfo(const char *arg, void *data, unsigned sz) |
| 2012 | { |
| 2013 | char response[64]; |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 2014 | snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false")); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2015 | fastboot_info(response); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 2016 | snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false")); |
| 2017 | fastboot_info(response); |
| 2018 | snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false")); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2019 | fastboot_info(response); |
| 2020 | fastboot_okay(""); |
| 2021 | } |
| 2022 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 2023 | void cmd_preflash(const char *arg, void *data, unsigned sz) |
| 2024 | { |
| 2025 | fastboot_okay(""); |
| 2026 | } |
| 2027 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 2028 | static struct fbimage logo_header = {0}; |
| 2029 | struct fbimage* splash_screen_flash(); |
| 2030 | |
| 2031 | int splash_screen_check_header(struct fbimage *logo) |
| 2032 | { |
| 2033 | if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8)) |
| 2034 | return -1; |
| 2035 | if (logo->header.width == 0 || logo->header.height == 0) |
| 2036 | return -1; |
| 2037 | return 0; |
| 2038 | } |
| 2039 | |
| 2040 | struct fbimage* splash_screen_flash() |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 2041 | { |
| 2042 | struct ptentry *ptn; |
| 2043 | struct ptable *ptable; |
| 2044 | struct fbcon_config *fb_display = NULL; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 2045 | struct fbimage *logo = &logo_header; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 2046 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 2047 | |
| 2048 | ptable = flash_get_ptable(); |
| 2049 | if (ptable == NULL) { |
| 2050 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 2051 | return NULL; |
| 2052 | } |
| 2053 | ptn = ptable_find(ptable, "splash"); |
| 2054 | if (ptn == NULL) { |
| 2055 | dprintf(CRITICAL, "ERROR: splash Partition not found\n"); |
| 2056 | return NULL; |
| 2057 | } |
| 2058 | |
| 2059 | if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) { |
| 2060 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 2061 | return NULL; |
| 2062 | } |
| 2063 | |
| 2064 | if (splash_screen_check_header(logo)) { |
| 2065 | dprintf(CRITICAL, "ERROR: Boot image header invalid\n"); |
| 2066 | return NULL; |
| 2067 | } |
| 2068 | |
| 2069 | fb_display = fbcon_display(); |
| 2070 | if (fb_display) { |
| 2071 | uint8_t *base = (uint8_t *) fb_display->base; |
| 2072 | if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) { |
| 2073 | base += LOGO_IMG_OFFSET; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 2074 | } |
| 2075 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 2076 | if (flash_read(ptn + sizeof(logo->header), 0, |
| 2077 | base, |
| 2078 | ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) { |
| 2079 | fbcon_clear(); |
| 2080 | dprintf(CRITICAL, "ERROR: Cannot read splash image\n"); |
| 2081 | return NULL; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 2082 | } |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 2083 | logo->image = base; |
| 2084 | } |
| 2085 | |
| 2086 | return logo; |
| 2087 | } |
| 2088 | |
| 2089 | struct fbimage* splash_screen_mmc() |
| 2090 | { |
| 2091 | int index = INVALID_PTN; |
| 2092 | unsigned long long ptn = 0; |
| 2093 | struct fbcon_config *fb_display = NULL; |
| 2094 | struct fbimage *logo = &logo_header; |
| 2095 | |
| 2096 | index = partition_get_index("splash"); |
| 2097 | if (index == 0) { |
| 2098 | dprintf(CRITICAL, "ERROR: splash Partition table not found\n"); |
| 2099 | return NULL; |
| 2100 | } |
| 2101 | |
| 2102 | ptn = partition_get_offset(index); |
| 2103 | if (ptn == 0) { |
| 2104 | dprintf(CRITICAL, "ERROR: splash Partition invalid\n"); |
| 2105 | return NULL; |
| 2106 | } |
| 2107 | |
| 2108 | if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) { |
| 2109 | dprintf(CRITICAL, "ERROR: Cannot read splash image header\n"); |
| 2110 | return NULL; |
| 2111 | } |
| 2112 | |
| 2113 | if (splash_screen_check_header(logo)) { |
| 2114 | dprintf(CRITICAL, "ERROR: Splash image header invalid\n"); |
| 2115 | return NULL; |
| 2116 | } |
| 2117 | |
| 2118 | fb_display = fbcon_display(); |
| 2119 | if (fb_display) { |
| 2120 | uint8_t *base = (uint8_t *) fb_display->base; |
| 2121 | if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) |
| 2122 | base += LOGO_IMG_OFFSET; |
| 2123 | |
| 2124 | if (mmc_read(ptn + sizeof(logo->header), |
| 2125 | base, |
| 2126 | ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) { |
| 2127 | fbcon_clear(); |
| 2128 | dprintf(CRITICAL, "ERROR: Cannot read splash image\n"); |
| 2129 | return NULL; |
| 2130 | } |
| 2131 | |
| 2132 | logo->image = base; |
| 2133 | } |
| 2134 | |
| 2135 | return logo; |
| 2136 | } |
| 2137 | |
| 2138 | |
| 2139 | struct fbimage* fetch_image_from_partition() |
| 2140 | { |
| 2141 | if (target_is_emmc_boot()) { |
| 2142 | return splash_screen_mmc(); |
| 2143 | } else { |
| 2144 | return splash_screen_flash(); |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 2145 | } |
| 2146 | } |
| 2147 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 2148 | /* Get the size from partiton name */ |
| 2149 | static void get_partition_size(const char *arg, char *response) |
| 2150 | { |
| 2151 | uint64_t ptn = 0; |
| 2152 | uint64_t size; |
| 2153 | int index = INVALID_PTN; |
| 2154 | |
| 2155 | index = partition_get_index(arg); |
| 2156 | |
| 2157 | if (index == INVALID_PTN) |
| 2158 | { |
| 2159 | dprintf(CRITICAL, "Invalid partition index\n"); |
| 2160 | return; |
| 2161 | } |
| 2162 | |
| 2163 | ptn = partition_get_offset(index); |
| 2164 | |
| 2165 | if(!ptn) |
| 2166 | { |
| 2167 | dprintf(CRITICAL, "Invalid partition name %s\n", arg); |
| 2168 | return; |
| 2169 | } |
| 2170 | |
| 2171 | size = partition_get_size(index); |
| 2172 | |
| 2173 | snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size); |
| 2174 | return; |
| 2175 | } |
| 2176 | |
| 2177 | /* |
| 2178 | * Publish the partition type & size info |
| 2179 | * fastboot getvar will publish the required information. |
| 2180 | * fastboot getvar partition_size:<partition_name>: partition size in hex |
| 2181 | * fastboot getvar partition_type:<partition_name>: partition type (ext/fat) |
| 2182 | */ |
| 2183 | static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts) |
| 2184 | { |
| 2185 | uint8_t i; |
| 2186 | |
| 2187 | for (i = 0; i < num_parts; i++) { |
| 2188 | get_partition_size(info[i].part_name, info[i].size_response); |
| 2189 | |
| 2190 | if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE) |
| 2191 | { |
| 2192 | dprintf(CRITICAL, "partition size name truncated\n"); |
| 2193 | return; |
| 2194 | } |
| 2195 | if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE) |
| 2196 | { |
| 2197 | dprintf(CRITICAL, "partition type name truncated\n"); |
| 2198 | return; |
| 2199 | } |
| 2200 | |
| 2201 | /* publish partition size & type info */ |
| 2202 | fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response); |
| 2203 | fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response); |
| 2204 | } |
| 2205 | } |
| 2206 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 2207 | /* register commands and variables for fastboot */ |
| 2208 | void aboot_fastboot_register_commands(void) |
| 2209 | { |
| 2210 | if (target_is_emmc_boot()) |
| 2211 | { |
| 2212 | fastboot_register("flash:", cmd_flash_mmc); |
| 2213 | fastboot_register("erase:", cmd_erase_mmc); |
| 2214 | } |
| 2215 | else |
| 2216 | { |
| 2217 | fastboot_register("flash:", cmd_flash); |
| 2218 | fastboot_register("erase:", cmd_erase); |
| 2219 | } |
| 2220 | |
| 2221 | fastboot_register("boot", cmd_boot); |
| 2222 | fastboot_register("continue", cmd_continue); |
| 2223 | fastboot_register("reboot", cmd_reboot); |
| 2224 | fastboot_register("reboot-bootloader", cmd_reboot_bootloader); |
| 2225 | fastboot_register("oem unlock", cmd_oem_unlock); |
| 2226 | fastboot_register("oem device-info", cmd_oem_devinfo); |
| 2227 | fastboot_register("preflash", cmd_preflash); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 2228 | fastboot_register("oem enable-charger-screen", |
| 2229 | cmd_oem_enable_charger_screen); |
| 2230 | fastboot_register("oem disable-charger-screen", |
| 2231 | cmd_oem_disable_charger_screen); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 2232 | /* publish variables and their values */ |
| 2233 | fastboot_publish("product", TARGET(BOARD)); |
| 2234 | fastboot_publish("kernel", "lk"); |
| 2235 | fastboot_publish("serialno", sn_buf); |
| 2236 | |
| 2237 | /* |
| 2238 | * partition info is supported only for emmc partitions |
| 2239 | * Calling this for NAND prints some error messages which |
| 2240 | * is harmless but misleading. Avoid calling this for NAND |
| 2241 | * devices. |
| 2242 | */ |
| 2243 | if (target_is_emmc_boot()) |
| 2244 | publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info)); |
| 2245 | |
| 2246 | /* Max download size supported */ |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 2247 | snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", |
| 2248 | target_get_max_flash_size()); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 2249 | fastboot_publish("max-download-size", (const char *) max_download_size); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 2250 | /* Is the charger screen check enabled */ |
| 2251 | snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d", |
| 2252 | device.charger_screen_enabled); |
| 2253 | fastboot_publish("charger-screen-enabled", |
| 2254 | (const char *) charger_screen_enabled); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 2255 | } |
| 2256 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2257 | void aboot_init(const struct app_descriptor *app) |
| 2258 | { |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 2259 | unsigned reboot_mode = 0; |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 2260 | bool boot_into_fastboot = false; |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 2261 | |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 2262 | /* Setup page size information for nv storage */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 2263 | if (target_is_emmc_boot()) |
| 2264 | { |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 2265 | page_size = mmc_page_size(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 2266 | page_mask = page_size - 1; |
| 2267 | } |
| 2268 | else |
| 2269 | { |
| 2270 | page_size = flash_page_size(); |
| 2271 | page_mask = page_size - 1; |
| 2272 | } |
| 2273 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2274 | ASSERT((MEMBASE + MEMSIZE) > MEMBASE); |
| 2275 | |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 2276 | read_device_info(&device); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2277 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 2278 | target_serialno((unsigned char *) sn_buf); |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 2279 | dprintf(SPEW,"serial number: %s\n",sn_buf); |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 2280 | |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 2281 | memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE); |
| 2282 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 2283 | /* Check if we should do something other than booting up */ |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 2284 | if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN)) |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 2285 | { |
Pavel Nedev | a4c9d3a | 2013-05-15 14:42:34 +0300 | [diff] [blame] | 2286 | dprintf(ALWAYS,"dload mode key sequence detected\n"); |
| 2287 | if (set_download_mode(EMERGENCY_DLOAD)) |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 2288 | { |
Pavel Nedev | a4c9d3a | 2013-05-15 14:42:34 +0300 | [diff] [blame] | 2289 | dprintf(CRITICAL,"dload mode not supported by target\n"); |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 2290 | } |
| 2291 | else |
| 2292 | { |
| 2293 | reboot_device(0); |
Pavel Nedev | a4c9d3a | 2013-05-15 14:42:34 +0300 | [diff] [blame] | 2294 | dprintf(CRITICAL,"Failed to reboot into dload mode\n"); |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 2295 | } |
| 2296 | boot_into_fastboot = true; |
| 2297 | } |
| 2298 | if (!boot_into_fastboot) |
| 2299 | { |
| 2300 | if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP)) |
| 2301 | boot_into_recovery = 1; |
| 2302 | if (!boot_into_recovery && |
| 2303 | (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN))) |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 2304 | boot_into_fastboot = true; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 2305 | } |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 2306 | #if NO_KEYPAD_DRIVER |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 2307 | if (fastboot_trigger()) |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 2308 | boot_into_fastboot = true; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 2309 | #endif |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 2310 | |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 2311 | reboot_mode = check_reboot_mode(); |
| 2312 | if (reboot_mode == RECOVERY_MODE) { |
| 2313 | boot_into_recovery = 1; |
| 2314 | } else if(reboot_mode == FASTBOOT_MODE) { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 2315 | boot_into_fastboot = true; |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 2316 | } |
| 2317 | |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 2318 | if (!boot_into_fastboot) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 2319 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 2320 | if (target_is_emmc_boot()) |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2321 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 2322 | if(emmc_recovery_init()) |
| 2323 | dprintf(ALWAYS,"error in emmc_recovery_init\n"); |
| 2324 | if(target_use_signed_kernel()) |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2325 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 2326 | if((device.is_unlocked) || (device.is_tampered)) |
| 2327 | { |
| 2328 | #ifdef TZ_TAMPER_FUSE |
| 2329 | set_tamper_fuse_cmd(); |
| 2330 | #endif |
| 2331 | #if USE_PCOM_SECBOOT |
| 2332 | set_tamper_flag(device.is_tampered); |
| 2333 | #endif |
| 2334 | } |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2335 | } |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 2336 | boot_linux_from_mmc(); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2337 | } |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 2338 | else |
| 2339 | { |
| 2340 | recovery_init(); |
| 2341 | #if USE_PCOM_SECBOOT |
| 2342 | if((device.is_unlocked) || (device.is_tampered)) |
| 2343 | set_tamper_flag(device.is_tampered); |
| 2344 | #endif |
| 2345 | boot_linux_from_flash(); |
| 2346 | } |
| 2347 | dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting " |
| 2348 | "to fastboot mode.\n"); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 2349 | } |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 2350 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 2351 | /* We are here means regular boot did not happen. Start fastboot. */ |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 2352 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 2353 | /* register aboot specific fastboot commands */ |
| 2354 | aboot_fastboot_register_commands(); |
Amol Jadi | 57abe4c | 2011-05-24 15:47:27 -0700 | [diff] [blame] | 2355 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 2356 | /* dump partition table for debug info */ |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 2357 | partition_dump(); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 2358 | |
| 2359 | /* initialize and start fastboot */ |
| 2360 | fastboot_init(target_get_scratch_address(), target_get_max_flash_size()); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2361 | } |
| 2362 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 2363 | uint32_t get_page_size() |
| 2364 | { |
| 2365 | return page_size; |
| 2366 | } |
| 2367 | |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 2368 | /* |
| 2369 | * Calculated and save hash (SHA256) for non-signed boot image. |
| 2370 | * |
| 2371 | * Hash the same data that is checked on the signed boot image. |
| 2372 | * Kernel and Ramdisk are already read to memory buffers. |
| 2373 | * Need to read the entire device-tree from mmc |
| 2374 | * since non-signed image only read the DT tags of the relevant platform. |
| 2375 | * |
| 2376 | * @param kernel_addr - kernel bufer |
| 2377 | * @param kernel_actual - kernel size in bytes |
| 2378 | * @param ramdisk_addr - ramdisk buffer |
| 2379 | * @param ramdisk_actual - ramdisk size |
| 2380 | * @param ptn - partition |
| 2381 | * @param dt_offset - device tree offset on mmc partition |
| 2382 | * @param dt_size |
| 2383 | * |
| 2384 | * @return int - 0 on success, negative value on failure. |
| 2385 | */ |
| 2386 | int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual, |
| 2387 | void *ramdisk_addr, unsigned ramdisk_actual, |
| 2388 | unsigned long long ptn, |
| 2389 | unsigned dt_offset, unsigned dt_size) |
| 2390 | { |
| 2391 | SHA256_CTX sha256_ctx; |
| 2392 | char digest[32]={0}; |
| 2393 | char *buf = (char *)target_get_scratch_address(); |
| 2394 | unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask); |
| 2395 | unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual; |
| 2396 | |
| 2397 | SHA256_Init(&sha256_ctx); |
| 2398 | |
| 2399 | /* Read Boot Header */ |
| 2400 | if (mmc_read(ptn, buf, page_size)) |
| 2401 | { |
| 2402 | dprintf(CRITICAL, "ERROR: mmc_read() fail.\n"); |
| 2403 | return -1; |
| 2404 | } |
| 2405 | /* Read entire Device Tree */ |
| 2406 | if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual)) |
| 2407 | { |
| 2408 | dprintf(CRITICAL, "ERROR: mmc_read() fail.\n"); |
| 2409 | return -1; |
| 2410 | } |
| 2411 | SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header |
| 2412 | SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual); |
| 2413 | SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual); |
| 2414 | SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree |
| 2415 | |
| 2416 | SHA256_Final(digest, &sha256_ctx); |
| 2417 | |
| 2418 | save_kernel_hash_cmd(digest); |
| 2419 | dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual); |
| 2420 | |
| 2421 | return 0; |
| 2422 | } |
| 2423 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2424 | APP_START(aboot) |
| 2425 | .init = aboot_init, |
| 2426 | APP_END |