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