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