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