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