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