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