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