Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 5 | * Copyright (c) 2009-2016, 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> |
Tanya Brokhman | 1c94f1a | 2015-02-15 09:05:03 +0200 | [diff] [blame] | 43 | #include <dev/flash-ubi.h> |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 44 | #include <lib/ptable.h> |
Dima Zavin | b428360 | 2009-01-26 16:36:57 -0800 | [diff] [blame] | 45 | #include <dev/keys.h> |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 46 | #include <dev/fbcon.h> |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 47 | #include <baseband.h> |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 48 | #include <target.h> |
| 49 | #include <mmc.h> |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 50 | #include <partition_parser.h> |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 51 | #include <platform.h> |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 52 | #include <crypto_hash.h> |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 53 | #include <malloc.h> |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 54 | #include <boot_stats.h> |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 55 | #include <sha.h> |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 56 | #include <platform/iomap.h> |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 57 | #include <boot_device.h> |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 58 | #include <boot_verifier.h> |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 59 | #include <image_verify.h> |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 60 | #include <decompress.h> |
Unnati Gandhi | 17b3bfc | 2015-05-11 12:58:16 +0530 | [diff] [blame] | 61 | #include <platform/timer.h> |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 62 | #include <sys/types.h> |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 63 | #if USE_RPMB_FOR_DEVINFO |
| 64 | #include <rpmb.h> |
| 65 | #endif |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 66 | |
Channagoud Kadabi | 90869ce | 2015-04-27 11:15:14 -0700 | [diff] [blame] | 67 | #if ENABLE_WBC |
| 68 | #include <pm_app_smbchg.h> |
| 69 | #endif |
| 70 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 71 | #if DEVICE_TREE |
| 72 | #include <libfdt.h> |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 73 | #include <dev_tree.h> |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 74 | #endif |
| 75 | |
Aparna Mallavarapu | 118ccae | 2015-06-03 13:47:11 +0530 | [diff] [blame] | 76 | #if WDOG_SUPPORT |
| 77 | #include <wdog.h> |
| 78 | #endif |
| 79 | |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 80 | #include <reboot.h> |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 81 | #include "image_verify.h" |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 82 | #include "recovery.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 83 | #include "bootimg.h" |
| 84 | #include "fastboot.h" |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 85 | #include "sparse_format.h" |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 86 | #include "meta_format.h" |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 87 | #include "mmc.h" |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 88 | #include "devinfo.h" |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 89 | #include "board.h" |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 90 | #include "scm.h" |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 91 | #include "mdtp.h" |
Sridhar Parasuram | 32b3066 | 2015-07-10 13:33:22 -0700 | [diff] [blame] | 92 | #include "secapp_loader.h" |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 93 | #include <menu_keys_detect.h> |
| 94 | #include <display_menu.h> |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 95 | #include "fastboot_test.h" |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 96 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 97 | extern bool target_use_signed_kernel(void); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 98 | extern void platform_uninit(void); |
Channagoud Kadabi | 33defe2 | 2013-06-18 18:35:40 -0700 | [diff] [blame] | 99 | extern void target_uninit(void); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 100 | extern int get_target_boot_params(const char *cmdline, const char *part, |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 101 | char **buf); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 102 | |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 103 | void *info_buf; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 104 | void write_device_info_mmc(device_info *dev); |
| 105 | void write_device_info_flash(device_info *dev); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 106 | static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size); |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 107 | static int aboot_frp_unlock(char *pname, void *data, unsigned sz); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 108 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 109 | /* fastboot command function pointer */ |
| 110 | typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned); |
| 111 | |
| 112 | struct fastboot_cmd_desc { |
| 113 | char * name; |
| 114 | fastboot_cmd_fn cb; |
| 115 | }; |
| 116 | |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 117 | #define EXPAND(NAME) #NAME |
| 118 | #define TARGET(NAME) EXPAND(NAME) |
Brian Swetland | 2defe16 | 2009-08-18 14:35:59 -0700 | [diff] [blame] | 119 | |
Ajay Singh Parmar | a7865a8 | 2015-04-16 21:27:52 -0700 | [diff] [blame] | 120 | #define DISPLAY_PANEL_HDMI "hdmi" |
| 121 | |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 122 | #ifdef MEMBASE |
| 123 | #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE)) |
| 124 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 125 | #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000 |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 126 | #endif |
| 127 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 128 | #ifndef MEMSIZE |
| 129 | #define MEMSIZE 1024*1024 |
| 130 | #endif |
| 131 | |
| 132 | #define MAX_TAGS_SIZE 1024 |
| 133 | |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 134 | /* make 4096 as default size to ensure EFS,EXT4's erasing */ |
| 135 | #define DEFAULT_ERASE_SIZE 4096 |
Ujwal Patel | c0b0a25 | 2015-08-16 14:05:35 -0700 | [diff] [blame] | 136 | #define MAX_PANEL_BUF_SIZE 196 |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 137 | #define FOOTER_SIZE 16384 |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 138 | |
Dhaval Patel | f83d73b | 2014-06-23 16:24:37 -0700 | [diff] [blame] | 139 | #define DISPLAY_DEFAULT_PREFIX "mdss_mdp" |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 140 | #define BOOT_DEV_MAX_LEN 64 |
Sundarajan Srinivasan | 595b71e | 2013-11-05 12:44:34 -0800 | [diff] [blame] | 141 | |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 142 | #define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false |
| 143 | |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 144 | #define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX |
| 145 | |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 146 | //Size of the header that is used in case the boot image has |
| 147 | //a uncompressed kernel + appended dtb |
| 148 | #define PATCHED_KERNEL_HEADER_SIZE 20 |
| 149 | |
| 150 | //String used to determine if the boot image has |
| 151 | //a uncompressed kernel + appended dtb |
| 152 | #define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG" |
| 153 | |
Sridhar Parasuram | 7bd4aaf | 2015-02-12 11:14:38 -0800 | [diff] [blame] | 154 | #if USE_BOOTDEV_CMDLINE |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 155 | static const char *emmc_cmdline = " androidboot.bootdevice="; |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 156 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 157 | static const char *emmc_cmdline = " androidboot.emmc=true"; |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 158 | #endif |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 159 | static const char *usb_sn_cmdline = " androidboot.serialno="; |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 160 | static const char *androidboot_mode = " androidboot.mode="; |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 161 | static const char *alarmboot_cmdline = " androidboot.alarmboot=true"; |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 162 | static const char *loglevel = " quiet"; |
Ajay Dudani | ca3a33c | 2011-11-18 08:31:40 -0800 | [diff] [blame] | 163 | static const char *battchg_pause = " androidboot.mode=charger"; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 164 | static const char *auth_kernel = " androidboot.authorized_kernel=true"; |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 165 | static const char *secondary_gpt_enable = " gpt"; |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 166 | static const char *mdtp_activated_flag = " mdtp"; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 167 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 168 | static const char *baseband_apq = " androidboot.baseband=apq"; |
| 169 | static const char *baseband_msm = " androidboot.baseband=msm"; |
| 170 | static const char *baseband_csfb = " androidboot.baseband=csfb"; |
| 171 | static const char *baseband_svlte2a = " androidboot.baseband=svlte2a"; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 172 | static const char *baseband_mdm = " androidboot.baseband=mdm"; |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 173 | static const char *baseband_mdm2 = " androidboot.baseband=mdm2"; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 174 | static const char *baseband_sglte = " androidboot.baseband=sglte"; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 175 | static const char *baseband_dsda = " androidboot.baseband=dsda"; |
| 176 | static const char *baseband_dsda2 = " androidboot.baseband=dsda2"; |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 177 | static const char *baseband_sglte2 = " androidboot.baseband=sglte2"; |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 178 | static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1"; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 179 | static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr"; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 180 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 181 | #if VERIFIED_BOOT |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 182 | #if !VBOOT_MOTA |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 183 | static const char *verity_mode = " androidboot.veritymode="; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 184 | static const char *verified_state= " androidboot.verifiedbootstate="; |
Parth Dixit | a5715a0 | 2015-10-29 12:25:10 +0530 | [diff] [blame] | 185 | static const char *keymaster_v1= " androidboot.keymaster=1"; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 186 | //indexed based on enum values, green is 0 by default |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 187 | |
| 188 | struct verified_boot_verity_mode vbvm[] = |
| 189 | { |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame^] | 190 | #if ENABLE_VB_ATTEST |
| 191 | {false, "eio"}, |
| 192 | #else |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 193 | {false, "logging"}, |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame^] | 194 | #endif |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 195 | {true, "enforcing"}, |
| 196 | }; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 197 | struct verified_boot_state_name vbsn[] = |
| 198 | { |
| 199 | {GREEN, "green"}, |
| 200 | {ORANGE, "orange"}, |
| 201 | {YELLOW,"yellow"}, |
| 202 | {RED,"red" }, |
| 203 | }; |
| 204 | #endif |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 205 | #endif |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame^] | 206 | /*As per spec delay wait time before shutdown in Red state*/ |
| 207 | #define DELAY_WAIT 30000 |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 208 | static unsigned page_size = 0; |
| 209 | static unsigned page_mask = 0; |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 210 | static unsigned mmc_blocksize = 0; |
| 211 | static unsigned mmc_blocksize_mask = 0; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 212 | static char ffbm_mode_string[FFBM_MODE_BUF_SIZE]; |
| 213 | static bool boot_into_ffbm; |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 214 | static char *target_boot_params = NULL; |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 215 | static bool boot_reason_alarm; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 216 | static bool devinfo_present = true; |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 217 | bool boot_into_fastboot = false; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 218 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 219 | /* Assuming unauthorized kernel image by default */ |
| 220 | static int auth_kernel_img = 0; |
Parth Dixit | 6f5822f | 2015-10-18 01:20:53 +0530 | [diff] [blame] | 221 | #if VBOOT_MOTA |
| 222 | static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}}; |
| 223 | #else |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 224 | static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1}; |
Parth Dixit | 6f5822f | 2015-10-18 01:20:53 +0530 | [diff] [blame] | 225 | #endif |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 226 | static bool is_allow_unlock = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 227 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 228 | static char frp_ptns[2][8] = {"config","frp"}; |
| 229 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 230 | static const char *critical_flash_allowed_ptn[] = { |
| 231 | "aboot", |
| 232 | "rpm", |
| 233 | "tz", |
| 234 | "sbl", |
| 235 | "sdi", |
| 236 | "sbl1", |
| 237 | "xbl", |
| 238 | "hyp", |
| 239 | "pmic", |
| 240 | "bootloader", |
| 241 | "devinfo", |
| 242 | "partition"}; |
| 243 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 244 | struct atag_ptbl_entry |
| 245 | { |
| 246 | char name[16]; |
| 247 | unsigned offset; |
| 248 | unsigned size; |
| 249 | unsigned flags; |
| 250 | }; |
| 251 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 252 | /* |
| 253 | * Partition info, required to be published |
| 254 | * for fastboot |
| 255 | */ |
| 256 | struct getvar_partition_info { |
| 257 | const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */ |
| 258 | char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */ |
| 259 | char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */ |
| 260 | char size_response[MAX_RSP_SIZE]; /* fastboot response for size */ |
| 261 | char type_response[MAX_RSP_SIZE]; /* fastboot response for type */ |
| 262 | }; |
| 263 | |
| 264 | /* |
| 265 | * Right now, we are publishing the info for only |
| 266 | * three partitions |
| 267 | */ |
| 268 | struct getvar_partition_info part_info[] = |
| 269 | { |
| 270 | { "system" , "partition-size:", "partition-type:", "", "ext4" }, |
| 271 | { "userdata", "partition-size:", "partition-type:", "", "ext4" }, |
| 272 | { "cache" , "partition-size:", "partition-type:", "", "ext4" }, |
| 273 | }; |
| 274 | |
| 275 | char max_download_size[MAX_RSP_SIZE]; |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 276 | char charger_screen_enabled[MAX_RSP_SIZE]; |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 277 | char sn_buf[13]; |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 278 | char display_panel_buf[MAX_PANEL_BUF_SIZE]; |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 279 | char panel_display_mode[MAX_RSP_SIZE]; |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 280 | |
| 281 | #if CHECK_BAT_VOLTAGE |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 282 | char battery_voltage[MAX_RSP_SIZE]; |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 283 | char battery_soc_ok [MAX_RSP_SIZE]; |
| 284 | #endif |
| 285 | |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 286 | char get_variant[MAX_RSP_SIZE]; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 287 | |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 288 | extern int emmc_recovery_init(void); |
| 289 | |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 290 | #if NO_KEYPAD_DRIVER |
| 291 | extern int fastboot_trigger(void); |
| 292 | #endif |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 293 | |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 294 | 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] | 295 | { |
| 296 | /* overwrite the destination of specified for the project */ |
Channagoud Kadabi | 7042fa3 | 2013-04-26 16:44:14 -0700 | [diff] [blame] | 297 | #ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 298 | if (is_arm64) |
| 299 | hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR; |
| 300 | else |
| 301 | hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR; |
Channagoud Kadabi | 7042fa3 | 2013-04-26 16:44:14 -0700 | [diff] [blame] | 302 | hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR; |
| 303 | hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR; |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 304 | #endif |
| 305 | } |
| 306 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 307 | static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn) |
| 308 | { |
| 309 | struct atag_ptbl_entry atag_ptn; |
| 310 | |
| 311 | memcpy(atag_ptn.name, ptn->name, 16); |
| 312 | atag_ptn.name[15] = '\0'; |
| 313 | atag_ptn.offset = ptn->start; |
| 314 | atag_ptn.size = ptn->length; |
| 315 | atag_ptn.flags = ptn->flags; |
| 316 | memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry)); |
| 317 | *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned); |
| 318 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 319 | |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 320 | #if CHECK_BAT_VOLTAGE |
| 321 | void update_battery_status(void) |
| 322 | { |
| 323 | snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage()); |
| 324 | snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no"); |
| 325 | } |
| 326 | #endif |
| 327 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 328 | unsigned char *update_cmdline(const char * cmdline) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 329 | { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 330 | int cmdline_len = 0; |
| 331 | int have_cmdline = 0; |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 332 | unsigned char *cmdline_final = NULL; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 333 | int pause_at_bootup = 0; |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 334 | bool warm_boot = false; |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 335 | bool gpt_exists = partition_gpt_exists(); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 336 | int have_target_boot_params = 0; |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 337 | char *boot_dev_buf = NULL; |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 338 | bool is_mdtp_activated = 0; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 339 | #if VERIFIED_BOOT |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 340 | #if !VBOOT_MOTA |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 341 | uint32_t boot_state = boot_verify_get_state(); |
| 342 | #endif |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 343 | #endif |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 344 | |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 345 | #ifdef MDTP_SUPPORT |
| 346 | mdtp_activated(&is_mdtp_activated); |
| 347 | #endif /* MDTP_SUPPORT */ |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 348 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 349 | if (cmdline && cmdline[0]) { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 350 | cmdline_len = strlen(cmdline); |
| 351 | have_cmdline = 1; |
| 352 | } |
| 353 | if (target_is_emmc_boot()) { |
| 354 | cmdline_len += strlen(emmc_cmdline); |
Sridhar Parasuram | 7bd4aaf | 2015-02-12 11:14:38 -0800 | [diff] [blame] | 355 | #if USE_BOOTDEV_CMDLINE |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 356 | boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN); |
| 357 | ASSERT(boot_dev_buf); |
| 358 | platform_boot_dev_cmdline(boot_dev_buf); |
| 359 | cmdline_len += strlen(boot_dev_buf); |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 360 | #endif |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 361 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 362 | |
| 363 | cmdline_len += strlen(usb_sn_cmdline); |
| 364 | cmdline_len += strlen(sn_buf); |
| 365 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 366 | #if VERIFIED_BOOT |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 367 | #if !VBOOT_MOTA |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 368 | cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name); |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 369 | if ((device.verity_mode != 0 ) && (device.verity_mode != 1)) |
| 370 | { |
| 371 | dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n"); |
| 372 | ASSERT(0); |
| 373 | } |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 374 | cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name); |
Parth Dixit | a5715a0 | 2015-10-29 12:25:10 +0530 | [diff] [blame] | 375 | cmdline_len += strlen(keymaster_v1); |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 376 | #endif |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 377 | #endif |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 378 | |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 379 | if (boot_into_recovery && gpt_exists) |
| 380 | cmdline_len += strlen(secondary_gpt_enable); |
| 381 | |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 382 | if(is_mdtp_activated) |
| 383 | cmdline_len += strlen(mdtp_activated_flag); |
| 384 | |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 385 | if (boot_into_ffbm) { |
| 386 | cmdline_len += strlen(androidboot_mode); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 387 | cmdline_len += strlen(ffbm_mode_string); |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 388 | /* reduce kernel console messages to speed-up boot */ |
| 389 | cmdline_len += strlen(loglevel); |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 390 | } else if (boot_reason_alarm) { |
| 391 | cmdline_len += strlen(alarmboot_cmdline); |
Zhenhua Huang | 431dafa | 2015-06-30 16:13:37 +0800 | [diff] [blame] | 392 | } else if ((target_build_variant_user() || device.charger_screen_enabled) |
| 393 | && target_pause_for_battery_charge()) { |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 394 | pause_at_bootup = 1; |
| 395 | cmdline_len += strlen(battchg_pause); |
| 396 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 397 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 398 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 399 | cmdline_len += strlen(auth_kernel); |
| 400 | } |
| 401 | |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 402 | if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" : |
| 403 | "system", |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 404 | &target_boot_params) == 0) { |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 405 | have_target_boot_params = 1; |
| 406 | cmdline_len += strlen(target_boot_params); |
| 407 | } |
| 408 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 409 | /* Determine correct androidboot.baseband to use */ |
| 410 | switch(target_baseband()) |
| 411 | { |
| 412 | case BASEBAND_APQ: |
| 413 | cmdline_len += strlen(baseband_apq); |
| 414 | break; |
| 415 | |
| 416 | case BASEBAND_MSM: |
| 417 | cmdline_len += strlen(baseband_msm); |
| 418 | break; |
| 419 | |
| 420 | case BASEBAND_CSFB: |
| 421 | cmdline_len += strlen(baseband_csfb); |
| 422 | break; |
| 423 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 424 | case BASEBAND_SVLTE2A: |
| 425 | cmdline_len += strlen(baseband_svlte2a); |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 426 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 427 | |
| 428 | case BASEBAND_MDM: |
| 429 | cmdline_len += strlen(baseband_mdm); |
| 430 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 431 | |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 432 | case BASEBAND_MDM2: |
| 433 | cmdline_len += strlen(baseband_mdm2); |
| 434 | break; |
| 435 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 436 | case BASEBAND_SGLTE: |
| 437 | cmdline_len += strlen(baseband_sglte); |
| 438 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 439 | |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 440 | case BASEBAND_SGLTE2: |
| 441 | cmdline_len += strlen(baseband_sglte2); |
| 442 | break; |
| 443 | |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 444 | case BASEBAND_DSDA: |
| 445 | cmdline_len += strlen(baseband_dsda); |
| 446 | break; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 447 | |
| 448 | case BASEBAND_DSDA2: |
| 449 | cmdline_len += strlen(baseband_dsda2); |
| 450 | break; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 451 | case BASEBAND_APQ_NOWGR: |
| 452 | cmdline_len += strlen(baseband_apq_nowgr); |
| 453 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Lijuan Gao | 4a5b832 | 2014-07-24 10:38:42 +0800 | [diff] [blame] | 456 | if (cmdline) { |
| 457 | if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) && |
Padmanabhan Komanduru | bccbcdc | 2015-06-30 16:19:24 +0530 | [diff] [blame] | 458 | target_display_panel_node(display_panel_buf, |
| 459 | MAX_PANEL_BUF_SIZE) && |
Lijuan Gao | 4a5b832 | 2014-07-24 10:38:42 +0800 | [diff] [blame] | 460 | strlen(display_panel_buf)) { |
| 461 | cmdline_len += strlen(display_panel_buf); |
| 462 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 465 | if (target_warm_boot()) { |
| 466 | warm_boot = true; |
| 467 | cmdline_len += strlen(warmboot_cmdline); |
| 468 | } |
| 469 | |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 470 | #if TARGET_CMDLINE_SUPPORT |
| 471 | char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF); |
| 472 | int target_cmd_line_len; |
| 473 | ASSERT(target_cmdline_buf); |
| 474 | target_cmd_line_len = target_update_cmdline(target_cmdline_buf); |
| 475 | cmdline_len += target_cmd_line_len; |
| 476 | #endif |
| 477 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 478 | if (cmdline_len > 0) { |
| 479 | const char *src; |
Maria Yu | 52254c0 | 2014-07-04 16:14:54 +0800 | [diff] [blame] | 480 | unsigned char *dst; |
| 481 | |
| 482 | cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3)); |
| 483 | ASSERT(cmdline_final != NULL); |
vijay kumar | 287bb54 | 2015-09-29 13:01:52 +0530 | [diff] [blame] | 484 | memset((void *)cmdline_final, 0, sizeof(*cmdline_final)); |
Maria Yu | 52254c0 | 2014-07-04 16:14:54 +0800 | [diff] [blame] | 485 | dst = cmdline_final; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 486 | |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 487 | /* Save start ptr for debug print */ |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 488 | if (have_cmdline) { |
| 489 | src = cmdline; |
| 490 | while ((*dst++ = *src++)); |
| 491 | } |
| 492 | if (target_is_emmc_boot()) { |
| 493 | src = emmc_cmdline; |
| 494 | if (have_cmdline) --dst; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 495 | have_cmdline = 1; |
| 496 | while ((*dst++ = *src++)); |
Sridhar Parasuram | 7bd4aaf | 2015-02-12 11:14:38 -0800 | [diff] [blame] | 497 | #if USE_BOOTDEV_CMDLINE |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 498 | src = boot_dev_buf; |
| 499 | if (have_cmdline) --dst; |
| 500 | while ((*dst++ = *src++)); |
| 501 | #endif |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 502 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 503 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 504 | #if VERIFIED_BOOT |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 505 | #if !VBOOT_MOTA |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 506 | src = verified_state; |
| 507 | if(have_cmdline) --dst; |
| 508 | have_cmdline = 1; |
| 509 | while ((*dst++ = *src++)); |
| 510 | src = vbsn[boot_state].name; |
| 511 | if(have_cmdline) --dst; |
| 512 | while ((*dst++ = *src++)); |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 513 | |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 514 | if ((device.verity_mode != 0 ) && (device.verity_mode != 1)) |
| 515 | { |
| 516 | dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n"); |
| 517 | ASSERT(0); |
| 518 | } |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 519 | src = verity_mode; |
| 520 | if(have_cmdline) --dst; |
| 521 | while ((*dst++ = *src++)); |
| 522 | src = vbvm[device.verity_mode].name; |
| 523 | if(have_cmdline) -- dst; |
| 524 | while ((*dst++ = *src++)); |
Parth Dixit | a5715a0 | 2015-10-29 12:25:10 +0530 | [diff] [blame] | 525 | src = keymaster_v1; |
| 526 | if(have_cmdline) --dst; |
| 527 | while ((*dst++ = *src++)); |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 528 | #endif |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 529 | #endif |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 530 | src = usb_sn_cmdline; |
| 531 | if (have_cmdline) --dst; |
| 532 | have_cmdline = 1; |
| 533 | while ((*dst++ = *src++)); |
| 534 | src = sn_buf; |
| 535 | if (have_cmdline) --dst; |
| 536 | have_cmdline = 1; |
| 537 | while ((*dst++ = *src++)); |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 538 | if (warm_boot) { |
| 539 | if (have_cmdline) --dst; |
| 540 | src = warmboot_cmdline; |
| 541 | while ((*dst++ = *src++)); |
| 542 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 543 | |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 544 | if (boot_into_recovery && gpt_exists) { |
| 545 | src = secondary_gpt_enable; |
| 546 | if (have_cmdline) --dst; |
| 547 | while ((*dst++ = *src++)); |
| 548 | } |
| 549 | |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 550 | if (is_mdtp_activated) { |
| 551 | src = mdtp_activated_flag; |
| 552 | if (have_cmdline) --dst; |
| 553 | while ((*dst++ = *src++)); |
| 554 | } |
| 555 | |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 556 | if (boot_into_ffbm) { |
| 557 | src = androidboot_mode; |
| 558 | if (have_cmdline) --dst; |
| 559 | while ((*dst++ = *src++)); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 560 | src = ffbm_mode_string; |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 561 | if (have_cmdline) --dst; |
| 562 | while ((*dst++ = *src++)); |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 563 | src = loglevel; |
| 564 | if (have_cmdline) --dst; |
| 565 | while ((*dst++ = *src++)); |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 566 | } else if (boot_reason_alarm) { |
| 567 | src = alarmboot_cmdline; |
| 568 | if (have_cmdline) --dst; |
| 569 | while ((*dst++ = *src++)); |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 570 | } else if (pause_at_bootup) { |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 571 | src = battchg_pause; |
| 572 | if (have_cmdline) --dst; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 573 | while ((*dst++ = *src++)); |
| 574 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 575 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 576 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 577 | src = auth_kernel; |
| 578 | if (have_cmdline) --dst; |
| 579 | while ((*dst++ = *src++)); |
| 580 | } |
| 581 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 582 | switch(target_baseband()) |
| 583 | { |
| 584 | case BASEBAND_APQ: |
| 585 | src = baseband_apq; |
| 586 | if (have_cmdline) --dst; |
| 587 | while ((*dst++ = *src++)); |
| 588 | break; |
| 589 | |
| 590 | case BASEBAND_MSM: |
| 591 | src = baseband_msm; |
| 592 | if (have_cmdline) --dst; |
| 593 | while ((*dst++ = *src++)); |
| 594 | break; |
| 595 | |
| 596 | case BASEBAND_CSFB: |
| 597 | src = baseband_csfb; |
| 598 | if (have_cmdline) --dst; |
| 599 | while ((*dst++ = *src++)); |
| 600 | break; |
| 601 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 602 | case BASEBAND_SVLTE2A: |
| 603 | src = baseband_svlte2a; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 604 | if (have_cmdline) --dst; |
| 605 | while ((*dst++ = *src++)); |
| 606 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 607 | |
| 608 | case BASEBAND_MDM: |
| 609 | src = baseband_mdm; |
| 610 | if (have_cmdline) --dst; |
| 611 | while ((*dst++ = *src++)); |
| 612 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 613 | |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 614 | case BASEBAND_MDM2: |
| 615 | src = baseband_mdm2; |
| 616 | if (have_cmdline) --dst; |
| 617 | while ((*dst++ = *src++)); |
| 618 | break; |
| 619 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 620 | case BASEBAND_SGLTE: |
| 621 | src = baseband_sglte; |
| 622 | if (have_cmdline) --dst; |
| 623 | while ((*dst++ = *src++)); |
| 624 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 625 | |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 626 | case BASEBAND_SGLTE2: |
| 627 | src = baseband_sglte2; |
| 628 | if (have_cmdline) --dst; |
| 629 | while ((*dst++ = *src++)); |
| 630 | break; |
| 631 | |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 632 | case BASEBAND_DSDA: |
| 633 | src = baseband_dsda; |
| 634 | if (have_cmdline) --dst; |
| 635 | while ((*dst++ = *src++)); |
| 636 | break; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 637 | |
| 638 | case BASEBAND_DSDA2: |
| 639 | src = baseband_dsda2; |
| 640 | if (have_cmdline) --dst; |
| 641 | while ((*dst++ = *src++)); |
| 642 | break; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 643 | case BASEBAND_APQ_NOWGR: |
| 644 | src = baseband_apq_nowgr; |
| 645 | if (have_cmdline) --dst; |
| 646 | while ((*dst++ = *src++)); |
| 647 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 648 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 649 | |
| 650 | if (strlen(display_panel_buf)) { |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 651 | src = display_panel_buf; |
| 652 | if (have_cmdline) --dst; |
| 653 | while ((*dst++ = *src++)); |
| 654 | } |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 655 | |
| 656 | if (have_target_boot_params) { |
| 657 | if (have_cmdline) --dst; |
| 658 | src = target_boot_params; |
| 659 | while ((*dst++ = *src++)); |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 660 | free(target_boot_params); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 661 | } |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 662 | |
| 663 | #if TARGET_CMDLINE_SUPPORT |
| 664 | if (target_cmdline_buf && target_cmd_line_len) |
| 665 | { |
| 666 | if (have_cmdline) --dst; |
| 667 | src = target_cmdline_buf; |
| 668 | while((*dst++ = *src++)); |
| 669 | free(target_cmdline_buf); |
| 670 | } |
| 671 | #endif |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 672 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 673 | |
| 674 | |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 675 | if (boot_dev_buf) |
| 676 | free(boot_dev_buf); |
| 677 | |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 678 | if (cmdline_final) |
| 679 | dprintf(INFO, "cmdline: %s\n", cmdline_final); |
| 680 | else |
| 681 | dprintf(INFO, "cmdline is NULL\n"); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 682 | return cmdline_final; |
| 683 | } |
| 684 | |
| 685 | unsigned *atag_core(unsigned *ptr) |
| 686 | { |
| 687 | /* CORE */ |
| 688 | *ptr++ = 2; |
| 689 | *ptr++ = 0x54410001; |
| 690 | |
| 691 | return ptr; |
| 692 | |
| 693 | } |
| 694 | |
| 695 | unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk, |
| 696 | unsigned ramdisk_size) |
| 697 | { |
| 698 | if (ramdisk_size) { |
| 699 | *ptr++ = 4; |
| 700 | *ptr++ = 0x54420005; |
| 701 | *ptr++ = (unsigned)ramdisk; |
| 702 | *ptr++ = ramdisk_size; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 703 | } |
| 704 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 705 | return ptr; |
| 706 | } |
| 707 | |
| 708 | unsigned *atag_ptable(unsigned **ptr_addr) |
| 709 | { |
| 710 | int i; |
| 711 | struct ptable *ptable; |
| 712 | |
| 713 | if ((ptable = flash_get_ptable()) && (ptable->count != 0)) { |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 714 | *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) / |
| 715 | sizeof(unsigned))); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 716 | *(*ptr_addr)++ = 0x4d534d70; |
| 717 | for (i = 0; i < ptable->count; ++i) |
| 718 | ptentry_to_tag(ptr_addr, ptable_get(ptable, i)); |
| 719 | } |
| 720 | |
| 721 | return (*ptr_addr); |
| 722 | } |
| 723 | |
| 724 | unsigned *atag_cmdline(unsigned *ptr, const char *cmdline) |
| 725 | { |
| 726 | int cmdline_length = 0; |
| 727 | int n; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 728 | char *dest; |
| 729 | |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 730 | cmdline_length = strlen((const char*)cmdline); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 731 | n = (cmdline_length + 4) & (~3); |
| 732 | |
| 733 | *ptr++ = (n / 4) + 2; |
| 734 | *ptr++ = 0x54410009; |
| 735 | dest = (char *) ptr; |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 736 | while ((*dest++ = *cmdline++)); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 737 | ptr += (n / 4); |
| 738 | |
| 739 | return ptr; |
| 740 | } |
| 741 | |
| 742 | unsigned *atag_end(unsigned *ptr) |
| 743 | { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 744 | /* END */ |
| 745 | *ptr++ = 0; |
| 746 | *ptr++ = 0; |
| 747 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 748 | return ptr; |
| 749 | } |
| 750 | |
| 751 | void generate_atags(unsigned *ptr, const char *cmdline, |
| 752 | void *ramdisk, unsigned ramdisk_size) |
| 753 | { |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 754 | unsigned *orig_ptr = ptr; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 755 | ptr = atag_core(ptr); |
| 756 | ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size); |
| 757 | ptr = target_atag_mem(ptr); |
| 758 | |
| 759 | /* Skip NAND partition ATAGS for eMMC boot */ |
| 760 | if (!target_is_emmc_boot()){ |
| 761 | ptr = atag_ptable(&ptr); |
| 762 | } |
| 763 | |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 764 | /* |
| 765 | * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned |
| 766 | * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes |
| 767 | */ |
| 768 | if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) { |
| 769 | ptr = atag_cmdline(ptr, cmdline); |
| 770 | ptr = atag_end(ptr); |
| 771 | } |
| 772 | else { |
| 773 | dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n"); |
| 774 | ASSERT(0); |
| 775 | } |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 778 | typedef void entry_func_ptr(unsigned, unsigned, unsigned*); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 779 | void boot_linux(void *kernel, unsigned *tags, |
| 780 | const char *cmdline, unsigned machtype, |
| 781 | void *ramdisk, unsigned ramdisk_size) |
| 782 | { |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 783 | unsigned char *final_cmdline; |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 784 | #if DEVICE_TREE |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 785 | int ret = 0; |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 786 | #endif |
| 787 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 788 | void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel)); |
Deepa Dinamani | 25a9f76 | 2012-11-30 15:57:15 -0800 | [diff] [blame] | 789 | uint32_t tags_phys = PA((addr_t)tags); |
vijay kumar | 1a50a64 | 2015-11-16 12:41:15 +0530 | [diff] [blame] | 790 | struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel))); |
Deepa Dinamani | 25a9f76 | 2012-11-30 15:57:15 -0800 | [diff] [blame] | 791 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 792 | ramdisk = (void *)PA((addr_t)ramdisk); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 793 | |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 794 | final_cmdline = update_cmdline((const char*)cmdline); |
| 795 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 796 | #if DEVICE_TREE |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 797 | dprintf(INFO, "Updating device tree: start\n"); |
| 798 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 799 | /* Update the Device Tree */ |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 800 | ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 801 | if(ret) |
| 802 | { |
| 803 | dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n"); |
| 804 | ASSERT(0); |
| 805 | } |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 806 | dprintf(INFO, "Updating device tree: done\n"); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 807 | #else |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 808 | /* Generating the Atags */ |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 809 | generate_atags(tags, final_cmdline, ramdisk, ramdisk_size); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 810 | #endif |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 811 | |
Maria Yu | 52254c0 | 2014-07-04 16:14:54 +0800 | [diff] [blame] | 812 | free(final_cmdline); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 813 | |
| 814 | #if VERIFIED_BOOT |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 815 | #if !VBOOT_MOTA |
| 816 | if (device.verity_mode == 0) { |
| 817 | #if FBCON_DISPLAY_MSG |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame^] | 818 | #if ENABLE_VB_ATTEST |
| 819 | display_bootverify_menu(DISPLAY_MENU_EIO); |
| 820 | #else |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 821 | display_bootverify_menu(DISPLAY_MENU_LOGGING); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame^] | 822 | #endif |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 823 | wait_for_users_action(); |
| 824 | #else |
| 825 | dprintf(CRITICAL, |
| 826 | "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n"); |
| 827 | mdelay(5000); |
| 828 | #endif |
| 829 | } |
| 830 | |
| 831 | #endif |
| 832 | #endif |
| 833 | |
| 834 | #if VERIFIED_BOOT |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 835 | /* Write protect the device info */ |
Channagoud Kadabi | 3bd9d1e | 2015-05-05 16:18:20 -0700 | [diff] [blame] | 836 | if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1)) |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 837 | { |
| 838 | dprintf(INFO, "Failed to write protect dev info\n"); |
| 839 | ASSERT(0); |
| 840 | } |
| 841 | #endif |
| 842 | |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 843 | /* Turn off splash screen if enabled */ |
| 844 | #if DISPLAY_SPLASH_SCREEN |
| 845 | target_display_shutdown(); |
| 846 | #endif |
| 847 | |
Veera Sundaram Sankaran | 67ea093 | 2015-09-25 10:09:30 -0700 | [diff] [blame] | 848 | /* Perform target specific cleanup */ |
| 849 | target_uninit(); |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 850 | |
Deepa Dinamani | 33734bc | 2013-03-06 12:16:06 -0800 | [diff] [blame] | 851 | dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n", |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 852 | entry, ramdisk, ramdisk_size, (void *)tags_phys); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 853 | |
| 854 | enter_critical_section(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 855 | |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 856 | /* do any platform specific cleanup before kernel entry */ |
| 857 | platform_uninit(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 858 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 859 | arch_disable_cache(UCACHE); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 860 | |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 861 | #if ARM_WITH_MMU |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 862 | arch_disable_mmu(); |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 863 | #endif |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 864 | bs_set_timestamp(BS_KERNEL_ENTRY); |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 865 | |
| 866 | if (IS_ARM64(kptr)) |
| 867 | /* Jump to a 64bit kernel */ |
| 868 | scm_elexec_call((paddr_t)kernel, tags_phys); |
| 869 | else |
| 870 | /* Jump to a 32bit kernel */ |
| 871 | entry(0, machtype, (unsigned*)tags_phys); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 874 | /* Function to check if the memory address range falls within the aboot |
| 875 | * boundaries. |
| 876 | * start: Start of the memory region |
| 877 | * size: Size of the memory region |
| 878 | */ |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 879 | int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 880 | { |
| 881 | /* Check for boundary conditions. */ |
Sundarajan Srinivasan | ce2a0ea | 2013-12-16 17:02:56 -0800 | [diff] [blame] | 882 | if ((UINT_MAX - start) < size) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 883 | return -1; |
| 884 | |
| 885 | /* Check for memory overlap. */ |
| 886 | if ((start < MEMBASE) && ((start + size) <= MEMBASE)) |
| 887 | return 0; |
Channagoud Kadabi | 9414391 | 2013-10-15 12:53:52 -0700 | [diff] [blame] | 888 | else if (start >= (MEMBASE + MEMSIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 889 | return 0; |
| 890 | else |
| 891 | return -1; |
| 892 | } |
| 893 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 894 | #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y))) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 895 | |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 896 | 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] | 897 | #if DEVICE_TREE |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 898 | BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE); |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 899 | #endif |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 900 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 901 | static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size) |
| 902 | { |
| 903 | int ret; |
Channagoud Kadabi | a8c623f | 2015-01-13 14:48:48 -0800 | [diff] [blame] | 904 | |
| 905 | #if !VERIFIED_BOOT |
Sundarajan Srinivasan | ce54d6e | 2013-11-11 12:45:00 -0800 | [diff] [blame] | 906 | #if IMAGE_VERIF_ALGO_SHA1 |
| 907 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1; |
| 908 | #else |
| 909 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256; |
| 910 | #endif |
Channagoud Kadabi | a8c623f | 2015-01-13 14:48:48 -0800 | [diff] [blame] | 911 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 912 | |
| 913 | /* Assume device is rooted at this time. */ |
| 914 | device.is_tampered = 1; |
| 915 | |
| 916 | dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size); |
| 917 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 918 | #if VERIFIED_BOOT |
| 919 | if(boot_into_recovery) |
| 920 | { |
| 921 | ret = boot_verify_image((unsigned char *)bootimg_addr, |
Unnati Gandhi | 4705125 | 2015-03-02 15:21:09 +0530 | [diff] [blame] | 922 | bootimg_size, "/recovery"); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 923 | } |
| 924 | else |
| 925 | { |
| 926 | ret = boot_verify_image((unsigned char *)bootimg_addr, |
Unnati Gandhi | 4705125 | 2015-03-02 15:21:09 +0530 | [diff] [blame] | 927 | bootimg_size, "/boot"); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 928 | } |
| 929 | boot_verify_print_state(); |
| 930 | #else |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 931 | ret = image_verify((unsigned char *)bootimg_addr, |
| 932 | (unsigned char *)(bootimg_addr + bootimg_size), |
| 933 | bootimg_size, |
Sundarajan Srinivasan | ce54d6e | 2013-11-11 12:45:00 -0800 | [diff] [blame] | 934 | auth_algo); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 935 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 936 | dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret); |
| 937 | |
| 938 | if (ret) |
| 939 | { |
| 940 | /* Authorized kernel */ |
| 941 | device.is_tampered = 0; |
Sundarajan Srinivasan | 3fb21f1 | 2013-09-16 18:36:15 -0700 | [diff] [blame] | 942 | auth_kernel_img = 1; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 945 | #ifdef MDTP_SUPPORT |
| 946 | { |
| 947 | /* Verify MDTP lock. |
| 948 | * For boot & recovery partitions, use aboot's verification result. |
| 949 | */ |
| 950 | mdtp_ext_partition_verification_t ext_partition; |
| 951 | ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT; |
| 952 | ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID; |
| 953 | ext_partition.page_size = 0; /* Not needed since already validated */ |
| 954 | ext_partition.image_addr = 0; /* Not needed since already validated */ |
| 955 | ext_partition.image_size = 0; /* Not needed since already validated */ |
| 956 | ext_partition.sig_avail = FALSE; /* Not needed since already validated */ |
| 957 | mdtp_fwlock_verify_lock(&ext_partition); |
| 958 | } |
| 959 | #endif /* MDTP_SUPPORT */ |
| 960 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 961 | #if USE_PCOM_SECBOOT |
| 962 | set_tamper_flag(device.is_tampered); |
| 963 | #endif |
| 964 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 965 | #if VERIFIED_BOOT |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 966 | switch(boot_verify_get_state()) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 967 | { |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 968 | case RED: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 969 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 970 | display_bootverify_menu(DISPLAY_MENU_RED); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame^] | 971 | #if ENABLE_VB_ATTEST |
| 972 | mdelay(DELAY_WAIT); |
| 973 | shutdown_device(); |
| 974 | #else |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 975 | wait_for_users_action(); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame^] | 976 | #endif |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 977 | #else |
| 978 | dprintf(CRITICAL, |
| 979 | "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n"); |
| 980 | mdelay(5000); |
| 981 | #endif |
| 982 | |
| 983 | break; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 984 | case YELLOW: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 985 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 986 | display_bootverify_menu(DISPLAY_MENU_YELLOW); |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 987 | wait_for_users_action(); |
| 988 | #else |
| 989 | dprintf(CRITICAL, |
| 990 | "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n"); |
| 991 | mdelay(5000); |
| 992 | #endif |
| 993 | break; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 994 | default: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 995 | break; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 996 | } |
| 997 | #endif |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 998 | #if !VERIFIED_BOOT |
Unnati Gandhi | 1be0475 | 2015-03-27 19:41:53 +0530 | [diff] [blame] | 999 | if(device.is_tampered) |
| 1000 | { |
| 1001 | write_device_info_mmc(&device); |
| 1002 | #ifdef TZ_TAMPER_FUSE |
| 1003 | set_tamper_fuse_cmd(); |
| 1004 | #endif |
| 1005 | #ifdef ASSERT_ON_TAMPER |
| 1006 | dprintf(CRITICAL, "Device is tampered. Asserting..\n"); |
| 1007 | ASSERT(0); |
| 1008 | #endif |
| 1009 | } |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1010 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1013 | static bool check_format_bit() |
| 1014 | { |
| 1015 | bool ret = false; |
| 1016 | int index; |
| 1017 | uint64_t offset; |
| 1018 | struct boot_selection_info *in = NULL; |
| 1019 | char *buf = NULL; |
| 1020 | |
| 1021 | index = partition_get_index("bootselect"); |
| 1022 | if (index == INVALID_PTN) |
| 1023 | { |
| 1024 | dprintf(INFO, "Unable to locate /bootselect partition\n"); |
| 1025 | return ret; |
| 1026 | } |
| 1027 | offset = partition_get_offset(index); |
| 1028 | if(!offset) |
| 1029 | { |
| 1030 | dprintf(INFO, "partition /bootselect doesn't exist\n"); |
| 1031 | return ret; |
| 1032 | } |
| 1033 | buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE)); |
| 1034 | ASSERT(buf); |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1035 | if (mmc_read(offset, (uint32_t *)buf, page_size)) |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1036 | { |
| 1037 | dprintf(INFO, "mmc read failure /bootselect %d\n", page_size); |
| 1038 | free(buf); |
| 1039 | return ret; |
| 1040 | } |
| 1041 | in = (struct boot_selection_info *) buf; |
| 1042 | if ((in->signature == BOOTSELECT_SIGNATURE) && |
| 1043 | (in->version == BOOTSELECT_VERSION)) { |
| 1044 | if ((in->state_info & BOOTSELECT_FORMAT) && |
| 1045 | !(in->state_info & BOOTSELECT_FACTORY)) |
| 1046 | ret = true; |
| 1047 | } else { |
| 1048 | dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n", |
| 1049 | in->signature, in->version); |
| 1050 | ASSERT(0); |
| 1051 | } |
| 1052 | free(buf); |
| 1053 | return ret; |
| 1054 | } |
| 1055 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1056 | void boot_verifier_init() |
| 1057 | { |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1058 | uint32_t boot_state; |
| 1059 | /* Check if device unlock */ |
| 1060 | if(device.is_unlocked) |
| 1061 | { |
| 1062 | boot_verify_send_event(DEV_UNLOCK); |
| 1063 | boot_verify_print_state(); |
| 1064 | dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n"); |
| 1065 | return; |
| 1066 | } |
| 1067 | else |
| 1068 | { |
| 1069 | boot_verify_send_event(BOOT_INIT); |
| 1070 | } |
| 1071 | |
| 1072 | /* Initialize keystore */ |
| 1073 | boot_state = boot_verify_keystore_init(); |
| 1074 | if(boot_state == YELLOW) |
| 1075 | { |
| 1076 | boot_verify_print_state(); |
| 1077 | dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n"); |
| 1078 | } |
| 1079 | } |
| 1080 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1081 | int boot_linux_from_mmc(void) |
| 1082 | { |
| 1083 | struct boot_img_hdr *hdr = (void*) buf; |
| 1084 | struct boot_img_hdr *uhdr; |
| 1085 | unsigned offset = 0; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 1086 | int rcode; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1087 | unsigned long long ptn = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1088 | int index = INVALID_PTN; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1089 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1090 | unsigned char *image_addr = 0; |
| 1091 | unsigned kernel_actual; |
| 1092 | unsigned ramdisk_actual; |
| 1093 | unsigned imagesize_actual; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1094 | unsigned second_actual = 0; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1095 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1096 | unsigned int dtb_size = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1097 | unsigned int out_len = 0; |
| 1098 | unsigned int out_avai_len = 0; |
| 1099 | unsigned char *out_addr = NULL; |
| 1100 | uint32_t dtb_offset = 0; |
| 1101 | unsigned char *kernel_start_addr = NULL; |
| 1102 | unsigned int kernel_size = 0; |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 1103 | unsigned int patched_kernel_hdr_size = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1104 | int rc; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1105 | #if DEVICE_TREE |
| 1106 | struct dt_table *table; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1107 | struct dt_entry dt_entry; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1108 | unsigned dt_table_offset; |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1109 | uint32_t dt_actual; |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 1110 | uint32_t dt_hdr_size; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1111 | unsigned char *best_match_dt_addr = NULL; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1112 | #endif |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1113 | struct kernel64_hdr *kptr = NULL; |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1114 | |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1115 | if (check_format_bit()) |
| 1116 | boot_into_recovery = 1; |
| 1117 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 1118 | if (!boot_into_recovery) { |
| 1119 | memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string)); |
| 1120 | rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string)); |
| 1121 | if (rcode <= 0) { |
| 1122 | boot_into_ffbm = false; |
| 1123 | if (rcode < 0) |
| 1124 | dprintf(CRITICAL,"failed to get ffbm cookie"); |
| 1125 | } else |
| 1126 | boot_into_ffbm = true; |
| 1127 | } else |
| 1128 | boot_into_ffbm = false; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1129 | uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 1130 | if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 1131 | dprintf(INFO, "Unified boot method!\n"); |
| 1132 | hdr = uhdr; |
| 1133 | goto unified_boot; |
| 1134 | } |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 1135 | if (!boot_into_recovery) { |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1136 | index = partition_get_index("boot"); |
| 1137 | ptn = partition_get_offset(index); |
| 1138 | if(ptn == 0) { |
Shashank Mittal | 85b91f6 | 2010-10-30 10:12:38 -0700 | [diff] [blame] | 1139 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 1140 | return -1; |
| 1141 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1142 | } |
| 1143 | else { |
| 1144 | index = partition_get_index("recovery"); |
| 1145 | ptn = partition_get_offset(index); |
| 1146 | if(ptn == 0) { |
Shashank Mittal | 85b91f6 | 2010-10-30 10:12:38 -0700 | [diff] [blame] | 1147 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 1148 | return -1; |
| 1149 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1150 | } |
Channagoud Kadabi | ef0547c | 2015-02-10 12:57:38 -0800 | [diff] [blame] | 1151 | /* Set Lun for boot & recovery partitions */ |
| 1152 | mmc_set_lun(partition_get_lun(index)); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1153 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1154 | if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) { |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1155 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 1156 | return -1; |
| 1157 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1158 | |
| 1159 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 1160 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1161 | return -1; |
| 1162 | } |
| 1163 | |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 1164 | if (hdr->page_size && (hdr->page_size != page_size)) { |
vijay kumar | 2e21b3a | 2014-06-26 17:40:15 +0530 | [diff] [blame] | 1165 | |
| 1166 | if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) { |
| 1167 | dprintf(CRITICAL, "ERROR: Invalid page size\n"); |
| 1168 | return -1; |
| 1169 | } |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 1170 | page_size = hdr->page_size; |
| 1171 | page_mask = page_size - 1; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
vijay kumar | 287bb54 | 2015-09-29 13:01:52 +0530 | [diff] [blame] | 1174 | /* ensure commandline is terminated */ |
| 1175 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 1176 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1177 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 1178 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1179 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1180 | image_addr = (unsigned char *)target_get_scratch_address(); |
| 1181 | |
| 1182 | #if DEVICE_TREE |
| 1183 | dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask); |
| 1184 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual); |
| 1185 | #else |
| 1186 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual); |
| 1187 | #endif |
| 1188 | |
| 1189 | #if VERIFIED_BOOT |
| 1190 | boot_verifier_init(); |
| 1191 | #endif |
| 1192 | |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 1193 | if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual)) |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1194 | { |
| 1195 | dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n"); |
| 1196 | return -1; |
| 1197 | } |
| 1198 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1199 | /* |
| 1200 | * Update loading flow of bootimage to support compressed/uncompressed |
| 1201 | * bootimage on both 64bit and 32bit platform. |
| 1202 | * 1. Load bootimage from emmc partition onto DDR. |
| 1203 | * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel |
| 1204 | * 3. Check kernel header and update kernel load addr for 64bit and 32bit |
| 1205 | * platform accordingly. |
| 1206 | * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data. |
| 1207 | */ |
| 1208 | |
Channagoud Kadabi | f0705b5 | 2015-08-20 14:16:08 -0700 | [diff] [blame] | 1209 | dprintf(INFO, "Loading (%s) image (%d): start\n", |
| 1210 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1211 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
| 1212 | |
Gaurav Nebhwani | 43e2a46 | 2016-03-17 21:32:56 +0530 | [diff] [blame] | 1213 | if ((target_get_max_flash_size() - page_size) < imagesize_actual) |
| 1214 | { |
| 1215 | dprintf(CRITICAL, "booimage size is greater than DDR can hold\n"); |
| 1216 | return -1; |
| 1217 | } |
| 1218 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1219 | /* Read image without signature */ |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1220 | if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) |
| 1221 | { |
| 1222 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 1223 | return -1; |
| 1224 | } |
| 1225 | |
Channagoud Kadabi | f0705b5 | 2015-08-20 14:16:08 -0700 | [diff] [blame] | 1226 | dprintf(INFO, "Loading (%s) image (%d): done\n", |
| 1227 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
| 1228 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1229 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
| 1230 | |
| 1231 | /* Authenticate Kernel */ |
| 1232 | dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n", |
| 1233 | (int) target_use_signed_kernel(), |
| 1234 | device.is_unlocked, |
| 1235 | device.is_tampered); |
| 1236 | |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 1237 | /* Change the condition a little bit to include the test framework support. |
| 1238 | * We would never reach this point if device is in fastboot mode, even if we did |
| 1239 | * that means we are in test mode, so execute kernel authentication part for the |
| 1240 | * tests */ |
Parth Dixit | cb0c608 | 2015-12-30 15:01:13 +0530 | [diff] [blame] | 1241 | if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled()) |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1242 | { |
| 1243 | offset = imagesize_actual; |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 1244 | if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size)) |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1245 | { |
| 1246 | dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n"); |
| 1247 | return -1; |
| 1248 | } |
| 1249 | |
| 1250 | /* Read signature */ |
| 1251 | if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size)) |
| 1252 | { |
| 1253 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 1254 | return -1; |
| 1255 | } |
| 1256 | |
| 1257 | verify_signed_bootimg((uint32_t)image_addr, imagesize_actual); |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 1258 | /* The purpose of our test is done here */ |
Parth Dixit | cb0c608 | 2015-12-30 15:01:13 +0530 | [diff] [blame] | 1259 | if(is_test_mode_enabled() && auth_kernel_img) |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 1260 | return 0; |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1261 | } else { |
| 1262 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 1263 | #ifdef TZ_SAVE_KERNEL_HASH |
| 1264 | aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual); |
| 1265 | #endif /* TZ_SAVE_KERNEL_HASH */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 1266 | |
| 1267 | #ifdef MDTP_SUPPORT |
| 1268 | { |
| 1269 | /* Verify MDTP lock. |
| 1270 | * For boot & recovery partitions, MDTP will use boot_verifier APIs, |
| 1271 | * since verification was skipped in aboot. The signature is not part of the loaded image. |
| 1272 | */ |
| 1273 | mdtp_ext_partition_verification_t ext_partition; |
| 1274 | ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT; |
| 1275 | ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET; |
| 1276 | ext_partition.page_size = page_size; |
| 1277 | ext_partition.image_addr = (uint32)image_addr; |
| 1278 | ext_partition.image_size = imagesize_actual; |
| 1279 | ext_partition.sig_avail = FALSE; |
| 1280 | mdtp_fwlock_verify_lock(&ext_partition); |
| 1281 | } |
| 1282 | #endif /* MDTP_SUPPORT */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
Sridhar Parasuram | 00bfedb | 2015-05-26 14:21:27 -0700 | [diff] [blame] | 1285 | #if VERIFIED_BOOT |
Reut Zysman | ba8a9d5 | 2016-02-18 11:44:04 +0200 | [diff] [blame] | 1286 | if(boot_verify_get_state() == ORANGE) |
| 1287 | { |
| 1288 | #if FBCON_DISPLAY_MSG |
| 1289 | display_bootverify_menu(DISPLAY_MENU_ORANGE); |
| 1290 | wait_for_users_action(); |
| 1291 | #else |
| 1292 | dprintf(CRITICAL, |
| 1293 | "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n"); |
| 1294 | mdelay(5000); |
| 1295 | #endif |
| 1296 | } |
| 1297 | #endif |
| 1298 | |
| 1299 | #if VERIFIED_BOOT |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 1300 | #if !VBOOT_MOTA |
Sridhar Parasuram | 00bfedb | 2015-05-26 14:21:27 -0700 | [diff] [blame] | 1301 | // send root of trust |
Sridhar Parasuram | 96300dc | 2015-06-11 10:37:11 -0700 | [diff] [blame] | 1302 | if(!send_rot_command((uint32_t)device.is_unlocked)) |
Sridhar Parasuram | 00bfedb | 2015-05-26 14:21:27 -0700 | [diff] [blame] | 1303 | ASSERT(0); |
| 1304 | #endif |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 1305 | #endif |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 1306 | /* |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1307 | * Check if the kernel image is a gzip package. If yes, need to decompress it. |
| 1308 | * If not, continue booting. |
| 1309 | */ |
| 1310 | if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size)) |
| 1311 | { |
| 1312 | out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size); |
| 1313 | out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size; |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 1314 | dprintf(INFO, "decompressing kernel image: start\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1315 | rc = decompress((unsigned char *)(image_addr + page_size), |
| 1316 | hdr->kernel_size, out_addr, out_avai_len, |
| 1317 | &dtb_offset, &out_len); |
| 1318 | if (rc) |
| 1319 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 1320 | dprintf(CRITICAL, "decompressing kernel image failed!!!\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1321 | ASSERT(0); |
| 1322 | } |
| 1323 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 1324 | dprintf(INFO, "decompressing kernel image: done\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1325 | kptr = (struct kernel64_hdr *)out_addr; |
| 1326 | kernel_start_addr = out_addr; |
| 1327 | kernel_size = out_len; |
| 1328 | } else { |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 1329 | dprintf(INFO, "Uncpmpressed kernel in use\n"); |
| 1330 | if (!strncmp((char*)(image_addr + page_size), |
| 1331 | PATCHED_KERNEL_MAGIC, |
| 1332 | sizeof(PATCHED_KERNEL_MAGIC) - 1)) { |
| 1333 | dprintf(INFO, "Patched kernel detected\n"); |
| 1334 | kptr = (struct kernel64_hdr *)(image_addr + page_size + |
| 1335 | PATCHED_KERNEL_HEADER_SIZE); |
| 1336 | //The size of the kernel is stored at start of kernel image + 16 |
| 1337 | //The dtb would start just after the kernel |
| 1338 | dtb_offset = *((uint32_t*)((unsigned char*) |
| 1339 | (image_addr + page_size + |
| 1340 | sizeof(PATCHED_KERNEL_MAGIC) - |
| 1341 | 1))); |
| 1342 | //The actual kernel starts after the 20 byte header. |
| 1343 | kernel_start_addr = (unsigned char*)(image_addr + |
| 1344 | page_size + PATCHED_KERNEL_HEADER_SIZE); |
| 1345 | kernel_size = hdr->kernel_size; |
| 1346 | patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE; |
| 1347 | } else { |
| 1348 | dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n"); |
| 1349 | kptr = (struct kernel64_hdr *)(image_addr + page_size); |
| 1350 | kernel_start_addr = (unsigned char *)(image_addr + page_size); |
| 1351 | kernel_size = hdr->kernel_size; |
| 1352 | } |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | /* |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 1356 | * Update the kernel/ramdisk/tags address if the boot image header |
| 1357 | * has default values, these default values come from mkbootimg when |
| 1358 | * the boot image is flashed using fastboot flash:raw |
| 1359 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1360 | update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr)); |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 1361 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1362 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 1363 | hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr)); |
| 1364 | hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr)); |
| 1365 | hdr->tags_addr = VA((addr_t)(hdr->tags_addr)); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1366 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1367 | kernel_size = ROUND_TO_PAGE(kernel_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1368 | /* Check if the addresses in the header are valid. */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1369 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) || |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1370 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual)) |
| 1371 | { |
| 1372 | dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n"); |
| 1373 | return -1; |
| 1374 | } |
| 1375 | |
| 1376 | #ifndef DEVICE_TREE |
| 1377 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE)) |
| 1378 | { |
| 1379 | dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n"); |
| 1380 | return -1; |
| 1381 | } |
| 1382 | #endif |
| 1383 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1384 | /* Move kernel, ramdisk and device tree to correct address */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1385 | memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1386 | memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1387 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1388 | #if DEVICE_TREE |
| 1389 | if(hdr->dt_size) { |
| 1390 | dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual); |
| 1391 | table = (struct dt_table*) dt_table_offset; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1392 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1393 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
| 1394 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 1395 | return -1; |
| 1396 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1397 | |
P.V. Phani Kumar | 8291676 | 2016-03-09 09:20:19 +0530 | [diff] [blame] | 1398 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 1399 | goes beyound hdr->dt_size*/ |
| 1400 | if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) { |
| 1401 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 1402 | return -1; |
| 1403 | } |
| 1404 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1405 | /* Find index of device tree within device tree table */ |
| 1406 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
| 1407 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 1408 | return -1; |
| 1409 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1410 | |
P.V. Phani Kumar | 3c33320 | 2016-03-09 11:54:37 +0530 | [diff] [blame] | 1411 | if(dt_entry.offset > (UINT_MAX - dt_entry.size)) { |
| 1412 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 1413 | return -1; |
| 1414 | } |
| 1415 | |
| 1416 | /* Ensure we are not overshooting dt_size with the dt_entry selected */ |
| 1417 | if ((dt_entry.offset + dt_entry.size) > hdr->dt_size) { |
| 1418 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 1419 | return -1; |
| 1420 | } |
| 1421 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1422 | if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size)) |
| 1423 | { |
| 1424 | unsigned int compressed_size = 0; |
| 1425 | out_addr += out_len; |
| 1426 | out_avai_len -= out_len; |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 1427 | dprintf(INFO, "decompressing dtb: start\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1428 | rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset, |
| 1429 | dt_entry.size, out_addr, out_avai_len, |
| 1430 | &compressed_size, &dtb_size); |
| 1431 | if (rc) |
| 1432 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 1433 | dprintf(CRITICAL, "decompressing dtb failed!!!\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1434 | ASSERT(0); |
| 1435 | } |
| 1436 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 1437 | dprintf(INFO, "decompressing dtb: done\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1438 | best_match_dt_addr = out_addr; |
| 1439 | } else { |
| 1440 | best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset; |
| 1441 | dtb_size = dt_entry.size; |
| 1442 | } |
| 1443 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1444 | /* Validate and Read device device tree in the tags_addr */ |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1445 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1446 | { |
| 1447 | dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n"); |
| 1448 | return -1; |
| 1449 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1450 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1451 | memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1452 | } else { |
| 1453 | /* Validate the tags_addr */ |
| 1454 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual)) |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 1455 | { |
| 1456 | dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n"); |
| 1457 | return -1; |
| 1458 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1459 | /* |
| 1460 | * If appended dev tree is found, update the atags with |
| 1461 | * memory address to the DTB appended location on RAM. |
| 1462 | * Else update with the atags address in the kernel header |
| 1463 | */ |
| 1464 | void *dtb; |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 1465 | dtb = dev_tree_appended( |
| 1466 | (void*)(image_addr + page_size + |
| 1467 | patched_kernel_hdr_size), |
| 1468 | hdr->kernel_size, dtb_offset, |
| 1469 | (void *)hdr->tags_addr); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1470 | if (!dtb) { |
| 1471 | dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n"); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 1472 | return -1; |
| 1473 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1474 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1475 | #endif |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1476 | |
Stanimir Varbanov | 69ec546 | 2013-07-18 18:17:42 +0300 | [diff] [blame] | 1477 | if (boot_into_recovery && !device.is_unlocked && !device.is_tampered) |
| 1478 | target_load_ssd_keystore(); |
| 1479 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1480 | unified_boot: |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1481 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 1482 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Dima Zavin | bd3daa0 | 2013-03-26 11:06:44 -0700 | [diff] [blame] | 1483 | (const char *)hdr->cmdline, board_machtype(), |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1484 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 1485 | |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1489 | int boot_linux_from_flash(void) |
| 1490 | { |
| 1491 | struct boot_img_hdr *hdr = (void*) buf; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1492 | struct ptentry *ptn; |
| 1493 | struct ptable *ptable; |
| 1494 | unsigned offset = 0; |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 1495 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1496 | unsigned char *image_addr = 0; |
| 1497 | unsigned kernel_actual; |
| 1498 | unsigned ramdisk_actual; |
| 1499 | unsigned imagesize_actual; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 1500 | unsigned second_actual = 0; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1501 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1502 | #if DEVICE_TREE |
| 1503 | struct dt_table *table; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1504 | struct dt_entry dt_entry; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 1505 | unsigned dt_table_offset; |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1506 | uint32_t dt_actual; |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 1507 | uint32_t dt_hdr_size; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 1508 | unsigned int dtb_size = 0; |
| 1509 | unsigned char *best_match_dt_addr = NULL; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1510 | #endif |
| 1511 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 1512 | if (target_is_emmc_boot()) { |
| 1513 | hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 1514 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 1515 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
| 1516 | return -1; |
| 1517 | } |
| 1518 | goto continue_boot; |
| 1519 | } |
| 1520 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1521 | ptable = flash_get_ptable(); |
| 1522 | if (ptable == NULL) { |
| 1523 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 1524 | return -1; |
| 1525 | } |
| 1526 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 1527 | if(!boot_into_recovery) |
| 1528 | { |
| 1529 | ptn = ptable_find(ptable, "boot"); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1530 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 1531 | if (ptn == NULL) { |
| 1532 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 1533 | return -1; |
| 1534 | } |
| 1535 | } |
| 1536 | else |
| 1537 | { |
| 1538 | ptn = ptable_find(ptable, "recovery"); |
| 1539 | if (ptn == NULL) { |
| 1540 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 1541 | return -1; |
| 1542 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1543 | } |
| 1544 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 1545 | if (flash_read(ptn, offset, buf, page_size)) { |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1546 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 1547 | return -1; |
| 1548 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1549 | |
| 1550 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 1551 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1552 | return -1; |
| 1553 | } |
| 1554 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 1555 | if (hdr->page_size != page_size) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 1556 | 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] | 1557 | return -1; |
| 1558 | } |
| 1559 | |
vijay kumar | 287bb54 | 2015-09-29 13:01:52 +0530 | [diff] [blame] | 1560 | /* ensure commandline is terminated */ |
| 1561 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 1562 | |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 1563 | /* |
| 1564 | * Update the kernel/ramdisk/tags address if the boot image header |
| 1565 | * has default values, these default values come from mkbootimg when |
| 1566 | * the boot image is flashed using fastboot flash:raw |
| 1567 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1568 | update_ker_tags_rdisk_addr(hdr, false); |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 1569 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1570 | /* Get virtual addresses since the hdr saves physical addresses. */ |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1571 | hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr)); |
| 1572 | hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr)); |
| 1573 | hdr->tags_addr = VA((addr_t)(hdr->tags_addr)); |
| 1574 | |
| 1575 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 1576 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 1577 | |
| 1578 | /* Check if the addresses in the header are valid. */ |
| 1579 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) || |
| 1580 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual)) |
| 1581 | { |
| 1582 | dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n"); |
| 1583 | return -1; |
| 1584 | } |
| 1585 | |
| 1586 | #ifndef DEVICE_TREE |
| 1587 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE)) |
| 1588 | { |
| 1589 | dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n"); |
| 1590 | return -1; |
| 1591 | } |
| 1592 | #endif |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1593 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1594 | /* Authenticate Kernel */ |
Deepa Dinamani | 23b60d4 | 2013-06-24 18:10:52 -0700 | [diff] [blame] | 1595 | if(target_use_signed_kernel() && (!device.is_unlocked)) |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1596 | { |
| 1597 | image_addr = (unsigned char *)target_get_scratch_address(); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1598 | offset = 0; |
| 1599 | |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1600 | #if DEVICE_TREE |
| 1601 | dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask); |
vijay kumar | 966a982 | 2015-12-09 18:36:09 +0530 | [diff] [blame] | 1602 | |
| 1603 | if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) { |
| 1604 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n"); |
| 1605 | return -1; |
| 1606 | } |
| 1607 | |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1608 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1609 | |
| 1610 | if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size)) |
| 1611 | { |
| 1612 | dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n"); |
| 1613 | return -1; |
| 1614 | } |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1615 | #else |
vijay kumar | 966a982 | 2015-12-09 18:36:09 +0530 | [diff] [blame] | 1616 | if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) { |
| 1617 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n"); |
| 1618 | return -1; |
| 1619 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1620 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual); |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1621 | #endif |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1622 | |
Channagoud Kadabi | f0705b5 | 2015-08-20 14:16:08 -0700 | [diff] [blame] | 1623 | dprintf(INFO, "Loading (%s) image (%d): start\n", |
| 1624 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 1625 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1626 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1627 | /* Read image without signature */ |
| 1628 | if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual)) |
| 1629 | { |
| 1630 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 1631 | return -1; |
| 1632 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1633 | |
Channagoud Kadabi | f0705b5 | 2015-08-20 14:16:08 -0700 | [diff] [blame] | 1634 | dprintf(INFO, "Loading (%s) image (%d): done\n", |
| 1635 | (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual); |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 1636 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1637 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1638 | offset = imagesize_actual; |
| 1639 | /* Read signature */ |
| 1640 | if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size)) |
| 1641 | { |
| 1642 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1643 | return -1; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1644 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1645 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1646 | verify_signed_bootimg((uint32_t)image_addr, imagesize_actual); |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1647 | |
| 1648 | /* Move kernel and ramdisk to correct address */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1649 | memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size); |
| 1650 | 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] | 1651 | #if DEVICE_TREE |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 1652 | if(hdr->dt_size != 0) { |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1653 | |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 1654 | dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual); |
| 1655 | |
| 1656 | table = (struct dt_table*) dt_table_offset; |
| 1657 | |
| 1658 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){ |
| 1659 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 1660 | return -1; |
| 1661 | } |
| 1662 | |
| 1663 | /* Find index of device tree within device tree table */ |
| 1664 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
| 1665 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 1666 | return -1; |
| 1667 | } |
| 1668 | |
| 1669 | /* Validate and Read device device tree in the "tags_add */ |
| 1670 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){ |
| 1671 | dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n"); |
| 1672 | return -1; |
| 1673 | } |
| 1674 | |
| 1675 | best_match_dt_addr = (unsigned char *)table + dt_entry.offset; |
| 1676 | dtb_size = dt_entry.size; |
| 1677 | memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size); |
| 1678 | } |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1679 | #endif |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1680 | |
| 1681 | /* Make sure everything from scratch address is read before next step!*/ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1682 | if(device.is_tampered) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1683 | { |
| 1684 | write_device_info_flash(&device); |
| 1685 | } |
Channagoud Kadabi | 5c86fe3 | 2012-02-16 10:58:48 +0530 | [diff] [blame] | 1686 | #if USE_PCOM_SECBOOT |
| 1687 | set_tamper_flag(device.is_tampered); |
| 1688 | #endif |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1689 | } |
| 1690 | else |
| 1691 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1692 | offset = page_size; |
| 1693 | |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1694 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 1695 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 1696 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 1697 | |
Channagoud Kadabi | f0705b5 | 2015-08-20 14:16:08 -0700 | [diff] [blame] | 1698 | dprintf(INFO, "Loading (%s) image (%d): start\n", |
| 1699 | (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual); |
| 1700 | |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 1701 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1702 | |
| 1703 | if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1704 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 1705 | return -1; |
| 1706 | } |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1707 | offset += kernel_actual; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1708 | |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1709 | if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1710 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 1711 | return -1; |
| 1712 | } |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1713 | offset += ramdisk_actual; |
| 1714 | |
Channagoud Kadabi | f0705b5 | 2015-08-20 14:16:08 -0700 | [diff] [blame] | 1715 | dprintf(INFO, "Loading (%s) image (%d): done\n", |
| 1716 | (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual); |
| 1717 | |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 1718 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1719 | |
| 1720 | if(hdr->second_size != 0) { |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1721 | offset += second_actual; |
| 1722 | /* Second image loading not implemented. */ |
| 1723 | ASSERT(0); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | #if DEVICE_TREE |
| 1727 | if(hdr->dt_size != 0) { |
| 1728 | |
| 1729 | /* Read the device tree table into buffer */ |
| 1730 | if(flash_read(ptn, offset, (void *) dt_buf, page_size)) { |
| 1731 | dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n"); |
| 1732 | return -1; |
| 1733 | } |
| 1734 | |
| 1735 | table = (struct dt_table*) dt_buf; |
| 1736 | |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 1737 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1738 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 1739 | return -1; |
| 1740 | } |
| 1741 | |
P.V. Phani Kumar | 8291676 | 2016-03-09 09:20:19 +0530 | [diff] [blame] | 1742 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 1743 | goes beyound hdr->dt_size*/ |
| 1744 | if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) { |
| 1745 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 1746 | return -1; |
| 1747 | } |
| 1748 | |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 1749 | table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size); |
| 1750 | if (!table) |
| 1751 | return -1; |
| 1752 | |
| 1753 | /* Read the entire device tree table into buffer */ |
| 1754 | if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) { |
| 1755 | dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n"); |
| 1756 | return -1; |
| 1757 | } |
| 1758 | |
| 1759 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1760 | /* Find index of device tree within device tree table */ |
| 1761 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1762 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 1763 | return -1; |
| 1764 | } |
| 1765 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1766 | /* Validate and Read device device tree in the "tags_add */ |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1767 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1768 | { |
| 1769 | dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n"); |
| 1770 | return -1; |
| 1771 | } |
| 1772 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1773 | /* Read device device tree in the "tags_add */ |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1774 | if(flash_read(ptn, offset + dt_entry.offset, |
| 1775 | (void *)hdr->tags_addr, dt_entry.size)) { |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1776 | dprintf(CRITICAL, "ERROR: Cannot read device tree\n"); |
| 1777 | return -1; |
| 1778 | } |
| 1779 | } |
| 1780 | #endif |
| 1781 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1782 | } |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 1783 | continue_boot: |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1784 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1785 | /* TODO: create/pass atags to kernel */ |
| 1786 | |
Ajay Dudani | e28a607 | 2011-07-01 13:59:46 -0700 | [diff] [blame] | 1787 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Dima Zavin | bd3daa0 | 2013-03-26 11:06:44 -0700 | [diff] [blame] | 1788 | (const char *)hdr->cmdline, board_machtype(), |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1789 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 1790 | |
| 1791 | return 0; |
| 1792 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1793 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1794 | void write_device_info_mmc(device_info *dev) |
| 1795 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1796 | unsigned long long ptn = 0; |
| 1797 | unsigned long long size; |
| 1798 | int index = INVALID_PTN; |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 1799 | uint32_t blocksize; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 1800 | uint8_t lun = 0; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 1801 | uint32_t ret = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1802 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 1803 | if (devinfo_present) |
| 1804 | index = partition_get_index("devinfo"); |
| 1805 | else |
| 1806 | index = partition_get_index("aboot"); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 1807 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1808 | ptn = partition_get_offset(index); |
| 1809 | if(ptn == 0) |
| 1810 | { |
| 1811 | return; |
| 1812 | } |
| 1813 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 1814 | lun = partition_get_lun(index); |
| 1815 | mmc_set_lun(lun); |
| 1816 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1817 | size = partition_get_size(index); |
| 1818 | |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 1819 | blocksize = mmc_get_device_blocksize(); |
| 1820 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 1821 | if (devinfo_present) |
| 1822 | ret = mmc_write(ptn, blocksize, (void *)info_buf); |
| 1823 | else |
| 1824 | ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf); |
| 1825 | if (ret) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1826 | { |
| 1827 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 1828 | ASSERT(0); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 1832 | void read_device_info_mmc(struct device_info *info) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1833 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1834 | unsigned long long ptn = 0; |
| 1835 | unsigned long long size; |
| 1836 | int index = INVALID_PTN; |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 1837 | uint32_t blocksize; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 1838 | uint32_t ret = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1839 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 1840 | if ((index = partition_get_index("devinfo")) < 0) |
| 1841 | { |
| 1842 | devinfo_present = false; |
| 1843 | index = partition_get_index("aboot"); |
| 1844 | } |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 1845 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1846 | ptn = partition_get_offset(index); |
| 1847 | if(ptn == 0) |
| 1848 | { |
| 1849 | return; |
| 1850 | } |
| 1851 | |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 1852 | mmc_set_lun(partition_get_lun(index)); |
| 1853 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1854 | size = partition_get_size(index); |
| 1855 | |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 1856 | blocksize = mmc_get_device_blocksize(); |
| 1857 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 1858 | if (devinfo_present) |
| 1859 | ret = mmc_read(ptn, (void *)info_buf, blocksize); |
| 1860 | else |
| 1861 | ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize); |
| 1862 | if (ret) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1863 | { |
| 1864 | dprintf(CRITICAL, "ERROR: Cannot read device info\n"); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 1865 | ASSERT(0); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1866 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | void write_device_info_flash(device_info *dev) |
| 1870 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 1871 | struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1872 | struct ptentry *ptn; |
| 1873 | struct ptable *ptable; |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 1874 | if(info == NULL) |
| 1875 | { |
| 1876 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 1877 | ASSERT(0); |
| 1878 | } |
| 1879 | info_buf = info; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1880 | ptable = flash_get_ptable(); |
| 1881 | if (ptable == NULL) |
| 1882 | { |
| 1883 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 1884 | return; |
| 1885 | } |
| 1886 | |
| 1887 | ptn = ptable_find(ptable, "devinfo"); |
| 1888 | if (ptn == NULL) |
| 1889 | { |
Smita Ghosh | 670c8b8 | 2015-05-07 09:30:03 -0700 | [diff] [blame] | 1890 | dprintf(CRITICAL, "ERROR: No devinfo partition found\n"); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1891 | return; |
| 1892 | } |
| 1893 | |
| 1894 | memcpy(info, dev, sizeof(device_info)); |
| 1895 | |
| 1896 | if (flash_write(ptn, 0, (void *)info_buf, page_size)) |
| 1897 | { |
| 1898 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 1899 | return; |
| 1900 | } |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 1901 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1902 | } |
| 1903 | |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 1904 | static int read_allow_oem_unlock(device_info *dev) |
| 1905 | { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 1906 | unsigned offset; |
| 1907 | int index; |
| 1908 | unsigned long long ptn; |
| 1909 | unsigned long long ptn_size; |
| 1910 | unsigned blocksize = mmc_get_device_blocksize(); |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 1911 | STACKBUF_DMA_ALIGN(buf, blocksize); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 1912 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 1913 | index = partition_get_index(frp_ptns[0]); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 1914 | if (index == INVALID_PTN) |
| 1915 | { |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 1916 | index = partition_get_index(frp_ptns[1]); |
| 1917 | if (index == INVALID_PTN) |
| 1918 | { |
| 1919 | dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]); |
| 1920 | return -1; |
| 1921 | } |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | ptn = partition_get_offset(index); |
| 1925 | ptn_size = partition_get_size(index); |
| 1926 | offset = ptn_size - blocksize; |
| 1927 | |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 1928 | if (mmc_read(ptn + offset, (void *)buf, blocksize)) |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 1929 | { |
| 1930 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 1931 | return -1; |
| 1932 | } |
| 1933 | |
| 1934 | /*is_allow_unlock is a bool value stored at the LSB of last byte*/ |
| 1935 | is_allow_unlock = buf[blocksize-1] & 0x01; |
| 1936 | return 0; |
| 1937 | } |
| 1938 | |
| 1939 | static int write_allow_oem_unlock(bool allow_unlock) |
| 1940 | { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 1941 | unsigned offset; |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 1942 | int index; |
| 1943 | unsigned long long ptn; |
| 1944 | unsigned long long ptn_size; |
| 1945 | unsigned blocksize = mmc_get_device_blocksize(); |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 1946 | STACKBUF_DMA_ALIGN(buf, blocksize); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 1947 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 1948 | index = partition_get_index(frp_ptns[0]); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 1949 | if (index == INVALID_PTN) |
| 1950 | { |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 1951 | index = partition_get_index(frp_ptns[1]); |
| 1952 | if (index == INVALID_PTN) |
| 1953 | { |
| 1954 | dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]); |
| 1955 | return -1; |
| 1956 | } |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | ptn = partition_get_offset(index); |
| 1960 | ptn_size = partition_get_size(index); |
| 1961 | offset = ptn_size - blocksize; |
| 1962 | |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 1963 | if (mmc_read(ptn + offset, (void *)buf, blocksize)) |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 1964 | { |
| 1965 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 1966 | return -1; |
| 1967 | } |
| 1968 | |
| 1969 | /*is_allow_unlock is a bool value stored at the LSB of last byte*/ |
| 1970 | buf[blocksize-1] = allow_unlock; |
| 1971 | if (mmc_write(ptn + offset, blocksize, buf)) |
| 1972 | { |
| 1973 | dprintf(CRITICAL, "Writing MMC failed\n"); |
| 1974 | return -1; |
| 1975 | } |
| 1976 | |
| 1977 | return 0; |
| 1978 | } |
| 1979 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1980 | void read_device_info_flash(device_info *dev) |
| 1981 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 1982 | struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1983 | struct ptentry *ptn; |
| 1984 | struct ptable *ptable; |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 1985 | if(info == NULL) |
| 1986 | { |
| 1987 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 1988 | ASSERT(0); |
| 1989 | } |
| 1990 | info_buf = info; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1991 | ptable = flash_get_ptable(); |
| 1992 | if (ptable == NULL) |
| 1993 | { |
| 1994 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 1995 | return; |
| 1996 | } |
| 1997 | |
| 1998 | ptn = ptable_find(ptable, "devinfo"); |
| 1999 | if (ptn == NULL) |
| 2000 | { |
Smita Ghosh | 670c8b8 | 2015-05-07 09:30:03 -0700 | [diff] [blame] | 2001 | dprintf(CRITICAL, "ERROR: No devinfo partition found\n"); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2002 | return; |
| 2003 | } |
| 2004 | |
| 2005 | if (flash_read(ptn, 0, (void *)info_buf, page_size)) |
| 2006 | { |
| 2007 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 2008 | return; |
| 2009 | } |
| 2010 | |
| 2011 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 2012 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2013 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
| 2014 | info->is_unlocked = 0; |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2015 | info->is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2016 | write_device_info_flash(info); |
| 2017 | } |
| 2018 | memcpy(dev, info, sizeof(device_info)); |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2019 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | void write_device_info(device_info *dev) |
| 2023 | { |
| 2024 | if(target_is_emmc_boot()) |
| 2025 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2026 | struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 2027 | if(info == NULL) |
| 2028 | { |
| 2029 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 2030 | ASSERT(0); |
| 2031 | } |
| 2032 | info_buf = info; |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2033 | memcpy(info, dev, sizeof(struct device_info)); |
| 2034 | |
| 2035 | #if USE_RPMB_FOR_DEVINFO |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 2036 | if (is_secure_boot_enable()) { |
| 2037 | if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0) |
| 2038 | ASSERT(0); |
| 2039 | } |
Channagoud Kadabi | c80cb49 | 2015-04-28 16:08:28 -0700 | [diff] [blame] | 2040 | else |
| 2041 | write_device_info_mmc(info); |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2042 | #else |
| 2043 | write_device_info_mmc(info); |
| 2044 | #endif |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2045 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2046 | } |
| 2047 | else |
| 2048 | { |
| 2049 | write_device_info_flash(dev); |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | void read_device_info(device_info *dev) |
| 2054 | { |
| 2055 | if(target_is_emmc_boot()) |
| 2056 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2057 | struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 2058 | if(info == NULL) |
| 2059 | { |
| 2060 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 2061 | ASSERT(0); |
| 2062 | } |
| 2063 | info_buf = info; |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2064 | |
| 2065 | #if USE_RPMB_FOR_DEVINFO |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 2066 | if (is_secure_boot_enable()) { |
| 2067 | if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0) |
| 2068 | ASSERT(0); |
| 2069 | } |
Channagoud Kadabi | c80cb49 | 2015-04-28 16:08:28 -0700 | [diff] [blame] | 2070 | else |
| 2071 | read_device_info_mmc(info); |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2072 | #else |
| 2073 | read_device_info_mmc(info); |
| 2074 | #endif |
| 2075 | |
| 2076 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 2077 | { |
| 2078 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 2079 | if (is_secure_boot_enable()) { |
Channagoud Kadabi | 05f78ba | 2015-07-06 11:58:14 -0700 | [diff] [blame] | 2080 | info->is_unlocked = 0; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2081 | #if !VBOOT_MOTA |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 2082 | info->is_unlock_critical = 0; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2083 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 2084 | } else { |
Channagoud Kadabi | 2fda409 | 2015-07-07 13:34:11 -0700 | [diff] [blame] | 2085 | info->is_unlocked = 1; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2086 | #if !VBOOT_MOTA |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 2087 | info->is_unlock_critical = 1; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2088 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 2089 | } |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2090 | info->is_tampered = 0; |
| 2091 | info->charger_screen_enabled = 0; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2092 | #if !VBOOT_MOTA |
Sridhar Parasuram | 287e506 | 2015-08-24 16:17:22 -0700 | [diff] [blame] | 2093 | info->verity_mode = 1; //enforcing by default |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2094 | #endif |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2095 | write_device_info(info); |
| 2096 | } |
| 2097 | memcpy(dev, info, sizeof(device_info)); |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2098 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2099 | } |
| 2100 | else |
| 2101 | { |
| 2102 | read_device_info_flash(dev); |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | void reset_device_info() |
| 2107 | { |
| 2108 | dprintf(ALWAYS, "reset_device_info called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2109 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2110 | write_device_info(&device); |
| 2111 | } |
| 2112 | |
| 2113 | void set_device_root() |
| 2114 | { |
| 2115 | dprintf(ALWAYS, "set_device_root called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2116 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2117 | write_device_info(&device); |
| 2118 | } |
| 2119 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2120 | /* set device unlock value |
| 2121 | * Must check FRP before call this function |
| 2122 | * Need to wipe data when unlock status changed |
| 2123 | * type 0: oem unlock |
| 2124 | * type 1: unlock critical |
| 2125 | * status 0: unlock as false |
| 2126 | * status 1: lock as true |
| 2127 | */ |
| 2128 | void set_device_unlock_value(int type, bool status) |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 2129 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2130 | if (type == UNLOCK) |
| 2131 | device.is_unlocked = status; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2132 | #if !VBOOT_MOTA |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2133 | else if (type == UNLOCK_CRITICAL) |
| 2134 | device.is_unlock_critical = status; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2135 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2136 | write_device_info(&device); |
| 2137 | } |
| 2138 | |
| 2139 | static void set_device_unlock(int type, bool status) |
| 2140 | { |
| 2141 | int is_unlocked = -1; |
| 2142 | char response[MAX_RSP_SIZE]; |
| 2143 | |
| 2144 | /* check device unlock status if it is as expected */ |
| 2145 | if (type == UNLOCK) |
| 2146 | is_unlocked = device.is_unlocked; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2147 | #if !VBOOT_MOTA |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2148 | else if (type == UNLOCK_CRITICAL) |
| 2149 | is_unlocked = device.is_unlock_critical; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2150 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2151 | if (is_unlocked == status) { |
| 2152 | snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!")); |
| 2153 | fastboot_info(response); |
| 2154 | fastboot_okay(""); |
| 2155 | return; |
| 2156 | } |
| 2157 | |
| 2158 | /* status is true, it means to unlock device */ |
| 2159 | if (status) { |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 2160 | if(!is_allow_unlock) { |
| 2161 | fastboot_fail("oem unlock is not allowed"); |
| 2162 | return; |
| 2163 | } |
| 2164 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2165 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 2166 | display_unlock_menu(type); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2167 | fastboot_okay(""); |
| 2168 | return; |
| 2169 | #else |
| 2170 | if (type == UNLOCK) { |
| 2171 | fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'"); |
| 2172 | return; |
| 2173 | } |
| 2174 | #endif |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 2175 | } |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2176 | |
| 2177 | set_device_unlock_value(type, status); |
| 2178 | |
| 2179 | /* wipe data */ |
| 2180 | struct recovery_message msg; |
| 2181 | |
| 2182 | snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data"); |
| 2183 | write_misc(0, &msg, sizeof(msg)); |
| 2184 | |
| 2185 | fastboot_okay(""); |
| 2186 | reboot_device(RECOVERY_MODE); |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 2187 | } |
| 2188 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 2189 | static bool critical_flash_allowed(const char * entry) |
| 2190 | { |
| 2191 | uint32_t i = 0; |
| 2192 | if (entry == NULL) |
| 2193 | return false; |
| 2194 | |
| 2195 | for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) { |
| 2196 | if(!strcmp(entry, critical_flash_allowed_ptn[i])) |
| 2197 | return true; |
| 2198 | } |
| 2199 | return false; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | #if DEVICE_TREE |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2203 | int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset) |
| 2204 | { |
| 2205 | uint32 dt_image_offset = 0; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2206 | uint32_t n; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2207 | struct dt_table *table; |
| 2208 | struct dt_entry dt_entry; |
| 2209 | uint32_t dt_hdr_size; |
| 2210 | unsigned int compressed_size = 0; |
| 2211 | unsigned int dtb_size = 0; |
| 2212 | unsigned int out_avai_len = 0; |
| 2213 | unsigned char *out_addr = NULL; |
| 2214 | unsigned char *best_match_dt_addr = NULL; |
| 2215 | int rc; |
| 2216 | |
| 2217 | struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start); |
| 2218 | |
| 2219 | if(hdr->dt_size != 0) { |
| 2220 | /* add kernel offset */ |
| 2221 | dt_image_offset += page_size; |
| 2222 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2223 | dt_image_offset += n; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2224 | |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 2225 | /* add ramdisk offset */ |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2226 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 2227 | dt_image_offset += n; |
| 2228 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 2229 | /* add second offset */ |
| 2230 | if(hdr->second_size != 0) { |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2231 | n = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 2232 | dt_image_offset += n; |
| 2233 | } |
| 2234 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2235 | /* offset now point to start of dt.img */ |
| 2236 | table = (struct dt_table*)(boot_image_start + dt_image_offset); |
| 2237 | |
| 2238 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
| 2239 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2240 | return -1; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2241 | } |
P.V. Phani Kumar | 8291676 | 2016-03-09 09:20:19 +0530 | [diff] [blame] | 2242 | |
| 2243 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 2244 | goes beyound hdr->dt_size*/ |
| 2245 | if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) { |
| 2246 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 2247 | return -1; |
| 2248 | } |
| 2249 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2250 | /* Find index of device tree within device tree table */ |
| 2251 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
| 2252 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 2253 | return -1; |
| 2254 | } |
| 2255 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2256 | best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset; |
| 2257 | if (is_gzip_package(best_match_dt_addr, dt_entry.size)) |
| 2258 | { |
| 2259 | out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset; |
| 2260 | out_avai_len = target_get_max_flash_size() - scratch_offset; |
| 2261 | dprintf(INFO, "decompressing dtb: start\n"); |
| 2262 | rc = decompress(best_match_dt_addr, |
| 2263 | dt_entry.size, out_addr, out_avai_len, |
| 2264 | &compressed_size, &dtb_size); |
| 2265 | if (rc) |
| 2266 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2267 | dprintf(CRITICAL, "decompressing dtb failed!!!\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2268 | ASSERT(0); |
| 2269 | } |
| 2270 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2271 | dprintf(INFO, "decompressing dtb: done\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2272 | best_match_dt_addr = out_addr; |
| 2273 | } else { |
| 2274 | dtb_size = dt_entry.size; |
| 2275 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2276 | /* Validate and Read device device tree in the "tags_add */ |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2277 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2278 | { |
| 2279 | dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n"); |
| 2280 | return -1; |
| 2281 | } |
| 2282 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2283 | /* Read device device tree in the "tags_add */ |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2284 | memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 2285 | } else |
| 2286 | return -1; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2287 | |
| 2288 | /* Everything looks fine. Return success. */ |
| 2289 | return 0; |
| 2290 | } |
| 2291 | #endif |
| 2292 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2293 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 2294 | { |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 2295 | #ifdef MDTP_SUPPORT |
| 2296 | static bool is_mdtp_activated = 0; |
| 2297 | #endif /* MDTP_SUPPORT */ |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2298 | unsigned kernel_actual; |
| 2299 | unsigned ramdisk_actual; |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 2300 | uint32_t image_actual; |
| 2301 | uint32_t dt_actual = 0; |
P.V. Phani Kumar | eecfd82 | 2016-03-09 20:09:03 +0530 | [diff] [blame] | 2302 | uint32_t sig_actual = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2303 | struct boot_img_hdr *hdr = NULL; |
| 2304 | struct kernel64_hdr *kptr = NULL; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2305 | char *ptr = ((char*) data); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 2306 | int ret = 0; |
| 2307 | uint8_t dtb_copied = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2308 | unsigned int out_len = 0; |
| 2309 | unsigned int out_avai_len = 0; |
| 2310 | unsigned char *out_addr = NULL; |
| 2311 | uint32_t dtb_offset = 0; |
| 2312 | unsigned char *kernel_start_addr = NULL; |
| 2313 | unsigned int kernel_size = 0; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2314 | unsigned int scratch_offset = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2315 | |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2316 | #if FBCON_DISPLAY_MSG |
| 2317 | /* Exit keys' detection thread firstly */ |
| 2318 | exit_menu_keys_detection(); |
| 2319 | #endif |
| 2320 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 2321 | #if VERIFIED_BOOT |
Channagoud Kadabi | 6d5375e | 2015-06-23 17:15:42 -0700 | [diff] [blame] | 2322 | if(target_build_variant_user() && !device.is_unlocked) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 2323 | { |
| 2324 | fastboot_fail("unlock device to use this command"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2325 | goto boot_failed; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 2326 | } |
| 2327 | #endif |
| 2328 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2329 | if (sz < sizeof(hdr)) { |
| 2330 | fastboot_fail("invalid bootimage header"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2331 | goto boot_failed; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2332 | } |
| 2333 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2334 | hdr = (struct boot_img_hdr *)data; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2335 | |
| 2336 | /* ensure commandline is terminated */ |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2337 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2338 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2339 | if(target_is_emmc_boot() && hdr->page_size) { |
| 2340 | page_size = hdr->page_size; |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 2341 | page_mask = page_size - 1; |
| 2342 | } |
| 2343 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2344 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 2345 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 2346 | #if DEVICE_TREE |
| 2347 | dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask); |
| 2348 | #endif |
| 2349 | |
| 2350 | image_actual = ADD_OF(page_size, kernel_actual); |
| 2351 | image_actual = ADD_OF(image_actual, ramdisk_actual); |
| 2352 | image_actual = ADD_OF(image_actual, dt_actual); |
| 2353 | |
P.V. Phani Kumar | eecfd82 | 2016-03-09 20:09:03 +0530 | [diff] [blame] | 2354 | if (target_use_signed_kernel() && (!device.is_unlocked)) { |
| 2355 | /* Calculate the signature length from boot image */ |
| 2356 | sig_actual = read_der_message_length( |
| 2357 | (unsigned char*)(data + image_actual),sz); |
Channagoud Kadabi | 3d83901 | 2014-06-26 14:26:39 -0700 | [diff] [blame] | 2358 | image_actual = ADD_OF(image_actual, sig_actual); |
P.V. Phani Kumar | eecfd82 | 2016-03-09 20:09:03 +0530 | [diff] [blame] | 2359 | } |
Channagoud Kadabi | 3d83901 | 2014-06-26 14:26:39 -0700 | [diff] [blame] | 2360 | |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 2361 | /* sz should have atleast raw boot image */ |
| 2362 | if (image_actual > sz) { |
Channagoud Kadabi | 3d83901 | 2014-06-26 14:26:39 -0700 | [diff] [blame] | 2363 | fastboot_fail("bootimage: incomplete or not signed"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2364 | goto boot_failed; |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 2365 | } |
| 2366 | |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 2367 | // Initialize boot state before trying to verify boot.img |
| 2368 | #if VERIFIED_BOOT |
Channagoud Kadabi | 699466e | 2015-11-03 12:37:42 -0800 | [diff] [blame] | 2369 | boot_verifier_init(); |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 2370 | /* Handle overflow if the input image size is greater than |
| 2371 | * boot image buffer can hold |
| 2372 | */ |
| 2373 | if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size) |
| 2374 | { |
| 2375 | fastboot_fail("booimage: size is greater than boot image buffer can hold"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2376 | goto boot_failed; |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 2377 | } |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 2378 | #endif |
| 2379 | |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 2380 | /* Verify the boot image |
| 2381 | * device & page_size are initialized in aboot_init |
| 2382 | */ |
Sridhar Parasuram | 7ae9fbc | 2015-09-17 09:56:30 -0700 | [diff] [blame] | 2383 | if (target_use_signed_kernel() && (!device.is_unlocked)) { |
Channagoud Kadabi | 3d83901 | 2014-06-26 14:26:39 -0700 | [diff] [blame] | 2384 | /* Pass size excluding signature size, otherwise we would try to |
| 2385 | * access signature beyond its length |
| 2386 | */ |
| 2387 | verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual)); |
Sridhar Parasuram | 7ae9fbc | 2015-09-17 09:56:30 -0700 | [diff] [blame] | 2388 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 2389 | #ifdef MDTP_SUPPORT |
| 2390 | else |
| 2391 | { |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 2392 | /* fastboot boot is not allowed when MDTP is activated */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 2393 | mdtp_ext_partition_verification_t ext_partition; |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 2394 | |
| 2395 | if (!is_mdtp_activated) { |
| 2396 | ext_partition.partition = MDTP_PARTITION_NONE; |
| 2397 | mdtp_fwlock_verify_lock(&ext_partition); |
| 2398 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 2399 | } |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 2400 | |
Amir Kotzer | 7c768c0 | 2016-04-13 09:08:05 +0300 | [diff] [blame] | 2401 | /* If mdtp state cannot be validate, block fastboot boot*/ |
| 2402 | if(mdtp_activated(&is_mdtp_activated)){ |
| 2403 | dprintf(CRITICAL, "mdtp_activated cannot validate state.\n"); |
| 2404 | dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n"); |
| 2405 | goto boot_failed; |
| 2406 | } |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 2407 | if(is_mdtp_activated){ |
| 2408 | dprintf(CRITICAL, "fastboot boot command is not available.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2409 | goto boot_failed; |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 2410 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 2411 | #endif /* MDTP_SUPPORT */ |
| 2412 | |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 2413 | #if VERIFIED_BOOT |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 2414 | #if !VBOOT_MOTA |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 2415 | // send root of trust |
| 2416 | if(!send_rot_command((uint32_t)device.is_unlocked)) |
| 2417 | ASSERT(0); |
| 2418 | #endif |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 2419 | #endif |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2420 | /* |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2421 | * Check if the kernel image is a gzip package. If yes, need to decompress it. |
| 2422 | * If not, continue booting. |
| 2423 | */ |
| 2424 | if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size)) |
| 2425 | { |
| 2426 | out_addr = (unsigned char *)target_get_scratch_address(); |
| 2427 | out_addr = (unsigned char *)(out_addr + image_actual + page_size); |
| 2428 | out_avai_len = target_get_max_flash_size() - image_actual - page_size; |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2429 | dprintf(INFO, "decompressing kernel image: start\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2430 | ret = decompress((unsigned char *)(ptr + page_size), |
| 2431 | hdr->kernel_size, out_addr, out_avai_len, |
| 2432 | &dtb_offset, &out_len); |
| 2433 | if (ret) |
| 2434 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2435 | dprintf(CRITICAL, "decompressing image failed!!!\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2436 | ASSERT(0); |
| 2437 | } |
| 2438 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2439 | dprintf(INFO, "decompressing kernel image: done\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2440 | kptr = (struct kernel64_hdr *)out_addr; |
| 2441 | kernel_start_addr = out_addr; |
| 2442 | kernel_size = out_len; |
| 2443 | } else { |
| 2444 | kptr = (struct kernel64_hdr*)((char *)data + page_size); |
| 2445 | kernel_start_addr = (unsigned char *)((char *)data + page_size); |
| 2446 | kernel_size = hdr->kernel_size; |
| 2447 | } |
| 2448 | |
| 2449 | /* |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2450 | * Update the kernel/ramdisk/tags address if the boot image header |
| 2451 | * has default values, these default values come from mkbootimg when |
| 2452 | * the boot image is flashed using fastboot flash:raw |
| 2453 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2454 | update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr)); |
Dima Zavin | 3cadfff | 2013-03-21 14:30:48 -0700 | [diff] [blame] | 2455 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2456 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 2457 | hdr->kernel_addr = VA(hdr->kernel_addr); |
| 2458 | hdr->ramdisk_addr = VA(hdr->ramdisk_addr); |
| 2459 | hdr->tags_addr = VA(hdr->tags_addr); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2460 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2461 | kernel_size = ROUND_TO_PAGE(kernel_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2462 | /* Check if the addresses in the header are valid. */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2463 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) || |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2464 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual)) |
| 2465 | { |
| 2466 | dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2467 | goto boot_failed; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2468 | } |
| 2469 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2470 | #if DEVICE_TREE |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2471 | scratch_offset = image_actual + page_size + out_len; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2472 | /* find correct dtb and copy it to right location */ |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2473 | ret = copy_dtb(data, scratch_offset); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 2474 | |
| 2475 | dtb_copied = !ret ? 1 : 0; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2476 | #else |
| 2477 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE)) |
| 2478 | { |
| 2479 | dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2480 | goto boot_failed; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2481 | } |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 2482 | #endif |
| 2483 | |
| 2484 | /* Load ramdisk & kernel */ |
| 2485 | memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2486 | memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 2487 | |
| 2488 | #if DEVICE_TREE |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2489 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual)) |
| 2490 | { |
| 2491 | dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2492 | goto boot_failed; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2493 | } |
| 2494 | |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 2495 | /* |
| 2496 | * If dtb is not found look for appended DTB in the kernel. |
| 2497 | * If appended dev tree is found, update the atags with |
| 2498 | * memory address to the DTB appended location on RAM. |
| 2499 | * Else update with the atags address in the kernel header |
| 2500 | */ |
| 2501 | if (!dtb_copied) { |
| 2502 | void *dtb; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2503 | dtb = dev_tree_appended((void*)(ptr + page_size), |
| 2504 | hdr->kernel_size, dtb_offset, |
Dima Zavin | e63e557 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 2505 | (void *)hdr->tags_addr); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 2506 | if (!dtb) { |
| 2507 | fastboot_fail("dtb not found"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2508 | goto boot_failed; |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 2509 | } |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2510 | } |
| 2511 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2512 | |
| 2513 | fastboot_okay(""); |
Amol Jadi | 7c4316c | 2013-10-07 14:19:26 -0700 | [diff] [blame] | 2514 | fastboot_stop(); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2515 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 2516 | boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr, |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2517 | (const char*) hdr->cmdline, board_machtype(), |
| 2518 | (void*) hdr->ramdisk_addr, hdr->ramdisk_size); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2519 | |
| 2520 | /* fastboot already stop, it's no need to show fastboot menu */ |
| 2521 | return; |
| 2522 | boot_failed: |
| 2523 | #if FBCON_DISPLAY_MSG |
| 2524 | /* revert to fastboot menu if boot failed */ |
| 2525 | display_fastboot_menu(); |
| 2526 | #endif |
| 2527 | return; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2528 | } |
| 2529 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 2530 | void cmd_erase_nand(const char *arg, void *data, unsigned sz) |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2531 | { |
| 2532 | struct ptentry *ptn; |
| 2533 | struct ptable *ptable; |
| 2534 | |
| 2535 | ptable = flash_get_ptable(); |
| 2536 | if (ptable == NULL) { |
| 2537 | fastboot_fail("partition table doesn't exist"); |
| 2538 | return; |
| 2539 | } |
| 2540 | |
| 2541 | ptn = ptable_find(ptable, arg); |
| 2542 | if (ptn == NULL) { |
| 2543 | fastboot_fail("unknown partition name"); |
| 2544 | return; |
| 2545 | } |
| 2546 | |
| 2547 | if (flash_erase(ptn)) { |
| 2548 | fastboot_fail("failed to erase partition"); |
| 2549 | return; |
| 2550 | } |
| 2551 | fastboot_okay(""); |
| 2552 | } |
| 2553 | |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 2554 | |
| 2555 | void cmd_erase_mmc(const char *arg, void *data, unsigned sz) |
| 2556 | { |
| 2557 | unsigned long long ptn = 0; |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 2558 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 2559 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2560 | uint8_t lun = 0; |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 2561 | char *footer = NULL; |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 2562 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 2563 | #if VERIFIED_BOOT |
| 2564 | if(!strcmp(arg, KEYSTORE_PTN_NAME)) |
| 2565 | { |
| 2566 | if(!device.is_unlocked) |
| 2567 | { |
| 2568 | fastboot_fail("unlock device to erase keystore"); |
| 2569 | return; |
| 2570 | } |
| 2571 | } |
| 2572 | #endif |
| 2573 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 2574 | index = partition_get_index(arg); |
| 2575 | ptn = partition_get_offset(index); |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 2576 | size = partition_get_size(index); |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 2577 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 2578 | if(ptn == 0) { |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 2579 | fastboot_fail("Partition table doesn't exist\n"); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 2580 | return; |
| 2581 | } |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 2582 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2583 | lun = partition_get_lun(index); |
| 2584 | mmc_set_lun(lun); |
| 2585 | |
Sridhar Parasuram | d795712 | 2015-02-27 11:33:40 -0800 | [diff] [blame] | 2586 | if (platform_boot_dev_isemmc()) |
| 2587 | { |
| 2588 | if (mmc_erase_card(ptn, size)) { |
| 2589 | fastboot_fail("failed to erase partition\n"); |
| 2590 | return; |
| 2591 | } |
| 2592 | } else { |
| 2593 | BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE); |
| 2594 | size = partition_get_size(index); |
| 2595 | if (size > DEFAULT_ERASE_SIZE) |
| 2596 | size = DEFAULT_ERASE_SIZE; |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 2597 | |
Sridhar Parasuram | d795712 | 2015-02-27 11:33:40 -0800 | [diff] [blame] | 2598 | /* Simple inefficient version of erase. Just writing |
| 2599 | 0 in first several blocks */ |
| 2600 | if (mmc_write(ptn , size, (unsigned int *)out)) { |
| 2601 | fastboot_fail("failed to erase partition"); |
| 2602 | return; |
| 2603 | } |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 2604 | /*Erase FDE metadata at the userdata footer*/ |
| 2605 | if(!(strncmp(arg, "userdata", 8))) |
| 2606 | { |
| 2607 | footer = memalign(CACHE_LINE, FOOTER_SIZE); |
| 2608 | memset((void *)footer, 0, FOOTER_SIZE); |
| 2609 | |
| 2610 | size = partition_get_size(index); |
| 2611 | |
| 2612 | if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) { |
| 2613 | fastboot_fail("failed to erase userdata footer"); |
| 2614 | free(footer); |
| 2615 | return; |
| 2616 | } |
| 2617 | free(footer); |
| 2618 | } |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 2619 | } |
Sridhar Parasuram | 32b3066 | 2015-07-10 13:33:22 -0700 | [diff] [blame] | 2620 | #if VERIFIED_BOOT |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 2621 | #if !VBOOT_MOTA |
Sridhar Parasuram | 32b3066 | 2015-07-10 13:33:22 -0700 | [diff] [blame] | 2622 | if(!(strncmp(arg, "userdata", 8))) |
| 2623 | if(send_delete_keys_to_tz()) |
| 2624 | ASSERT(0); |
| 2625 | #endif |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 2626 | #endif |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 2627 | fastboot_okay(""); |
| 2628 | } |
| 2629 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 2630 | void cmd_erase(const char *arg, void *data, unsigned sz) |
| 2631 | { |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 2632 | #if VERIFIED_BOOT |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 2633 | if (target_build_variant_user()) |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 2634 | { |
Sridhar Parasuram | c32d07d | 2015-07-12 10:57:48 -0700 | [diff] [blame] | 2635 | if(!device.is_unlocked) |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 2636 | { |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 2637 | fastboot_fail("device is locked. Cannot erase"); |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 2638 | return; |
| 2639 | } |
| 2640 | } |
| 2641 | #endif |
| 2642 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 2643 | if(target_is_emmc_boot()) |
| 2644 | cmd_erase_mmc(arg, data, sz); |
| 2645 | else |
| 2646 | cmd_erase_nand(arg, data, sz); |
| 2647 | } |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 2648 | |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 2649 | static uint32_t aboot_get_secret_key() |
| 2650 | { |
| 2651 | /* 0 is invalid secret key, update this implementation to return |
| 2652 | * device specific unique secret key |
| 2653 | */ |
| 2654 | return 0; |
| 2655 | } |
| 2656 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2657 | void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz) |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2658 | { |
| 2659 | unsigned long long ptn = 0; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 2660 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 2661 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2662 | char *token = NULL; |
| 2663 | char *pname = NULL; |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 2664 | char *sp; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2665 | uint8_t lun = 0; |
| 2666 | bool lun_set = false; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 2667 | |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 2668 | token = strtok_r((char *)arg, ":", &sp); |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2669 | pname = token; |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 2670 | token = strtok_r(NULL, ":", &sp); |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2671 | if(token) |
| 2672 | { |
| 2673 | lun = atoi(token); |
| 2674 | mmc_set_lun(lun); |
| 2675 | lun_set = true; |
| 2676 | } |
| 2677 | |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 2678 | if (pname) |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 2679 | { |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 2680 | if (!strncmp(pname, "frp-unlock", strlen("frp-unlock"))) |
| 2681 | { |
| 2682 | if (!aboot_frp_unlock(pname, data, sz)) |
| 2683 | { |
| 2684 | fastboot_info("FRP unlock successful"); |
| 2685 | fastboot_okay(""); |
| 2686 | } |
| 2687 | else |
| 2688 | fastboot_fail("Secret key is invalid, please update the bootloader with secret key"); |
| 2689 | |
| 2690 | return; |
| 2691 | } |
| 2692 | |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 2693 | if (!strcmp(pname, "partition")) |
| 2694 | { |
| 2695 | dprintf(INFO, "Attempt to write partition image.\n"); |
| 2696 | if (write_partition(sz, (unsigned char *) data)) { |
| 2697 | fastboot_fail("failed to write partition"); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 2698 | return; |
| 2699 | } |
| 2700 | } |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 2701 | else |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2702 | { |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 2703 | #if VERIFIED_BOOT |
| 2704 | if(!strcmp(pname, KEYSTORE_PTN_NAME)) |
| 2705 | { |
| 2706 | if(!device.is_unlocked) |
| 2707 | { |
| 2708 | fastboot_fail("unlock device to flash keystore"); |
| 2709 | return; |
| 2710 | } |
Gaurav Nebhwani | c7313cc | 2015-12-15 22:25:04 +0530 | [diff] [blame] | 2711 | if(!boot_verify_validate_keystore((unsigned char *)data,sz)) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 2712 | { |
| 2713 | fastboot_fail("image is not a keystore file"); |
| 2714 | return; |
| 2715 | } |
| 2716 | } |
| 2717 | #endif |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 2718 | index = partition_get_index(pname); |
| 2719 | ptn = partition_get_offset(index); |
| 2720 | if(ptn == 0) { |
| 2721 | fastboot_fail("partition table doesn't exist"); |
| 2722 | return; |
| 2723 | } |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2724 | |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 2725 | if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) { |
| 2726 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 2727 | fastboot_fail("image is not a boot image"); |
| 2728 | return; |
| 2729 | } |
| 2730 | } |
| 2731 | |
| 2732 | if(!lun_set) |
| 2733 | { |
| 2734 | lun = partition_get_lun(index); |
| 2735 | mmc_set_lun(lun); |
| 2736 | } |
| 2737 | |
| 2738 | size = partition_get_size(index); |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 2739 | if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) { |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 2740 | fastboot_fail("size too large"); |
| 2741 | return; |
| 2742 | } |
| 2743 | else if (mmc_write(ptn , sz, (unsigned int *)data)) { |
| 2744 | fastboot_fail("flash write failure"); |
| 2745 | return; |
| 2746 | } |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 2747 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2748 | } |
| 2749 | fastboot_okay(""); |
| 2750 | return; |
| 2751 | } |
| 2752 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 2753 | void cmd_flash_meta_img(const char *arg, void *data, unsigned sz) |
| 2754 | { |
| 2755 | int i, images; |
| 2756 | meta_header_t *meta_header; |
| 2757 | img_header_entry_t *img_header_entry; |
Parth Dixit | 3e2d303 | 2016-03-04 17:11:52 +0530 | [diff] [blame] | 2758 | /*End of the image address*/ |
| 2759 | uintptr_t data_end; |
| 2760 | |
| 2761 | if( (UINT_MAX - sz) > (uintptr_t)data ) |
| 2762 | data_end = (uintptr_t)data + sz; |
| 2763 | else |
| 2764 | { |
| 2765 | fastboot_fail("Cannot flash: image header corrupt"); |
| 2766 | return; |
| 2767 | } |
| 2768 | |
| 2769 | if( data_end < ((uintptr_t)data + sizeof(meta_header_t))) |
| 2770 | { |
| 2771 | fastboot_fail("Cannot flash: image header corrupt"); |
| 2772 | return; |
| 2773 | } |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 2774 | |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 2775 | /* If device is locked: |
| 2776 | * Forbid to flash image to avoid the device to bypass the image |
| 2777 | * which with "any" name other than bootloader. Because it maybe |
| 2778 | * a meta package of all partitions. |
| 2779 | */ |
| 2780 | #if VERIFIED_BOOT |
| 2781 | if (target_build_variant_user()) { |
| 2782 | if (!device.is_unlocked) { |
| 2783 | fastboot_fail("Device is locked, meta image flashing is not allowed"); |
| 2784 | return; |
| 2785 | } |
| 2786 | #if !VBOOT_MOTA |
| 2787 | if(!device.is_unlock_critical) { |
| 2788 | fastboot_fail("Device is critical locked, Meta image flashing is not allowed"); |
| 2789 | return; |
| 2790 | } |
| 2791 | #endif |
| 2792 | } |
| 2793 | #endif |
| 2794 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 2795 | meta_header = (meta_header_t*) data; |
Parth Dixit | 3e2d303 | 2016-03-04 17:11:52 +0530 | [diff] [blame] | 2796 | if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz)) |
| 2797 | { |
| 2798 | fastboot_fail("Cannot flash: image header corrupt"); |
| 2799 | return; |
| 2800 | } |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 2801 | img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t)); |
| 2802 | |
| 2803 | images = meta_header->img_hdr_sz / sizeof(img_header_entry_t); |
| 2804 | |
| 2805 | for (i=0; i<images; i++) { |
| 2806 | |
| 2807 | if((img_header_entry[i].ptn_name == NULL) || |
| 2808 | (img_header_entry[i].start_offset == 0) || |
| 2809 | (img_header_entry[i].size == 0)) |
| 2810 | break; |
| 2811 | |
Parth Dixit | 3e2d303 | 2016-03-04 17:11:52 +0530 | [diff] [blame] | 2812 | if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset |
| 2813 | + img_header_entry[i].size) ) |
| 2814 | { |
| 2815 | fastboot_fail("Cannot flash: image size mismatch"); |
| 2816 | break; |
| 2817 | } |
| 2818 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 2819 | cmd_flash_mmc_img(img_header_entry[i].ptn_name, |
| 2820 | (void *) data + img_header_entry[i].start_offset, |
| 2821 | img_header_entry[i].size); |
| 2822 | } |
| 2823 | |
Channagoud Kadabi | aafbbfc | 2015-03-04 17:52:56 -0800 | [diff] [blame] | 2824 | if (!strncmp(arg, "bootloader", strlen("bootloader"))) |
| 2825 | { |
| 2826 | strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN); |
| 2827 | strlcat(device.bootloader_version, "-", MAX_VERSION_LEN); |
| 2828 | strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN); |
| 2829 | } |
| 2830 | else |
| 2831 | { |
| 2832 | strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN); |
| 2833 | strlcat(device.radio_version, "-", MAX_VERSION_LEN); |
| 2834 | strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN); |
| 2835 | } |
| 2836 | |
| 2837 | write_device_info(&device); |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 2838 | fastboot_okay(""); |
| 2839 | return; |
| 2840 | } |
| 2841 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2842 | void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz) |
| 2843 | { |
| 2844 | unsigned int chunk; |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 2845 | uint64_t chunk_data_sz; |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 2846 | uint32_t *fill_buf = NULL; |
| 2847 | uint32_t fill_val; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2848 | sparse_header_t *sparse_header; |
| 2849 | chunk_header_t *chunk_header; |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 2850 | uint32_t total_blocks = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2851 | unsigned long long ptn = 0; |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 2852 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 2853 | int index = INVALID_PTN; |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 2854 | uint32_t i; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2855 | uint8_t lun = 0; |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 2856 | /*End of the sparse image address*/ |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 2857 | uintptr_t data_end = (uintptr_t)data + sz; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2858 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 2859 | index = partition_get_index(arg); |
| 2860 | ptn = partition_get_offset(index); |
| 2861 | if(ptn == 0) { |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2862 | fastboot_fail("partition table doesn't exist"); |
| 2863 | return; |
| 2864 | } |
| 2865 | |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 2866 | size = partition_get_size(index); |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 2867 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2868 | lun = partition_get_lun(index); |
| 2869 | mmc_set_lun(lun); |
| 2870 | |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 2871 | if (sz < sizeof(sparse_header_t)) { |
| 2872 | fastboot_fail("size too low"); |
| 2873 | return; |
| 2874 | } |
| 2875 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2876 | /* Read and skip over sparse image header */ |
| 2877 | sparse_header = (sparse_header_t *) data; |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 2878 | |
vijay kumar | 1321f34 | 2015-03-27 12:13:42 +0530 | [diff] [blame] | 2879 | if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) { |
Ajay Dudani | 876b328 | 2012-12-21 14:12:17 -0800 | [diff] [blame] | 2880 | fastboot_fail("size too large"); |
| 2881 | return; |
| 2882 | } |
| 2883 | |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 2884 | data += sizeof(sparse_header_t); |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 2885 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 2886 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 2887 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 2888 | return; |
| 2889 | } |
| 2890 | |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 2891 | if(sparse_header->file_hdr_sz != sizeof(sparse_header_t)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2892 | { |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 2893 | fastboot_fail("sparse header size mismatch"); |
| 2894 | return; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2895 | } |
| 2896 | |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 2897 | dprintf (SPEW, "=== Sparse Image Header ===\n"); |
| 2898 | dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic); |
| 2899 | dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version); |
| 2900 | dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version); |
| 2901 | dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz); |
| 2902 | dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz); |
| 2903 | dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz); |
| 2904 | dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks); |
| 2905 | dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2906 | |
| 2907 | /* Start processing chunks */ |
| 2908 | for (chunk=0; chunk<sparse_header->total_chunks; chunk++) |
| 2909 | { |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 2910 | /* Make sure the total image size does not exceed the partition size */ |
| 2911 | if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) { |
| 2912 | fastboot_fail("size too large"); |
| 2913 | return; |
| 2914 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2915 | /* Read and skip over chunk header */ |
| 2916 | chunk_header = (chunk_header_t *) data; |
| 2917 | data += sizeof(chunk_header_t); |
| 2918 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 2919 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 2920 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 2921 | return; |
| 2922 | } |
| 2923 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2924 | dprintf (SPEW, "=== Chunk Header ===\n"); |
| 2925 | dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type); |
| 2926 | dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz); |
| 2927 | dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz); |
| 2928 | |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 2929 | if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2930 | { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 2931 | fastboot_fail("chunk header size mismatch"); |
| 2932 | return; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2933 | } |
| 2934 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 2935 | if (!sparse_header->blk_sz ){ |
| 2936 | fastboot_fail("Invalid block size\n"); |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 2937 | return; |
| 2938 | } |
| 2939 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 2940 | chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz; |
| 2941 | |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 2942 | /* Make sure that the chunk size calculated from sparse image does not |
| 2943 | * exceed partition size |
| 2944 | */ |
| 2945 | if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size) |
| 2946 | { |
| 2947 | fastboot_fail("Chunk data size exceeds partition size"); |
| 2948 | return; |
| 2949 | } |
| 2950 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2951 | switch (chunk_header->chunk_type) |
| 2952 | { |
| 2953 | case CHUNK_TYPE_RAW: |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 2954 | if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz + |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2955 | chunk_data_sz)) |
| 2956 | { |
| 2957 | fastboot_fail("Bogus chunk size for chunk type Raw"); |
| 2958 | return; |
| 2959 | } |
| 2960 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 2961 | if (data_end < (uintptr_t)data + chunk_data_sz) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 2962 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 2963 | return; |
| 2964 | } |
| 2965 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 2966 | /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32 |
| 2967 | otherwise it will return in the line above |
| 2968 | */ |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 2969 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 2970 | (uint32_t)chunk_data_sz, |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 2971 | (unsigned int*)data)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2972 | { |
| 2973 | fastboot_fail("flash write failure"); |
| 2974 | return; |
| 2975 | } |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 2976 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 2977 | fastboot_fail("Bogus size for RAW chunk type"); |
| 2978 | return; |
| 2979 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2980 | total_blocks += chunk_header->chunk_sz; |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 2981 | data += (uint32_t)chunk_data_sz; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 2982 | break; |
| 2983 | |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 2984 | case CHUNK_TYPE_FILL: |
| 2985 | if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz + |
| 2986 | sizeof(uint32_t))) |
| 2987 | { |
| 2988 | fastboot_fail("Bogus chunk size for chunk type FILL"); |
| 2989 | return; |
| 2990 | } |
| 2991 | |
| 2992 | fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE)); |
| 2993 | if (!fill_buf) |
| 2994 | { |
| 2995 | fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL"); |
| 2996 | return; |
| 2997 | } |
| 2998 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 2999 | if (data_end < (uintptr_t)data + sizeof(uint32_t)) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3000 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
Gaurav Nebhwani | 163cbf8 | 2016-03-17 21:21:27 +0530 | [diff] [blame] | 3001 | free(fill_buf); |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3002 | return; |
| 3003 | } |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 3004 | fill_val = *(uint32_t *)data; |
| 3005 | data = (char *) data + sizeof(uint32_t); |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 3006 | |
| 3007 | for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++) |
| 3008 | { |
| 3009 | fill_buf[i] = fill_val; |
| 3010 | } |
| 3011 | |
Gaurav Nebhwani | e94cbe1 | 2016-03-17 21:16:34 +0530 | [diff] [blame] | 3012 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) |
| 3013 | { |
| 3014 | fastboot_fail("bogus size for chunk FILL type"); |
| 3015 | free(fill_buf); |
| 3016 | return; |
| 3017 | } |
| 3018 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 3019 | for (i = 0; i < chunk_header->chunk_sz; i++) |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 3020 | { |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 3021 | /* Make sure that the data written to partition does not exceed partition size */ |
| 3022 | if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size) |
| 3023 | { |
| 3024 | fastboot_fail("Chunk data size for fill type exceeds partition size"); |
Gaurav Nebhwani | 163cbf8 | 2016-03-17 21:21:27 +0530 | [diff] [blame] | 3025 | free(fill_buf); |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 3026 | return; |
| 3027 | } |
| 3028 | |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 3029 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
| 3030 | sparse_header->blk_sz, |
| 3031 | fill_buf)) |
| 3032 | { |
| 3033 | fastboot_fail("flash write failure"); |
| 3034 | free(fill_buf); |
| 3035 | return; |
| 3036 | } |
| 3037 | |
| 3038 | total_blocks++; |
| 3039 | } |
| 3040 | |
| 3041 | free(fill_buf); |
| 3042 | break; |
| 3043 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3044 | case CHUNK_TYPE_DONT_CARE: |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 3045 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 3046 | fastboot_fail("bogus size for chunk DONT CARE type"); |
| 3047 | return; |
| 3048 | } |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 3049 | total_blocks += chunk_header->chunk_sz; |
| 3050 | break; |
| 3051 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3052 | case CHUNK_TYPE_CRC: |
| 3053 | if(chunk_header->total_sz != sparse_header->chunk_hdr_sz) |
| 3054 | { |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 3055 | fastboot_fail("Bogus chunk size for chunk type CRC"); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3056 | return; |
| 3057 | } |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 3058 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 3059 | fastboot_fail("bogus size for chunk CRC type"); |
| 3060 | return; |
| 3061 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3062 | total_blocks += chunk_header->chunk_sz; |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 3063 | if ((uintptr_t)data > UINT_MAX - chunk_data_sz) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3064 | fastboot_fail("integer overflow occured"); |
| 3065 | return; |
| 3066 | } |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 3067 | data += (uint32_t)chunk_data_sz; |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 3068 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3069 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 3070 | return; |
| 3071 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3072 | break; |
| 3073 | |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 3074 | default: |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 3075 | dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3076 | fastboot_fail("Unknown chunk type"); |
| 3077 | return; |
| 3078 | } |
| 3079 | } |
| 3080 | |
Ajay Dudani | 0c6927b | 2011-05-18 11:12:16 -0700 | [diff] [blame] | 3081 | dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n", |
| 3082 | total_blocks, sparse_header->total_blks); |
| 3083 | |
| 3084 | if(total_blocks != sparse_header->total_blks) |
| 3085 | { |
| 3086 | fastboot_fail("sparse image write failure"); |
| 3087 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3088 | |
| 3089 | fastboot_okay(""); |
| 3090 | return; |
| 3091 | } |
| 3092 | |
| 3093 | void cmd_flash_mmc(const char *arg, void *data, unsigned sz) |
| 3094 | { |
| 3095 | sparse_header_t *sparse_header; |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 3096 | meta_header_t *meta_header; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3097 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 3098 | #ifdef SSD_ENABLE |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 3099 | /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */ |
| 3100 | unsigned int *magic_number = (unsigned int *) data; |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 3101 | int ret=0; |
| 3102 | uint32 major_version=0; |
| 3103 | uint32 minor_version=0; |
| 3104 | |
| 3105 | ret = scm_svc_version(&major_version,&minor_version); |
| 3106 | if(!ret) |
| 3107 | { |
| 3108 | if(major_version >= 2) |
| 3109 | { |
Amir Samuelov | bb65ce0 | 2013-05-05 12:20:18 +0300 | [diff] [blame] | 3110 | if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") ) |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 3111 | { |
| 3112 | ret = encrypt_scm((uint32 **) &data, &sz); |
| 3113 | if (ret != 0) { |
| 3114 | dprintf(CRITICAL, "ERROR: Encryption Failure\n"); |
| 3115 | return; |
| 3116 | } |
| 3117 | |
Amir Samuelov | bb65ce0 | 2013-05-05 12:20:18 +0300 | [diff] [blame] | 3118 | /* Protect only for SSD */ |
| 3119 | if (!strcmp(arg, "ssd")) { |
| 3120 | ret = scm_protect_keystore((uint32 *) data, sz); |
| 3121 | if (ret != 0) { |
| 3122 | dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n"); |
| 3123 | return; |
| 3124 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 3125 | } |
| 3126 | } |
| 3127 | else |
| 3128 | { |
| 3129 | ret = decrypt_scm_v2((uint32 **) &data, &sz); |
| 3130 | if(ret != 0) |
| 3131 | { |
| 3132 | dprintf(CRITICAL,"ERROR: Decryption Failure\n"); |
| 3133 | return; |
| 3134 | } |
| 3135 | } |
| 3136 | } |
| 3137 | else |
| 3138 | { |
| 3139 | if (magic_number[0] == DECRYPT_MAGIC_0 && |
| 3140 | magic_number[1] == DECRYPT_MAGIC_1) |
| 3141 | { |
| 3142 | ret = decrypt_scm((uint32 **) &data, &sz); |
| 3143 | if (ret != 0) { |
| 3144 | dprintf(CRITICAL, "ERROR: Invalid secure image\n"); |
| 3145 | return; |
| 3146 | } |
| 3147 | } |
| 3148 | else if (magic_number[0] == ENCRYPT_MAGIC_0 && |
| 3149 | magic_number[1] == ENCRYPT_MAGIC_1) |
| 3150 | { |
| 3151 | ret = encrypt_scm((uint32 **) &data, &sz); |
| 3152 | if (ret != 0) { |
| 3153 | dprintf(CRITICAL, "ERROR: Encryption Failure\n"); |
| 3154 | return; |
| 3155 | } |
| 3156 | } |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 3157 | } |
| 3158 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 3159 | else |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 3160 | { |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 3161 | dprintf(CRITICAL,"INVALID SVC Version\n"); |
| 3162 | return; |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 3163 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 3164 | #endif /* SSD_ENABLE */ |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 3165 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3166 | #if VERIFIED_BOOT |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 3167 | if (target_build_variant_user()) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3168 | { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3169 | /* if device is locked: |
| 3170 | * common partition will not allow to be flashed |
| 3171 | * critical partition will allow to flash image. |
| 3172 | */ |
| 3173 | if(!device.is_unlocked && !critical_flash_allowed(arg)) { |
| 3174 | fastboot_fail("Partition flashing is not allowed"); |
| 3175 | return; |
| 3176 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3177 | #if !VBOOT_MOTA |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3178 | /* if device critical is locked: |
| 3179 | * common partition will allow to be flashed |
| 3180 | * critical partition will not allow to flash image. |
| 3181 | */ |
| 3182 | if(!device.is_unlock_critical && critical_flash_allowed(arg)) { |
| 3183 | fastboot_fail("Critical partition flashing is not allowed"); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3184 | return; |
| 3185 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3186 | #endif |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3187 | } |
| 3188 | #endif |
| 3189 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 3190 | sparse_header = (sparse_header_t *) data; |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 3191 | meta_header = (meta_header_t *) data; |
| 3192 | if (sparse_header->magic == SPARSE_HEADER_MAGIC) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3193 | cmd_flash_mmc_sparse_img(arg, data, sz); |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 3194 | else if (meta_header->magic == META_HEADER_MAGIC) |
| 3195 | cmd_flash_meta_img(arg, data, sz); |
| 3196 | else |
| 3197 | cmd_flash_mmc_img(arg, data, sz); |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 3198 | |
| 3199 | #if VERIFIED_BOOT |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3200 | #if !VBOOT_MOTA |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 3201 | if((!strncmp(arg, "system", 6)) && !device.verity_mode) |
| 3202 | { |
| 3203 | // reset dm_verity mode to enforcing |
| 3204 | device.verity_mode = 1; |
| 3205 | write_device_info(&device); |
| 3206 | } |
| 3207 | #endif |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3208 | #endif |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 3209 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3210 | return; |
| 3211 | } |
| 3212 | |
Tanya Brokhman | 84eaaf6 | 2015-02-24 21:57:06 +0200 | [diff] [blame] | 3213 | void cmd_updatevol(const char *vol_name, void *data, unsigned sz) |
| 3214 | { |
| 3215 | struct ptentry *sys_ptn; |
| 3216 | struct ptable *ptable; |
| 3217 | |
| 3218 | ptable = flash_get_ptable(); |
| 3219 | if (ptable == NULL) { |
| 3220 | fastboot_fail("partition table doesn't exist"); |
| 3221 | return; |
| 3222 | } |
| 3223 | |
| 3224 | sys_ptn = ptable_find(ptable, "system"); |
| 3225 | if (sys_ptn == NULL) { |
| 3226 | fastboot_fail("system partition not found"); |
| 3227 | return; |
| 3228 | } |
| 3229 | |
| 3230 | sz = ROUND_TO_PAGE(sz, page_mask); |
| 3231 | if (update_ubi_vol(sys_ptn, vol_name, data, sz)) |
| 3232 | fastboot_fail("update_ubi_vol failed"); |
| 3233 | else |
| 3234 | fastboot_okay(""); |
| 3235 | } |
| 3236 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3237 | void cmd_flash_nand(const char *arg, void *data, unsigned sz) |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3238 | { |
| 3239 | struct ptentry *ptn; |
| 3240 | struct ptable *ptable; |
| 3241 | unsigned extra = 0; |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 3242 | uint64_t partition_size = 0; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3243 | |
| 3244 | ptable = flash_get_ptable(); |
| 3245 | if (ptable == NULL) { |
| 3246 | fastboot_fail("partition table doesn't exist"); |
| 3247 | return; |
| 3248 | } |
| 3249 | |
| 3250 | ptn = ptable_find(ptable, arg); |
| 3251 | if (ptn == NULL) { |
Tanya Brokhman | 84eaaf6 | 2015-02-24 21:57:06 +0200 | [diff] [blame] | 3252 | dprintf(INFO, "unknown partition name (%s). Trying updatevol\n", |
| 3253 | arg); |
| 3254 | cmd_updatevol(arg, data, sz); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3255 | return; |
| 3256 | } |
| 3257 | |
| 3258 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
| 3259 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 3260 | fastboot_fail("image is not a boot image"); |
| 3261 | return; |
| 3262 | } |
| 3263 | } |
| 3264 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 3265 | if (!strcmp(ptn->name, "system") |
Deepa Dinamani | 13e32c4 | 2012-03-12 14:34:17 -0700 | [diff] [blame] | 3266 | || !strcmp(ptn->name, "userdata") |
| 3267 | || !strcmp(ptn->name, "persist") |
Sundarajan Srinivasan | b063a85 | 2013-11-19 14:02:27 -0800 | [diff] [blame] | 3268 | || !strcmp(ptn->name, "recoveryfs") |
Sundarajan Srinivasan | 595b71e | 2013-11-05 12:44:34 -0800 | [diff] [blame] | 3269 | || !strcmp(ptn->name, "modem")) |
Tanya Brokhman | 1c94f1a | 2015-02-15 09:05:03 +0200 | [diff] [blame] | 3270 | extra = 1; |
Sundarajan Srinivasan | 595b71e | 2013-11-05 12:44:34 -0800 | [diff] [blame] | 3271 | else |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 3272 | sz = ROUND_TO_PAGE(sz, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3273 | |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 3274 | partition_size = (uint64_t)ptn->length * (uint64_t)flash_num_pages_per_blk() * (uint64_t)flash_page_size(); |
| 3275 | if (partition_size > UINT_MAX) { |
| 3276 | fastboot_fail("Invalid partition size"); |
| 3277 | return; |
| 3278 | } |
| 3279 | |
| 3280 | if (sz > partition_size) { |
| 3281 | fastboot_fail("Image size too large"); |
| 3282 | return; |
| 3283 | } |
| 3284 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3285 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
Tanya Brokhman | 1c94f1a | 2015-02-15 09:05:03 +0200 | [diff] [blame] | 3286 | if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) { |
| 3287 | if (flash_ubi_img(ptn, data, sz)) { |
| 3288 | fastboot_fail("flash write failure"); |
| 3289 | return; |
| 3290 | } |
| 3291 | } else { |
| 3292 | if (flash_write(ptn, extra, data, sz)) { |
| 3293 | fastboot_fail("flash write failure"); |
| 3294 | return; |
| 3295 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3296 | } |
| 3297 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 3298 | fastboot_okay(""); |
| 3299 | } |
| 3300 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3301 | void cmd_flash(const char *arg, void *data, unsigned sz) |
| 3302 | { |
| 3303 | if(target_is_emmc_boot()) |
| 3304 | cmd_flash_mmc(arg, data, sz); |
| 3305 | else |
| 3306 | cmd_flash_nand(arg, data, sz); |
| 3307 | } |
| 3308 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3309 | void cmd_continue(const char *arg, void *data, unsigned sz) |
| 3310 | { |
| 3311 | fastboot_okay(""); |
Amol Jadi | 7c4316c | 2013-10-07 14:19:26 -0700 | [diff] [blame] | 3312 | fastboot_stop(); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3313 | |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 3314 | if (target_is_emmc_boot()) |
| 3315 | { |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 3316 | #if FBCON_DISPLAY_MSG |
lijuang | de34d50 | 2016-02-26 16:04:50 +0800 | [diff] [blame] | 3317 | /* Exit keys' detection thread firstly */ |
| 3318 | exit_menu_keys_detection(); |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 3319 | #endif |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 3320 | boot_linux_from_mmc(); |
| 3321 | } |
| 3322 | else |
| 3323 | { |
| 3324 | boot_linux_from_flash(); |
| 3325 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3326 | } |
| 3327 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 3328 | void cmd_reboot(const char *arg, void *data, unsigned sz) |
| 3329 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 3330 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 3331 | fastboot_okay(""); |
| 3332 | reboot_device(0); |
| 3333 | } |
| 3334 | |
| 3335 | void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz) |
| 3336 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 3337 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 3338 | fastboot_okay(""); |
| 3339 | reboot_device(FASTBOOT_MODE); |
| 3340 | } |
| 3341 | |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 3342 | void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size) |
| 3343 | { |
| 3344 | dprintf(INFO, "Enabling charger screen check\n"); |
| 3345 | device.charger_screen_enabled = 1; |
| 3346 | write_device_info(&device); |
| 3347 | fastboot_okay(""); |
| 3348 | } |
| 3349 | |
| 3350 | void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size) |
| 3351 | { |
| 3352 | dprintf(INFO, "Disabling charger screen check\n"); |
| 3353 | device.charger_screen_enabled = 0; |
| 3354 | write_device_info(&device); |
| 3355 | fastboot_okay(""); |
| 3356 | } |
| 3357 | |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 3358 | void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size) |
| 3359 | { |
| 3360 | char *p = NULL; |
| 3361 | const char *delim = " \t\n\r"; |
Parth Dixit | 407f15b | 2016-01-07 14:47:37 +0530 | [diff] [blame] | 3362 | char *sp; |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 3363 | |
| 3364 | if (arg) { |
Parth Dixit | 407f15b | 2016-01-07 14:47:37 +0530 | [diff] [blame] | 3365 | p = strtok_r((char *)arg, delim, &sp); |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 3366 | if (p) { |
| 3367 | if (!strncmp(p, "0", 1)) { |
| 3368 | device.charger_screen_enabled = 0; |
| 3369 | } else if (!strncmp(p, "1", 1)) { |
| 3370 | device.charger_screen_enabled = 1; |
| 3371 | } |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | /* update charger_screen_enabled value for getvar |
| 3376 | * command |
| 3377 | */ |
| 3378 | snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d", |
| 3379 | device.charger_screen_enabled); |
| 3380 | |
| 3381 | write_device_info(&device); |
| 3382 | fastboot_okay(""); |
| 3383 | } |
| 3384 | |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 3385 | void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size) |
| 3386 | { |
| 3387 | dprintf(INFO, "Selecting display panel %s\n", arg); |
| 3388 | if (arg) |
| 3389 | strlcpy(device.display_panel, arg, |
| 3390 | sizeof(device.display_panel)); |
| 3391 | write_device_info(&device); |
| 3392 | fastboot_okay(""); |
| 3393 | } |
| 3394 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3395 | void cmd_oem_unlock(const char *arg, void *data, unsigned sz) |
| 3396 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3397 | set_device_unlock(UNLOCK, TRUE); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz) |
| 3401 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3402 | if(!device.is_unlocked) { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 3403 | if(!is_allow_unlock) { |
| 3404 | fastboot_fail("oem unlock is not allowed"); |
| 3405 | return; |
| 3406 | } |
| 3407 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3408 | set_device_unlock_value(UNLOCK, TRUE); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 3409 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3410 | /* wipe data */ |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 3411 | struct recovery_message msg; |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3412 | |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 3413 | snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data"); |
| 3414 | write_misc(0, &msg, sizeof(msg)); |
| 3415 | |
| 3416 | fastboot_okay(""); |
| 3417 | reboot_device(RECOVERY_MODE); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3418 | } |
| 3419 | fastboot_okay(""); |
| 3420 | } |
| 3421 | |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 3422 | static int aboot_frp_unlock(char *pname, void *data, unsigned sz) |
| 3423 | { |
| 3424 | int ret = 1; |
| 3425 | uint32_t secret_key; |
| 3426 | char seckey_buffer[MAX_RSP_SIZE]; |
| 3427 | |
| 3428 | secret_key = aboot_get_secret_key(); |
| 3429 | if (secret_key) |
| 3430 | { |
| 3431 | snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key); |
| 3432 | if (!memcmp((void *)data, (void *)seckey_buffer, sz)) |
| 3433 | { |
| 3434 | is_allow_unlock = true; |
| 3435 | write_allow_oem_unlock(is_allow_unlock); |
| 3436 | ret = 0; |
| 3437 | } |
| 3438 | } |
| 3439 | return ret; |
| 3440 | } |
| 3441 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3442 | void cmd_oem_lock(const char *arg, void *data, unsigned sz) |
| 3443 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3444 | set_device_unlock(UNLOCK, FALSE); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3445 | } |
| 3446 | |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3447 | void cmd_oem_devinfo(const char *arg, void *data, unsigned sz) |
| 3448 | { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3449 | char response[MAX_RSP_SIZE]; |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 3450 | snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false")); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3451 | fastboot_info(response); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 3452 | snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false")); |
| 3453 | fastboot_info(response); |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3454 | #if !VBOOT_MOTA |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3455 | snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false")); |
| 3456 | fastboot_info(response); |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3457 | #endif |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 3458 | 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] | 3459 | fastboot_info(response); |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 3460 | snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel)); |
| 3461 | fastboot_info(response); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3462 | fastboot_okay(""); |
| 3463 | } |
| 3464 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3465 | void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz) |
| 3466 | { |
| 3467 | char response[MAX_RSP_SIZE]; |
| 3468 | snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock); |
| 3469 | fastboot_info(response); |
| 3470 | fastboot_okay(""); |
| 3471 | } |
| 3472 | |
| 3473 | void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz) |
| 3474 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3475 | set_device_unlock(UNLOCK_CRITICAL, FALSE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3476 | } |
| 3477 | |
| 3478 | void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz) |
| 3479 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3480 | set_device_unlock(UNLOCK_CRITICAL, TRUE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3481 | } |
| 3482 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 3483 | void cmd_preflash(const char *arg, void *data, unsigned sz) |
| 3484 | { |
| 3485 | fastboot_okay(""); |
| 3486 | } |
| 3487 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3488 | static uint8_t logo_header[LOGO_IMG_HEADER_SIZE]; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3489 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3490 | int splash_screen_check_header(logo_img_header *header) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3491 | { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3492 | if (memcmp(header->magic, LOGO_IMG_MAGIC, 8)) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3493 | return -1; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3494 | if (header->width == 0 || header->height == 0) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3495 | return -1; |
| 3496 | return 0; |
| 3497 | } |
| 3498 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3499 | int splash_screen_flash() |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 3500 | { |
| 3501 | struct ptentry *ptn; |
| 3502 | struct ptable *ptable; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3503 | struct logo_img_header *header; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 3504 | struct fbcon_config *fb_display = NULL; |
Channagoud Kadabi | b3ccf5c | 2014-12-03 12:39:29 -0800 | [diff] [blame] | 3505 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3506 | ptable = flash_get_ptable(); |
| 3507 | if (ptable == NULL) { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3508 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 3509 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3510 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3511 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3512 | ptn = ptable_find(ptable, "splash"); |
| 3513 | if (ptn == NULL) { |
| 3514 | dprintf(CRITICAL, "ERROR: splash Partition not found\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3515 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3516 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3517 | if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3518 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3519 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3520 | } |
| 3521 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3522 | header = (struct logo_img_header *)logo_header; |
| 3523 | if (splash_screen_check_header(header)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3524 | dprintf(CRITICAL, "ERROR: Boot image header invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3525 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | fb_display = fbcon_display(); |
| 3529 | if (fb_display) { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3530 | if (header->type && (header->blocks != 0)) { // RLE24 compressed data |
| 3531 | uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET; |
| 3532 | |
| 3533 | /* if the logo is full-screen size, remove "fbcon_clear()" */ |
| 3534 | if ((header->width != fb_display->width) |
| 3535 | || (header->height != fb_display->height)) |
| 3536 | fbcon_clear(); |
| 3537 | |
| 3538 | if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0, |
| 3539 | (uint32_t *)base, |
| 3540 | (header->blocks * 512))) { |
| 3541 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 3542 | return -1; |
| 3543 | } |
| 3544 | fbcon_extract_to_screen(header, base); |
| 3545 | return 0; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 3546 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3547 | |
Veera Sundaram Sankaran | c33416f | 2015-09-28 10:25:55 -0700 | [diff] [blame] | 3548 | if ((header->width > fb_display->width) || (header->height > fb_display->height)) { |
| 3549 | dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3550 | return -1; |
| 3551 | } |
| 3552 | |
Vineet Bajaj | 99291ed | 2014-09-09 12:29:46 +0530 | [diff] [blame] | 3553 | uint8_t *base = (uint8_t *) fb_display->base; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3554 | if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0, |
| 3555 | (uint32_t *)base, |
| 3556 | ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3557 | fbcon_clear(); |
Vineet Bajaj | 99291ed | 2014-09-09 12:29:46 +0530 | [diff] [blame] | 3558 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3559 | return -1; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 3560 | } |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3561 | } |
| 3562 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3563 | return 0; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3564 | } |
| 3565 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3566 | int splash_screen_mmc() |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3567 | { |
| 3568 | int index = INVALID_PTN; |
| 3569 | unsigned long long ptn = 0; |
| 3570 | struct fbcon_config *fb_display = NULL; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3571 | struct logo_img_header *header; |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 3572 | uint32_t blocksize, realsize, readsize; |
| 3573 | uint8_t *base; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3574 | |
| 3575 | index = partition_get_index("splash"); |
| 3576 | if (index == 0) { |
| 3577 | dprintf(CRITICAL, "ERROR: splash Partition table not found\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3578 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | ptn = partition_get_offset(index); |
| 3582 | if (ptn == 0) { |
| 3583 | dprintf(CRITICAL, "ERROR: splash Partition invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3584 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3585 | } |
| 3586 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 3587 | mmc_set_lun(partition_get_lun(index)); |
| 3588 | |
| 3589 | blocksize = mmc_get_device_blocksize(); |
| 3590 | if (blocksize == 0) { |
| 3591 | dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n"); |
| 3592 | return -1; |
| 3593 | } |
| 3594 | |
| 3595 | fb_display = fbcon_display(); |
Channagoud Kadabi | daf10a6 | 2015-07-22 11:51:55 -0700 | [diff] [blame] | 3596 | if (!fb_display) |
| 3597 | { |
| 3598 | dprintf(CRITICAL, "ERROR: fb config is not allocated\n"); |
| 3599 | return -1; |
| 3600 | } |
| 3601 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 3602 | base = (uint8_t *) fb_display->base; |
| 3603 | |
| 3604 | if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3605 | dprintf(CRITICAL, "ERROR: Cannot read splash image header\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3606 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3607 | } |
| 3608 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 3609 | header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3610 | if (splash_screen_check_header(header)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3611 | dprintf(CRITICAL, "ERROR: Splash image header invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3612 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3613 | } |
| 3614 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3615 | if (fb_display) { |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 3616 | if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */ |
| 3617 | base += LOGO_IMG_OFFSET; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3618 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 3619 | realsize = header->blocks * 512; |
| 3620 | readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize; |
| 3621 | |
| 3622 | /* if the logo is not full-screen size, clean screen */ |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3623 | if ((header->width != fb_display->width) |
| 3624 | || (header->height != fb_display->height)) |
| 3625 | fbcon_clear(); |
| 3626 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 3627 | if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3628 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 3629 | return -1; |
| 3630 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3631 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 3632 | fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE)); |
| 3633 | } else { /* 2 Raw BGR data */ |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3634 | |
Veera Sundaram Sankaran | c33416f | 2015-09-28 10:25:55 -0700 | [diff] [blame] | 3635 | if ((header->width > fb_display->width) || (header->height > fb_display->height)) { |
| 3636 | dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n"); |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 3637 | return -1; |
| 3638 | } |
| 3639 | |
| 3640 | realsize = header->width * header->height * fb_display->bpp / 8; |
| 3641 | readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize; |
| 3642 | |
| 3643 | if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */ |
| 3644 | if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) { |
| 3645 | fbcon_clear(); |
| 3646 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 3647 | return -1; |
| 3648 | } |
| 3649 | } else { |
| 3650 | if (mmc_read(ptn + blocksize , |
| 3651 | (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) { |
| 3652 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 3653 | return -1; |
| 3654 | } |
| 3655 | memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize); |
| 3656 | } |
| 3657 | } |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3658 | } |
| 3659 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3660 | return 0; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3661 | } |
| 3662 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 3663 | int fetch_image_from_partition() |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 3664 | { |
| 3665 | if (target_is_emmc_boot()) { |
| 3666 | return splash_screen_mmc(); |
| 3667 | } else { |
| 3668 | return splash_screen_flash(); |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 3669 | } |
| 3670 | } |
| 3671 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 3672 | /* Get the size from partiton name */ |
| 3673 | static void get_partition_size(const char *arg, char *response) |
| 3674 | { |
| 3675 | uint64_t ptn = 0; |
| 3676 | uint64_t size; |
| 3677 | int index = INVALID_PTN; |
| 3678 | |
| 3679 | index = partition_get_index(arg); |
| 3680 | |
| 3681 | if (index == INVALID_PTN) |
| 3682 | { |
| 3683 | dprintf(CRITICAL, "Invalid partition index\n"); |
| 3684 | return; |
| 3685 | } |
| 3686 | |
| 3687 | ptn = partition_get_offset(index); |
| 3688 | |
| 3689 | if(!ptn) |
| 3690 | { |
| 3691 | dprintf(CRITICAL, "Invalid partition name %s\n", arg); |
| 3692 | return; |
| 3693 | } |
| 3694 | |
| 3695 | size = partition_get_size(index); |
| 3696 | |
| 3697 | snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size); |
| 3698 | return; |
| 3699 | } |
| 3700 | |
| 3701 | /* |
| 3702 | * Publish the partition type & size info |
| 3703 | * fastboot getvar will publish the required information. |
| 3704 | * fastboot getvar partition_size:<partition_name>: partition size in hex |
| 3705 | * fastboot getvar partition_type:<partition_name>: partition type (ext/fat) |
| 3706 | */ |
| 3707 | static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts) |
| 3708 | { |
| 3709 | uint8_t i; |
| 3710 | |
| 3711 | for (i = 0; i < num_parts; i++) { |
| 3712 | get_partition_size(info[i].part_name, info[i].size_response); |
| 3713 | |
| 3714 | if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE) |
| 3715 | { |
| 3716 | dprintf(CRITICAL, "partition size name truncated\n"); |
| 3717 | return; |
| 3718 | } |
| 3719 | if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE) |
| 3720 | { |
| 3721 | dprintf(CRITICAL, "partition type name truncated\n"); |
| 3722 | return; |
| 3723 | } |
| 3724 | |
| 3725 | /* publish partition size & type info */ |
| 3726 | fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response); |
| 3727 | fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response); |
| 3728 | } |
| 3729 | } |
| 3730 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3731 | void get_product_name(unsigned char *buf) |
| 3732 | { |
| 3733 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD)); |
| 3734 | return; |
| 3735 | } |
| 3736 | |
| 3737 | void get_bootloader_version(unsigned char *buf) |
| 3738 | { |
| 3739 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version); |
| 3740 | return; |
| 3741 | } |
| 3742 | |
| 3743 | void get_baseband_version(unsigned char *buf) |
| 3744 | { |
| 3745 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version); |
| 3746 | return; |
| 3747 | } |
| 3748 | |
| 3749 | bool is_device_locked() |
| 3750 | { |
| 3751 | return device.is_unlocked ? false:true; |
| 3752 | } |
| 3753 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 3754 | /* register commands and variables for fastboot */ |
| 3755 | void aboot_fastboot_register_commands(void) |
| 3756 | { |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3757 | int i; |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 3758 | char hw_platform_buf[MAX_RSP_SIZE]; |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 3759 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3760 | struct fastboot_cmd_desc cmd_list[] = { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3761 | /* By default the enabled list is empty. */ |
| 3762 | {"", NULL}, |
| 3763 | /* move commands enclosed within the below ifndef to here |
| 3764 | * if they need to be enabled in user build. |
| 3765 | */ |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3766 | #ifndef DISABLE_FASTBOOT_CMDS |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3767 | /* Register the following commands only for non-user builds */ |
| 3768 | {"flash:", cmd_flash}, |
| 3769 | {"erase:", cmd_erase}, |
| 3770 | {"boot", cmd_boot}, |
| 3771 | {"continue", cmd_continue}, |
| 3772 | {"reboot", cmd_reboot}, |
| 3773 | {"reboot-bootloader", cmd_reboot_bootloader}, |
| 3774 | {"oem unlock", cmd_oem_unlock}, |
| 3775 | {"oem unlock-go", cmd_oem_unlock_go}, |
| 3776 | {"oem lock", cmd_oem_lock}, |
| 3777 | {"flashing unlock", cmd_oem_unlock}, |
| 3778 | {"flashing lock", cmd_oem_lock}, |
| 3779 | {"flashing lock_critical", cmd_flashing_lock_critical}, |
| 3780 | {"flashing unlock_critical", cmd_flashing_unlock_critical}, |
| 3781 | {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability}, |
| 3782 | {"oem device-info", cmd_oem_devinfo}, |
| 3783 | {"preflash", cmd_preflash}, |
| 3784 | {"oem enable-charger-screen", cmd_oem_enable_charger_screen}, |
| 3785 | {"oem disable-charger-screen", cmd_oem_disable_charger_screen}, |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 3786 | {"oem off-mode-charge", cmd_oem_off_mode_charger}, |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3787 | {"oem select-display-panel", cmd_oem_select_display_panel}, |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 3788 | #if UNITTEST_FW_SUPPORT |
Channagoud Kadabi | d5ddf48 | 2015-09-28 10:31:35 -0700 | [diff] [blame] | 3789 | {"oem run-tests", cmd_oem_runtests}, |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 3790 | #endif |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3791 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3792 | }; |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3793 | |
| 3794 | int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]); |
| 3795 | for (i = 1; i < fastboot_cmds_count; i++) |
| 3796 | fastboot_register(cmd_list[i].name,cmd_list[i].cb); |
| 3797 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 3798 | /* publish variables and their values */ |
| 3799 | fastboot_publish("product", TARGET(BOARD)); |
| 3800 | fastboot_publish("kernel", "lk"); |
| 3801 | fastboot_publish("serialno", sn_buf); |
| 3802 | |
| 3803 | /* |
| 3804 | * partition info is supported only for emmc partitions |
| 3805 | * Calling this for NAND prints some error messages which |
| 3806 | * is harmless but misleading. Avoid calling this for NAND |
| 3807 | * devices. |
| 3808 | */ |
| 3809 | if (target_is_emmc_boot()) |
| 3810 | publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info)); |
| 3811 | |
| 3812 | /* Max download size supported */ |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 3813 | snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", |
| 3814 | target_get_max_flash_size()); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 3815 | fastboot_publish("max-download-size", (const char *) max_download_size); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 3816 | /* Is the charger screen check enabled */ |
| 3817 | snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d", |
| 3818 | device.charger_screen_enabled); |
| 3819 | fastboot_publish("charger-screen-enabled", |
| 3820 | (const char *) charger_screen_enabled); |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 3821 | fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled); |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 3822 | snprintf(panel_display_mode, MAX_RSP_SIZE, "%s", |
| 3823 | device.display_panel); |
| 3824 | fastboot_publish("display-panel", |
| 3825 | (const char *) panel_display_mode); |
Channagoud Kadabi | aafbbfc | 2015-03-04 17:52:56 -0800 | [diff] [blame] | 3826 | fastboot_publish("version-bootloader", (const char *) device.bootloader_version); |
| 3827 | fastboot_publish("version-baseband", (const char *) device.radio_version); |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 3828 | fastboot_publish("secure", is_secure_boot_enable()? "yes":"no"); |
| 3829 | smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf)); |
| 3830 | snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf, |
| 3831 | target_is_emmc_boot()? "eMMC":"UFS"); |
| 3832 | fastboot_publish("variant", (const char *) get_variant); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 3833 | #if CHECK_BAT_VOLTAGE |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 3834 | update_battery_status(); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 3835 | fastboot_publish("battery-voltage", (const char *) battery_voltage); |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 3836 | fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 3837 | #endif |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 3838 | } |
| 3839 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3840 | void aboot_init(const struct app_descriptor *app) |
| 3841 | { |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 3842 | unsigned reboot_mode = 0; |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 3843 | |
lijuang | 3983173 | 2016-01-08 17:49:02 +0800 | [diff] [blame] | 3844 | /* Initialise wdog to catch early lk crashes */ |
| 3845 | #if WDOG_SUPPORT |
| 3846 | msm_wdog_init(); |
| 3847 | #endif |
| 3848 | |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 3849 | /* Setup page size information for nv storage */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 3850 | if (target_is_emmc_boot()) |
| 3851 | { |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 3852 | page_size = mmc_page_size(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 3853 | page_mask = page_size - 1; |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 3854 | mmc_blocksize = mmc_get_device_blocksize(); |
| 3855 | mmc_blocksize_mask = mmc_blocksize - 1; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 3856 | } |
| 3857 | else |
| 3858 | { |
| 3859 | page_size = flash_page_size(); |
| 3860 | page_mask = page_size - 1; |
| 3861 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3862 | ASSERT((MEMBASE + MEMSIZE) > MEMBASE); |
| 3863 | |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 3864 | read_device_info(&device); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 3865 | read_allow_oem_unlock(&device); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3866 | |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 3867 | /* Display splash screen if enabled */ |
| 3868 | #if DISPLAY_SPLASH_SCREEN |
lijuang | 99c02d8 | 2015-02-13 19:04:34 +0800 | [diff] [blame] | 3869 | #if NO_ALARM_DISPLAY |
| 3870 | if (!check_alarm_boot()) { |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 3871 | #endif |
lijuang | 99c02d8 | 2015-02-13 19:04:34 +0800 | [diff] [blame] | 3872 | dprintf(SPEW, "Display Init: Start\n"); |
Ajay Singh Parmar | a7865a8 | 2015-04-16 21:27:52 -0700 | [diff] [blame] | 3873 | #if DISPLAY_HDMI_PRIMARY |
| 3874 | if (!strlen(device.display_panel)) |
| 3875 | strlcpy(device.display_panel, DISPLAY_PANEL_HDMI, |
| 3876 | sizeof(device.display_panel)); |
| 3877 | #endif |
Channagoud Kadabi | 90869ce | 2015-04-27 11:15:14 -0700 | [diff] [blame] | 3878 | #if ENABLE_WBC |
Channagoud Kadabi | d801ac7 | 2015-08-26 11:21:51 -0700 | [diff] [blame] | 3879 | /* Wait if the display shutdown is in progress */ |
| 3880 | while(pm_app_display_shutdown_in_prgs()); |
Channagoud Kadabi | 90869ce | 2015-04-27 11:15:14 -0700 | [diff] [blame] | 3881 | if (!pm_appsbl_display_init_done()) |
| 3882 | target_display_init(device.display_panel); |
| 3883 | else |
| 3884 | display_image_on_screen(); |
| 3885 | #else |
lijuang | 99c02d8 | 2015-02-13 19:04:34 +0800 | [diff] [blame] | 3886 | target_display_init(device.display_panel); |
Channagoud Kadabi | 90869ce | 2015-04-27 11:15:14 -0700 | [diff] [blame] | 3887 | #endif |
lijuang | 99c02d8 | 2015-02-13 19:04:34 +0800 | [diff] [blame] | 3888 | dprintf(SPEW, "Display Init: Done\n"); |
| 3889 | #if NO_ALARM_DISPLAY |
| 3890 | } |
| 3891 | #endif |
| 3892 | #endif |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 3893 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 3894 | target_serialno((unsigned char *) sn_buf); |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 3895 | dprintf(SPEW,"serial number: %s\n",sn_buf); |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 3896 | |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 3897 | memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE); |
| 3898 | |
Matthew Qin | defd556 | 2014-07-11 18:02:40 +0800 | [diff] [blame] | 3899 | /* |
| 3900 | * Check power off reason if user force reset, |
| 3901 | * if yes phone will do normal boot. |
| 3902 | */ |
| 3903 | if (is_user_force_reset()) |
| 3904 | goto normal_boot; |
| 3905 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 3906 | /* Check if we should do something other than booting up */ |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 3907 | if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN)) |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 3908 | { |
Pavel Nedev | a4c9d3a | 2013-05-15 14:42:34 +0300 | [diff] [blame] | 3909 | dprintf(ALWAYS,"dload mode key sequence detected\n"); |
lijuang | 395b5e6 | 2015-11-19 17:39:44 +0800 | [diff] [blame] | 3910 | reboot_device(EMERGENCY_DLOAD); |
| 3911 | dprintf(CRITICAL,"Failed to reboot into dload mode\n"); |
| 3912 | |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 3913 | boot_into_fastboot = true; |
| 3914 | } |
| 3915 | if (!boot_into_fastboot) |
| 3916 | { |
| 3917 | if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP)) |
| 3918 | boot_into_recovery = 1; |
| 3919 | if (!boot_into_recovery && |
| 3920 | (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN))) |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 3921 | boot_into_fastboot = true; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 3922 | } |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 3923 | #if NO_KEYPAD_DRIVER |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 3924 | if (fastboot_trigger()) |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 3925 | boot_into_fastboot = true; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 3926 | #endif |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 3927 | |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 3928 | #if USE_PON_REBOOT_REG |
| 3929 | reboot_mode = check_hard_reboot_mode(); |
| 3930 | #else |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 3931 | reboot_mode = check_reboot_mode(); |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 3932 | #endif |
| 3933 | if (reboot_mode == RECOVERY_MODE) |
| 3934 | { |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 3935 | boot_into_recovery = 1; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 3936 | } |
| 3937 | else if(reboot_mode == FASTBOOT_MODE) |
| 3938 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 3939 | boot_into_fastboot = true; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 3940 | } |
| 3941 | else if(reboot_mode == ALARM_BOOT) |
| 3942 | { |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 3943 | boot_reason_alarm = true; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 3944 | } |
Parth Dixit | 207573a | 2015-10-27 17:26:21 +0530 | [diff] [blame] | 3945 | #if VERIFIED_BOOT |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3946 | #if !VBOOT_MOTA |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 3947 | else if (reboot_mode == DM_VERITY_ENFORCING) |
| 3948 | { |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 3949 | device.verity_mode = 1; |
| 3950 | write_device_info(&device); |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 3951 | } |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame^] | 3952 | #if ENABLE_VB_ATTEST |
| 3953 | else if (reboot_mode == DM_VERITY_EIO) |
| 3954 | #else |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 3955 | else if (reboot_mode == DM_VERITY_LOGGING) |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame^] | 3956 | #endif |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 3957 | { |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 3958 | device.verity_mode = 0; |
| 3959 | write_device_info(&device); |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 3960 | } |
| 3961 | else if (reboot_mode == DM_VERITY_KEYSCLEAR) |
| 3962 | { |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 3963 | if(send_delete_keys_to_tz()) |
| 3964 | ASSERT(0); |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 3965 | } |
Parth Dixit | 207573a | 2015-10-27 17:26:21 +0530 | [diff] [blame] | 3966 | #endif |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3967 | #endif |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 3968 | |
Matthew Qin | defd556 | 2014-07-11 18:02:40 +0800 | [diff] [blame] | 3969 | normal_boot: |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 3970 | if (!boot_into_fastboot) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 3971 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 3972 | if (target_is_emmc_boot()) |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3973 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 3974 | if(emmc_recovery_init()) |
| 3975 | dprintf(ALWAYS,"error in emmc_recovery_init\n"); |
| 3976 | if(target_use_signed_kernel()) |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3977 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 3978 | if((device.is_unlocked) || (device.is_tampered)) |
| 3979 | { |
| 3980 | #ifdef TZ_TAMPER_FUSE |
| 3981 | set_tamper_fuse_cmd(); |
| 3982 | #endif |
| 3983 | #if USE_PCOM_SECBOOT |
| 3984 | set_tamper_flag(device.is_tampered); |
| 3985 | #endif |
| 3986 | } |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3987 | } |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 3988 | |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 3989 | boot_linux_from_mmc(); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3990 | } |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 3991 | else |
| 3992 | { |
| 3993 | recovery_init(); |
| 3994 | #if USE_PCOM_SECBOOT |
| 3995 | if((device.is_unlocked) || (device.is_tampered)) |
| 3996 | set_tamper_flag(device.is_tampered); |
| 3997 | #endif |
| 3998 | boot_linux_from_flash(); |
| 3999 | } |
| 4000 | dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting " |
| 4001 | "to fastboot mode.\n"); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4002 | } |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 4003 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 4004 | /* We are here means regular boot did not happen. Start fastboot. */ |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 4005 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 4006 | /* register aboot specific fastboot commands */ |
| 4007 | aboot_fastboot_register_commands(); |
Amol Jadi | 57abe4c | 2011-05-24 15:47:27 -0700 | [diff] [blame] | 4008 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 4009 | /* dump partition table for debug info */ |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 4010 | partition_dump(); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 4011 | |
| 4012 | /* initialize and start fastboot */ |
| 4013 | fastboot_init(target_get_scratch_address(), target_get_max_flash_size()); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 4014 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 4015 | display_fastboot_menu(); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 4016 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 4017 | } |
| 4018 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 4019 | uint32_t get_page_size() |
| 4020 | { |
| 4021 | return page_size; |
| 4022 | } |
| 4023 | |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 4024 | /* |
| 4025 | * Calculated and save hash (SHA256) for non-signed boot image. |
| 4026 | * |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 4027 | * @param image_addr - Boot image address |
| 4028 | * @param image_size - Size of the boot image |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 4029 | * |
| 4030 | * @return int - 0 on success, negative value on failure. |
| 4031 | */ |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 4032 | static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size) |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 4033 | { |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 4034 | unsigned int digest[8]; |
| 4035 | #if IMAGE_VERIF_ALGO_SHA1 |
| 4036 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1; |
| 4037 | #else |
| 4038 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256; |
| 4039 | #endif |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 4040 | |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 4041 | target_crypto_init_params(); |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 4042 | hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo); |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 4043 | |
| 4044 | save_kernel_hash_cmd(digest); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 4045 | dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size); |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 4046 | |
| 4047 | return 0; |
| 4048 | } |
| 4049 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 4050 | APP_START(aboot) |
| 4051 | .init = aboot_init, |
| 4052 | APP_END |