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