Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 5 | * Copyright (c) 2009-2018, 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> |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 51 | #include <ab_partition_parser.h> |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 52 | #include <verifiedboot.h> |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 53 | #include <platform.h> |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 54 | #include <crypto_hash.h> |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 55 | #include <malloc.h> |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 56 | #include <boot_stats.h> |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 57 | #include <sha.h> |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 58 | #include <platform/iomap.h> |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 59 | #include <boot_device.h> |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 60 | #include <boot_verifier.h> |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 61 | #include <image_verify.h> |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 62 | #include <decompress.h> |
Unnati Gandhi | 17b3bfc | 2015-05-11 12:58:16 +0530 | [diff] [blame] | 63 | #include <platform/timer.h> |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 64 | #include <sys/types.h> |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 65 | #if USE_RPMB_FOR_DEVINFO |
| 66 | #include <rpmb.h> |
| 67 | #endif |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 68 | |
Channagoud Kadabi | 90869ce | 2015-04-27 11:15:14 -0700 | [diff] [blame] | 69 | #if ENABLE_WBC |
| 70 | #include <pm_app_smbchg.h> |
| 71 | #endif |
| 72 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 73 | #if DEVICE_TREE |
| 74 | #include <libfdt.h> |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 75 | #include <dev_tree.h> |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 76 | #endif |
| 77 | |
Aparna Mallavarapu | 118ccae | 2015-06-03 13:47:11 +0530 | [diff] [blame] | 78 | #if WDOG_SUPPORT |
| 79 | #include <wdog.h> |
| 80 | #endif |
| 81 | |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 82 | #include <reboot.h> |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 83 | #include "image_verify.h" |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 84 | #include "recovery.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 85 | #include "bootimg.h" |
| 86 | #include "fastboot.h" |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 87 | #include "sparse_format.h" |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 88 | #include "meta_format.h" |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 89 | #include "mmc.h" |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 90 | #include "devinfo.h" |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 91 | #include "board.h" |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 92 | #include "scm.h" |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 93 | #include "mdtp.h" |
Sridhar Parasuram | 32b3066 | 2015-07-10 13:33:22 -0700 | [diff] [blame] | 94 | #include "secapp_loader.h" |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 95 | #include <menu_keys_detect.h> |
| 96 | #include <display_menu.h> |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 97 | #include "fastboot_test.h" |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 98 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 99 | extern bool target_use_signed_kernel(void); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 100 | extern void platform_uninit(void); |
Channagoud Kadabi | 33defe2 | 2013-06-18 18:35:40 -0700 | [diff] [blame] | 101 | extern void target_uninit(void); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 102 | extern int get_target_boot_params(const char *cmdline, const char *part, |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 103 | char **buf); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 104 | |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 105 | void *info_buf; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 106 | void write_device_info_mmc(device_info *dev); |
| 107 | void write_device_info_flash(device_info *dev); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 108 | 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] | 109 | static int aboot_frp_unlock(char *pname, void *data, unsigned sz); |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 110 | static inline uint64_t validate_partition_size(); |
Parth Dixit | 54ac3bb | 2017-03-07 15:52:48 +0530 | [diff] [blame] | 111 | bool pwr_key_is_pressed = false; |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 112 | static bool is_systemd_present=false; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 113 | static void publish_getvar_multislot_vars(); |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 114 | /* fastboot command function pointer */ |
| 115 | typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 116 | bool get_perm_attr_status(); |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 117 | |
| 118 | struct fastboot_cmd_desc { |
| 119 | char * name; |
| 120 | fastboot_cmd_fn cb; |
| 121 | }; |
| 122 | |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 123 | #define EXPAND(NAME) #NAME |
| 124 | #define TARGET(NAME) EXPAND(NAME) |
Brian Swetland | 2defe16 | 2009-08-18 14:35:59 -0700 | [diff] [blame] | 125 | |
Ajay Singh Parmar | a7865a8 | 2015-04-16 21:27:52 -0700 | [diff] [blame] | 126 | #define DISPLAY_PANEL_HDMI "hdmi" |
| 127 | |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 128 | #ifdef MEMBASE |
| 129 | #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE)) |
| 130 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 131 | #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000 |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 132 | #endif |
| 133 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 134 | #ifndef MEMSIZE |
| 135 | #define MEMSIZE 1024*1024 |
| 136 | #endif |
| 137 | |
| 138 | #define MAX_TAGS_SIZE 1024 |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 139 | #define PLL_CODES_OFFSET 4096 |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 140 | /* make 4096 as default size to ensure EFS,EXT4's erasing */ |
| 141 | #define DEFAULT_ERASE_SIZE 4096 |
Ujwal Patel | c0b0a25 | 2015-08-16 14:05:35 -0700 | [diff] [blame] | 142 | #define MAX_PANEL_BUF_SIZE 196 |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 143 | #define FOOTER_SIZE 16384 |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 144 | |
Dhaval Patel | f83d73b | 2014-06-23 16:24:37 -0700 | [diff] [blame] | 145 | #define DISPLAY_DEFAULT_PREFIX "mdss_mdp" |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 146 | #define BOOT_DEV_MAX_LEN 64 |
Sundarajan Srinivasan | 595b71e | 2013-11-05 12:44:34 -0800 | [diff] [blame] | 147 | |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 148 | #define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false |
| 149 | |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 150 | #define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX |
| 151 | |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 152 | //Size of the header that is used in case the boot image has |
| 153 | //a uncompressed kernel + appended dtb |
| 154 | #define PATCHED_KERNEL_HEADER_SIZE 20 |
| 155 | |
| 156 | //String used to determine if the boot image has |
| 157 | //a uncompressed kernel + appended dtb |
| 158 | #define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG" |
| 159 | |
Sridhar Parasuram | 7bd4aaf | 2015-02-12 11:14:38 -0800 | [diff] [blame] | 160 | #if USE_BOOTDEV_CMDLINE |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 161 | static const char *emmc_cmdline = " androidboot.bootdevice="; |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 162 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 163 | static const char *emmc_cmdline = " androidboot.emmc=true"; |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 164 | #endif |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 165 | static const char *usb_sn_cmdline = " androidboot.serialno="; |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 166 | static const char *androidboot_mode = " androidboot.mode="; |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 167 | |
| 168 | static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target"; |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 169 | static const char *alarmboot_cmdline = " androidboot.alarmboot=true"; |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 170 | static const char *loglevel = " quiet"; |
Ajay Dudani | ca3a33c | 2011-11-18 08:31:40 -0800 | [diff] [blame] | 171 | static const char *battchg_pause = " androidboot.mode=charger"; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 172 | static const char *auth_kernel = " androidboot.authorized_kernel=true"; |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 173 | static const char *secondary_gpt_enable = " gpt"; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 174 | #ifdef MDTP_SUPPORT |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 175 | static const char *mdtp_activated_flag = " mdtp"; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 176 | #endif |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 177 | static const char *baseband_apq = " androidboot.baseband=apq"; |
| 178 | static const char *baseband_msm = " androidboot.baseband=msm"; |
| 179 | static const char *baseband_csfb = " androidboot.baseband=csfb"; |
| 180 | static const char *baseband_svlte2a = " androidboot.baseband=svlte2a"; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 181 | static const char *baseband_mdm = " androidboot.baseband=mdm"; |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 182 | static const char *baseband_mdm2 = " androidboot.baseband=mdm2"; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 183 | static const char *baseband_sglte = " androidboot.baseband=sglte"; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 184 | static const char *baseband_dsda = " androidboot.baseband=dsda"; |
| 185 | static const char *baseband_dsda2 = " androidboot.baseband=dsda2"; |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 186 | static const char *baseband_sglte2 = " androidboot.baseband=sglte2"; |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 187 | static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1"; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 188 | static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr"; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 189 | static const char *androidboot_slot_suffix = " androidboot.slot_suffix="; |
| 190 | static const char *skip_ramfs = " skip_initramfs"; |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 191 | |
| 192 | #if HIBERNATION_SUPPORT |
| 193 | static const char *resume = " resume=/dev/mmcblk0p"; |
| 194 | #endif |
| 195 | |
Sourabh Banerjee | 6c7153c | 2018-03-20 01:21:57 +0530 | [diff] [blame] | 196 | #ifdef INIT_BIN_LE |
| 197 | static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE; |
| 198 | #else |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 199 | static const char *sys_path_cmdline = " rootwait ro init=/init"; |
Sourabh Banerjee | 6c7153c | 2018-03-20 01:21:57 +0530 | [diff] [blame] | 200 | #endif |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 201 | |
| 202 | #if VERITY_LE |
| 203 | static const char *verity_dev = " root=/dev/dm-0"; |
| 204 | static const char *verity_system_part = " dm=\"system"; |
| 205 | static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p"; |
| 206 | #else |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 207 | static const char *sys_path = " root=/dev/mmcblk0p"; |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 208 | |
| 209 | #define MAX_DTBO_IDX_STR 64 |
| 210 | static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx="; |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 211 | #endif |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 212 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 213 | #if VERIFIED_BOOT |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 214 | static const char *verity_mode = " androidboot.veritymode="; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 215 | static const char *verified_state= " androidboot.verifiedbootstate="; |
Parth Dixit | a5715a0 | 2015-10-29 12:25:10 +0530 | [diff] [blame] | 216 | static const char *keymaster_v1= " androidboot.keymaster=1"; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 217 | //indexed based on enum values, green is 0 by default |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 218 | |
| 219 | struct verified_boot_verity_mode vbvm[] = |
| 220 | { |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 221 | #if ENABLE_VB_ATTEST |
| 222 | {false, "eio"}, |
| 223 | #else |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 224 | {false, "logging"}, |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 225 | #endif |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 226 | {true, "enforcing"}, |
| 227 | }; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 228 | struct verified_boot_state_name vbsn[] = |
| 229 | { |
| 230 | {GREEN, "green"}, |
| 231 | {ORANGE, "orange"}, |
| 232 | {YELLOW,"yellow"}, |
| 233 | {RED,"red" }, |
| 234 | }; |
| 235 | #endif |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 236 | /*As per spec delay wait time before shutdown in Red state*/ |
| 237 | #define DELAY_WAIT 30000 |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 238 | static unsigned page_size = 0; |
| 239 | static unsigned page_mask = 0; |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 240 | static unsigned mmc_blocksize = 0; |
| 241 | static unsigned mmc_blocksize_mask = 0; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 242 | static char ffbm_mode_string[FFBM_MODE_BUF_SIZE]; |
| 243 | static bool boot_into_ffbm; |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 244 | static char *target_boot_params = NULL; |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 245 | static bool boot_reason_alarm; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 246 | static bool devinfo_present = true; |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 247 | bool boot_into_fastboot = false; |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 248 | static uint32_t dt_size = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 249 | static char *vbcmdline; |
| 250 | static bootinfo info = {0}; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 251 | static void *recovery_dtbo_buf = NULL; |
| 252 | static uint32_t recovery_dtbo_size = 0; |
| 253 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 254 | /* Assuming unauthorized kernel image by default */ |
| 255 | static int auth_kernel_img = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 256 | static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}}; |
| 257 | |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 258 | static bool is_allow_unlock = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 259 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 260 | static char frp_ptns[2][8] = {"config","frp"}; |
| 261 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 262 | static const char *critical_flash_allowed_ptn[] = { |
| 263 | "aboot", |
| 264 | "rpm", |
| 265 | "tz", |
| 266 | "sbl", |
| 267 | "sdi", |
| 268 | "sbl1", |
| 269 | "xbl", |
| 270 | "hyp", |
| 271 | "pmic", |
| 272 | "bootloader", |
| 273 | "devinfo", |
| 274 | "partition"}; |
| 275 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 276 | struct atag_ptbl_entry |
| 277 | { |
| 278 | char name[16]; |
| 279 | unsigned offset; |
| 280 | unsigned size; |
| 281 | unsigned flags; |
| 282 | }; |
| 283 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 284 | /* |
| 285 | * Partition info, required to be published |
| 286 | * for fastboot |
| 287 | */ |
| 288 | struct getvar_partition_info { |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 289 | char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */ |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 290 | char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */ |
| 291 | char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */ |
| 292 | char size_response[MAX_RSP_SIZE]; /* fastboot response for size */ |
| 293 | char type_response[MAX_RSP_SIZE]; /* fastboot response for type */ |
| 294 | }; |
| 295 | |
| 296 | /* |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 297 | * Update the part_type_known for known paritions types. |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 298 | */ |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 299 | #define RAW_STR "raw" |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 300 | #define EXT_STR "ext4" |
| 301 | #define F2FS_STR "f2fs" |
| 302 | |
| 303 | #define FS_SUPERBLOCK_OFFSET 0x400 |
| 304 | #define EXT_MAGIC 0xEF53 |
| 305 | #define EXT_MAGIC_OFFSET_SB 0x38 |
| 306 | #define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number |
| 307 | #define F2FS_MAGIC_OFFSET_SB 0x0 |
| 308 | |
| 309 | typedef enum fs_signature_type { |
| 310 | EXT_FS_SIGNATURE = 1, |
| 311 | EXT_F2FS_SIGNATURE = 2, |
| 312 | NO_FS = -1 |
| 313 | } fs_signature_type; |
| 314 | |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 315 | struct getvar_partition_info part_info[NUM_PARTITIONS]; |
| 316 | struct getvar_partition_info part_type_known[] = |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 317 | { |
Mayank Grover | 4992021 | 2018-02-09 18:15:55 +0530 | [diff] [blame] | 318 | { "system" , "partition-size:", "partition-type:", "", "ext4" }, |
| 319 | { "userdata" , "partition-size:", "partition-type:", "", "ext4" }, |
| 320 | { "cache" , "partition-size:", "partition-type:", "", "ext4" }, |
| 321 | { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" }, |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | char max_download_size[MAX_RSP_SIZE]; |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 325 | char charger_screen_enabled[MAX_RSP_SIZE]; |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 326 | char sn_buf[13]; |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 327 | char display_panel_buf[MAX_PANEL_BUF_SIZE]; |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 328 | char panel_display_mode[MAX_RSP_SIZE]; |
Mayank Grover | a64dfbe | 2018-05-17 14:06:34 +0530 | [diff] [blame] | 329 | char soc_version_str[MAX_RSP_SIZE]; |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 330 | char block_size_string[MAX_RSP_SIZE]; |
Mukesh Ojha | bb6e7f3 | 2018-03-02 15:04:06 +0530 | [diff] [blame] | 331 | #if PRODUCT_IOT |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 332 | |
| 333 | /* For IOT we are using custom version */ |
| 334 | #define PRODUCT_IOT_VERSION "IOT001" |
| 335 | char bootloader_version_string[MAX_RSP_SIZE]; |
| 336 | #endif |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 337 | |
| 338 | #if CHECK_BAT_VOLTAGE |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 339 | char battery_voltage[MAX_RSP_SIZE]; |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 340 | char battery_soc_ok [MAX_RSP_SIZE]; |
| 341 | #endif |
| 342 | |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 343 | char get_variant[MAX_RSP_SIZE]; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 344 | |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 345 | extern int emmc_recovery_init(void); |
| 346 | |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 347 | #if NO_KEYPAD_DRIVER |
| 348 | extern int fastboot_trigger(void); |
| 349 | #endif |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 350 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 351 | static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64) |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 352 | { |
| 353 | /* overwrite the destination of specified for the project */ |
Channagoud Kadabi | 7042fa3 | 2013-04-26 16:44:14 -0700 | [diff] [blame] | 354 | #ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 355 | if (is_arm64) |
| 356 | hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR; |
| 357 | else |
| 358 | hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR; |
Channagoud Kadabi | 7042fa3 | 2013-04-26 16:44:14 -0700 | [diff] [blame] | 359 | hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR; |
| 360 | hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR; |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 361 | #endif |
| 362 | } |
| 363 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 364 | static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn) |
| 365 | { |
| 366 | struct atag_ptbl_entry atag_ptn; |
| 367 | |
| 368 | memcpy(atag_ptn.name, ptn->name, 16); |
| 369 | atag_ptn.name[15] = '\0'; |
| 370 | atag_ptn.offset = ptn->start; |
| 371 | atag_ptn.size = ptn->length; |
| 372 | atag_ptn.flags = ptn->flags; |
| 373 | memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry)); |
| 374 | *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned); |
| 375 | } |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 376 | #ifdef VERIFIED_BOOT_2 |
| 377 | void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz) |
| 378 | { |
| 379 | int index = 0; |
| 380 | char *vbm_img_buf = NULL; |
| 381 | unsigned long long ptn = 0; |
| 382 | unsigned long long ptn_size = 0; |
| 383 | |
| 384 | /* Immediately return if dtbo is not supported */ |
| 385 | index = partition_get_index("vbmeta"); |
| 386 | ptn = partition_get_offset(index); |
| 387 | if(!ptn) |
| 388 | { |
| 389 | dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n"); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | ptn_size = partition_get_size(index); |
| 394 | if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF) |
| 395 | { |
| 396 | dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n"); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE)); |
| 401 | if (!vbm_img_buf) |
| 402 | { |
| 403 | dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n"); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | mmc_set_lun(partition_get_lun(index)); |
| 408 | if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size)) |
| 409 | { |
| 410 | dprintf(CRITICAL, "ERROR: vbmeta read failure\n"); |
| 411 | free(vbm_img_buf); |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | *vbmeta_image_buf = vbm_img_buf; |
| 416 | *vbmeta_image_sz = (uint32_t)ptn_size; |
| 417 | return; |
| 418 | } |
| 419 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 420 | |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 421 | #if CHECK_BAT_VOLTAGE |
| 422 | void update_battery_status(void) |
| 423 | { |
| 424 | snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage()); |
| 425 | snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no"); |
| 426 | } |
| 427 | #endif |
| 428 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 429 | unsigned char *update_cmdline(const char * cmdline) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 430 | { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 431 | int cmdline_len = 0; |
| 432 | int have_cmdline = 0; |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 433 | unsigned char *cmdline_final = NULL; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 434 | int pause_at_bootup = 0; |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 435 | bool warm_boot = false; |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 436 | bool gpt_exists = partition_gpt_exists(); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 437 | int have_target_boot_params = 0; |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 438 | char *boot_dev_buf = NULL; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 439 | #ifdef MDTP_SUPPORT |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 440 | bool is_mdtp_activated = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 441 | #endif |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 442 | int current_active_slot = INVALID; |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 443 | int system_ptn_index = -1; |
| 444 | unsigned int lun = 0; |
| 445 | char lun_char_base = 'a'; |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 446 | #if VERITY_LE |
| 447 | int syspath_buflen = strlen(verity_dev) |
| 448 | + strlen(verity_system_part) + (sizeof(char) * 2) + 2 |
| 449 | + strlen(verity_params) + sizeof(int) + 2; |
| 450 | #else |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 451 | int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/ |
| 452 | char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0"; |
| 453 | int dtbo_idx = INVALID_PTN; |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 454 | #endif |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 455 | char syspath_buf[syspath_buflen]; |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 456 | #if HIBERNATION_SUPPORT |
| 457 | int resume_buflen = strlen(resume) + sizeof(int) + 2; |
| 458 | char resume_buf[resume_buflen]; |
| 459 | int swap_ptn_index = INVALID_PTN; |
| 460 | #endif |
| 461 | |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 462 | #if VERIFIED_BOOT |
| 463 | uint32_t boot_state = RED; |
| 464 | #endif |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 465 | |
| 466 | #if USE_LE_SYSTEMD |
| 467 | is_systemd_present=true; |
| 468 | #endif |
| 469 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 470 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 471 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 472 | { |
| 473 | boot_state = boot_verify_get_state(); |
| 474 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 475 | #endif |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 476 | |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 477 | #ifdef MDTP_SUPPORT |
| 478 | mdtp_activated(&is_mdtp_activated); |
| 479 | #endif /* MDTP_SUPPORT */ |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 480 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 481 | if (cmdline && cmdline[0]) { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 482 | cmdline_len = strlen(cmdline); |
| 483 | have_cmdline = 1; |
| 484 | } |
| 485 | if (target_is_emmc_boot()) { |
| 486 | cmdline_len += strlen(emmc_cmdline); |
Sridhar Parasuram | 7bd4aaf | 2015-02-12 11:14:38 -0800 | [diff] [blame] | 487 | #if USE_BOOTDEV_CMDLINE |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 488 | boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN); |
| 489 | ASSERT(boot_dev_buf); |
| 490 | platform_boot_dev_cmdline(boot_dev_buf); |
| 491 | cmdline_len += strlen(boot_dev_buf); |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 492 | #endif |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 493 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 494 | |
| 495 | cmdline_len += strlen(usb_sn_cmdline); |
| 496 | cmdline_len += strlen(sn_buf); |
| 497 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 498 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 499 | if (VB_M <= target_get_vb_version()) |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 500 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 501 | cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name); |
| 502 | if ((device.verity_mode != 0 ) && (device.verity_mode != 1)) |
| 503 | { |
| 504 | dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n"); |
| 505 | ASSERT(0); |
| 506 | } |
| 507 | cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name); |
| 508 | cmdline_len += strlen(keymaster_v1); |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 509 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 510 | #endif |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 511 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 512 | |
| 513 | if (vbcmdline != NULL) { |
| 514 | dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n", |
| 515 | strlen(vbcmdline)); |
| 516 | cmdline_len += strlen(vbcmdline); |
| 517 | } |
| 518 | |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 519 | if (boot_into_recovery && gpt_exists) |
| 520 | cmdline_len += strlen(secondary_gpt_enable); |
| 521 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 522 | #ifdef MDTP_SUPPORT |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 523 | if(is_mdtp_activated) |
| 524 | cmdline_len += strlen(mdtp_activated_flag); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 525 | #endif |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 526 | if (boot_into_ffbm) { |
| 527 | cmdline_len += strlen(androidboot_mode); |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 528 | |
| 529 | if(is_systemd_present) |
| 530 | cmdline_len += strlen(systemd_ffbm_mode); |
| 531 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 532 | cmdline_len += strlen(ffbm_mode_string); |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 533 | /* reduce kernel console messages to speed-up boot */ |
| 534 | cmdline_len += strlen(loglevel); |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 535 | } else if (boot_reason_alarm) { |
| 536 | cmdline_len += strlen(alarmboot_cmdline); |
Zhenhua Huang | 431dafa | 2015-06-30 16:13:37 +0800 | [diff] [blame] | 537 | } else if ((target_build_variant_user() || device.charger_screen_enabled) |
lijuang | 266b17d | 2018-08-17 18:53:42 +0800 | [diff] [blame] | 538 | && target_pause_for_battery_charge() && !boot_into_recovery) { |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 539 | pause_at_bootup = 1; |
| 540 | cmdline_len += strlen(battchg_pause); |
| 541 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 542 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 543 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 544 | cmdline_len += strlen(auth_kernel); |
| 545 | } |
| 546 | |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 547 | if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" : |
| 548 | "system", |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 549 | &target_boot_params) == 0) { |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 550 | have_target_boot_params = 1; |
| 551 | cmdline_len += strlen(target_boot_params); |
| 552 | } |
| 553 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 554 | /* Determine correct androidboot.baseband to use */ |
| 555 | switch(target_baseband()) |
| 556 | { |
| 557 | case BASEBAND_APQ: |
| 558 | cmdline_len += strlen(baseband_apq); |
| 559 | break; |
| 560 | |
| 561 | case BASEBAND_MSM: |
| 562 | cmdline_len += strlen(baseband_msm); |
| 563 | break; |
| 564 | |
| 565 | case BASEBAND_CSFB: |
| 566 | cmdline_len += strlen(baseband_csfb); |
| 567 | break; |
| 568 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 569 | case BASEBAND_SVLTE2A: |
| 570 | cmdline_len += strlen(baseband_svlte2a); |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 571 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 572 | |
| 573 | case BASEBAND_MDM: |
| 574 | cmdline_len += strlen(baseband_mdm); |
| 575 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 576 | |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 577 | case BASEBAND_MDM2: |
| 578 | cmdline_len += strlen(baseband_mdm2); |
| 579 | break; |
| 580 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 581 | case BASEBAND_SGLTE: |
| 582 | cmdline_len += strlen(baseband_sglte); |
| 583 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 584 | |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 585 | case BASEBAND_SGLTE2: |
| 586 | cmdline_len += strlen(baseband_sglte2); |
| 587 | break; |
| 588 | |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 589 | case BASEBAND_DSDA: |
| 590 | cmdline_len += strlen(baseband_dsda); |
| 591 | break; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 592 | |
| 593 | case BASEBAND_DSDA2: |
| 594 | cmdline_len += strlen(baseband_dsda2); |
| 595 | break; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 596 | case BASEBAND_APQ_NOWGR: |
| 597 | cmdline_len += strlen(baseband_apq_nowgr); |
| 598 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 599 | } |
| 600 | |
Vladislav Levenetz | 3cd4524 | 2017-04-05 08:58:07 +0300 | [diff] [blame] | 601 | #if ENABLE_DISPLAY |
Lijuan Gao | 4a5b832 | 2014-07-24 10:38:42 +0800 | [diff] [blame] | 602 | if (cmdline) { |
| 603 | if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) && |
Padmanabhan Komanduru | bccbcdc | 2015-06-30 16:19:24 +0530 | [diff] [blame] | 604 | target_display_panel_node(display_panel_buf, |
| 605 | MAX_PANEL_BUF_SIZE) && |
Lijuan Gao | 4a5b832 | 2014-07-24 10:38:42 +0800 | [diff] [blame] | 606 | strlen(display_panel_buf)) { |
| 607 | cmdline_len += strlen(display_panel_buf); |
| 608 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 609 | } |
Vladislav Levenetz | 3cd4524 | 2017-04-05 08:58:07 +0300 | [diff] [blame] | 610 | #endif |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 611 | |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 612 | if (target_warm_boot()) { |
| 613 | warm_boot = true; |
| 614 | cmdline_len += strlen(warmboot_cmdline); |
| 615 | } |
| 616 | |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 617 | if (target_uses_system_as_root() || |
| 618 | partition_multislot_is_supported()) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 619 | { |
| 620 | current_active_slot = partition_find_active_slot(); |
| 621 | cmdline_len += (strlen(androidboot_slot_suffix)+ |
| 622 | strlen(SUFFIX_SLOT(current_active_slot))); |
| 623 | |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 624 | system_ptn_index = partition_get_index("system"); |
| 625 | if (platform_boot_dev_isemmc()) |
| 626 | { |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 627 | #if VERITY_LE |
| 628 | /* |
| 629 | Condition 4: Verity and A/B both enabled |
| 630 | Eventual command line looks like: |
| 631 | ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ... |
| 632 | ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>" |
| 633 | */ |
| 634 | snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"", |
| 635 | verity_dev, |
| 636 | verity_system_part, suffix_slot[current_active_slot], |
| 637 | verity_params, system_ptn_index + 1); |
| 638 | #else |
| 639 | /* |
| 640 | Condition 5: A/B enabled, but verity disabled |
| 641 | Eventual command line looks like: |
| 642 | ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ... |
| 643 | ... root=/dev/mmcblk0p<NN> ... |
| 644 | */ |
| 645 | snprintf(syspath_buf, syspath_buflen, " %s%d", |
| 646 | sys_path, system_ptn_index + 1); |
| 647 | #endif |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 648 | } |
| 649 | else |
| 650 | { |
| 651 | lun = partition_get_lun(system_ptn_index); |
| 652 | snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d", |
| 653 | lun_char_base + lun, |
| 654 | partition_get_index_in_lun("system", lun)); |
| 655 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 656 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 657 | #ifndef VERIFIED_BOOT_2 |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 658 | cmdline_len += strlen(syspath_buf); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 659 | #endif |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | if (target_uses_system_as_root() || |
| 663 | partition_multislot_is_supported()) |
| 664 | { |
| 665 | cmdline_len += strlen(sys_path_cmdline); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 666 | if (!boot_into_recovery) |
| 667 | cmdline_len += strlen(skip_ramfs); |
| 668 | } |
| 669 | |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 670 | #if HIBERNATION_SUPPORT |
| 671 | if (platform_boot_dev_isemmc()) |
| 672 | { |
| 673 | swap_ptn_index = partition_get_index("swap"); |
| 674 | if (swap_ptn_index != INVALID_PTN) |
| 675 | { |
| 676 | snprintf(resume_buf, resume_buflen, |
| 677 | " %s%d", resume, |
| 678 | (swap_ptn_index + 1)); |
| 679 | cmdline_len += strlen(resume_buf); |
| 680 | } |
| 681 | else |
| 682 | { |
| 683 | dprintf(INFO, "WARNING: swap partition not found\n"); |
| 684 | } |
| 685 | } |
| 686 | #endif |
| 687 | |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 688 | #if TARGET_CMDLINE_SUPPORT |
| 689 | char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF); |
| 690 | int target_cmd_line_len; |
| 691 | ASSERT(target_cmdline_buf); |
| 692 | target_cmd_line_len = target_update_cmdline(target_cmdline_buf); |
| 693 | cmdline_len += target_cmd_line_len; |
| 694 | #endif |
| 695 | |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 696 | #if !VERITY_LE |
| 697 | dtbo_idx = get_dtbo_idx (); |
| 698 | if (dtbo_idx != INVALID_PTN) { |
| 699 | snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d", |
| 700 | android_boot_dtbo_idx, dtbo_idx); |
| 701 | cmdline_len += strlen (dtbo_idx_str); |
| 702 | } |
| 703 | #endif |
| 704 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 705 | if (cmdline_len > 0) { |
| 706 | const char *src; |
Maria Yu | 52254c0 | 2014-07-04 16:14:54 +0800 | [diff] [blame] | 707 | unsigned char *dst; |
| 708 | |
| 709 | cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3)); |
| 710 | ASSERT(cmdline_final != NULL); |
vijay kumar | 287bb54 | 2015-09-29 13:01:52 +0530 | [diff] [blame] | 711 | memset((void *)cmdline_final, 0, sizeof(*cmdline_final)); |
Maria Yu | 52254c0 | 2014-07-04 16:14:54 +0800 | [diff] [blame] | 712 | dst = cmdline_final; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 713 | |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 714 | /* Save start ptr for debug print */ |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 715 | if (have_cmdline) { |
| 716 | src = cmdline; |
| 717 | while ((*dst++ = *src++)); |
| 718 | } |
| 719 | if (target_is_emmc_boot()) { |
| 720 | src = emmc_cmdline; |
| 721 | if (have_cmdline) --dst; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 722 | have_cmdline = 1; |
| 723 | while ((*dst++ = *src++)); |
Sridhar Parasuram | 7bd4aaf | 2015-02-12 11:14:38 -0800 | [diff] [blame] | 724 | #if USE_BOOTDEV_CMDLINE |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 725 | src = boot_dev_buf; |
| 726 | if (have_cmdline) --dst; |
| 727 | while ((*dst++ = *src++)); |
| 728 | #endif |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 729 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 730 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 731 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 732 | if (VB_M <= target_get_vb_version()) |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 733 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 734 | src = verified_state; |
| 735 | if(have_cmdline) --dst; |
| 736 | have_cmdline = 1; |
| 737 | while ((*dst++ = *src++)); |
| 738 | src = vbsn[boot_state].name; |
| 739 | if(have_cmdline) --dst; |
| 740 | while ((*dst++ = *src++)); |
| 741 | |
| 742 | if ((device.verity_mode != 0 ) && (device.verity_mode != 1)) |
| 743 | { |
| 744 | dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n"); |
| 745 | ASSERT(0); |
| 746 | } |
| 747 | src = verity_mode; |
| 748 | if(have_cmdline) --dst; |
| 749 | while ((*dst++ = *src++)); |
| 750 | src = vbvm[device.verity_mode].name; |
| 751 | if(have_cmdline) -- dst; |
| 752 | while ((*dst++ = *src++)); |
| 753 | src = keymaster_v1; |
| 754 | if(have_cmdline) --dst; |
| 755 | while ((*dst++ = *src++)); |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 756 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 757 | #endif |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 758 | |
| 759 | if (vbcmdline != NULL) { |
| 760 | src = vbcmdline; |
| 761 | if (have_cmdline) --dst; |
| 762 | while ((*dst++ = *src++)); |
| 763 | } |
| 764 | |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 765 | src = usb_sn_cmdline; |
| 766 | if (have_cmdline) --dst; |
| 767 | have_cmdline = 1; |
| 768 | while ((*dst++ = *src++)); |
| 769 | src = sn_buf; |
| 770 | if (have_cmdline) --dst; |
| 771 | have_cmdline = 1; |
| 772 | while ((*dst++ = *src++)); |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 773 | if (warm_boot) { |
| 774 | if (have_cmdline) --dst; |
| 775 | src = warmboot_cmdline; |
| 776 | while ((*dst++ = *src++)); |
| 777 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 778 | |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 779 | if (boot_into_recovery && gpt_exists) { |
| 780 | src = secondary_gpt_enable; |
| 781 | if (have_cmdline) --dst; |
| 782 | while ((*dst++ = *src++)); |
| 783 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 784 | #ifdef MDTP_SUPPORT |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 785 | if (is_mdtp_activated) { |
| 786 | src = mdtp_activated_flag; |
| 787 | if (have_cmdline) --dst; |
| 788 | while ((*dst++ = *src++)); |
| 789 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 790 | #endif |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 791 | if (boot_into_ffbm) { |
| 792 | src = androidboot_mode; |
| 793 | if (have_cmdline) --dst; |
| 794 | while ((*dst++ = *src++)); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 795 | src = ffbm_mode_string; |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 796 | if (have_cmdline) --dst; |
| 797 | while ((*dst++ = *src++)); |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 798 | |
| 799 | if(is_systemd_present) { |
| 800 | src = systemd_ffbm_mode; |
| 801 | if (have_cmdline) --dst; |
| 802 | while ((*dst++ = *src++)); |
| 803 | } |
| 804 | |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 805 | src = loglevel; |
| 806 | if (have_cmdline) --dst; |
| 807 | while ((*dst++ = *src++)); |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 808 | } else if (boot_reason_alarm) { |
| 809 | src = alarmboot_cmdline; |
| 810 | if (have_cmdline) --dst; |
| 811 | while ((*dst++ = *src++)); |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 812 | } else if (pause_at_bootup) { |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 813 | src = battchg_pause; |
| 814 | if (have_cmdline) --dst; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 815 | while ((*dst++ = *src++)); |
| 816 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 817 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 818 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 819 | src = auth_kernel; |
| 820 | if (have_cmdline) --dst; |
| 821 | while ((*dst++ = *src++)); |
| 822 | } |
| 823 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 824 | switch(target_baseband()) |
| 825 | { |
| 826 | case BASEBAND_APQ: |
| 827 | src = baseband_apq; |
| 828 | if (have_cmdline) --dst; |
| 829 | while ((*dst++ = *src++)); |
| 830 | break; |
| 831 | |
| 832 | case BASEBAND_MSM: |
| 833 | src = baseband_msm; |
| 834 | if (have_cmdline) --dst; |
| 835 | while ((*dst++ = *src++)); |
| 836 | break; |
| 837 | |
| 838 | case BASEBAND_CSFB: |
| 839 | src = baseband_csfb; |
| 840 | if (have_cmdline) --dst; |
| 841 | while ((*dst++ = *src++)); |
| 842 | break; |
| 843 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 844 | case BASEBAND_SVLTE2A: |
| 845 | src = baseband_svlte2a; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 846 | if (have_cmdline) --dst; |
| 847 | while ((*dst++ = *src++)); |
| 848 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 849 | |
| 850 | case BASEBAND_MDM: |
| 851 | src = baseband_mdm; |
| 852 | if (have_cmdline) --dst; |
| 853 | while ((*dst++ = *src++)); |
| 854 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 855 | |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 856 | case BASEBAND_MDM2: |
| 857 | src = baseband_mdm2; |
| 858 | if (have_cmdline) --dst; |
| 859 | while ((*dst++ = *src++)); |
| 860 | break; |
| 861 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 862 | case BASEBAND_SGLTE: |
| 863 | src = baseband_sglte; |
| 864 | if (have_cmdline) --dst; |
| 865 | while ((*dst++ = *src++)); |
| 866 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 867 | |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 868 | case BASEBAND_SGLTE2: |
| 869 | src = baseband_sglte2; |
| 870 | if (have_cmdline) --dst; |
| 871 | while ((*dst++ = *src++)); |
| 872 | break; |
| 873 | |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 874 | case BASEBAND_DSDA: |
| 875 | src = baseband_dsda; |
| 876 | if (have_cmdline) --dst; |
| 877 | while ((*dst++ = *src++)); |
| 878 | break; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 879 | |
| 880 | case BASEBAND_DSDA2: |
| 881 | src = baseband_dsda2; |
| 882 | if (have_cmdline) --dst; |
| 883 | while ((*dst++ = *src++)); |
| 884 | break; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 885 | case BASEBAND_APQ_NOWGR: |
| 886 | src = baseband_apq_nowgr; |
| 887 | if (have_cmdline) --dst; |
| 888 | while ((*dst++ = *src++)); |
| 889 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 890 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 891 | |
| 892 | if (strlen(display_panel_buf)) { |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 893 | src = display_panel_buf; |
| 894 | if (have_cmdline) --dst; |
| 895 | while ((*dst++ = *src++)); |
| 896 | } |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 897 | |
| 898 | if (have_target_boot_params) { |
| 899 | if (have_cmdline) --dst; |
| 900 | src = target_boot_params; |
| 901 | while ((*dst++ = *src++)); |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 902 | free(target_boot_params); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 903 | } |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 904 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 905 | if (partition_multislot_is_supported() && have_cmdline) |
| 906 | { |
| 907 | src = androidboot_slot_suffix; |
| 908 | --dst; |
| 909 | while ((*dst++ = *src++)); |
| 910 | --dst; |
| 911 | src = SUFFIX_SLOT(current_active_slot); |
| 912 | while ((*dst++ = *src++)); |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 913 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 914 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 915 | |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 916 | /* |
| 917 | * System-As-Root behaviour, system.img should contain both |
| 918 | * system content and ramdisk content, and should be mounted at |
| 919 | * root(a/b). |
| 920 | * Apending skip_ramfs for non a/b builds which use, system as root. |
| 921 | */ |
| 922 | if ((target_uses_system_as_root() || |
| 923 | partition_multislot_is_supported()) && |
| 924 | have_cmdline) |
| 925 | { |
| 926 | if (!boot_into_recovery) |
| 927 | { |
| 928 | src = skip_ramfs; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 929 | --dst; |
| 930 | while ((*dst++ = *src++)); |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | src = sys_path_cmdline; |
| 934 | --dst; |
| 935 | while ((*dst++ = *src++)); |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 936 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 937 | #ifndef VERIFIED_BOOT_2 |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 938 | src = syspath_buf; |
| 939 | --dst; |
| 940 | while ((*dst++ = *src++)); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 941 | #endif |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 942 | } |
| 943 | |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 944 | #if HIBERNATION_SUPPORT |
| 945 | if (swap_ptn_index != INVALID_PTN) |
| 946 | { |
| 947 | src = resume_buf; |
| 948 | --dst; |
| 949 | while ((*dst++ = *src++)); |
| 950 | } |
| 951 | #endif |
| 952 | |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 953 | #if TARGET_CMDLINE_SUPPORT |
| 954 | if (target_cmdline_buf && target_cmd_line_len) |
| 955 | { |
| 956 | if (have_cmdline) --dst; |
| 957 | src = target_cmdline_buf; |
| 958 | while((*dst++ = *src++)); |
| 959 | free(target_cmdline_buf); |
| 960 | } |
| 961 | #endif |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 962 | |
| 963 | #if !VERITY_LE |
| 964 | if (dtbo_idx != INVALID_PTN) { |
| 965 | src = dtbo_idx_str; |
| 966 | --dst; |
| 967 | while ((*dst++ = *src++)); |
| 968 | } |
| 969 | #endif |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 970 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 971 | |
| 972 | |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 973 | if (boot_dev_buf) |
| 974 | free(boot_dev_buf); |
| 975 | |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 976 | if (cmdline_final) |
| 977 | dprintf(INFO, "cmdline: %s\n", cmdline_final); |
| 978 | else |
| 979 | dprintf(INFO, "cmdline is NULL\n"); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 980 | return cmdline_final; |
| 981 | } |
| 982 | |
| 983 | unsigned *atag_core(unsigned *ptr) |
| 984 | { |
| 985 | /* CORE */ |
| 986 | *ptr++ = 2; |
| 987 | *ptr++ = 0x54410001; |
| 988 | |
| 989 | return ptr; |
| 990 | |
| 991 | } |
| 992 | |
| 993 | unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk, |
| 994 | unsigned ramdisk_size) |
| 995 | { |
| 996 | if (ramdisk_size) { |
| 997 | *ptr++ = 4; |
| 998 | *ptr++ = 0x54420005; |
| 999 | *ptr++ = (unsigned)ramdisk; |
| 1000 | *ptr++ = ramdisk_size; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1003 | return ptr; |
| 1004 | } |
| 1005 | |
| 1006 | unsigned *atag_ptable(unsigned **ptr_addr) |
| 1007 | { |
| 1008 | int i; |
| 1009 | struct ptable *ptable; |
| 1010 | |
| 1011 | if ((ptable = flash_get_ptable()) && (ptable->count != 0)) { |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1012 | *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) / |
| 1013 | sizeof(unsigned))); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1014 | *(*ptr_addr)++ = 0x4d534d70; |
| 1015 | for (i = 0; i < ptable->count; ++i) |
| 1016 | ptentry_to_tag(ptr_addr, ptable_get(ptable, i)); |
| 1017 | } |
| 1018 | |
| 1019 | return (*ptr_addr); |
| 1020 | } |
| 1021 | |
| 1022 | unsigned *atag_cmdline(unsigned *ptr, const char *cmdline) |
| 1023 | { |
| 1024 | int cmdline_length = 0; |
| 1025 | int n; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1026 | char *dest; |
| 1027 | |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1028 | cmdline_length = strlen((const char*)cmdline); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1029 | n = (cmdline_length + 4) & (~3); |
| 1030 | |
| 1031 | *ptr++ = (n / 4) + 2; |
| 1032 | *ptr++ = 0x54410009; |
| 1033 | dest = (char *) ptr; |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1034 | while ((*dest++ = *cmdline++)); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1035 | ptr += (n / 4); |
| 1036 | |
| 1037 | return ptr; |
| 1038 | } |
| 1039 | |
| 1040 | unsigned *atag_end(unsigned *ptr) |
| 1041 | { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1042 | /* END */ |
| 1043 | *ptr++ = 0; |
| 1044 | *ptr++ = 0; |
| 1045 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1046 | return ptr; |
| 1047 | } |
| 1048 | |
| 1049 | void generate_atags(unsigned *ptr, const char *cmdline, |
| 1050 | void *ramdisk, unsigned ramdisk_size) |
| 1051 | { |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 1052 | unsigned *orig_ptr = ptr; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1053 | ptr = atag_core(ptr); |
| 1054 | ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size); |
| 1055 | ptr = target_atag_mem(ptr); |
| 1056 | |
| 1057 | /* Skip NAND partition ATAGS for eMMC boot */ |
| 1058 | if (!target_is_emmc_boot()){ |
| 1059 | ptr = atag_ptable(&ptr); |
| 1060 | } |
| 1061 | |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 1062 | /* |
| 1063 | * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned |
| 1064 | * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes |
| 1065 | */ |
Mayank Grover | bc8a2ef | 2018-02-23 18:03:36 +0530 | [diff] [blame] | 1066 | if (!cmdline) |
| 1067 | return; |
| 1068 | |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 1069 | if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) { |
| 1070 | ptr = atag_cmdline(ptr, cmdline); |
| 1071 | ptr = atag_end(ptr); |
| 1072 | } |
| 1073 | else { |
| 1074 | dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n"); |
| 1075 | ASSERT(0); |
| 1076 | } |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1079 | typedef void entry_func_ptr(unsigned, unsigned, unsigned*); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1080 | void boot_linux(void *kernel, unsigned *tags, |
| 1081 | const char *cmdline, unsigned machtype, |
| 1082 | void *ramdisk, unsigned ramdisk_size) |
| 1083 | { |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1084 | unsigned char *final_cmdline; |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1085 | #if DEVICE_TREE |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1086 | int ret = 0; |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1087 | #endif |
| 1088 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1089 | void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel)); |
Deepa Dinamani | 25a9f76 | 2012-11-30 15:57:15 -0800 | [diff] [blame] | 1090 | uint32_t tags_phys = PA((addr_t)tags); |
vijay kumar | 1a50a64 | 2015-11-16 12:41:15 +0530 | [diff] [blame] | 1091 | struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel))); |
Deepa Dinamani | 25a9f76 | 2012-11-30 15:57:15 -0800 | [diff] [blame] | 1092 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1093 | ramdisk = (void *)PA((addr_t)ramdisk); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1094 | |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1095 | final_cmdline = update_cmdline((const char*)cmdline); |
| 1096 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1097 | #if DEVICE_TREE |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1098 | dprintf(INFO, "Updating device tree: start\n"); |
| 1099 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1100 | /* Update the Device Tree */ |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1101 | 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] | 1102 | if(ret) |
| 1103 | { |
| 1104 | dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n"); |
| 1105 | ASSERT(0); |
| 1106 | } |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1107 | dprintf(INFO, "Updating device tree: done\n"); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1108 | #else |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1109 | /* Generating the Atags */ |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1110 | generate_atags(tags, final_cmdline, ramdisk, ramdisk_size); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1111 | #endif |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1112 | |
Monika Singh | b0db4b8 | 2018-09-26 12:18:02 +0530 | [diff] [blame] | 1113 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
| 1114 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1115 | { |
| 1116 | if (device.verity_mode == 0) { |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1117 | #if FBCON_DISPLAY_MSG |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1118 | #if ENABLE_VB_ATTEST |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1119 | display_bootverify_menu(DISPLAY_MENU_EIO); |
| 1120 | wait_for_users_action(); |
| 1121 | if(!pwr_key_is_pressed) |
| 1122 | shutdown_device(); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1123 | #else |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1124 | display_bootverify_menu(DISPLAY_MENU_LOGGING); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1125 | #endif |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1126 | wait_for_users_action(); |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1127 | #else |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1128 | dprintf(CRITICAL, |
| 1129 | "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n"); |
| 1130 | mdelay(5000); |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1131 | #endif |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1132 | } |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1133 | } |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1134 | #endif |
| 1135 | |
| 1136 | #if VERIFIED_BOOT |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 1137 | /* Write protect the device info */ |
Channagoud Kadabi | 3bd9d1e | 2015-05-05 16:18:20 -0700 | [diff] [blame] | 1138 | 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] | 1139 | { |
| 1140 | dprintf(INFO, "Failed to write protect dev info\n"); |
| 1141 | ASSERT(0); |
| 1142 | } |
| 1143 | #endif |
| 1144 | |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 1145 | /* Turn off splash screen if enabled */ |
| 1146 | #if DISPLAY_SPLASH_SCREEN |
| 1147 | target_display_shutdown(); |
| 1148 | #endif |
| 1149 | |
Veera Sundaram Sankaran | 67ea093 | 2015-09-25 10:09:30 -0700 | [diff] [blame] | 1150 | /* Perform target specific cleanup */ |
| 1151 | target_uninit(); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1152 | free_verified_boot_resource(&info); |
| 1153 | if (final_cmdline) |
| 1154 | free(final_cmdline); |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 1155 | |
Deepa Dinamani | 33734bc | 2013-03-06 12:16:06 -0800 | [diff] [blame] | 1156 | 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] | 1157 | entry, ramdisk, ramdisk_size, (void *)tags_phys); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1158 | |
| 1159 | enter_critical_section(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1160 | |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 1161 | /* do any platform specific cleanup before kernel entry */ |
| 1162 | platform_uninit(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1163 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1164 | arch_disable_cache(UCACHE); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1165 | |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 1166 | #if ARM_WITH_MMU |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1167 | arch_disable_mmu(); |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 1168 | #endif |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 1169 | bs_set_timestamp(BS_KERNEL_ENTRY); |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1170 | |
| 1171 | if (IS_ARM64(kptr)) |
| 1172 | /* Jump to a 64bit kernel */ |
| 1173 | scm_elexec_call((paddr_t)kernel, tags_phys); |
| 1174 | else |
| 1175 | /* Jump to a 32bit kernel */ |
| 1176 | entry(0, machtype, (unsigned*)tags_phys); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1179 | /* Function to check if the memory address range falls within the aboot |
| 1180 | * boundaries. |
| 1181 | * start: Start of the memory region |
| 1182 | * size: Size of the memory region |
| 1183 | */ |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 1184 | int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1185 | { |
| 1186 | /* Check for boundary conditions. */ |
Sundarajan Srinivasan | ce2a0ea | 2013-12-16 17:02:56 -0800 | [diff] [blame] | 1187 | if ((UINT_MAX - start) < size) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1188 | return -1; |
| 1189 | |
| 1190 | /* Check for memory overlap. */ |
| 1191 | if ((start < MEMBASE) && ((start + size) <= MEMBASE)) |
| 1192 | return 0; |
Channagoud Kadabi | 9414391 | 2013-10-15 12:53:52 -0700 | [diff] [blame] | 1193 | else if (start >= (MEMBASE + MEMSIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1194 | return 0; |
| 1195 | else |
| 1196 | return -1; |
| 1197 | } |
| 1198 | |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 1199 | /* Function to check if the memory address range falls beyond ddr region. |
| 1200 | * start: Start of the memory region |
| 1201 | * size: Size of the memory region |
| 1202 | */ |
| 1203 | int check_ddr_addr_range_bound(uintptr_t start, uint32_t size) |
| 1204 | { |
| 1205 | uintptr_t ddr_pa_start_addr = PA(get_ddr_start()); |
| 1206 | uint64_t ddr_size = smem_get_ddr_size(); |
| 1207 | uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size; |
| 1208 | uintptr_t pa_start_addr = PA(start); |
| 1209 | |
| 1210 | /* Check for boundary conditions. */ |
| 1211 | if ((UINT_MAX - start) < size) |
| 1212 | return -1; |
| 1213 | |
| 1214 | /* Check if memory range is beyond the ddr range. */ |
| 1215 | if (pa_start_addr < ddr_pa_start_addr || |
| 1216 | pa_start_addr >= (ddr_pa_end_addr) || |
| 1217 | (pa_start_addr + size) > ddr_pa_end_addr) |
| 1218 | return -1; |
| 1219 | else |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1223 | BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1224 | |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1225 | int getimage(void **image_buffer, uint32_t *imgsize, |
| 1226 | const char *imgname) |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1227 | { |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1228 | uint32_t loadedindex; |
| 1229 | if (image_buffer == NULL || imgsize == NULL || |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1230 | imgname == NULL) { |
| 1231 | dprintf(CRITICAL, "getimage: invalid parameters\n"); |
| 1232 | return -1; |
| 1233 | } |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1234 | for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) { |
| 1235 | if (!strncmp(info.images[loadedindex].name, imgname, |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1236 | strlen(imgname))) { |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1237 | *image_buffer = info.images[loadedindex].image_buffer; |
| 1238 | *imgsize = info.images[loadedindex].imgsize; |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 1239 | dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n", |
| 1240 | info.images[loadedindex].name, |
| 1241 | info.images[loadedindex].imgsize); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1242 | return 0; |
| 1243 | } |
| 1244 | } |
| 1245 | return -1; |
| 1246 | } |
| 1247 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1248 | static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size) |
| 1249 | { |
| 1250 | int ret; |
Channagoud Kadabi | a8c623f | 2015-01-13 14:48:48 -0800 | [diff] [blame] | 1251 | |
| 1252 | #if !VERIFIED_BOOT |
Sundarajan Srinivasan | ce54d6e | 2013-11-11 12:45:00 -0800 | [diff] [blame] | 1253 | #if IMAGE_VERIF_ALGO_SHA1 |
| 1254 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1; |
| 1255 | #else |
| 1256 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256; |
| 1257 | #endif |
Channagoud Kadabi | a8c623f | 2015-01-13 14:48:48 -0800 | [diff] [blame] | 1258 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1259 | |
| 1260 | /* Assume device is rooted at this time. */ |
| 1261 | device.is_tampered = 1; |
| 1262 | |
| 1263 | dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size); |
| 1264 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1265 | #if VERIFIED_BOOT |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 1266 | uint32_t bootstate; |
| 1267 | if(boot_into_recovery && |
Mayank Grover | 68fbf0d | 2017-10-24 14:13:39 +0530 | [diff] [blame] | 1268 | (!partition_multislot_is_supported())) |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 1269 | { |
| 1270 | ret = boot_verify_image((unsigned char *)bootimg_addr, |
| 1271 | bootimg_size, "/recovery", &bootstate); |
| 1272 | } |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1273 | else |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 1274 | { |
| 1275 | ret = boot_verify_image((unsigned char *)bootimg_addr, |
| 1276 | bootimg_size, "/boot", &bootstate); |
| 1277 | } |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1278 | boot_verify_print_state(); |
| 1279 | #else |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1280 | ret = image_verify((unsigned char *)bootimg_addr, |
| 1281 | (unsigned char *)(bootimg_addr + bootimg_size), |
| 1282 | bootimg_size, |
Sundarajan Srinivasan | ce54d6e | 2013-11-11 12:45:00 -0800 | [diff] [blame] | 1283 | auth_algo); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1284 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1285 | dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret); |
| 1286 | |
| 1287 | if (ret) |
| 1288 | { |
| 1289 | /* Authorized kernel */ |
| 1290 | device.is_tampered = 0; |
Sundarajan Srinivasan | 3fb21f1 | 2013-09-16 18:36:15 -0700 | [diff] [blame] | 1291 | auth_kernel_img = 1; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 1294 | #ifdef MDTP_SUPPORT |
| 1295 | { |
| 1296 | /* Verify MDTP lock. |
| 1297 | * For boot & recovery partitions, use aboot's verification result. |
| 1298 | */ |
| 1299 | mdtp_ext_partition_verification_t ext_partition; |
| 1300 | ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT; |
| 1301 | ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID; |
| 1302 | ext_partition.page_size = 0; /* Not needed since already validated */ |
| 1303 | ext_partition.image_addr = 0; /* Not needed since already validated */ |
| 1304 | ext_partition.image_size = 0; /* Not needed since already validated */ |
| 1305 | ext_partition.sig_avail = FALSE; /* Not needed since already validated */ |
| 1306 | mdtp_fwlock_verify_lock(&ext_partition); |
| 1307 | } |
| 1308 | #endif /* MDTP_SUPPORT */ |
| 1309 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1310 | #if USE_PCOM_SECBOOT |
| 1311 | set_tamper_flag(device.is_tampered); |
| 1312 | #endif |
| 1313 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1314 | #if VERIFIED_BOOT |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1315 | switch(boot_verify_get_state()) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1316 | { |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1317 | case RED: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1318 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 1319 | display_bootverify_menu(DISPLAY_MENU_RED); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1320 | #if ENABLE_VB_ATTEST |
| 1321 | mdelay(DELAY_WAIT); |
| 1322 | shutdown_device(); |
| 1323 | #else |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1324 | wait_for_users_action(); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1325 | #endif |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1326 | #else |
| 1327 | dprintf(CRITICAL, |
| 1328 | "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n"); |
| 1329 | mdelay(5000); |
| 1330 | #endif |
| 1331 | |
| 1332 | break; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1333 | case YELLOW: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1334 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 1335 | display_bootverify_menu(DISPLAY_MENU_YELLOW); |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1336 | wait_for_users_action(); |
| 1337 | #else |
| 1338 | dprintf(CRITICAL, |
| 1339 | "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n"); |
| 1340 | mdelay(5000); |
| 1341 | #endif |
| 1342 | break; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1343 | default: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1344 | break; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1345 | } |
| 1346 | #endif |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1347 | #if !VERIFIED_BOOT |
Unnati Gandhi | 1be0475 | 2015-03-27 19:41:53 +0530 | [diff] [blame] | 1348 | if(device.is_tampered) |
| 1349 | { |
| 1350 | write_device_info_mmc(&device); |
| 1351 | #ifdef TZ_TAMPER_FUSE |
Monika Singh | 9825746 | 2018-06-06 11:28:49 +0530 | [diff] [blame] | 1352 | set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE); |
Unnati Gandhi | 1be0475 | 2015-03-27 19:41:53 +0530 | [diff] [blame] | 1353 | #endif |
| 1354 | #ifdef ASSERT_ON_TAMPER |
| 1355 | dprintf(CRITICAL, "Device is tampered. Asserting..\n"); |
| 1356 | ASSERT(0); |
| 1357 | #endif |
| 1358 | } |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1359 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1362 | static bool check_format_bit() |
| 1363 | { |
| 1364 | bool ret = false; |
| 1365 | int index; |
| 1366 | uint64_t offset; |
| 1367 | struct boot_selection_info *in = NULL; |
| 1368 | char *buf = NULL; |
| 1369 | |
| 1370 | index = partition_get_index("bootselect"); |
| 1371 | if (index == INVALID_PTN) |
| 1372 | { |
| 1373 | dprintf(INFO, "Unable to locate /bootselect partition\n"); |
| 1374 | return ret; |
| 1375 | } |
| 1376 | offset = partition_get_offset(index); |
| 1377 | if(!offset) |
| 1378 | { |
| 1379 | dprintf(INFO, "partition /bootselect doesn't exist\n"); |
| 1380 | return ret; |
| 1381 | } |
| 1382 | buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE)); |
Mayank Grover | 4886040 | 2016-11-29 12:34:53 +0530 | [diff] [blame] | 1383 | mmc_set_lun(partition_get_lun(index)); |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1384 | ASSERT(buf); |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1385 | if (mmc_read(offset, (uint32_t *)buf, page_size)) |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1386 | { |
| 1387 | dprintf(INFO, "mmc read failure /bootselect %d\n", page_size); |
| 1388 | free(buf); |
| 1389 | return ret; |
| 1390 | } |
| 1391 | in = (struct boot_selection_info *) buf; |
| 1392 | if ((in->signature == BOOTSELECT_SIGNATURE) && |
| 1393 | (in->version == BOOTSELECT_VERSION)) { |
| 1394 | if ((in->state_info & BOOTSELECT_FORMAT) && |
| 1395 | !(in->state_info & BOOTSELECT_FACTORY)) |
| 1396 | ret = true; |
| 1397 | } else { |
| 1398 | dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n", |
| 1399 | in->signature, in->version); |
| 1400 | ASSERT(0); |
| 1401 | } |
| 1402 | free(buf); |
| 1403 | return ret; |
| 1404 | } |
| 1405 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1406 | void boot_verifier_init() |
| 1407 | { |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1408 | uint32_t boot_state; |
| 1409 | /* Check if device unlock */ |
| 1410 | if(device.is_unlocked) |
| 1411 | { |
| 1412 | boot_verify_send_event(DEV_UNLOCK); |
| 1413 | boot_verify_print_state(); |
| 1414 | dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n"); |
| 1415 | return; |
| 1416 | } |
| 1417 | else |
| 1418 | { |
| 1419 | boot_verify_send_event(BOOT_INIT); |
| 1420 | } |
| 1421 | |
| 1422 | /* Initialize keystore */ |
| 1423 | boot_state = boot_verify_keystore_init(); |
| 1424 | if(boot_state == YELLOW) |
| 1425 | { |
| 1426 | boot_verify_print_state(); |
| 1427 | dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n"); |
| 1428 | } |
| 1429 | } |
| 1430 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1431 | /* Function to return recovery appended dtbo buffer info */ |
| 1432 | void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf) |
| 1433 | { |
| 1434 | *dtbo_size = recovery_dtbo_size; |
| 1435 | *dtbo_buf = recovery_dtbo_buf; |
| 1436 | return; |
| 1437 | } |
| 1438 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1439 | int boot_linux_from_mmc(void) |
| 1440 | { |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1441 | boot_img_hdr *hdr = (void*) buf; |
| 1442 | boot_img_hdr *uhdr; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1443 | unsigned offset = 0; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 1444 | int rcode; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1445 | unsigned long long ptn = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1446 | int index = INVALID_PTN; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1447 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1448 | unsigned char *image_addr = 0; |
| 1449 | unsigned kernel_actual; |
| 1450 | unsigned ramdisk_actual; |
| 1451 | unsigned imagesize_actual; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1452 | unsigned second_actual = 0; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1453 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1454 | unsigned int dtb_size = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1455 | unsigned int out_len = 0; |
| 1456 | unsigned int out_avai_len = 0; |
| 1457 | unsigned char *out_addr = NULL; |
| 1458 | uint32_t dtb_offset = 0; |
| 1459 | unsigned char *kernel_start_addr = NULL; |
| 1460 | unsigned int kernel_size = 0; |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 1461 | unsigned int patched_kernel_hdr_size = 0; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1462 | uint64_t image_size = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1463 | int rc; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1464 | #if VERIFIED_BOOT_2 |
| 1465 | int status; |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 1466 | void *dtbo_image_buf = NULL; |
| 1467 | uint32_t dtbo_image_sz = 0; |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 1468 | void *vbmeta_image_buf = NULL; |
| 1469 | uint32_t vbmeta_image_sz = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1470 | #endif |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1471 | char *ptn_name = NULL; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1472 | #if DEVICE_TREE |
| 1473 | struct dt_table *table; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1474 | struct dt_entry dt_entry; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1475 | unsigned dt_table_offset; |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1476 | uint32_t dt_actual; |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 1477 | uint32_t dt_hdr_size; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1478 | unsigned char *best_match_dt_addr = NULL; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1479 | #endif |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1480 | struct kernel64_hdr *kptr = NULL; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1481 | int current_active_slot = INVALID; |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1482 | |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1483 | if (check_format_bit()) |
| 1484 | boot_into_recovery = 1; |
| 1485 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 1486 | if (!boot_into_recovery) { |
| 1487 | memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string)); |
| 1488 | rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string)); |
| 1489 | if (rcode <= 0) { |
| 1490 | boot_into_ffbm = false; |
| 1491 | if (rcode < 0) |
| 1492 | dprintf(CRITICAL,"failed to get ffbm cookie"); |
| 1493 | } else |
| 1494 | boot_into_ffbm = true; |
| 1495 | } else |
| 1496 | boot_into_ffbm = false; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1497 | uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1498 | if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 1499 | dprintf(INFO, "Unified boot method!\n"); |
| 1500 | hdr = uhdr; |
| 1501 | goto unified_boot; |
| 1502 | } |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1503 | |
| 1504 | /* For a/b recovery image code is on boot partition. |
| 1505 | If we support multislot, always use boot partition. */ |
| 1506 | if (boot_into_recovery && |
| 1507 | (!partition_multislot_is_supported())) |
| 1508 | ptn_name = "recovery"; |
| 1509 | else |
| 1510 | ptn_name = "boot"; |
| 1511 | |
| 1512 | index = partition_get_index(ptn_name); |
| 1513 | ptn = partition_get_offset(index); |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1514 | image_size = partition_get_size(index); |
| 1515 | if(ptn == 0 || image_size == 0) { |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1516 | dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name); |
| 1517 | return -1; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1518 | } |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1519 | |
Channagoud Kadabi | ef0547c | 2015-02-10 12:57:38 -0800 | [diff] [blame] | 1520 | /* Set Lun for boot & recovery partitions */ |
| 1521 | mmc_set_lun(partition_get_lun(index)); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1522 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1523 | if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) { |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1524 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 1525 | return -1; |
| 1526 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1527 | |
| 1528 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 1529 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1530 | return ERR_INVALID_BOOT_MAGIC; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 1533 | if (hdr->page_size && (hdr->page_size != page_size)) { |
vijay kumar | 2e21b3a | 2014-06-26 17:40:15 +0530 | [diff] [blame] | 1534 | |
| 1535 | if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) { |
| 1536 | dprintf(CRITICAL, "ERROR: Invalid page size\n"); |
| 1537 | return -1; |
| 1538 | } |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 1539 | page_size = hdr->page_size; |
| 1540 | page_mask = page_size - 1; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1543 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 1544 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1545 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1546 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1547 | image_addr = (unsigned char *)target_get_scratch_address(); |
Mayank Grover | 6fd3679 | 2018-09-10 13:26:58 +0530 | [diff] [blame] | 1548 | #if VERIFIED_BOOT_2 |
| 1549 | /* Create hole in start of image for VB salt to copy */ |
| 1550 | image_addr += SALT_BUFF_OFFSET; |
| 1551 | #endif |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 1552 | memcpy(image_addr, (void *)buf, page_size); |
| 1553 | |
| 1554 | /* ensure commandline is terminated */ |
| 1555 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1556 | |
| 1557 | #if DEVICE_TREE |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 1558 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 1559 | dt_size = hdr->dt_size; |
Mayank Grover | 5a50258 | 2018-09-12 11:24:49 +0530 | [diff] [blame] | 1560 | #else |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1561 | dprintf(INFO, "BootImage Header: %d\n", hdr->header_version); |
Mayank Grover | 5a50258 | 2018-09-12 11:24:49 +0530 | [diff] [blame] | 1562 | #endif |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1563 | |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 1564 | dt_actual = ROUND_TO_PAGE(dt_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1565 | if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + (uint64_t)dt_actual + page_size)) { |
Vijay Kumar Pendoti | 208f962 | 2016-06-09 19:34:01 +0530 | [diff] [blame] | 1566 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__); |
| 1567 | return -1; |
| 1568 | } |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1569 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1570 | #else |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1571 | if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual + (uint64_t)second_actual + page_size)) { |
Vijay Kumar Pendoti | 208f962 | 2016-06-09 19:34:01 +0530 | [diff] [blame] | 1572 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__); |
| 1573 | return -1; |
| 1574 | } |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1575 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1576 | #endif |
| 1577 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1578 | #ifdef OSVERSION_IN_BOOTIMAGE |
| 1579 | /* If header version is ONE and booting into recovery, |
| 1580 | dtbo is appended with recovery image. |
| 1581 | Doing following: |
| 1582 | * Validating the recovery offset and size. |
| 1583 | * Extracting recovery dtbo to be used as dtbo. |
| 1584 | */ |
| 1585 | if (boot_into_recovery && |
| 1586 | hdr->header_version == BOOT_HEADER_VERSION_ONE) |
| 1587 | { |
| 1588 | struct boot_img_hdr_v1 *hdr1 = |
| 1589 | (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr)); |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1590 | unsigned int recovery_dtbo_actual = 0; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1591 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1592 | recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask); |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1593 | if ((hdr1->header_size != |
| 1594 | sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr))) |
| 1595 | { |
| 1596 | dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size); |
| 1597 | return -1; |
| 1598 | } |
| 1599 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1600 | if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF) |
| 1601 | { |
| 1602 | dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual, |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1603 | MAX_SUPPORTED_DTBO_IMG_BUF); |
| 1604 | return -1; |
| 1605 | } |
| 1606 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1607 | if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual)) |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1608 | { |
| 1609 | dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__); |
| 1610 | return -1; |
| 1611 | } |
| 1612 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1613 | if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) { |
| 1614 | dprintf(CRITICAL, |
| 1615 | "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__); |
| 1616 | return -1; |
| 1617 | } |
| 1618 | |
| 1619 | if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size) |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1620 | { |
| 1621 | dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx," |
| 1622 | " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n", |
| 1623 | hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size); |
| 1624 | return -1; |
| 1625 | } |
| 1626 | |
| 1627 | recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr); |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1628 | recovery_dtbo_size = recovery_dtbo_actual; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1629 | imagesize_actual += recovery_dtbo_size; |
| 1630 | |
| 1631 | dprintf(SPEW, "Header version: %d\n", hdr->header_version); |
| 1632 | dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size); |
| 1633 | dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset); |
| 1634 | |
| 1635 | } |
| 1636 | #endif |
| 1637 | |
Saranya Chidura | b1aaf23 | 2018-11-19 15:47:50 +0530 | [diff] [blame] | 1638 | /* Validate the boot/recovery image size is within the bounds of partition size */ |
| 1639 | if (imagesize_actual > image_size) { |
| 1640 | dprintf(CRITICAL, "Image size is greater than partition size.\n"); |
| 1641 | return -1; |
| 1642 | } |
| 1643 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1644 | #if VERIFIED_BOOT |
| 1645 | boot_verifier_init(); |
| 1646 | #endif |
| 1647 | |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 1648 | if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual)) |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1649 | { |
| 1650 | dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n"); |
| 1651 | return -1; |
| 1652 | } |
| 1653 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1654 | /* |
| 1655 | * Update loading flow of bootimage to support compressed/uncompressed |
| 1656 | * bootimage on both 64bit and 32bit platform. |
| 1657 | * 1. Load bootimage from emmc partition onto DDR. |
| 1658 | * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel |
| 1659 | * 3. Check kernel header and update kernel load addr for 64bit and 32bit |
| 1660 | * platform accordingly. |
| 1661 | * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data. |
| 1662 | */ |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1663 | if (partition_multislot_is_supported()) |
| 1664 | { |
| 1665 | current_active_slot = partition_find_active_slot(); |
| 1666 | dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n", |
| 1667 | imagesize_actual, SUFFIX_SLOT(current_active_slot)); |
| 1668 | } |
| 1669 | else |
| 1670 | { |
| 1671 | dprintf(INFO, "Loading (%s) image (%d): start\n", |
| 1672 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
| 1673 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1674 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
| 1675 | |
Gaurav Nebhwani | 43e2a46 | 2016-03-17 21:32:56 +0530 | [diff] [blame] | 1676 | if ((target_get_max_flash_size() - page_size) < imagesize_actual) |
| 1677 | { |
| 1678 | dprintf(CRITICAL, "booimage size is greater than DDR can hold\n"); |
| 1679 | return -1; |
| 1680 | } |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 1681 | offset = page_size; |
| 1682 | /* Read image without signature and header*/ |
| 1683 | if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size)) |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1684 | { |
| 1685 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 1686 | return -1; |
| 1687 | } |
| 1688 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1689 | if (partition_multislot_is_supported()) |
| 1690 | { |
| 1691 | dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n", |
| 1692 | imagesize_actual, SUFFIX_SLOT(current_active_slot)); |
| 1693 | } |
| 1694 | else |
| 1695 | { |
| 1696 | dprintf(INFO, "Loading (%s) image (%d): done\n", |
Channagoud Kadabi | f0705b5 | 2015-08-20 14:16:08 -0700 | [diff] [blame] | 1697 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
| 1698 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1699 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1700 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
| 1701 | |
| 1702 | /* Authenticate Kernel */ |
| 1703 | dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n", |
| 1704 | (int) target_use_signed_kernel(), |
| 1705 | device.is_unlocked, |
| 1706 | device.is_tampered); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1707 | #if VERIFIED_BOOT_2 |
| 1708 | offset = imagesize_actual; |
| 1709 | if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size)) |
| 1710 | { |
| 1711 | dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n"); |
| 1712 | return -1; |
| 1713 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1714 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1715 | /* Read signature */ |
| 1716 | if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size)) |
| 1717 | { |
| 1718 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 1719 | return -1; |
| 1720 | } |
| 1721 | |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 1722 | /* load and validate dtbo partition */ |
| 1723 | load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz); |
| 1724 | |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 1725 | /* load vbmeta partition */ |
| 1726 | load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz); |
| 1727 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1728 | memset(&info, 0, sizeof(bootinfo)); |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 1729 | |
| 1730 | /* Pass loaded boot image passed */ |
Mayank Grover | 6fd3679 | 2018-09-10 13:26:58 +0530 | [diff] [blame] | 1731 | info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr); |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 1732 | info.images[IMG_BOOT].imgsize = imagesize_actual; |
| 1733 | info.images[IMG_BOOT].name = ptn_name; |
| 1734 | ++info.num_loaded_images; |
| 1735 | |
| 1736 | /* Pass loaded dtbo image */ |
| 1737 | if (dtbo_image_buf != NULL) { |
Mayank Grover | 6fd3679 | 2018-09-10 13:26:58 +0530 | [diff] [blame] | 1738 | info.images[IMG_DTBO].image_buffer = |
| 1739 | SUB_SALT_BUFF_OFFSET(dtbo_image_buf); |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 1740 | info.images[IMG_DTBO].imgsize = dtbo_image_sz; |
| 1741 | info.images[IMG_DTBO].name = "dtbo"; |
| 1742 | ++info.num_loaded_images; |
| 1743 | } |
| 1744 | |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 1745 | /* Pass loaded vbmeta image */ |
| 1746 | if (vbmeta_image_buf != NULL) { |
| 1747 | info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf; |
| 1748 | info.images[IMG_VBMETA].imgsize = vbmeta_image_sz; |
| 1749 | info.images[IMG_VBMETA].name = "vbmeta"; |
| 1750 | ++info.num_loaded_images; |
| 1751 | } |
| 1752 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1753 | info.header_version = hdr->header_version; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1754 | info.multi_slot_boot = partition_multislot_is_supported(); |
| 1755 | info.bootreason_alarm = boot_reason_alarm; |
| 1756 | info.bootinto_recovery = boot_into_recovery; |
| 1757 | status = load_image_and_auth(&info); |
| 1758 | if(status) |
| 1759 | return -1; |
| 1760 | |
| 1761 | vbcmdline = info.vbcmdline; |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 1762 | |
| 1763 | /* Free the buffer allocated to vbmeta post verification */ |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 1764 | if (vbmeta_image_buf != NULL) { |
| 1765 | free(vbmeta_image_buf); |
| 1766 | --info.num_loaded_images; |
| 1767 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1768 | #else |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 1769 | /* Change the condition a little bit to include the test framework support. |
| 1770 | * We would never reach this point if device is in fastboot mode, even if we did |
| 1771 | * that means we are in test mode, so execute kernel authentication part for the |
| 1772 | * tests */ |
Parth Dixit | cb0c608 | 2015-12-30 15:01:13 +0530 | [diff] [blame] | 1773 | if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled()) |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1774 | { |
| 1775 | offset = imagesize_actual; |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 1776 | 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] | 1777 | { |
| 1778 | dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n"); |
| 1779 | return -1; |
| 1780 | } |
| 1781 | |
| 1782 | /* Read signature */ |
| 1783 | if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size)) |
| 1784 | { |
| 1785 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 1786 | return -1; |
| 1787 | } |
| 1788 | |
| 1789 | verify_signed_bootimg((uint32_t)image_addr, imagesize_actual); |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 1790 | /* The purpose of our test is done here */ |
Parth Dixit | cb0c608 | 2015-12-30 15:01:13 +0530 | [diff] [blame] | 1791 | if(is_test_mode_enabled() && auth_kernel_img) |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 1792 | return 0; |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1793 | } else { |
| 1794 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 1795 | #ifdef TZ_SAVE_KERNEL_HASH |
| 1796 | aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual); |
| 1797 | #endif /* TZ_SAVE_KERNEL_HASH */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 1798 | |
| 1799 | #ifdef MDTP_SUPPORT |
| 1800 | { |
| 1801 | /* Verify MDTP lock. |
| 1802 | * For boot & recovery partitions, MDTP will use boot_verifier APIs, |
| 1803 | * since verification was skipped in aboot. The signature is not part of the loaded image. |
| 1804 | */ |
| 1805 | mdtp_ext_partition_verification_t ext_partition; |
| 1806 | ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT; |
| 1807 | ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET; |
| 1808 | ext_partition.page_size = page_size; |
| 1809 | ext_partition.image_addr = (uint32)image_addr; |
| 1810 | ext_partition.image_size = imagesize_actual; |
| 1811 | ext_partition.sig_avail = FALSE; |
| 1812 | mdtp_fwlock_verify_lock(&ext_partition); |
| 1813 | } |
| 1814 | #endif /* MDTP_SUPPORT */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1815 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1816 | #endif |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1817 | |
Sridhar Parasuram | 00bfedb | 2015-05-26 14:21:27 -0700 | [diff] [blame] | 1818 | #if VERIFIED_BOOT |
taozhang | 93088bd | 2016-11-16 15:50:46 +0800 | [diff] [blame] | 1819 | if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm)) |
Reut Zysman | ba8a9d5 | 2016-02-18 11:44:04 +0200 | [diff] [blame] | 1820 | { |
| 1821 | #if FBCON_DISPLAY_MSG |
| 1822 | display_bootverify_menu(DISPLAY_MENU_ORANGE); |
| 1823 | wait_for_users_action(); |
| 1824 | #else |
| 1825 | dprintf(CRITICAL, |
| 1826 | "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n"); |
| 1827 | mdelay(5000); |
| 1828 | #endif |
| 1829 | } |
| 1830 | #endif |
| 1831 | |
| 1832 | #if VERIFIED_BOOT |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1833 | if (VB_M == target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1834 | { |
| 1835 | /* set boot and system versions. */ |
| 1836 | set_os_version((unsigned char *)image_addr); |
| 1837 | // send root of trust |
| 1838 | if(!send_rot_command((uint32_t)device.is_unlocked)) |
| 1839 | ASSERT(0); |
| 1840 | } |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 1841 | #endif |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 1842 | /* |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1843 | * Check if the kernel image is a gzip package. If yes, need to decompress it. |
| 1844 | * If not, continue booting. |
| 1845 | */ |
| 1846 | if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size)) |
| 1847 | { |
| 1848 | out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size); |
| 1849 | out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size; |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 1850 | #if VERIFIED_BOOT_2 |
| 1851 | if (dtbo_image_sz) |
| 1852 | out_avai_len -= DTBO_IMG_BUF; |
| 1853 | #endif |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 1854 | dprintf(INFO, "decompressing kernel image: start\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1855 | rc = decompress((unsigned char *)(image_addr + page_size), |
| 1856 | hdr->kernel_size, out_addr, out_avai_len, |
| 1857 | &dtb_offset, &out_len); |
| 1858 | if (rc) |
| 1859 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 1860 | dprintf(CRITICAL, "decompressing kernel image failed!!!\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1861 | ASSERT(0); |
| 1862 | } |
| 1863 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 1864 | dprintf(INFO, "decompressing kernel image: done\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1865 | kptr = (struct kernel64_hdr *)out_addr; |
| 1866 | kernel_start_addr = out_addr; |
| 1867 | kernel_size = out_len; |
| 1868 | } else { |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 1869 | dprintf(INFO, "Uncpmpressed kernel in use\n"); |
| 1870 | if (!strncmp((char*)(image_addr + page_size), |
| 1871 | PATCHED_KERNEL_MAGIC, |
| 1872 | sizeof(PATCHED_KERNEL_MAGIC) - 1)) { |
| 1873 | dprintf(INFO, "Patched kernel detected\n"); |
| 1874 | kptr = (struct kernel64_hdr *)(image_addr + page_size + |
| 1875 | PATCHED_KERNEL_HEADER_SIZE); |
| 1876 | //The size of the kernel is stored at start of kernel image + 16 |
| 1877 | //The dtb would start just after the kernel |
| 1878 | dtb_offset = *((uint32_t*)((unsigned char*) |
| 1879 | (image_addr + page_size + |
| 1880 | sizeof(PATCHED_KERNEL_MAGIC) - |
| 1881 | 1))); |
| 1882 | //The actual kernel starts after the 20 byte header. |
| 1883 | kernel_start_addr = (unsigned char*)(image_addr + |
| 1884 | page_size + PATCHED_KERNEL_HEADER_SIZE); |
| 1885 | kernel_size = hdr->kernel_size; |
| 1886 | patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE; |
| 1887 | } else { |
| 1888 | dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n"); |
| 1889 | kptr = (struct kernel64_hdr *)(image_addr + page_size); |
| 1890 | kernel_start_addr = (unsigned char *)(image_addr + page_size); |
| 1891 | kernel_size = hdr->kernel_size; |
| 1892 | } |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | /* |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 1896 | * Update the kernel/ramdisk/tags address if the boot image header |
| 1897 | * has default values, these default values come from mkbootimg when |
| 1898 | * the boot image is flashed using fastboot flash:raw |
| 1899 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1900 | update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr)); |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 1901 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1902 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 1903 | hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr)); |
| 1904 | hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr)); |
| 1905 | hdr->tags_addr = VA((addr_t)(hdr->tags_addr)); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1906 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1907 | kernel_size = ROUND_TO_PAGE(kernel_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1908 | /* Check if the addresses in the header are valid. */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1909 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) || |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 1910 | check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) || |
| 1911 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) || |
| 1912 | check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1913 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 1914 | dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1915 | return -1; |
| 1916 | } |
| 1917 | |
| 1918 | #ifndef DEVICE_TREE |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 1919 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) || |
| 1920 | check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1921 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 1922 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1923 | return -1; |
| 1924 | } |
| 1925 | #endif |
| 1926 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1927 | /* Move kernel, ramdisk and device tree to correct address */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1928 | memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1929 | 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] | 1930 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1931 | #if DEVICE_TREE |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 1932 | if(dt_size) { |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1933 | dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual); |
| 1934 | table = (struct dt_table*) dt_table_offset; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1935 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1936 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
| 1937 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 1938 | return -1; |
| 1939 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1940 | |
P.V. Phani Kumar | 8291676 | 2016-03-09 09:20:19 +0530 | [diff] [blame] | 1941 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 1942 | goes beyound hdr->dt_size*/ |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 1943 | if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) { |
P.V. Phani Kumar | 8291676 | 2016-03-09 09:20:19 +0530 | [diff] [blame] | 1944 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 1945 | return -1; |
| 1946 | } |
| 1947 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1948 | /* Find index of device tree within device tree table */ |
| 1949 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
| 1950 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 1951 | return -1; |
| 1952 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1953 | |
P.V. Phani Kumar | 3c33320 | 2016-03-09 11:54:37 +0530 | [diff] [blame] | 1954 | if(dt_entry.offset > (UINT_MAX - dt_entry.size)) { |
| 1955 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 1956 | return -1; |
| 1957 | } |
| 1958 | |
| 1959 | /* Ensure we are not overshooting dt_size with the dt_entry selected */ |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 1960 | if ((dt_entry.offset + dt_entry.size) > dt_size) { |
P.V. Phani Kumar | 3c33320 | 2016-03-09 11:54:37 +0530 | [diff] [blame] | 1961 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 1962 | return -1; |
| 1963 | } |
| 1964 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1965 | if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size)) |
| 1966 | { |
| 1967 | unsigned int compressed_size = 0; |
| 1968 | out_addr += out_len; |
| 1969 | out_avai_len -= out_len; |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 1970 | dprintf(INFO, "decompressing dtb: start\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1971 | rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset, |
| 1972 | dt_entry.size, out_addr, out_avai_len, |
| 1973 | &compressed_size, &dtb_size); |
| 1974 | if (rc) |
| 1975 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 1976 | dprintf(CRITICAL, "decompressing dtb failed!!!\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1977 | ASSERT(0); |
| 1978 | } |
| 1979 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 1980 | dprintf(INFO, "decompressing dtb: done\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1981 | best_match_dt_addr = out_addr; |
| 1982 | } else { |
| 1983 | best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset; |
| 1984 | dtb_size = dt_entry.size; |
| 1985 | } |
| 1986 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1987 | /* Validate and Read device device tree in the tags_addr */ |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 1988 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) || |
| 1989 | check_ddr_addr_range_bound(hdr->tags_addr, dtb_size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1990 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 1991 | dprintf(CRITICAL, "Device tree addresses are not valid\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1992 | return -1; |
| 1993 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1994 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1995 | memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1996 | } else { |
| 1997 | /* Validate the tags_addr */ |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 1998 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) || |
| 1999 | check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual)) |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 2000 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2001 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 2002 | return -1; |
| 2003 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2004 | /* |
| 2005 | * If appended dev tree is found, update the atags with |
| 2006 | * memory address to the DTB appended location on RAM. |
| 2007 | * Else update with the atags address in the kernel header |
Mayank Grover | 1ed3e8b | 2018-09-19 15:16:50 +0530 | [diff] [blame] | 2008 | * |
| 2009 | * Make sure everything from scratch address is read before next step! |
| 2010 | * In case of dtbo, this API is going to read dtbo on scratch. |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2011 | */ |
| 2012 | void *dtb; |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 2013 | dtb = dev_tree_appended( |
| 2014 | (void*)(image_addr + page_size + |
| 2015 | patched_kernel_hdr_size), |
| 2016 | hdr->kernel_size, dtb_offset, |
| 2017 | (void *)hdr->tags_addr); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2018 | if (!dtb) { |
| 2019 | dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n"); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 2020 | return -1; |
| 2021 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2022 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2023 | #endif |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2024 | |
Stanimir Varbanov | 69ec546 | 2013-07-18 18:17:42 +0300 | [diff] [blame] | 2025 | if (boot_into_recovery && !device.is_unlocked && !device.is_tampered) |
| 2026 | target_load_ssd_keystore(); |
| 2027 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2028 | unified_boot: |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2029 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 2030 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Dima Zavin | bd3daa0 | 2013-03-26 11:06:44 -0700 | [diff] [blame] | 2031 | (const char *)hdr->cmdline, board_machtype(), |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2032 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 2033 | |
| 2034 | return 0; |
| 2035 | } |
| 2036 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2037 | int boot_linux_from_flash(void) |
| 2038 | { |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2039 | boot_img_hdr *hdr = (void*) buf; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2040 | struct ptentry *ptn; |
| 2041 | struct ptable *ptable; |
| 2042 | unsigned offset = 0; |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 2043 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2044 | unsigned char *image_addr = 0; |
| 2045 | unsigned kernel_actual; |
| 2046 | unsigned ramdisk_actual; |
| 2047 | unsigned imagesize_actual; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2048 | unsigned second_actual = 0; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2049 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2050 | #if DEVICE_TREE |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2051 | struct dt_table *table = NULL; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 2052 | struct dt_entry dt_entry; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2053 | unsigned dt_table_offset; |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 2054 | uint32_t dt_actual; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2055 | uint32_t dt_hdr_size = 0; |
Vivek Kumar | 07bd086 | 2018-04-10 14:19:23 +0530 | [diff] [blame] | 2056 | uint32_t dtb_offset = 0; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2057 | unsigned int dtb_size = 0; |
| 2058 | unsigned char *best_match_dt_addr = NULL; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2059 | #endif |
| 2060 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 2061 | if (target_is_emmc_boot()) { |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2062 | hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 2063 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 2064 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
| 2065 | return -1; |
| 2066 | } |
| 2067 | goto continue_boot; |
| 2068 | } |
| 2069 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2070 | ptable = flash_get_ptable(); |
| 2071 | if (ptable == NULL) { |
| 2072 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 2073 | return -1; |
| 2074 | } |
| 2075 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 2076 | if(!boot_into_recovery) |
| 2077 | { |
| 2078 | ptn = ptable_find(ptable, "boot"); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2079 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 2080 | if (ptn == NULL) { |
| 2081 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 2082 | return -1; |
| 2083 | } |
| 2084 | } |
| 2085 | else |
| 2086 | { |
| 2087 | ptn = ptable_find(ptable, "recovery"); |
| 2088 | if (ptn == NULL) { |
| 2089 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 2090 | return -1; |
| 2091 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2092 | } |
| 2093 | |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2094 | /* Read boot.img header from flash */ |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 2095 | if (flash_read(ptn, offset, buf, page_size)) { |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2096 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 2097 | return -1; |
| 2098 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2099 | |
| 2100 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 2101 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2102 | return -1; |
| 2103 | } |
| 2104 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 2105 | if (hdr->page_size != page_size) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 2106 | 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] | 2107 | return -1; |
| 2108 | } |
| 2109 | |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 2110 | image_addr = (unsigned char *)target_get_scratch_address(); |
| 2111 | memcpy(image_addr, (void *)buf, page_size); |
vijay kumar | 287bb54 | 2015-09-29 13:01:52 +0530 | [diff] [blame] | 2112 | |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2113 | /* |
| 2114 | * Update the kernel/ramdisk/tags address if the boot image header |
| 2115 | * has default values, these default values come from mkbootimg when |
| 2116 | * the boot image is flashed using fastboot flash:raw |
| 2117 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2118 | update_ker_tags_rdisk_addr(hdr, false); |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2119 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2120 | /* Get virtual addresses since the hdr saves physical addresses. */ |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2121 | hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr)); |
| 2122 | hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr)); |
| 2123 | hdr->tags_addr = VA((addr_t)(hdr->tags_addr)); |
| 2124 | |
| 2125 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 2126 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2127 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2128 | |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 2129 | /* ensure commandline is terminated */ |
| 2130 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 2131 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2132 | /* Check if the addresses in the header are valid. */ |
| 2133 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) || |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2134 | check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) || |
| 2135 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) || |
| 2136 | check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2137 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2138 | dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2139 | return -1; |
| 2140 | } |
| 2141 | |
| 2142 | #ifndef DEVICE_TREE |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2143 | if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) { |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2144 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n"); |
| 2145 | return -1; |
| 2146 | } |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2147 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2148 | |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2149 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) || |
| 2150 | check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE)) |
Vijay Kumar Pendoti | d3ed20e | 2016-09-20 00:34:46 +0530 | [diff] [blame] | 2151 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2152 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
Vijay Kumar Pendoti | d3ed20e | 2016-09-20 00:34:46 +0530 | [diff] [blame] | 2153 | return -1; |
| 2154 | } |
| 2155 | #else |
| 2156 | |
| 2157 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 2158 | dt_size = hdr->dt_size; |
| 2159 | #endif |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2160 | dt_actual = ROUND_TO_PAGE(dt_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2161 | if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + (uint64_t)dt_actual + page_size)) { |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2162 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n"); |
| 2163 | return -1; |
| 2164 | } |
| 2165 | |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2166 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2167 | |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2168 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) || |
| 2169 | check_ddr_addr_range_bound(hdr->tags_addr, dt_size)) |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2170 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2171 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2172 | return -1; |
| 2173 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2174 | #endif |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2175 | |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2176 | /* Read full boot.img from flash */ |
| 2177 | dprintf(INFO, "Loading (%s) image (%d): start\n", |
| 2178 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
| 2179 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
| 2180 | |
| 2181 | if (UINT_MAX - page_size < imagesize_actual) |
| 2182 | { |
| 2183 | dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__); |
| 2184 | return -1; |
| 2185 | } |
| 2186 | |
| 2187 | /*Check the availability of RAM before reading boot image + max signature length from flash*/ |
| 2188 | if (target_get_max_flash_size() < (imagesize_actual + page_size)) |
| 2189 | { |
| 2190 | dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n"); |
| 2191 | return -1; |
| 2192 | } |
| 2193 | |
| 2194 | offset = page_size; |
| 2195 | /* Read image without signature and header */ |
| 2196 | if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size)) |
| 2197 | { |
| 2198 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 2199 | return -1; |
| 2200 | } |
| 2201 | |
| 2202 | dprintf(INFO, "Loading (%s) image (%d): done\n", |
| 2203 | (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual); |
| 2204 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
| 2205 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2206 | /* Authenticate Kernel */ |
Deepa Dinamani | 23b60d4 | 2013-06-24 18:10:52 -0700 | [diff] [blame] | 2207 | if(target_use_signed_kernel() && (!device.is_unlocked)) |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2208 | { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2209 | offset = imagesize_actual; |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2210 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2211 | /* Read signature */ |
| 2212 | if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size)) |
| 2213 | { |
| 2214 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2215 | return -1; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2216 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2217 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 2218 | verify_signed_bootimg((uint32_t)image_addr, imagesize_actual); |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2219 | } |
| 2220 | offset = page_size; |
| 2221 | if(hdr->second_size != 0) { |
| 2222 | if (UINT_MAX - offset < second_actual) |
| 2223 | { |
| 2224 | dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__); |
| 2225 | return -1; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2226 | } |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2227 | offset += second_actual; |
| 2228 | /* Second image loading not implemented. */ |
| 2229 | ASSERT(0); |
| 2230 | } |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2231 | /* Move kernel and ramdisk to correct address */ |
| 2232 | memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size); |
| 2233 | memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size); |
| 2234 | |
| 2235 | #if DEVICE_TREE |
| 2236 | if(dt_size != 0) { |
| 2237 | |
| 2238 | dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual); |
| 2239 | |
| 2240 | table = (struct dt_table*) dt_table_offset; |
| 2241 | |
| 2242 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
| 2243 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 2244 | return -1; |
| 2245 | } |
| 2246 | |
| 2247 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 2248 | goes beyound hdr->dt_size*/ |
| 2249 | if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) { |
| 2250 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 2251 | return -1; |
| 2252 | } |
| 2253 | |
| 2254 | /* Find index of device tree within device tree table */ |
| 2255 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
| 2256 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 2257 | return -1; |
| 2258 | } |
| 2259 | |
| 2260 | /* Validate and Read device device tree in the "tags_add */ |
| 2261 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) || |
| 2262 | check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size)) |
| 2263 | { |
| 2264 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
| 2265 | return -1; |
| 2266 | } |
| 2267 | |
| 2268 | if(dt_entry.offset > (UINT_MAX - dt_entry.size)) { |
| 2269 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2270 | return -1; |
| 2271 | } |
| 2272 | |
| 2273 | /* Ensure we are not overshooting dt_size with the dt_entry selected */ |
| 2274 | if ((dt_entry.offset + dt_entry.size) > dt_size) { |
| 2275 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2276 | return -1; |
| 2277 | } |
| 2278 | |
| 2279 | best_match_dt_addr = (unsigned char *)table + dt_entry.offset; |
| 2280 | dtb_size = dt_entry.size; |
| 2281 | memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size); |
Vivek Kumar | 07bd086 | 2018-04-10 14:19:23 +0530 | [diff] [blame] | 2282 | |
| 2283 | } else { |
| 2284 | /* Validate the tags_addr */ |
| 2285 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) || |
| 2286 | check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual)) |
| 2287 | { |
| 2288 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
| 2289 | return -1; |
| 2290 | } |
| 2291 | /* |
| 2292 | * If appended dev tree is found, update the atags with |
| 2293 | * memory address to the DTB appended location on RAM. |
| 2294 | * Else update with the atags address in the kernel header |
Mayank Grover | 1ed3e8b | 2018-09-19 15:16:50 +0530 | [diff] [blame] | 2295 | * |
| 2296 | * Make sure everything from scratch address is read before next step! |
| 2297 | * In case of dtbo, this API is going to read dtbo on scratch. |
Vivek Kumar | 07bd086 | 2018-04-10 14:19:23 +0530 | [diff] [blame] | 2298 | */ |
| 2299 | void *dtb = NULL; |
| 2300 | dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr); |
| 2301 | if (!dtb) { |
| 2302 | dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n"); |
| 2303 | return -1; |
| 2304 | } |
| 2305 | } |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2306 | #endif |
| 2307 | if(target_use_signed_kernel() && (!device.is_unlocked)) |
| 2308 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2309 | /* Make sure everything from scratch address is read before next step!*/ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2310 | if(device.is_tampered) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2311 | { |
| 2312 | write_device_info_flash(&device); |
| 2313 | } |
Channagoud Kadabi | 5c86fe3 | 2012-02-16 10:58:48 +0530 | [diff] [blame] | 2314 | #if USE_PCOM_SECBOOT |
| 2315 | set_tamper_flag(device.is_tampered); |
| 2316 | #endif |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2317 | } |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 2318 | continue_boot: |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2319 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2320 | /* TODO: create/pass atags to kernel */ |
| 2321 | |
Ajay Dudani | e28a607 | 2011-07-01 13:59:46 -0700 | [diff] [blame] | 2322 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Dima Zavin | bd3daa0 | 2013-03-26 11:06:44 -0700 | [diff] [blame] | 2323 | (const char *)hdr->cmdline, board_machtype(), |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2324 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 2325 | |
| 2326 | return 0; |
| 2327 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2328 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2329 | void write_device_info_mmc(device_info *dev) |
| 2330 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2331 | unsigned long long ptn = 0; |
| 2332 | unsigned long long size; |
| 2333 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2334 | uint8_t lun = 0; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2335 | uint32_t ret = 0; |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2336 | uint32_t device_info_sz = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2337 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2338 | if (devinfo_present) |
| 2339 | index = partition_get_index("devinfo"); |
| 2340 | else |
| 2341 | index = partition_get_index("aboot"); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 2342 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2343 | ptn = partition_get_offset(index); |
| 2344 | if(ptn == 0) |
| 2345 | { |
| 2346 | return; |
| 2347 | } |
| 2348 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2349 | lun = partition_get_lun(index); |
| 2350 | mmc_set_lun(lun); |
| 2351 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2352 | size = partition_get_size(index); |
| 2353 | |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2354 | device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info), |
| 2355 | mmc_blocksize_mask); |
| 2356 | if (device_info_sz == UINT_MAX) |
| 2357 | { |
| 2358 | dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n"); |
| 2359 | return; |
| 2360 | } |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 2361 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2362 | if (devinfo_present) |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2363 | ret = mmc_write(ptn, device_info_sz, (void *)info_buf); |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2364 | else |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2365 | ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf); |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2366 | if (ret) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2367 | { |
| 2368 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 2369 | ASSERT(0); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2370 | } |
| 2371 | } |
| 2372 | |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2373 | void read_device_info_mmc(struct device_info *info) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2374 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2375 | unsigned long long ptn = 0; |
| 2376 | unsigned long long size; |
| 2377 | int index = INVALID_PTN; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2378 | uint32_t ret = 0; |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2379 | uint32_t device_info_sz = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2380 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2381 | if ((index = partition_get_index("devinfo")) < 0) |
| 2382 | { |
| 2383 | devinfo_present = false; |
| 2384 | index = partition_get_index("aboot"); |
| 2385 | } |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 2386 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2387 | ptn = partition_get_offset(index); |
| 2388 | if(ptn == 0) |
| 2389 | { |
| 2390 | return; |
| 2391 | } |
| 2392 | |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 2393 | mmc_set_lun(partition_get_lun(index)); |
| 2394 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2395 | size = partition_get_size(index); |
| 2396 | |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2397 | device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info), |
| 2398 | mmc_blocksize_mask); |
| 2399 | if (device_info_sz == UINT_MAX) |
| 2400 | { |
| 2401 | dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n"); |
| 2402 | return; |
| 2403 | } |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 2404 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2405 | if (devinfo_present) |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2406 | ret = mmc_read(ptn, (void *)info_buf, device_info_sz); |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2407 | else |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2408 | ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz); |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2409 | if (ret) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2410 | { |
| 2411 | dprintf(CRITICAL, "ERROR: Cannot read device info\n"); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 2412 | ASSERT(0); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2413 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | void write_device_info_flash(device_info *dev) |
| 2417 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2418 | 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] | 2419 | struct ptentry *ptn; |
| 2420 | struct ptable *ptable; |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2421 | if(info == NULL) |
| 2422 | { |
| 2423 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 2424 | ASSERT(0); |
| 2425 | } |
| 2426 | info_buf = info; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2427 | ptable = flash_get_ptable(); |
| 2428 | if (ptable == NULL) |
| 2429 | { |
| 2430 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 2431 | return; |
| 2432 | } |
| 2433 | |
| 2434 | ptn = ptable_find(ptable, "devinfo"); |
| 2435 | if (ptn == NULL) |
| 2436 | { |
Smita Ghosh | 670c8b8 | 2015-05-07 09:30:03 -0700 | [diff] [blame] | 2437 | dprintf(CRITICAL, "ERROR: No devinfo partition found\n"); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2438 | return; |
| 2439 | } |
| 2440 | |
Mayank Grover | dedbc89 | 2017-10-24 13:41:34 +0530 | [diff] [blame] | 2441 | memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2442 | memcpy(info, dev, sizeof(device_info)); |
| 2443 | |
| 2444 | if (flash_write(ptn, 0, (void *)info_buf, page_size)) |
| 2445 | { |
| 2446 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 2447 | return; |
| 2448 | } |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2449 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2450 | } |
| 2451 | |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2452 | static int read_allow_oem_unlock(device_info *dev) |
| 2453 | { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2454 | unsigned offset; |
| 2455 | int index; |
| 2456 | unsigned long long ptn; |
| 2457 | unsigned long long ptn_size; |
| 2458 | unsigned blocksize = mmc_get_device_blocksize(); |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2459 | STACKBUF_DMA_ALIGN(buf, blocksize); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2460 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2461 | index = partition_get_index(frp_ptns[0]); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2462 | if (index == INVALID_PTN) |
| 2463 | { |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2464 | index = partition_get_index(frp_ptns[1]); |
| 2465 | if (index == INVALID_PTN) |
| 2466 | { |
| 2467 | dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]); |
| 2468 | return -1; |
| 2469 | } |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | ptn = partition_get_offset(index); |
| 2473 | ptn_size = partition_get_size(index); |
| 2474 | offset = ptn_size - blocksize; |
| 2475 | |
Mayank Grover | 4886040 | 2016-11-29 12:34:53 +0530 | [diff] [blame] | 2476 | /* Set Lun for partition */ |
| 2477 | mmc_set_lun(partition_get_lun(index)); |
| 2478 | |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2479 | if (mmc_read(ptn + offset, (void *)buf, blocksize)) |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2480 | { |
| 2481 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 2482 | return -1; |
| 2483 | } |
| 2484 | |
| 2485 | /*is_allow_unlock is a bool value stored at the LSB of last byte*/ |
| 2486 | is_allow_unlock = buf[blocksize-1] & 0x01; |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
| 2490 | static int write_allow_oem_unlock(bool allow_unlock) |
| 2491 | { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2492 | unsigned offset; |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2493 | int index; |
| 2494 | unsigned long long ptn; |
| 2495 | unsigned long long ptn_size; |
| 2496 | unsigned blocksize = mmc_get_device_blocksize(); |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2497 | STACKBUF_DMA_ALIGN(buf, blocksize); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2498 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2499 | index = partition_get_index(frp_ptns[0]); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2500 | if (index == INVALID_PTN) |
| 2501 | { |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2502 | index = partition_get_index(frp_ptns[1]); |
| 2503 | if (index == INVALID_PTN) |
| 2504 | { |
| 2505 | dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]); |
| 2506 | return -1; |
| 2507 | } |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | ptn = partition_get_offset(index); |
| 2511 | ptn_size = partition_get_size(index); |
| 2512 | offset = ptn_size - blocksize; |
Mayank Grover | 4886040 | 2016-11-29 12:34:53 +0530 | [diff] [blame] | 2513 | mmc_set_lun(partition_get_lun(index)); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2514 | |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2515 | if (mmc_read(ptn + offset, (void *)buf, blocksize)) |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2516 | { |
| 2517 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 2518 | return -1; |
| 2519 | } |
| 2520 | |
| 2521 | /*is_allow_unlock is a bool value stored at the LSB of last byte*/ |
| 2522 | buf[blocksize-1] = allow_unlock; |
| 2523 | if (mmc_write(ptn + offset, blocksize, buf)) |
| 2524 | { |
| 2525 | dprintf(CRITICAL, "Writing MMC failed\n"); |
| 2526 | return -1; |
| 2527 | } |
| 2528 | |
| 2529 | return 0; |
| 2530 | } |
| 2531 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2532 | void read_device_info_flash(device_info *dev) |
| 2533 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2534 | 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] | 2535 | struct ptentry *ptn; |
| 2536 | struct ptable *ptable; |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2537 | if(info == NULL) |
| 2538 | { |
| 2539 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 2540 | ASSERT(0); |
| 2541 | } |
| 2542 | info_buf = info; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2543 | ptable = flash_get_ptable(); |
| 2544 | if (ptable == NULL) |
| 2545 | { |
| 2546 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 2547 | return; |
| 2548 | } |
| 2549 | |
| 2550 | ptn = ptable_find(ptable, "devinfo"); |
| 2551 | if (ptn == NULL) |
| 2552 | { |
Smita Ghosh | 670c8b8 | 2015-05-07 09:30:03 -0700 | [diff] [blame] | 2553 | dprintf(CRITICAL, "ERROR: No devinfo partition found\n"); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2554 | return; |
| 2555 | } |
| 2556 | |
| 2557 | if (flash_read(ptn, 0, (void *)info_buf, page_size)) |
| 2558 | { |
| 2559 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 2560 | return; |
| 2561 | } |
| 2562 | |
| 2563 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 2564 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2565 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
| 2566 | info->is_unlocked = 0; |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2567 | info->is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2568 | write_device_info_flash(info); |
| 2569 | } |
| 2570 | memcpy(dev, info, sizeof(device_info)); |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2571 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | void write_device_info(device_info *dev) |
| 2575 | { |
| 2576 | if(target_is_emmc_boot()) |
| 2577 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2578 | struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 2579 | if(info == NULL) |
| 2580 | { |
| 2581 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 2582 | ASSERT(0); |
| 2583 | } |
| 2584 | info_buf = info; |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2585 | memcpy(info, dev, sizeof(struct device_info)); |
| 2586 | |
| 2587 | #if USE_RPMB_FOR_DEVINFO |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 2588 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 2589 | is_secure_boot_enable()) { |
| 2590 | if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0) |
| 2591 | ASSERT(0); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 2592 | } |
Channagoud Kadabi | c80cb49 | 2015-04-28 16:08:28 -0700 | [diff] [blame] | 2593 | else |
| 2594 | write_device_info_mmc(info); |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2595 | #else |
| 2596 | write_device_info_mmc(info); |
| 2597 | #endif |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2598 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2599 | } |
| 2600 | else |
| 2601 | { |
| 2602 | write_device_info_flash(dev); |
| 2603 | } |
| 2604 | } |
| 2605 | |
Monika Singh | 9431646 | 2018-03-15 18:39:01 +0530 | [diff] [blame] | 2606 | int read_rollback_index(uint32_t loc, uint64_t *roll_back_index) |
| 2607 | { |
| 2608 | if (!devinfo_present) { |
| 2609 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 2610 | return -EINVAL; |
| 2611 | } |
| 2612 | if (loc >= ARRAY_SIZE(device.rollback_index)) { |
| 2613 | dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n", |
| 2614 | __func__, loc, ARRAY_SIZE(device.rollback_index)); |
| 2615 | ASSERT(0); |
| 2616 | } |
| 2617 | |
| 2618 | *roll_back_index = device.rollback_index[loc]; |
| 2619 | return 0; |
| 2620 | } |
| 2621 | |
| 2622 | int write_rollback_index(uint32_t loc, uint64_t roll_back_index) |
| 2623 | { |
| 2624 | if (!devinfo_present) { |
| 2625 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 2626 | return -EINVAL; |
| 2627 | } |
| 2628 | if (loc >= ARRAY_SIZE(device.rollback_index)) { |
| 2629 | dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n", |
| 2630 | __func__, loc, ARRAY_SIZE(device.rollback_index)); |
| 2631 | ASSERT(0); |
| 2632 | } |
| 2633 | |
| 2634 | device.rollback_index[loc] = roll_back_index; |
| 2635 | write_device_info(&device); |
| 2636 | return 0; |
| 2637 | } |
| 2638 | |
Monika Singh | b0fad82 | 2018-03-15 18:50:55 +0530 | [diff] [blame] | 2639 | int store_userkey(uint8_t *user_key, uint32_t user_key_size) |
| 2640 | { |
| 2641 | if (!devinfo_present) { |
| 2642 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 2643 | return -EINVAL; |
| 2644 | } |
| 2645 | |
| 2646 | if (user_key_size > ARRAY_SIZE(device.user_public_key)) { |
| 2647 | dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n"); |
| 2648 | return -ENODEV; |
| 2649 | } |
| 2650 | |
| 2651 | memcpy(device.user_public_key, user_key, user_key_size); |
| 2652 | device.user_public_key_length = user_key_size; |
| 2653 | write_device_info(&device); |
| 2654 | return 0; |
| 2655 | } |
| 2656 | |
| 2657 | int erase_userkey() |
| 2658 | { |
| 2659 | if (!devinfo_present) { |
| 2660 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 2661 | return -EINVAL; |
| 2662 | } |
| 2663 | memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key)); |
| 2664 | device.user_public_key_length = 0; |
| 2665 | write_device_info(&device); |
| 2666 | return 0; |
| 2667 | } |
| 2668 | |
| 2669 | int get_userkey(uint8_t **user_key, uint32_t *user_key_size) |
| 2670 | { |
| 2671 | if (!devinfo_present) { |
| 2672 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 2673 | return -EINVAL; |
| 2674 | } |
| 2675 | *user_key = device.user_public_key; |
| 2676 | *user_key_size = device.user_public_key_length; |
| 2677 | return 0; |
| 2678 | } |
| 2679 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2680 | void read_device_info(device_info *dev) |
| 2681 | { |
| 2682 | if(target_is_emmc_boot()) |
| 2683 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2684 | struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 2685 | if(info == NULL) |
| 2686 | { |
| 2687 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 2688 | ASSERT(0); |
| 2689 | } |
| 2690 | info_buf = info; |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2691 | |
| 2692 | #if USE_RPMB_FOR_DEVINFO |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 2693 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 2694 | is_secure_boot_enable()) { |
| 2695 | if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0) |
| 2696 | ASSERT(0); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 2697 | } |
Channagoud Kadabi | c80cb49 | 2015-04-28 16:08:28 -0700 | [diff] [blame] | 2698 | else |
| 2699 | read_device_info_mmc(info); |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2700 | #else |
| 2701 | read_device_info_mmc(info); |
| 2702 | #endif |
| 2703 | |
| 2704 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 2705 | { |
| 2706 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 2707 | if (is_secure_boot_enable()) { |
Channagoud Kadabi | 05f78ba | 2015-07-06 11:58:14 -0700 | [diff] [blame] | 2708 | info->is_unlocked = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2709 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 2710 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 2711 | info->is_unlock_critical = 0; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2712 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 2713 | } else { |
Channagoud Kadabi | 2fda409 | 2015-07-07 13:34:11 -0700 | [diff] [blame] | 2714 | info->is_unlocked = 1; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2715 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 2716 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 2717 | info->is_unlock_critical = 1; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2718 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 2719 | } |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2720 | info->is_tampered = 0; |
| 2721 | info->charger_screen_enabled = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2722 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 2723 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 2724 | info->verity_mode = 1; //enforcing by default |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2725 | #endif |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2726 | write_device_info(info); |
| 2727 | } |
| 2728 | memcpy(dev, info, sizeof(device_info)); |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2729 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2730 | } |
| 2731 | else |
| 2732 | { |
| 2733 | read_device_info_flash(dev); |
| 2734 | } |
| 2735 | } |
| 2736 | |
| 2737 | void reset_device_info() |
| 2738 | { |
| 2739 | dprintf(ALWAYS, "reset_device_info called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2740 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2741 | write_device_info(&device); |
| 2742 | } |
| 2743 | |
| 2744 | void set_device_root() |
| 2745 | { |
| 2746 | dprintf(ALWAYS, "set_device_root called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2747 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2748 | write_device_info(&device); |
| 2749 | } |
| 2750 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2751 | /* set device unlock value |
| 2752 | * Must check FRP before call this function |
| 2753 | * Need to wipe data when unlock status changed |
| 2754 | * type 0: oem unlock |
| 2755 | * type 1: unlock critical |
| 2756 | * status 0: unlock as false |
| 2757 | * status 1: lock as true |
| 2758 | */ |
| 2759 | void set_device_unlock_value(int type, bool status) |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 2760 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2761 | if (type == UNLOCK) |
| 2762 | device.is_unlocked = status; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2763 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 2764 | else if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 2765 | type == UNLOCK_CRITICAL) |
| 2766 | device.is_unlock_critical = status; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2767 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2768 | write_device_info(&device); |
| 2769 | } |
| 2770 | |
| 2771 | static void set_device_unlock(int type, bool status) |
| 2772 | { |
| 2773 | int is_unlocked = -1; |
| 2774 | char response[MAX_RSP_SIZE]; |
| 2775 | |
| 2776 | /* check device unlock status if it is as expected */ |
| 2777 | if (type == UNLOCK) |
| 2778 | is_unlocked = device.is_unlocked; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2779 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 2780 | if(VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 2781 | type == UNLOCK_CRITICAL) |
| 2782 | { |
| 2783 | is_unlocked = device.is_unlock_critical; |
| 2784 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 2785 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2786 | if (is_unlocked == status) { |
| 2787 | snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!")); |
| 2788 | fastboot_info(response); |
| 2789 | fastboot_okay(""); |
| 2790 | return; |
| 2791 | } |
| 2792 | |
| 2793 | /* status is true, it means to unlock device */ |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 2794 | if (status && !is_allow_unlock) { |
| 2795 | fastboot_fail("oem unlock is not allowed"); |
| 2796 | return; |
| 2797 | } |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 2798 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2799 | #if FBCON_DISPLAY_MSG |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 2800 | display_unlock_menu(type, status); |
| 2801 | fastboot_okay(""); |
| 2802 | return; |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2803 | #else |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 2804 | if (status && type == UNLOCK) { |
| 2805 | fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'"); |
| 2806 | return; |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 2807 | } |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 2808 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2809 | |
| 2810 | set_device_unlock_value(type, status); |
| 2811 | |
| 2812 | /* wipe data */ |
| 2813 | struct recovery_message msg; |
Kishor PK | 60a6821 | 2017-05-08 16:55:57 +0530 | [diff] [blame] | 2814 | memset(&msg, 0, sizeof(msg)); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 2815 | snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data"); |
| 2816 | write_misc(0, &msg, sizeof(msg)); |
| 2817 | |
| 2818 | fastboot_okay(""); |
| 2819 | reboot_device(RECOVERY_MODE); |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 2820 | } |
| 2821 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 2822 | static bool critical_flash_allowed(const char * entry) |
| 2823 | { |
| 2824 | uint32_t i = 0; |
| 2825 | if (entry == NULL) |
| 2826 | return false; |
| 2827 | |
| 2828 | for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) { |
| 2829 | if(!strcmp(entry, critical_flash_allowed_ptn[i])) |
| 2830 | return true; |
| 2831 | } |
| 2832 | return false; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | #if DEVICE_TREE |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2836 | int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset) |
| 2837 | { |
| 2838 | uint32 dt_image_offset = 0; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2839 | uint32_t n; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2840 | struct dt_table *table = NULL; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2841 | struct dt_entry dt_entry; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2842 | uint32_t dt_hdr_size = 0; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2843 | unsigned int compressed_size = 0; |
| 2844 | unsigned int dtb_size = 0; |
| 2845 | unsigned int out_avai_len = 0; |
| 2846 | unsigned char *out_addr = NULL; |
| 2847 | unsigned char *best_match_dt_addr = NULL; |
| 2848 | int rc; |
| 2849 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2850 | boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start); |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2851 | |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 2852 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 2853 | dt_size = hdr->dt_size; |
| 2854 | #endif |
| 2855 | |
| 2856 | if(dt_size != 0) { |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2857 | /* add kernel offset */ |
| 2858 | dt_image_offset += page_size; |
| 2859 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 2860 | dt_image_offset += n; |
| 2861 | |
| 2862 | /* add ramdisk offset */ |
| 2863 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 2864 | dt_image_offset += n; |
| 2865 | |
| 2866 | /* add second offset */ |
| 2867 | if(hdr->second_size != 0) { |
| 2868 | n = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 2869 | dt_image_offset += n; |
| 2870 | } |
| 2871 | |
| 2872 | /* offset now point to start of dt.img */ |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2873 | table = (struct dt_table*)(boot_image_start + dt_image_offset); |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2874 | |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 2875 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2876 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 2877 | return -1; |
| 2878 | } |
P.V. Phani Kumar | 8291676 | 2016-03-09 09:20:19 +0530 | [diff] [blame] | 2879 | |
| 2880 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 2881 | goes beyound hdr->dt_size*/ |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 2882 | if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) { |
P.V. Phani Kumar | 8291676 | 2016-03-09 09:20:19 +0530 | [diff] [blame] | 2883 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 2884 | return -1; |
| 2885 | } |
| 2886 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 2887 | /* Find index of device tree within device tree table */ |
| 2888 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2889 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 2890 | return -1; |
| 2891 | } |
| 2892 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2893 | best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset; |
| 2894 | if (is_gzip_package(best_match_dt_addr, dt_entry.size)) |
| 2895 | { |
| 2896 | out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset; |
| 2897 | out_avai_len = target_get_max_flash_size() - scratch_offset; |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2898 | dprintf(INFO, "decompressing dtb: start\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2899 | rc = decompress(best_match_dt_addr, |
| 2900 | dt_entry.size, out_addr, out_avai_len, |
| 2901 | &compressed_size, &dtb_size); |
| 2902 | if (rc) |
| 2903 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2904 | dprintf(CRITICAL, "decompressing dtb failed!!!\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2905 | ASSERT(0); |
| 2906 | } |
| 2907 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2908 | dprintf(INFO, "decompressing dtb: done\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2909 | best_match_dt_addr = out_addr; |
| 2910 | } else { |
| 2911 | dtb_size = dt_entry.size; |
| 2912 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2913 | /* Validate and Read device device tree in the "tags_add */ |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2914 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) || |
| 2915 | check_ddr_addr_range_bound(hdr->tags_addr, dtb_size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2916 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2917 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2918 | return -1; |
| 2919 | } |
| 2920 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2921 | /* Read device device tree in the "tags_add */ |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2922 | memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 2923 | } else |
| 2924 | return -1; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 2925 | |
| 2926 | /* Everything looks fine. Return success. */ |
| 2927 | return 0; |
| 2928 | } |
| 2929 | #endif |
| 2930 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2931 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 2932 | { |
| 2933 | unsigned kernel_actual; |
| 2934 | unsigned ramdisk_actual; |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2935 | unsigned second_actual; |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 2936 | uint32_t image_actual; |
| 2937 | uint32_t dt_actual = 0; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2938 | boot_img_hdr *hdr = NULL; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2939 | struct kernel64_hdr *kptr = NULL; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2940 | char *ptr = ((char*) data); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 2941 | int ret = 0; |
| 2942 | uint8_t dtb_copied = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2943 | unsigned int out_len = 0; |
| 2944 | unsigned int out_avai_len = 0; |
| 2945 | unsigned char *out_addr = NULL; |
| 2946 | uint32_t dtb_offset = 0; |
| 2947 | unsigned char *kernel_start_addr = NULL; |
| 2948 | unsigned int kernel_size = 0; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2949 | unsigned int scratch_offset = 0; |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 2950 | #if VERIFIED_BOOT_2 |
| 2951 | void *dtbo_image_buf = NULL; |
| 2952 | uint32_t dtbo_image_sz = 0; |
| 2953 | void *vbmeta_image_buf = NULL; |
| 2954 | uint32_t vbmeta_image_sz = 0; |
| 2955 | #endif |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2956 | #if !VERIFIED_BOOT_2 |
| 2957 | uint32_t sig_actual = 0; |
| 2958 | uint32_t sig_size = 0; |
| 2959 | #ifdef MDTP_SUPPORT |
| 2960 | static bool is_mdtp_activated = 0; |
| 2961 | #endif /* MDTP_SUPPORT */ |
| 2962 | #endif |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2963 | |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2964 | #if FBCON_DISPLAY_MSG |
| 2965 | /* Exit keys' detection thread firstly */ |
| 2966 | exit_menu_keys_detection(); |
| 2967 | #endif |
| 2968 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2969 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Channagoud Kadabi | 6d5375e | 2015-06-23 17:15:42 -0700 | [diff] [blame] | 2970 | if(target_build_variant_user() && !device.is_unlocked) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 2971 | { |
| 2972 | fastboot_fail("unlock device to use this command"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2973 | goto boot_failed; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 2974 | } |
| 2975 | #endif |
| 2976 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2977 | if (sz < sizeof(hdr)) { |
| 2978 | fastboot_fail("invalid bootimage header"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 2979 | goto boot_failed; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2980 | } |
| 2981 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2982 | hdr = (boot_img_hdr *)data; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2983 | |
| 2984 | /* ensure commandline is terminated */ |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2985 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2986 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2987 | if(target_is_emmc_boot() && hdr->page_size) { |
| 2988 | page_size = hdr->page_size; |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 2989 | page_mask = page_size - 1; |
| 2990 | } |
| 2991 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2992 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 2993 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2994 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 2995 | #if DEVICE_TREE |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 2996 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 2997 | dt_size = hdr->dt_size; |
| 2998 | #endif |
| 2999 | dt_actual = ROUND_TO_PAGE(dt_size, page_mask); |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3000 | #endif |
| 3001 | |
| 3002 | image_actual = ADD_OF(page_size, kernel_actual); |
| 3003 | image_actual = ADD_OF(image_actual, ramdisk_actual); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 3004 | image_actual = ADD_OF(image_actual, second_actual); |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3005 | image_actual = ADD_OF(image_actual, dt_actual); |
| 3006 | |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3007 | /* Checking to prevent oob access in read_der_message_length */ |
| 3008 | if (image_actual > sz) { |
| 3009 | fastboot_fail("bootimage header fields are invalid"); |
| 3010 | goto boot_failed; |
| 3011 | } |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3012 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3013 | #if VERIFIED_BOOT_2 |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3014 | |
| 3015 | /* load and validate dtbo partition */ |
| 3016 | load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz); |
| 3017 | |
| 3018 | /* load vbmeta partition */ |
| 3019 | load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz); |
| 3020 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3021 | memset(&info, 0, sizeof(bootinfo)); |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3022 | |
| 3023 | info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data); |
| 3024 | info.images[IMG_BOOT].imgsize = image_actual; |
| 3025 | info.images[IMG_BOOT].name = "boot"; |
| 3026 | ++info.num_loaded_images; |
| 3027 | |
| 3028 | /* Pass loaded dtbo image */ |
| 3029 | if (dtbo_image_buf != NULL) { |
| 3030 | info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf); |
| 3031 | info.images[IMG_DTBO].imgsize = dtbo_image_sz; |
| 3032 | info.images[IMG_DTBO].name = "dtbo"; |
| 3033 | ++info.num_loaded_images; |
| 3034 | } |
| 3035 | |
| 3036 | /* Pass loaded vbmeta image */ |
| 3037 | if (vbmeta_image_buf != NULL) { |
| 3038 | info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf; |
| 3039 | info.images[IMG_VBMETA].imgsize = vbmeta_image_sz; |
| 3040 | info.images[IMG_VBMETA].name = "vbmeta"; |
| 3041 | ++info.num_loaded_images; |
| 3042 | } |
| 3043 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3044 | info.multi_slot_boot = partition_multislot_is_supported(); |
| 3045 | if (load_image_and_auth(&info)) |
| 3046 | goto boot_failed; |
| 3047 | vbcmdline = info.vbcmdline; |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3048 | |
| 3049 | /* Free the buffer allocated to vbmeta post verification */ |
| 3050 | if (vbmeta_image_buf != NULL) { |
| 3051 | free(vbmeta_image_buf); |
| 3052 | --info.num_loaded_images; |
| 3053 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3054 | #else |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3055 | sig_size = sz - image_actual; |
| 3056 | |
P.V. Phani Kumar | eecfd82 | 2016-03-09 20:09:03 +0530 | [diff] [blame] | 3057 | if (target_use_signed_kernel() && (!device.is_unlocked)) { |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3058 | unsigned chk; |
P.V. Phani Kumar | eecfd82 | 2016-03-09 20:09:03 +0530 | [diff] [blame] | 3059 | /* Calculate the signature length from boot image */ |
| 3060 | sig_actual = read_der_message_length( |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3061 | (unsigned char*)(data + image_actual), sig_size); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3062 | chk = ADD_OF(image_actual, sig_actual); |
Channagoud Kadabi | 3d83901 | 2014-06-26 14:26:39 -0700 | [diff] [blame] | 3063 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3064 | if (chk > sz) { |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3065 | fastboot_fail("bootimage header fields are invalid"); |
| 3066 | goto boot_failed; |
| 3067 | } |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3068 | } |
| 3069 | |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 3070 | // Initialize boot state before trying to verify boot.img |
| 3071 | #if VERIFIED_BOOT |
Channagoud Kadabi | 699466e | 2015-11-03 12:37:42 -0800 | [diff] [blame] | 3072 | boot_verifier_init(); |
Mayank Grover | b337e93 | 2017-01-18 20:00:40 +0530 | [diff] [blame] | 3073 | #endif |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 3074 | /* Handle overflow if the input image size is greater than |
| 3075 | * boot image buffer can hold |
| 3076 | */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3077 | if ((target_get_max_flash_size() - page_size) < image_actual) |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 3078 | { |
| 3079 | fastboot_fail("booimage: size is greater than boot image buffer can hold"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3080 | goto boot_failed; |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 3081 | } |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 3082 | |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3083 | /* Verify the boot image |
| 3084 | * device & page_size are initialized in aboot_init |
| 3085 | */ |
Sridhar Parasuram | 7ae9fbc | 2015-09-17 09:56:30 -0700 | [diff] [blame] | 3086 | if (target_use_signed_kernel() && (!device.is_unlocked)) { |
Channagoud Kadabi | 3d83901 | 2014-06-26 14:26:39 -0700 | [diff] [blame] | 3087 | /* Pass size excluding signature size, otherwise we would try to |
| 3088 | * access signature beyond its length |
| 3089 | */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3090 | verify_signed_bootimg((uint32_t)data, image_actual); |
Sridhar Parasuram | 7ae9fbc | 2015-09-17 09:56:30 -0700 | [diff] [blame] | 3091 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3092 | #ifdef MDTP_SUPPORT |
| 3093 | else |
| 3094 | { |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3095 | /* fastboot boot is not allowed when MDTP is activated */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3096 | mdtp_ext_partition_verification_t ext_partition; |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 3097 | |
| 3098 | if (!is_mdtp_activated) { |
| 3099 | ext_partition.partition = MDTP_PARTITION_NONE; |
| 3100 | mdtp_fwlock_verify_lock(&ext_partition); |
| 3101 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3102 | } |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3103 | |
Amir Kotzer | 7c768c0 | 2016-04-13 09:08:05 +0300 | [diff] [blame] | 3104 | /* If mdtp state cannot be validate, block fastboot boot*/ |
| 3105 | if(mdtp_activated(&is_mdtp_activated)){ |
| 3106 | dprintf(CRITICAL, "mdtp_activated cannot validate state.\n"); |
| 3107 | dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n"); |
| 3108 | goto boot_failed; |
| 3109 | } |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3110 | if(is_mdtp_activated){ |
| 3111 | dprintf(CRITICAL, "fastboot boot command is not available.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3112 | goto boot_failed; |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3113 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3114 | #endif /* MDTP_SUPPORT */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3115 | #endif /* VERIFIED_BOOT_2 else */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3116 | |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 3117 | #if VERIFIED_BOOT |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3118 | if (VB_M == target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3119 | { |
| 3120 | /* set boot and system versions. */ |
| 3121 | set_os_version((unsigned char *)data); |
| 3122 | // send root of trust |
| 3123 | if(!send_rot_command((uint32_t)device.is_unlocked)) |
| 3124 | ASSERT(0); |
| 3125 | } |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 3126 | #endif |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 3127 | /* |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3128 | * Check if the kernel image is a gzip package. If yes, need to decompress it. |
| 3129 | * If not, continue booting. |
| 3130 | */ |
| 3131 | if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size)) |
| 3132 | { |
| 3133 | out_addr = (unsigned char *)target_get_scratch_address(); |
| 3134 | out_addr = (unsigned char *)(out_addr + image_actual + page_size); |
| 3135 | out_avai_len = target_get_max_flash_size() - image_actual - page_size; |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3136 | #if VERIFIED_BOOT_2 |
| 3137 | if (dtbo_image_sz) |
| 3138 | out_avai_len -= DTBO_IMG_BUF; |
| 3139 | #endif |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3140 | dprintf(INFO, "decompressing kernel image: start\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3141 | ret = decompress((unsigned char *)(ptr + page_size), |
| 3142 | hdr->kernel_size, out_addr, out_avai_len, |
| 3143 | &dtb_offset, &out_len); |
| 3144 | if (ret) |
| 3145 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3146 | dprintf(CRITICAL, "decompressing image failed!!!\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3147 | ASSERT(0); |
| 3148 | } |
| 3149 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3150 | dprintf(INFO, "decompressing kernel image: done\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3151 | kptr = (struct kernel64_hdr *)out_addr; |
| 3152 | kernel_start_addr = out_addr; |
| 3153 | kernel_size = out_len; |
| 3154 | } else { |
| 3155 | kptr = (struct kernel64_hdr*)((char *)data + page_size); |
| 3156 | kernel_start_addr = (unsigned char *)((char *)data + page_size); |
| 3157 | kernel_size = hdr->kernel_size; |
| 3158 | } |
| 3159 | |
| 3160 | /* |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 3161 | * Update the kernel/ramdisk/tags address if the boot image header |
| 3162 | * has default values, these default values come from mkbootimg when |
| 3163 | * the boot image is flashed using fastboot flash:raw |
| 3164 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 3165 | update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr)); |
Dima Zavin | 3cadfff | 2013-03-21 14:30:48 -0700 | [diff] [blame] | 3166 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3167 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 3168 | hdr->kernel_addr = VA(hdr->kernel_addr); |
| 3169 | hdr->ramdisk_addr = VA(hdr->ramdisk_addr); |
| 3170 | hdr->tags_addr = VA(hdr->tags_addr); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3171 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3172 | kernel_size = ROUND_TO_PAGE(kernel_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3173 | /* Check if the addresses in the header are valid. */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3174 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) || |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3175 | check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) || |
| 3176 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) || |
| 3177 | check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3178 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3179 | dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3180 | goto boot_failed; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3181 | } |
| 3182 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3183 | #if DEVICE_TREE |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3184 | scratch_offset = image_actual + page_size + out_len; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3185 | /* find correct dtb and copy it to right location */ |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3186 | ret = copy_dtb(data, scratch_offset); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3187 | |
| 3188 | dtb_copied = !ret ? 1 : 0; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3189 | #else |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3190 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) || |
| 3191 | check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3192 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3193 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3194 | goto boot_failed; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3195 | } |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3196 | #endif |
| 3197 | |
| 3198 | /* Load ramdisk & kernel */ |
| 3199 | 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] | 3200 | memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3201 | |
| 3202 | #if DEVICE_TREE |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3203 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) || |
| 3204 | check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual)) |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3205 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3206 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3207 | goto boot_failed; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3208 | } |
| 3209 | |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3210 | /* |
| 3211 | * If dtb is not found look for appended DTB in the kernel. |
| 3212 | * If appended dev tree is found, update the atags with |
| 3213 | * memory address to the DTB appended location on RAM. |
| 3214 | * Else update with the atags address in the kernel header |
| 3215 | */ |
| 3216 | if (!dtb_copied) { |
| 3217 | void *dtb; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3218 | dtb = dev_tree_appended((void*)(ptr + page_size), |
| 3219 | hdr->kernel_size, dtb_offset, |
Dima Zavin | e63e557 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 3220 | (void *)hdr->tags_addr); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3221 | if (!dtb) { |
| 3222 | fastboot_fail("dtb not found"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3223 | goto boot_failed; |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3224 | } |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3225 | } |
| 3226 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3227 | |
| 3228 | fastboot_okay(""); |
Amol Jadi | 7c4316c | 2013-10-07 14:19:26 -0700 | [diff] [blame] | 3229 | fastboot_stop(); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3230 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 3231 | boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr, |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3232 | (const char*) hdr->cmdline, board_machtype(), |
| 3233 | (void*) hdr->ramdisk_addr, hdr->ramdisk_size); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3234 | |
| 3235 | /* fastboot already stop, it's no need to show fastboot menu */ |
| 3236 | return; |
| 3237 | boot_failed: |
| 3238 | #if FBCON_DISPLAY_MSG |
| 3239 | /* revert to fastboot menu if boot failed */ |
| 3240 | display_fastboot_menu(); |
| 3241 | #endif |
| 3242 | return; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3243 | } |
| 3244 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3245 | void cmd_erase_nand(const char *arg, void *data, unsigned sz) |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3246 | { |
| 3247 | struct ptentry *ptn; |
| 3248 | struct ptable *ptable; |
| 3249 | |
| 3250 | ptable = flash_get_ptable(); |
| 3251 | if (ptable == NULL) { |
| 3252 | fastboot_fail("partition table doesn't exist"); |
| 3253 | return; |
| 3254 | } |
| 3255 | |
| 3256 | ptn = ptable_find(ptable, arg); |
| 3257 | if (ptn == NULL) { |
| 3258 | fastboot_fail("unknown partition name"); |
| 3259 | return; |
| 3260 | } |
| 3261 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3262 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 3263 | dprintf(INFO, "erasing avb_custom_key\n"); |
| 3264 | if (erase_userkey()) { |
| 3265 | fastboot_fail("Erasing avb_custom_key failed"); |
| 3266 | } else { |
| 3267 | fastboot_okay(""); |
| 3268 | } |
| 3269 | return; |
| 3270 | } |
| 3271 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3272 | if (flash_erase(ptn)) { |
| 3273 | fastboot_fail("failed to erase partition"); |
| 3274 | return; |
| 3275 | } |
| 3276 | fastboot_okay(""); |
| 3277 | } |
| 3278 | |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3279 | |
| 3280 | void cmd_erase_mmc(const char *arg, void *data, unsigned sz) |
| 3281 | { |
| 3282 | unsigned long long ptn = 0; |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 3283 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3284 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3285 | uint8_t lun = 0; |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 3286 | char *footer = NULL; |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3287 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3288 | #if VERIFIED_BOOT |
| 3289 | if(!strcmp(arg, KEYSTORE_PTN_NAME)) |
| 3290 | { |
| 3291 | if(!device.is_unlocked) |
| 3292 | { |
| 3293 | fastboot_fail("unlock device to erase keystore"); |
| 3294 | return; |
| 3295 | } |
| 3296 | } |
| 3297 | #endif |
| 3298 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3299 | index = partition_get_index(arg); |
| 3300 | ptn = partition_get_offset(index); |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 3301 | size = partition_get_size(index); |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 3302 | |
Monika Singh | c4778b7 | 2018-05-16 11:16:42 +0530 | [diff] [blame] | 3303 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 3304 | dprintf(INFO, "erasing avb_custom_key\n"); |
| 3305 | if (erase_userkey()) { |
| 3306 | fastboot_fail("Erasing avb_custom_key failed"); |
| 3307 | } else { |
| 3308 | fastboot_okay(""); |
| 3309 | } |
| 3310 | return; |
| 3311 | } |
| 3312 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3313 | if(ptn == 0) { |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 3314 | fastboot_fail("Partition table doesn't exist\n"); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3315 | return; |
| 3316 | } |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 3317 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3318 | lun = partition_get_lun(index); |
| 3319 | mmc_set_lun(lun); |
| 3320 | |
Sridhar Parasuram | d795712 | 2015-02-27 11:33:40 -0800 | [diff] [blame] | 3321 | if (platform_boot_dev_isemmc()) |
| 3322 | { |
| 3323 | if (mmc_erase_card(ptn, size)) { |
| 3324 | fastboot_fail("failed to erase partition\n"); |
| 3325 | return; |
| 3326 | } |
| 3327 | } else { |
| 3328 | BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE); |
| 3329 | size = partition_get_size(index); |
| 3330 | if (size > DEFAULT_ERASE_SIZE) |
| 3331 | size = DEFAULT_ERASE_SIZE; |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 3332 | |
Sridhar Parasuram | d795712 | 2015-02-27 11:33:40 -0800 | [diff] [blame] | 3333 | /* Simple inefficient version of erase. Just writing |
| 3334 | 0 in first several blocks */ |
| 3335 | if (mmc_write(ptn , size, (unsigned int *)out)) { |
| 3336 | fastboot_fail("failed to erase partition"); |
| 3337 | return; |
| 3338 | } |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 3339 | /*Erase FDE metadata at the userdata footer*/ |
| 3340 | if(!(strncmp(arg, "userdata", 8))) |
| 3341 | { |
| 3342 | footer = memalign(CACHE_LINE, FOOTER_SIZE); |
| 3343 | memset((void *)footer, 0, FOOTER_SIZE); |
| 3344 | |
| 3345 | size = partition_get_size(index); |
| 3346 | |
| 3347 | if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) { |
| 3348 | fastboot_fail("failed to erase userdata footer"); |
| 3349 | free(footer); |
| 3350 | return; |
| 3351 | } |
| 3352 | free(footer); |
| 3353 | } |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3354 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3355 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3356 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3357 | !(strncmp(arg, "userdata", 8)) && |
| 3358 | send_delete_keys_to_tz()) |
Sridhar Parasuram | 32b3066 | 2015-07-10 13:33:22 -0700 | [diff] [blame] | 3359 | ASSERT(0); |
| 3360 | #endif |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3361 | fastboot_okay(""); |
| 3362 | } |
| 3363 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3364 | void cmd_erase(const char *arg, void *data, unsigned sz) |
| 3365 | { |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3366 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 3367 | if (target_build_variant_user()) |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 3368 | { |
Sridhar Parasuram | c32d07d | 2015-07-12 10:57:48 -0700 | [diff] [blame] | 3369 | if(!device.is_unlocked) |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 3370 | { |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 3371 | fastboot_fail("device is locked. Cannot erase"); |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 3372 | return; |
| 3373 | } |
| 3374 | } |
| 3375 | #endif |
| 3376 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3377 | if(target_is_emmc_boot()) |
| 3378 | cmd_erase_mmc(arg, data, sz); |
| 3379 | else |
| 3380 | cmd_erase_nand(arg, data, sz); |
| 3381 | } |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3382 | |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 3383 | /* Get the size from partiton name */ |
| 3384 | static void get_partition_size(const char *arg, char *response) |
| 3385 | { |
| 3386 | uint64_t ptn = 0; |
| 3387 | uint64_t size; |
| 3388 | int index = INVALID_PTN; |
| 3389 | |
| 3390 | index = partition_get_index(arg); |
| 3391 | |
| 3392 | if (index == INVALID_PTN) |
| 3393 | { |
| 3394 | dprintf(CRITICAL, "Invalid partition index\n"); |
| 3395 | return; |
| 3396 | } |
| 3397 | |
| 3398 | ptn = partition_get_offset(index); |
| 3399 | |
| 3400 | if(!ptn) |
| 3401 | { |
| 3402 | dprintf(CRITICAL, "Invalid partition name %s\n", arg); |
| 3403 | return; |
| 3404 | } |
| 3405 | |
| 3406 | size = partition_get_size(index); |
| 3407 | |
| 3408 | snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size); |
| 3409 | return; |
| 3410 | } |
| 3411 | |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3412 | /* Function to check partition type of a partition*/ |
| 3413 | static fs_signature_type |
| 3414 | check_partition_fs_signature(const char *arg) |
| 3415 | { |
| 3416 | fs_signature_type ret = NO_FS; |
| 3417 | int index; |
| 3418 | unsigned long long ptn; |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3419 | char *buffer = memalign(CACHE_LINE, mmc_blocksize); |
| 3420 | uint32_t sb_blk_offset = 0; |
| 3421 | char *sb_buffer = buffer; |
| 3422 | |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3423 | if (!sb_buffer) |
| 3424 | { |
| 3425 | dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n"); |
| 3426 | goto out; |
| 3427 | } |
| 3428 | |
| 3429 | /* Read super block */ |
| 3430 | if ((index = partition_get_index(arg)) < 0) |
| 3431 | { |
| 3432 | dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg); |
| 3433 | goto out; |
| 3434 | } |
| 3435 | ptn = partition_get_offset(index); |
| 3436 | mmc_set_lun(partition_get_lun(index)); |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3437 | sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize); |
| 3438 | |
| 3439 | if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize), |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3440 | (void *)sb_buffer, mmc_blocksize)) |
| 3441 | { |
| 3442 | dprintf(CRITICAL, "ERROR: Failed to read Superblock\n"); |
| 3443 | goto out; |
| 3444 | } |
| 3445 | |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3446 | if (sb_blk_offset == 0) |
| 3447 | sb_buffer += FS_SUPERBLOCK_OFFSET; |
| 3448 | |
| 3449 | if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC) |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3450 | { |
| 3451 | dprintf(SPEW, "%s() Found EXT FS\n", arg); |
| 3452 | ret = EXT_FS_SIGNATURE; |
| 3453 | } |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3454 | else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC) |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3455 | { |
| 3456 | dprintf(SPEW, "%s() Found F2FS FS\n", arg); |
| 3457 | ret = EXT_F2FS_SIGNATURE; |
| 3458 | } |
| 3459 | else |
| 3460 | { |
| 3461 | dprintf(SPEW, "%s() Reverting to default 0x%x\n", |
| 3462 | arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB]))); |
| 3463 | ret = NO_FS; |
| 3464 | } |
| 3465 | |
| 3466 | out: |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3467 | if(buffer) |
| 3468 | free(buffer); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3469 | return ret; |
| 3470 | } |
| 3471 | |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 3472 | /* Function to get partition type */ |
| 3473 | static void get_partition_type(const char *arg, char *response) |
| 3474 | { |
| 3475 | uint n = 0; |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3476 | fs_signature_type fs_signature; |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 3477 | |
| 3478 | if (arg == NULL || |
| 3479 | response == NULL) |
| 3480 | { |
| 3481 | dprintf(CRITICAL, "Invalid input parameter\n"); |
| 3482 | return; |
| 3483 | } |
| 3484 | |
| 3485 | /* By default copy raw to response */ |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 3486 | strlcpy(response, RAW_STR, MAX_RSP_SIZE); |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 3487 | |
| 3488 | /* Mark partiton type for known paritions only */ |
| 3489 | for (n=0; n < ARRAY_SIZE(part_type_known); n++) |
| 3490 | { |
| 3491 | if (!strncmp(part_type_known[n].part_name, arg, strlen(arg))) |
| 3492 | { |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3493 | /* Check partition for FS signature */ |
| 3494 | fs_signature = check_partition_fs_signature(arg); |
| 3495 | switch (fs_signature) |
| 3496 | { |
| 3497 | case EXT_FS_SIGNATURE: |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 3498 | strlcpy(response, EXT_STR, MAX_RSP_SIZE); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3499 | break; |
| 3500 | case EXT_F2FS_SIGNATURE: |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 3501 | strlcpy(response, F2FS_STR, MAX_RSP_SIZE); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3502 | break; |
| 3503 | case NO_FS: |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 3504 | strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3505 | } |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 3506 | } |
| 3507 | } |
| 3508 | return; |
| 3509 | } |
| 3510 | |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 3511 | /* |
| 3512 | * Publish the partition type & size info |
| 3513 | * fastboot getvar will publish the required information. |
| 3514 | * fastboot getvar partition_size:<partition_name>: partition size in hex |
| 3515 | * fastboot getvar partition_type:<partition_name>: partition type (ext/fat) |
| 3516 | */ |
| 3517 | static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts) |
| 3518 | { |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 3519 | uint8_t i; |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 3520 | static bool published = false; |
| 3521 | struct partition_entry *ptn_entry = |
| 3522 | partition_get_partition_entries(); |
| 3523 | memset(info, 0, sizeof(struct getvar_partition_info)* num_parts); |
| 3524 | |
| 3525 | for (i = 0; i < num_parts; i++) { |
| 3526 | strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE); |
| 3527 | strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE); |
| 3528 | strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE); |
| 3529 | |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 3530 | get_partition_type(info[i].part_name, info[i].type_response); |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 3531 | get_partition_size(info[i].part_name, info[i].size_response); |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 3532 | if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE) |
| 3533 | { |
| 3534 | dprintf(CRITICAL, "partition size name truncated\n"); |
| 3535 | return; |
| 3536 | } |
| 3537 | if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE) |
| 3538 | { |
| 3539 | dprintf(CRITICAL, "partition type name truncated\n"); |
| 3540 | return; |
| 3541 | } |
| 3542 | |
| 3543 | if (!published) |
| 3544 | { |
| 3545 | /* publish partition size & type info */ |
| 3546 | fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response); |
| 3547 | fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response); |
| 3548 | } |
| 3549 | } |
| 3550 | if (!published) |
| 3551 | published = true; |
| 3552 | } |
| 3553 | |
| 3554 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3555 | void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz) |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 3556 | { |
| 3557 | unsigned long long ptn = 0; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 3558 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3559 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3560 | char *token = NULL; |
| 3561 | char *pname = NULL; |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 3562 | char *sp; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3563 | uint8_t lun = 0; |
| 3564 | bool lun_set = false; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 3565 | int current_active_slot = INVALID; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 3566 | |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 3567 | token = strtok_r((char *)arg, ":", &sp); |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3568 | pname = token; |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 3569 | token = strtok_r(NULL, ":", &sp); |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3570 | if(token) |
| 3571 | { |
| 3572 | lun = atoi(token); |
| 3573 | mmc_set_lun(lun); |
| 3574 | lun_set = true; |
| 3575 | } |
| 3576 | |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 3577 | if (pname) |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 3578 | { |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 3579 | if (!strncmp(pname, "frp-unlock", strlen("frp-unlock"))) |
| 3580 | { |
| 3581 | if (!aboot_frp_unlock(pname, data, sz)) |
| 3582 | { |
| 3583 | fastboot_info("FRP unlock successful"); |
| 3584 | fastboot_okay(""); |
| 3585 | } |
| 3586 | else |
| 3587 | fastboot_fail("Secret key is invalid, please update the bootloader with secret key"); |
| 3588 | |
| 3589 | return; |
| 3590 | } |
| 3591 | |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 3592 | if (!strcmp(pname, "partition")) |
| 3593 | { |
| 3594 | dprintf(INFO, "Attempt to write partition image.\n"); |
| 3595 | if (write_partition(sz, (unsigned char *) data)) { |
| 3596 | fastboot_fail("failed to write partition"); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 3597 | return; |
| 3598 | } |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 3599 | /* Re-publish partition table */ |
| 3600 | publish_getvar_partition_info(part_info, partition_get_partition_count()); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 3601 | |
| 3602 | /* Rescan partition table to ensure we have multislot support*/ |
| 3603 | if (partition_scan_for_multislot()) |
| 3604 | { |
| 3605 | current_active_slot = partition_find_active_slot(); |
| 3606 | dprintf(INFO, "Multislot supported: Slot %s active", |
| 3607 | (SUFFIX_SLOT(current_active_slot))); |
| 3608 | } |
| 3609 | partition_mark_active_slot(current_active_slot); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 3610 | } |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 3611 | else |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3612 | { |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3613 | #if VERIFIED_BOOT |
| 3614 | if(!strcmp(pname, KEYSTORE_PTN_NAME)) |
| 3615 | { |
| 3616 | if(!device.is_unlocked) |
| 3617 | { |
| 3618 | fastboot_fail("unlock device to flash keystore"); |
| 3619 | return; |
| 3620 | } |
Gaurav Nebhwani | c7313cc | 2015-12-15 22:25:04 +0530 | [diff] [blame] | 3621 | if(!boot_verify_validate_keystore((unsigned char *)data,sz)) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3622 | { |
| 3623 | fastboot_fail("image is not a keystore file"); |
| 3624 | return; |
| 3625 | } |
| 3626 | } |
| 3627 | #endif |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 3628 | index = partition_get_index(pname); |
| 3629 | ptn = partition_get_offset(index); |
| 3630 | if(ptn == 0) { |
| 3631 | fastboot_fail("partition table doesn't exist"); |
| 3632 | return; |
| 3633 | } |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3634 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 3635 | if (!strncmp(pname, "boot", strlen("boot")) |
| 3636 | || !strcmp(pname, "recovery")) |
| 3637 | { |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 3638 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 3639 | fastboot_fail("image is not a boot image"); |
| 3640 | return; |
| 3641 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 3642 | |
| 3643 | /* Reset multislot_partition attributes in case of flashing boot */ |
| 3644 | if (partition_multislot_is_supported()) |
| 3645 | { |
| 3646 | partition_reset_attributes(index); |
| 3647 | } |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 3648 | } |
| 3649 | |
| 3650 | if(!lun_set) |
| 3651 | { |
| 3652 | lun = partition_get_lun(index); |
| 3653 | mmc_set_lun(lun); |
| 3654 | } |
| 3655 | |
| 3656 | size = partition_get_size(index); |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 3657 | if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) { |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 3658 | fastboot_fail("size too large"); |
| 3659 | return; |
| 3660 | } |
| 3661 | else if (mmc_write(ptn , sz, (unsigned int *)data)) { |
| 3662 | fastboot_fail("flash write failure"); |
| 3663 | return; |
| 3664 | } |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 3665 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 3666 | } |
| 3667 | fastboot_okay(""); |
| 3668 | return; |
| 3669 | } |
| 3670 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 3671 | void cmd_flash_meta_img(const char *arg, void *data, unsigned sz) |
| 3672 | { |
| 3673 | int i, images; |
| 3674 | meta_header_t *meta_header; |
| 3675 | img_header_entry_t *img_header_entry; |
Parth Dixit | 3e2d303 | 2016-03-04 17:11:52 +0530 | [diff] [blame] | 3676 | /*End of the image address*/ |
| 3677 | uintptr_t data_end; |
| 3678 | |
| 3679 | if( (UINT_MAX - sz) > (uintptr_t)data ) |
| 3680 | data_end = (uintptr_t)data + sz; |
| 3681 | else |
| 3682 | { |
| 3683 | fastboot_fail("Cannot flash: image header corrupt"); |
| 3684 | return; |
| 3685 | } |
| 3686 | |
| 3687 | if( data_end < ((uintptr_t)data + sizeof(meta_header_t))) |
| 3688 | { |
| 3689 | fastboot_fail("Cannot flash: image header corrupt"); |
| 3690 | return; |
| 3691 | } |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 3692 | |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 3693 | /* If device is locked: |
| 3694 | * Forbid to flash image to avoid the device to bypass the image |
| 3695 | * which with "any" name other than bootloader. Because it maybe |
| 3696 | * a meta package of all partitions. |
| 3697 | */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3698 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 3699 | if (target_build_variant_user()) { |
| 3700 | if (!device.is_unlocked) { |
| 3701 | fastboot_fail("Device is locked, meta image flashing is not allowed"); |
| 3702 | return; |
| 3703 | } |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3704 | |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3705 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3706 | !device.is_unlock_critical) |
| 3707 | { |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 3708 | fastboot_fail("Device is critical locked, Meta image flashing is not allowed"); |
| 3709 | return; |
| 3710 | } |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 3711 | } |
| 3712 | #endif |
| 3713 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 3714 | meta_header = (meta_header_t*) data; |
Parth Dixit | 3e2d303 | 2016-03-04 17:11:52 +0530 | [diff] [blame] | 3715 | if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz)) |
| 3716 | { |
| 3717 | fastboot_fail("Cannot flash: image header corrupt"); |
| 3718 | return; |
| 3719 | } |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 3720 | img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t)); |
| 3721 | |
| 3722 | images = meta_header->img_hdr_sz / sizeof(img_header_entry_t); |
| 3723 | |
| 3724 | for (i=0; i<images; i++) { |
| 3725 | |
| 3726 | if((img_header_entry[i].ptn_name == NULL) || |
| 3727 | (img_header_entry[i].start_offset == 0) || |
| 3728 | (img_header_entry[i].size == 0)) |
| 3729 | break; |
Kishor PK | 4983150 | 2017-04-21 17:55:43 +0530 | [diff] [blame] | 3730 | if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) { |
| 3731 | fastboot_fail("Integer overflow detected in start_offset of img"); |
| 3732 | break; |
| 3733 | } |
| 3734 | else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) { |
| 3735 | fastboot_fail("Integer overflow detected in size of img"); |
| 3736 | break; |
| 3737 | } |
Parth Dixit | 3e2d303 | 2016-03-04 17:11:52 +0530 | [diff] [blame] | 3738 | if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset |
| 3739 | + img_header_entry[i].size) ) |
| 3740 | { |
| 3741 | fastboot_fail("Cannot flash: image size mismatch"); |
| 3742 | break; |
| 3743 | } |
| 3744 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 3745 | cmd_flash_mmc_img(img_header_entry[i].ptn_name, |
| 3746 | (void *) data + img_header_entry[i].start_offset, |
| 3747 | img_header_entry[i].size); |
| 3748 | } |
| 3749 | |
Channagoud Kadabi | aafbbfc | 2015-03-04 17:52:56 -0800 | [diff] [blame] | 3750 | if (!strncmp(arg, "bootloader", strlen("bootloader"))) |
| 3751 | { |
| 3752 | strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN); |
| 3753 | strlcat(device.bootloader_version, "-", MAX_VERSION_LEN); |
| 3754 | strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN); |
| 3755 | } |
| 3756 | else |
| 3757 | { |
| 3758 | strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN); |
| 3759 | strlcat(device.radio_version, "-", MAX_VERSION_LEN); |
| 3760 | strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN); |
| 3761 | } |
| 3762 | |
| 3763 | write_device_info(&device); |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 3764 | fastboot_okay(""); |
| 3765 | return; |
| 3766 | } |
| 3767 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3768 | void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz) |
| 3769 | { |
| 3770 | unsigned int chunk; |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 3771 | uint64_t chunk_data_sz; |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 3772 | uint32_t *fill_buf = NULL; |
| 3773 | uint32_t fill_val; |
Mayank Grover | 4f50bba | 2017-07-19 18:17:08 +0530 | [diff] [blame] | 3774 | uint32_t blk_sz_actual = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3775 | sparse_header_t *sparse_header; |
| 3776 | chunk_header_t *chunk_header; |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 3777 | uint32_t total_blocks = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3778 | unsigned long long ptn = 0; |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 3779 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3780 | int index = INVALID_PTN; |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 3781 | uint32_t i; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3782 | uint8_t lun = 0; |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3783 | /*End of the sparse image address*/ |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 3784 | uintptr_t data_end = (uintptr_t)data + sz; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3785 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3786 | index = partition_get_index(arg); |
| 3787 | ptn = partition_get_offset(index); |
| 3788 | if(ptn == 0) { |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3789 | fastboot_fail("partition table doesn't exist"); |
| 3790 | return; |
| 3791 | } |
| 3792 | |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 3793 | size = partition_get_size(index); |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 3794 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3795 | lun = partition_get_lun(index); |
| 3796 | mmc_set_lun(lun); |
| 3797 | |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3798 | if (sz < sizeof(sparse_header_t)) { |
| 3799 | fastboot_fail("size too low"); |
| 3800 | return; |
| 3801 | } |
| 3802 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3803 | /* Read and skip over sparse image header */ |
| 3804 | sparse_header = (sparse_header_t *) data; |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3805 | |
Mayank Grover | 48bd9bb | 2017-07-19 12:04:16 +0530 | [diff] [blame] | 3806 | if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){ |
| 3807 | fastboot_fail("Invalid block size\n"); |
| 3808 | return; |
| 3809 | } |
| 3810 | |
vijay kumar | 1321f34 | 2015-03-27 12:13:42 +0530 | [diff] [blame] | 3811 | 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] | 3812 | fastboot_fail("size too large"); |
| 3813 | return; |
| 3814 | } |
| 3815 | |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 3816 | data += sizeof(sparse_header_t); |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3817 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 3818 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3819 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 3820 | return; |
| 3821 | } |
| 3822 | |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 3823 | if(sparse_header->file_hdr_sz != sizeof(sparse_header_t)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3824 | { |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 3825 | fastboot_fail("sparse header size mismatch"); |
| 3826 | return; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3827 | } |
| 3828 | |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 3829 | dprintf (SPEW, "=== Sparse Image Header ===\n"); |
| 3830 | dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic); |
| 3831 | dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version); |
| 3832 | dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version); |
| 3833 | dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz); |
| 3834 | dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz); |
| 3835 | dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz); |
| 3836 | dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks); |
| 3837 | dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3838 | |
| 3839 | /* Start processing chunks */ |
| 3840 | for (chunk=0; chunk<sparse_header->total_chunks; chunk++) |
| 3841 | { |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 3842 | /* Make sure the total image size does not exceed the partition size */ |
| 3843 | if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) { |
| 3844 | fastboot_fail("size too large"); |
| 3845 | return; |
| 3846 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3847 | /* Read and skip over chunk header */ |
| 3848 | chunk_header = (chunk_header_t *) data; |
| 3849 | data += sizeof(chunk_header_t); |
| 3850 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 3851 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3852 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 3853 | return; |
| 3854 | } |
| 3855 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3856 | dprintf (SPEW, "=== Chunk Header ===\n"); |
| 3857 | dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type); |
| 3858 | dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz); |
| 3859 | dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz); |
| 3860 | |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3861 | if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3862 | { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3863 | fastboot_fail("chunk header size mismatch"); |
| 3864 | return; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3865 | } |
| 3866 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 3867 | chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz; |
| 3868 | |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 3869 | /* Make sure that the chunk size calculated from sparse image does not |
| 3870 | * exceed partition size |
| 3871 | */ |
| 3872 | if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size) |
| 3873 | { |
| 3874 | fastboot_fail("Chunk data size exceeds partition size"); |
| 3875 | return; |
| 3876 | } |
| 3877 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3878 | switch (chunk_header->chunk_type) |
| 3879 | { |
| 3880 | case CHUNK_TYPE_RAW: |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 3881 | 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] | 3882 | chunk_data_sz)) |
| 3883 | { |
| 3884 | fastboot_fail("Bogus chunk size for chunk type Raw"); |
| 3885 | return; |
| 3886 | } |
| 3887 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 3888 | if (data_end < (uintptr_t)data + chunk_data_sz) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3889 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 3890 | return; |
| 3891 | } |
| 3892 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 3893 | /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32 |
| 3894 | otherwise it will return in the line above |
| 3895 | */ |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 3896 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 3897 | (uint32_t)chunk_data_sz, |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 3898 | (unsigned int*)data)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3899 | { |
| 3900 | fastboot_fail("flash write failure"); |
| 3901 | return; |
| 3902 | } |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 3903 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 3904 | fastboot_fail("Bogus size for RAW chunk type"); |
| 3905 | return; |
| 3906 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3907 | total_blocks += chunk_header->chunk_sz; |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 3908 | data += (uint32_t)chunk_data_sz; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3909 | break; |
| 3910 | |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 3911 | case CHUNK_TYPE_FILL: |
| 3912 | if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz + |
| 3913 | sizeof(uint32_t))) |
| 3914 | { |
| 3915 | fastboot_fail("Bogus chunk size for chunk type FILL"); |
| 3916 | return; |
| 3917 | } |
| 3918 | |
Mayank Grover | 4f50bba | 2017-07-19 18:17:08 +0530 | [diff] [blame] | 3919 | blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE); |
| 3920 | /* Integer overflow detected */ |
| 3921 | if (blk_sz_actual < sparse_header->blk_sz) |
| 3922 | { |
| 3923 | fastboot_fail("Invalid block size"); |
| 3924 | return; |
| 3925 | } |
| 3926 | |
| 3927 | fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual); |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 3928 | if (!fill_buf) |
| 3929 | { |
| 3930 | fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL"); |
| 3931 | return; |
| 3932 | } |
| 3933 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 3934 | if (data_end < (uintptr_t)data + sizeof(uint32_t)) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3935 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
Gaurav Nebhwani | 163cbf8 | 2016-03-17 21:21:27 +0530 | [diff] [blame] | 3936 | free(fill_buf); |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3937 | return; |
| 3938 | } |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 3939 | fill_val = *(uint32_t *)data; |
| 3940 | data = (char *) data + sizeof(uint32_t); |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 3941 | |
| 3942 | for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++) |
| 3943 | { |
| 3944 | fill_buf[i] = fill_val; |
| 3945 | } |
| 3946 | |
Gaurav Nebhwani | e94cbe1 | 2016-03-17 21:16:34 +0530 | [diff] [blame] | 3947 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) |
| 3948 | { |
| 3949 | fastboot_fail("bogus size for chunk FILL type"); |
| 3950 | free(fill_buf); |
| 3951 | return; |
| 3952 | } |
| 3953 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 3954 | for (i = 0; i < chunk_header->chunk_sz; i++) |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 3955 | { |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 3956 | /* Make sure that the data written to partition does not exceed partition size */ |
| 3957 | if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size) |
| 3958 | { |
| 3959 | fastboot_fail("Chunk data size for fill type exceeds partition size"); |
Gaurav Nebhwani | 163cbf8 | 2016-03-17 21:21:27 +0530 | [diff] [blame] | 3960 | free(fill_buf); |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 3961 | return; |
| 3962 | } |
| 3963 | |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 3964 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
| 3965 | sparse_header->blk_sz, |
| 3966 | fill_buf)) |
| 3967 | { |
| 3968 | fastboot_fail("flash write failure"); |
| 3969 | free(fill_buf); |
| 3970 | return; |
| 3971 | } |
| 3972 | |
| 3973 | total_blocks++; |
| 3974 | } |
| 3975 | |
| 3976 | free(fill_buf); |
| 3977 | break; |
| 3978 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3979 | case CHUNK_TYPE_DONT_CARE: |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 3980 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 3981 | fastboot_fail("bogus size for chunk DONT CARE type"); |
| 3982 | return; |
| 3983 | } |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 3984 | total_blocks += chunk_header->chunk_sz; |
| 3985 | break; |
| 3986 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3987 | case CHUNK_TYPE_CRC: |
| 3988 | if(chunk_header->total_sz != sparse_header->chunk_hdr_sz) |
| 3989 | { |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 3990 | fastboot_fail("Bogus chunk size for chunk type CRC"); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3991 | return; |
| 3992 | } |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 3993 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 3994 | fastboot_fail("bogus size for chunk CRC type"); |
| 3995 | return; |
| 3996 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 3997 | total_blocks += chunk_header->chunk_sz; |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 3998 | if ((uintptr_t)data > UINT_MAX - chunk_data_sz) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 3999 | fastboot_fail("integer overflow occured"); |
| 4000 | return; |
| 4001 | } |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4002 | data += (uint32_t)chunk_data_sz; |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4003 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4004 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 4005 | return; |
| 4006 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4007 | break; |
| 4008 | |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 4009 | default: |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4010 | dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4011 | fastboot_fail("Unknown chunk type"); |
| 4012 | return; |
| 4013 | } |
| 4014 | } |
| 4015 | |
Ajay Dudani | 0c6927b | 2011-05-18 11:12:16 -0700 | [diff] [blame] | 4016 | dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n", |
| 4017 | total_blocks, sparse_header->total_blks); |
| 4018 | |
| 4019 | if(total_blocks != sparse_header->total_blks) |
| 4020 | { |
| 4021 | fastboot_fail("sparse image write failure"); |
| 4022 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4023 | |
| 4024 | fastboot_okay(""); |
| 4025 | return; |
| 4026 | } |
| 4027 | |
| 4028 | void cmd_flash_mmc(const char *arg, void *data, unsigned sz) |
| 4029 | { |
| 4030 | sparse_header_t *sparse_header; |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4031 | meta_header_t *meta_header; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4032 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 4033 | #ifdef SSD_ENABLE |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 4034 | /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */ |
| 4035 | unsigned int *magic_number = (unsigned int *) data; |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4036 | int ret=0; |
| 4037 | uint32 major_version=0; |
| 4038 | uint32 minor_version=0; |
| 4039 | |
| 4040 | ret = scm_svc_version(&major_version,&minor_version); |
| 4041 | if(!ret) |
| 4042 | { |
| 4043 | if(major_version >= 2) |
| 4044 | { |
Amir Samuelov | bb65ce0 | 2013-05-05 12:20:18 +0300 | [diff] [blame] | 4045 | if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") ) |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4046 | { |
| 4047 | ret = encrypt_scm((uint32 **) &data, &sz); |
| 4048 | if (ret != 0) { |
| 4049 | dprintf(CRITICAL, "ERROR: Encryption Failure\n"); |
| 4050 | return; |
| 4051 | } |
| 4052 | |
Amir Samuelov | bb65ce0 | 2013-05-05 12:20:18 +0300 | [diff] [blame] | 4053 | /* Protect only for SSD */ |
| 4054 | if (!strcmp(arg, "ssd")) { |
| 4055 | ret = scm_protect_keystore((uint32 *) data, sz); |
| 4056 | if (ret != 0) { |
| 4057 | dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n"); |
| 4058 | return; |
| 4059 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4060 | } |
| 4061 | } |
| 4062 | else |
| 4063 | { |
| 4064 | ret = decrypt_scm_v2((uint32 **) &data, &sz); |
| 4065 | if(ret != 0) |
| 4066 | { |
| 4067 | dprintf(CRITICAL,"ERROR: Decryption Failure\n"); |
| 4068 | return; |
| 4069 | } |
| 4070 | } |
| 4071 | } |
| 4072 | else |
| 4073 | { |
| 4074 | if (magic_number[0] == DECRYPT_MAGIC_0 && |
| 4075 | magic_number[1] == DECRYPT_MAGIC_1) |
| 4076 | { |
| 4077 | ret = decrypt_scm((uint32 **) &data, &sz); |
| 4078 | if (ret != 0) { |
| 4079 | dprintf(CRITICAL, "ERROR: Invalid secure image\n"); |
| 4080 | return; |
| 4081 | } |
| 4082 | } |
| 4083 | else if (magic_number[0] == ENCRYPT_MAGIC_0 && |
| 4084 | magic_number[1] == ENCRYPT_MAGIC_1) |
| 4085 | { |
| 4086 | ret = encrypt_scm((uint32 **) &data, &sz); |
| 4087 | if (ret != 0) { |
| 4088 | dprintf(CRITICAL, "ERROR: Encryption Failure\n"); |
| 4089 | return; |
| 4090 | } |
| 4091 | } |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 4092 | } |
| 4093 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4094 | else |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 4095 | { |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4096 | dprintf(CRITICAL,"INVALID SVC Version\n"); |
| 4097 | return; |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 4098 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4099 | #endif /* SSD_ENABLE */ |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 4100 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4101 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 4102 | if (target_build_variant_user()) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4103 | { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4104 | /* if device is locked: |
| 4105 | * common partition will not allow to be flashed |
| 4106 | * critical partition will allow to flash image. |
| 4107 | */ |
| 4108 | if(!device.is_unlocked && !critical_flash_allowed(arg)) { |
| 4109 | fastboot_fail("Partition flashing is not allowed"); |
| 4110 | return; |
| 4111 | } |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4112 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4113 | /* if device critical is locked: |
| 4114 | * common partition will allow to be flashed |
| 4115 | * critical partition will not allow to flash image. |
| 4116 | */ |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 4117 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4118 | !device.is_unlock_critical && |
| 4119 | critical_flash_allowed(arg)) { |
| 4120 | fastboot_fail("Critical partition flashing is not allowed"); |
| 4121 | return; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4122 | } |
| 4123 | } |
| 4124 | #endif |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4125 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 4126 | dprintf(INFO, "flashing avb_custom_key\n"); |
| 4127 | if (store_userkey(data, sz)) { |
| 4128 | fastboot_fail("Flashing avb_custom_key failed"); |
| 4129 | } else { |
| 4130 | fastboot_okay(""); |
| 4131 | } |
| 4132 | return; |
| 4133 | } |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4134 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 4135 | sparse_header = (sparse_header_t *) data; |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4136 | meta_header = (meta_header_t *) data; |
| 4137 | if (sparse_header->magic == SPARSE_HEADER_MAGIC) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4138 | cmd_flash_mmc_sparse_img(arg, data, sz); |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4139 | else if (meta_header->magic == META_HEADER_MAGIC) |
| 4140 | cmd_flash_meta_img(arg, data, sz); |
| 4141 | else |
| 4142 | cmd_flash_mmc_img(arg, data, sz); |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 4143 | |
| 4144 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 4145 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4146 | (!strncmp(arg, "system", 6)) && |
| 4147 | !device.verity_mode) |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 4148 | // reset dm_verity mode to enforcing |
| 4149 | device.verity_mode = 1; |
| 4150 | write_device_info(&device); |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 4151 | #endif |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 4152 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4153 | return; |
| 4154 | } |
| 4155 | |
Tanya Brokhman | 84eaaf6 | 2015-02-24 21:57:06 +0200 | [diff] [blame] | 4156 | void cmd_updatevol(const char *vol_name, void *data, unsigned sz) |
| 4157 | { |
| 4158 | struct ptentry *sys_ptn; |
| 4159 | struct ptable *ptable; |
| 4160 | |
| 4161 | ptable = flash_get_ptable(); |
| 4162 | if (ptable == NULL) { |
| 4163 | fastboot_fail("partition table doesn't exist"); |
| 4164 | return; |
| 4165 | } |
| 4166 | |
| 4167 | sys_ptn = ptable_find(ptable, "system"); |
| 4168 | if (sys_ptn == NULL) { |
| 4169 | fastboot_fail("system partition not found"); |
| 4170 | return; |
| 4171 | } |
| 4172 | |
| 4173 | sz = ROUND_TO_PAGE(sz, page_mask); |
| 4174 | if (update_ubi_vol(sys_ptn, vol_name, data, sz)) |
| 4175 | fastboot_fail("update_ubi_vol failed"); |
| 4176 | else |
| 4177 | fastboot_okay(""); |
| 4178 | } |
| 4179 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 4180 | void cmd_flash_nand(const char *arg, void *data, unsigned sz) |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4181 | { |
| 4182 | struct ptentry *ptn; |
| 4183 | struct ptable *ptable; |
| 4184 | unsigned extra = 0; |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 4185 | uint64_t partition_size = 0; |
Vijay Kumar Pendoti | c6f22a7 | 2017-05-19 22:44:34 +0530 | [diff] [blame] | 4186 | unsigned bytes_to_round_page = 0; |
| 4187 | unsigned rounded_size = 0; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4188 | |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4189 | if((uintptr_t)data > (UINT_MAX - sz)) { |
| 4190 | fastboot_fail("Cannot flash: image header corrupt"); |
| 4191 | return; |
| 4192 | } |
| 4193 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4194 | ptable = flash_get_ptable(); |
| 4195 | if (ptable == NULL) { |
| 4196 | fastboot_fail("partition table doesn't exist"); |
| 4197 | return; |
| 4198 | } |
| 4199 | |
| 4200 | ptn = ptable_find(ptable, arg); |
| 4201 | if (ptn == NULL) { |
Tanya Brokhman | 84eaaf6 | 2015-02-24 21:57:06 +0200 | [diff] [blame] | 4202 | dprintf(INFO, "unknown partition name (%s). Trying updatevol\n", |
| 4203 | arg); |
| 4204 | cmd_updatevol(arg, data, sz); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4205 | return; |
| 4206 | } |
| 4207 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4208 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 4209 | dprintf(INFO, "flashing avb_custom_key\n"); |
| 4210 | if (store_userkey(data, sz)) { |
| 4211 | fastboot_fail("Flashing avb_custom_key failed"); |
| 4212 | } else { |
| 4213 | fastboot_okay(""); |
| 4214 | } |
| 4215 | return; |
| 4216 | } |
| 4217 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4218 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4219 | if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) { |
| 4220 | dprintf(INFO, "Verified the BOOT_MAGIC in image header \n"); |
| 4221 | } else { |
| 4222 | fastboot_fail("Image is not a boot image"); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4223 | return; |
| 4224 | } |
| 4225 | } |
| 4226 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 4227 | if (!strcmp(ptn->name, "system") |
Deepa Dinamani | 13e32c4 | 2012-03-12 14:34:17 -0700 | [diff] [blame] | 4228 | || !strcmp(ptn->name, "userdata") |
| 4229 | || !strcmp(ptn->name, "persist") |
Sundarajan Srinivasan | b063a85 | 2013-11-19 14:02:27 -0800 | [diff] [blame] | 4230 | || !strcmp(ptn->name, "recoveryfs") |
Sundarajan Srinivasan | 595b71e | 2013-11-05 12:44:34 -0800 | [diff] [blame] | 4231 | || !strcmp(ptn->name, "modem")) |
Tanya Brokhman | 1c94f1a | 2015-02-15 09:05:03 +0200 | [diff] [blame] | 4232 | extra = 1; |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4233 | else { |
Vijay Kumar Pendoti | c6f22a7 | 2017-05-19 22:44:34 +0530 | [diff] [blame] | 4234 | rounded_size = ROUNDUP(sz, page_size); |
| 4235 | bytes_to_round_page = rounded_size - sz; |
| 4236 | if (bytes_to_round_page) { |
| 4237 | if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) { |
| 4238 | fastboot_fail("Integer overflow detected"); |
| 4239 | return; |
| 4240 | } |
| 4241 | if (((uintptr_t)data + sz + bytes_to_round_page) > |
| 4242 | ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) { |
| 4243 | fastboot_fail("Buffer size is not aligned to page_size"); |
| 4244 | return; |
| 4245 | } |
| 4246 | else { |
| 4247 | memset(data + sz, 0, bytes_to_round_page); |
| 4248 | sz = rounded_size; |
| 4249 | } |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4250 | } |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 4251 | } |
| 4252 | |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4253 | /*Checking partition_size for the possible integer overflow */ |
| 4254 | partition_size = validate_partition_size(ptn); |
| 4255 | |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 4256 | if (sz > partition_size) { |
| 4257 | fastboot_fail("Image size too large"); |
| 4258 | return; |
| 4259 | } |
| 4260 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4261 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
Mayank Grover | 6cde935 | 2017-06-06 18:45:23 +0530 | [diff] [blame] | 4262 | if ((sz > UBI_EC_HDR_SIZE) && |
| 4263 | (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) { |
Tanya Brokhman | 1c94f1a | 2015-02-15 09:05:03 +0200 | [diff] [blame] | 4264 | if (flash_ubi_img(ptn, data, sz)) { |
| 4265 | fastboot_fail("flash write failure"); |
| 4266 | return; |
| 4267 | } |
| 4268 | } else { |
| 4269 | if (flash_write(ptn, extra, data, sz)) { |
| 4270 | fastboot_fail("flash write failure"); |
| 4271 | return; |
| 4272 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4273 | } |
| 4274 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 4275 | fastboot_okay(""); |
| 4276 | } |
| 4277 | |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4278 | |
| 4279 | static inline uint64_t validate_partition_size(struct ptentry *ptn) |
| 4280 | { |
| 4281 | if (ptn->length && flash_num_pages_per_blk() && page_size) { |
| 4282 | if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) { |
| 4283 | return ptn->length * flash_num_pages_per_blk() * page_size; |
| 4284 | } |
| 4285 | } |
| 4286 | return 0; |
| 4287 | } |
| 4288 | |
| 4289 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 4290 | void cmd_flash(const char *arg, void *data, unsigned sz) |
| 4291 | { |
| 4292 | if(target_is_emmc_boot()) |
| 4293 | cmd_flash_mmc(arg, data, sz); |
| 4294 | else |
| 4295 | cmd_flash_nand(arg, data, sz); |
| 4296 | } |
| 4297 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4298 | void cmd_continue(const char *arg, void *data, unsigned sz) |
| 4299 | { |
| 4300 | fastboot_okay(""); |
Amol Jadi | 7c4316c | 2013-10-07 14:19:26 -0700 | [diff] [blame] | 4301 | fastboot_stop(); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4302 | |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4303 | if (target_is_emmc_boot()) |
| 4304 | { |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 4305 | #if FBCON_DISPLAY_MSG |
lijuang | de34d50 | 2016-02-26 16:04:50 +0800 | [diff] [blame] | 4306 | /* Exit keys' detection thread firstly */ |
| 4307 | exit_menu_keys_detection(); |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 4308 | #endif |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4309 | boot_linux_from_mmc(); |
| 4310 | } |
| 4311 | else |
| 4312 | { |
| 4313 | boot_linux_from_flash(); |
| 4314 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4315 | } |
| 4316 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 4317 | void cmd_reboot(const char *arg, void *data, unsigned sz) |
| 4318 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4319 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 4320 | fastboot_okay(""); |
| 4321 | reboot_device(0); |
| 4322 | } |
| 4323 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4324 | void cmd_set_active(const char *arg, void *data, unsigned sz) |
| 4325 | { |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4326 | char *p, *sp = NULL; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4327 | unsigned i,current_active_slot; |
| 4328 | const char *current_slot_suffix; |
| 4329 | |
| 4330 | if (!partition_multislot_is_supported()) |
| 4331 | { |
| 4332 | fastboot_fail("Command not supported"); |
| 4333 | return; |
| 4334 | } |
| 4335 | |
| 4336 | if (arg) |
| 4337 | { |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4338 | p = strtok_r((char *)arg, ":", &sp); |
Mayank Grover | 5eb5f69 | 2017-07-19 20:16:04 +0530 | [diff] [blame] | 4339 | if (p) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4340 | { |
| 4341 | current_active_slot = partition_find_active_slot(); |
| 4342 | |
| 4343 | /* Check if trying to make curent slot active */ |
| 4344 | current_slot_suffix = SUFFIX_SLOT(current_active_slot); |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4345 | current_slot_suffix = strtok_r((char *)current_slot_suffix, |
| 4346 | (char *)suffix_delimiter, &sp); |
| 4347 | |
Mayank Grover | 5eb5f69 | 2017-07-19 20:16:04 +0530 | [diff] [blame] | 4348 | if (current_slot_suffix && |
| 4349 | !strncmp(p, current_slot_suffix, strlen(current_slot_suffix))) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4350 | { |
| 4351 | fastboot_okay("Slot already set active"); |
| 4352 | return; |
| 4353 | } |
| 4354 | else |
| 4355 | { |
| 4356 | for (i = 0; i < AB_SUPPORTED_SLOTS; i++) |
| 4357 | { |
| 4358 | current_slot_suffix = SUFFIX_SLOT(i); |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4359 | current_slot_suffix = strtok_r((char *)current_slot_suffix, |
| 4360 | (char *)suffix_delimiter, &sp); |
Mayank Grover | 5eb5f69 | 2017-07-19 20:16:04 +0530 | [diff] [blame] | 4361 | if (current_slot_suffix && |
| 4362 | !strncmp(p, current_slot_suffix, strlen(current_slot_suffix))) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4363 | { |
| 4364 | partition_switch_slots(current_active_slot, i); |
| 4365 | publish_getvar_multislot_vars(); |
| 4366 | fastboot_okay(""); |
| 4367 | return; |
| 4368 | } |
| 4369 | } |
| 4370 | } |
| 4371 | } |
| 4372 | } |
| 4373 | fastboot_fail("Invalid slot suffix."); |
| 4374 | return; |
| 4375 | } |
| 4376 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 4377 | void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz) |
| 4378 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4379 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 4380 | fastboot_okay(""); |
| 4381 | reboot_device(FASTBOOT_MODE); |
| 4382 | } |
| 4383 | |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 4384 | void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size) |
| 4385 | { |
| 4386 | dprintf(INFO, "Enabling charger screen check\n"); |
| 4387 | device.charger_screen_enabled = 1; |
| 4388 | write_device_info(&device); |
| 4389 | fastboot_okay(""); |
| 4390 | } |
| 4391 | |
| 4392 | void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size) |
| 4393 | { |
| 4394 | dprintf(INFO, "Disabling charger screen check\n"); |
| 4395 | device.charger_screen_enabled = 0; |
| 4396 | write_device_info(&device); |
| 4397 | fastboot_okay(""); |
| 4398 | } |
| 4399 | |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 4400 | void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size) |
| 4401 | { |
| 4402 | char *p = NULL; |
| 4403 | const char *delim = " \t\n\r"; |
Parth Dixit | 407f15b | 2016-01-07 14:47:37 +0530 | [diff] [blame] | 4404 | char *sp; |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 4405 | |
| 4406 | if (arg) { |
Parth Dixit | 407f15b | 2016-01-07 14:47:37 +0530 | [diff] [blame] | 4407 | p = strtok_r((char *)arg, delim, &sp); |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 4408 | if (p) { |
| 4409 | if (!strncmp(p, "0", 1)) { |
| 4410 | device.charger_screen_enabled = 0; |
| 4411 | } else if (!strncmp(p, "1", 1)) { |
| 4412 | device.charger_screen_enabled = 1; |
| 4413 | } |
| 4414 | } |
| 4415 | } |
| 4416 | |
| 4417 | /* update charger_screen_enabled value for getvar |
| 4418 | * command |
| 4419 | */ |
| 4420 | snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d", |
| 4421 | device.charger_screen_enabled); |
| 4422 | |
| 4423 | write_device_info(&device); |
| 4424 | fastboot_okay(""); |
| 4425 | } |
| 4426 | |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 4427 | void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size) |
| 4428 | { |
| 4429 | dprintf(INFO, "Selecting display panel %s\n", arg); |
| 4430 | if (arg) |
| 4431 | strlcpy(device.display_panel, arg, |
| 4432 | sizeof(device.display_panel)); |
| 4433 | write_device_info(&device); |
| 4434 | fastboot_okay(""); |
| 4435 | } |
| 4436 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 4437 | void cmd_oem_unlock(const char *arg, void *data, unsigned sz) |
| 4438 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 4439 | set_device_unlock(UNLOCK, TRUE); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz) |
| 4443 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 4444 | if(!device.is_unlocked) { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 4445 | if(!is_allow_unlock) { |
| 4446 | fastboot_fail("oem unlock is not allowed"); |
| 4447 | return; |
| 4448 | } |
| 4449 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 4450 | set_device_unlock_value(UNLOCK, TRUE); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 4451 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 4452 | /* wipe data */ |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 4453 | struct recovery_message msg; |
Kishor PK | 60a6821 | 2017-05-08 16:55:57 +0530 | [diff] [blame] | 4454 | memset(&msg, 0, sizeof(msg)); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 4455 | snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data"); |
| 4456 | write_misc(0, &msg, sizeof(msg)); |
| 4457 | |
| 4458 | fastboot_okay(""); |
| 4459 | reboot_device(RECOVERY_MODE); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4460 | } |
| 4461 | fastboot_okay(""); |
| 4462 | } |
| 4463 | |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 4464 | static int aboot_frp_unlock(char *pname, void *data, unsigned sz) |
| 4465 | { |
Mayank Grover | 70f8b0e | 2017-05-17 11:49:00 +0530 | [diff] [blame] | 4466 | int ret=1; |
| 4467 | bool authentication_success=false; |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 4468 | |
Mayank Grover | 70f8b0e | 2017-05-17 11:49:00 +0530 | [diff] [blame] | 4469 | /* |
| 4470 | Authentication method not implemented. |
| 4471 | |
| 4472 | OEM to implement, authentication system which on successful validataion, |
| 4473 | calls write_allow_oem_unlock() with is_allow_unlock. |
| 4474 | */ |
| 4475 | #if 0 |
| 4476 | authentication_success = oem_specific_auth_mthd(); |
| 4477 | #endif |
| 4478 | |
| 4479 | if (authentication_success) |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 4480 | { |
Mayank Grover | 70f8b0e | 2017-05-17 11:49:00 +0530 | [diff] [blame] | 4481 | is_allow_unlock = true; |
| 4482 | write_allow_oem_unlock(is_allow_unlock); |
| 4483 | ret = 0; |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 4484 | } |
| 4485 | return ret; |
| 4486 | } |
| 4487 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4488 | void cmd_oem_lock(const char *arg, void *data, unsigned sz) |
| 4489 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 4490 | set_device_unlock(UNLOCK, FALSE); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 4491 | } |
| 4492 | |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 4493 | void cmd_oem_devinfo(const char *arg, void *data, unsigned sz) |
| 4494 | { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4495 | char response[MAX_RSP_SIZE]; |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 4496 | snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false")); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 4497 | fastboot_info(response); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 4498 | snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false")); |
| 4499 | fastboot_info(response); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4500 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 4501 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4502 | { |
| 4503 | snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", |
| 4504 | (device.is_unlock_critical ? "true" : "false")); |
| 4505 | fastboot_info(response); |
| 4506 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 4507 | #endif |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 4508 | 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] | 4509 | fastboot_info(response); |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 4510 | snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel)); |
| 4511 | fastboot_info(response); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 4512 | fastboot_okay(""); |
| 4513 | } |
| 4514 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4515 | void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz) |
| 4516 | { |
| 4517 | char response[MAX_RSP_SIZE]; |
| 4518 | snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock); |
| 4519 | fastboot_info(response); |
| 4520 | fastboot_okay(""); |
| 4521 | } |
| 4522 | |
| 4523 | void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz) |
| 4524 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 4525 | set_device_unlock(UNLOCK_CRITICAL, FALSE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz) |
| 4529 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 4530 | set_device_unlock(UNLOCK_CRITICAL, TRUE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4531 | } |
| 4532 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 4533 | void cmd_preflash(const char *arg, void *data, unsigned sz) |
| 4534 | { |
| 4535 | fastboot_okay(""); |
| 4536 | } |
| 4537 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4538 | static uint8_t logo_header[LOGO_IMG_HEADER_SIZE]; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4539 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4540 | int splash_screen_check_header(logo_img_header *header) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4541 | { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4542 | if (memcmp(header->magic, LOGO_IMG_MAGIC, 8)) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4543 | return -1; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4544 | if (header->width == 0 || header->height == 0) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4545 | return -1; |
| 4546 | return 0; |
| 4547 | } |
| 4548 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4549 | int splash_screen_flash() |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 4550 | { |
| 4551 | struct ptentry *ptn; |
| 4552 | struct ptable *ptable; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4553 | struct logo_img_header *header; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 4554 | struct fbcon_config *fb_display = NULL; |
Channagoud Kadabi | b3ccf5c | 2014-12-03 12:39:29 -0800 | [diff] [blame] | 4555 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4556 | ptable = flash_get_ptable(); |
| 4557 | if (ptable == NULL) { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4558 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 4559 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4560 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4561 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4562 | ptn = ptable_find(ptable, "splash"); |
| 4563 | if (ptn == NULL) { |
| 4564 | dprintf(CRITICAL, "ERROR: splash Partition not found\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4565 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4566 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4567 | if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4568 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4569 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4570 | } |
| 4571 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4572 | header = (struct logo_img_header *)logo_header; |
| 4573 | if (splash_screen_check_header(header)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4574 | dprintf(CRITICAL, "ERROR: Boot image header invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4575 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4576 | } |
| 4577 | |
| 4578 | fb_display = fbcon_display(); |
| 4579 | if (fb_display) { |
raghavendra ambadas | 554f4fd | 2018-10-09 15:50:40 +0530 | [diff] [blame] | 4580 | if (header->type && (header->blocks != 0) && |
| 4581 | (UINT_MAX >= header->blocks * 512) && |
| 4582 | ((header->blocks * 512) <= (fb_display->width * |
| 4583 | fb_display->height * (fb_display->bpp / 8)))) { |
| 4584 | /* RLE24 compressed data */ |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4585 | uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET; |
| 4586 | |
| 4587 | /* if the logo is full-screen size, remove "fbcon_clear()" */ |
| 4588 | if ((header->width != fb_display->width) |
| 4589 | || (header->height != fb_display->height)) |
| 4590 | fbcon_clear(); |
| 4591 | |
| 4592 | if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0, |
| 4593 | (uint32_t *)base, |
| 4594 | (header->blocks * 512))) { |
| 4595 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 4596 | return -1; |
| 4597 | } |
| 4598 | fbcon_extract_to_screen(header, base); |
| 4599 | return 0; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 4600 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4601 | |
Veera Sundaram Sankaran | c33416f | 2015-09-28 10:25:55 -0700 | [diff] [blame] | 4602 | if ((header->width > fb_display->width) || (header->height > fb_display->height)) { |
| 4603 | 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] | 4604 | return -1; |
| 4605 | } |
| 4606 | |
Vineet Bajaj | 99291ed | 2014-09-09 12:29:46 +0530 | [diff] [blame] | 4607 | uint8_t *base = (uint8_t *) fb_display->base; |
Sachin Bhayare | 619e9e4 | 2017-05-15 13:10:31 +0530 | [diff] [blame] | 4608 | uint32_t fb_size = ROUNDUP(fb_display->width * |
| 4609 | fb_display->height * |
| 4610 | (fb_display->bpp / 8), 4096); |
| 4611 | uint32_t splash_size = ((((header->width * header->height * |
| 4612 | fb_display->bpp/8) + 511) >> 9) << 9); |
| 4613 | |
| 4614 | if (splash_size > fb_size) { |
| 4615 | dprintf(CRITICAL, "ERROR: Splash image size invalid\n"); |
| 4616 | return -1; |
| 4617 | } |
| 4618 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4619 | if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0, |
| 4620 | (uint32_t *)base, |
| 4621 | ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4622 | fbcon_clear(); |
Vineet Bajaj | 99291ed | 2014-09-09 12:29:46 +0530 | [diff] [blame] | 4623 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4624 | return -1; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 4625 | } |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4626 | } |
| 4627 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4628 | return 0; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4629 | } |
| 4630 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4631 | int splash_screen_mmc() |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4632 | { |
| 4633 | int index = INVALID_PTN; |
| 4634 | unsigned long long ptn = 0; |
| 4635 | struct fbcon_config *fb_display = NULL; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4636 | struct logo_img_header *header; |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 4637 | uint32_t blocksize, realsize, readsize; |
| 4638 | uint8_t *base; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4639 | |
| 4640 | index = partition_get_index("splash"); |
| 4641 | if (index == 0) { |
| 4642 | dprintf(CRITICAL, "ERROR: splash Partition table not found\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4643 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | ptn = partition_get_offset(index); |
| 4647 | if (ptn == 0) { |
| 4648 | dprintf(CRITICAL, "ERROR: splash Partition invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4649 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4650 | } |
| 4651 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 4652 | mmc_set_lun(partition_get_lun(index)); |
| 4653 | |
| 4654 | blocksize = mmc_get_device_blocksize(); |
| 4655 | if (blocksize == 0) { |
| 4656 | dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n"); |
| 4657 | return -1; |
| 4658 | } |
| 4659 | |
| 4660 | fb_display = fbcon_display(); |
Channagoud Kadabi | daf10a6 | 2015-07-22 11:51:55 -0700 | [diff] [blame] | 4661 | if (!fb_display) |
| 4662 | { |
| 4663 | dprintf(CRITICAL, "ERROR: fb config is not allocated\n"); |
| 4664 | return -1; |
| 4665 | } |
| 4666 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 4667 | base = (uint8_t *) fb_display->base; |
| 4668 | |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 4669 | if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4670 | dprintf(CRITICAL, "ERROR: Cannot read splash image header\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4671 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4672 | } |
| 4673 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 4674 | header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4675 | if (splash_screen_check_header(header)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4676 | dprintf(CRITICAL, "ERROR: Splash image header invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4677 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4678 | } |
| 4679 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4680 | if (fb_display) { |
raghavendra ambadas | 554f4fd | 2018-10-09 15:50:40 +0530 | [diff] [blame] | 4681 | if (header->type && (header->blocks != 0) && |
| 4682 | (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) && |
| 4683 | ((header->blocks * 512) <= (fb_display->width * |
| 4684 | fb_display->height * (fb_display->bpp / 8)))) { |
| 4685 | /* 1 RLE24 compressed data */ |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 4686 | base += LOGO_IMG_OFFSET; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4687 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 4688 | realsize = header->blocks * 512; |
| 4689 | readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize; |
| 4690 | |
| 4691 | /* if the logo is not full-screen size, clean screen */ |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4692 | if ((header->width != fb_display->width) |
| 4693 | || (header->height != fb_display->height)) |
| 4694 | fbcon_clear(); |
| 4695 | |
Sachin Bhayare | 619e9e4 | 2017-05-15 13:10:31 +0530 | [diff] [blame] | 4696 | uint32_t fb_size = ROUNDUP(fb_display->width * |
| 4697 | fb_display->height * |
| 4698 | (fb_display->bpp / 8), 4096); |
| 4699 | |
| 4700 | if (readsize > fb_size) { |
| 4701 | dprintf(CRITICAL, "ERROR: Splash image size invalid\n"); |
| 4702 | return -1; |
| 4703 | } |
| 4704 | |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 4705 | if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4706 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 4707 | return -1; |
| 4708 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4709 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 4710 | fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE)); |
| 4711 | } else { /* 2 Raw BGR data */ |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4712 | |
Veera Sundaram Sankaran | c33416f | 2015-09-28 10:25:55 -0700 | [diff] [blame] | 4713 | if ((header->width > fb_display->width) || (header->height > fb_display->height)) { |
| 4714 | 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] | 4715 | return -1; |
| 4716 | } |
| 4717 | |
| 4718 | realsize = header->width * header->height * fb_display->bpp / 8; |
| 4719 | readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize; |
| 4720 | |
| 4721 | if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */ |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 4722 | if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) { |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 4723 | fbcon_clear(); |
| 4724 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 4725 | return -1; |
| 4726 | } |
| 4727 | } else { |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 4728 | if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize , |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 4729 | (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) { |
| 4730 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 4731 | return -1; |
| 4732 | } |
| 4733 | memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize); |
| 4734 | } |
| 4735 | } |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4736 | } |
| 4737 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4738 | return 0; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4739 | } |
| 4740 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 4741 | int fetch_image_from_partition() |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 4742 | { |
| 4743 | if (target_is_emmc_boot()) { |
| 4744 | return splash_screen_mmc(); |
| 4745 | } else { |
| 4746 | return splash_screen_flash(); |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 4747 | } |
| 4748 | } |
| 4749 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4750 | void publish_getvar_multislot_vars() |
| 4751 | { |
| 4752 | int i,count; |
| 4753 | static bool published = false; |
| 4754 | static char slot_count[MAX_RSP_SIZE]; |
| 4755 | static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS]; |
| 4756 | static char active_slot_suffix[MAX_RSP_SIZE]; |
| 4757 | static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE]; |
| 4758 | static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE]; |
| 4759 | const char *tmp; |
| 4760 | char tmpbuff[MAX_GET_VAR_NAME_SIZE]; |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 4761 | signed active_slt; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4762 | |
| 4763 | if (!published) |
| 4764 | { |
| 4765 | /* Update slot meta info */ |
| 4766 | count = partition_fill_partition_meta(has_slot_pname, has_slot_reply, |
| 4767 | partition_get_partition_count()); |
| 4768 | for(i=0; i<count; i++) |
| 4769 | { |
| 4770 | memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE); |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 4771 | snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s", |
| 4772 | has_slot_pname[i]); |
| 4773 | strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4774 | fastboot_publish(has_slot_pname[i], has_slot_reply[i]); |
| 4775 | } |
| 4776 | |
| 4777 | for (i=0; i<AB_SUPPORTED_SLOTS; i++) |
| 4778 | { |
| 4779 | tmp = SUFFIX_SLOT(i); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 4780 | tmp++; // to remove "_" from slot_suffix. |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 4781 | snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable), |
| 4782 | "slot-unbootable:%s", tmp); |
| 4783 | snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active), |
| 4784 | "slot-active:%s", tmp); |
| 4785 | snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful), |
| 4786 | "slot-success:%s", tmp); |
| 4787 | snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count), |
| 4788 | "slot-retry-count:%s", tmp); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4789 | fastboot_publish(slot_info[i].slot_is_unbootable, |
| 4790 | slot_info[i].slot_is_unbootable_rsp); |
| 4791 | fastboot_publish(slot_info[i].slot_is_active, |
| 4792 | slot_info[i].slot_is_active_rsp); |
| 4793 | fastboot_publish(slot_info[i].slot_is_succesful, |
| 4794 | slot_info[i].slot_is_succesful_rsp); |
| 4795 | fastboot_publish(slot_info[i].slot_retry_count, |
| 4796 | slot_info[i].slot_retry_count_rsp); |
| 4797 | } |
| 4798 | fastboot_publish("current-slot", active_slot_suffix); |
| 4799 | snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS); |
| 4800 | fastboot_publish("slot-count", slot_count); |
| 4801 | published = true; |
| 4802 | } |
| 4803 | |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 4804 | active_slt = partition_find_active_slot(); |
| 4805 | if (active_slt != INVALID) |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 4806 | { |
| 4807 | tmp = SUFFIX_SLOT(active_slt); |
| 4808 | tmp++; // to remove "_" from slot_suffix. |
| 4809 | snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp); |
| 4810 | } |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 4811 | else |
| 4812 | strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix)); |
| 4813 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4814 | /* Update partition meta information */ |
| 4815 | partition_fill_slot_meta(slot_info); |
| 4816 | return; |
| 4817 | } |
| 4818 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 4819 | void get_product_name(unsigned char *buf) |
| 4820 | { |
| 4821 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD)); |
| 4822 | return; |
| 4823 | } |
| 4824 | |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 4825 | #if PRODUCT_IOT |
| 4826 | void get_bootloader_version_iot(unsigned char *buf) |
| 4827 | { |
| 4828 | if (buf != NULL) |
| 4829 | { |
| 4830 | strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN); |
| 4831 | strlcat(buf, "-", MAX_VERSION_LEN); |
| 4832 | strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN); |
| 4833 | } |
| 4834 | return; |
| 4835 | } |
| 4836 | #endif |
| 4837 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 4838 | void get_bootloader_version(unsigned char *buf) |
| 4839 | { |
| 4840 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version); |
| 4841 | return; |
| 4842 | } |
| 4843 | |
| 4844 | void get_baseband_version(unsigned char *buf) |
| 4845 | { |
| 4846 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version); |
| 4847 | return; |
| 4848 | } |
| 4849 | |
Monika Singh | 32a09f7 | 2018-03-14 13:08:29 +0530 | [diff] [blame] | 4850 | bool is_device_locked_critical() |
| 4851 | { |
| 4852 | return device.is_unlock_critical ? false:true; |
| 4853 | } |
| 4854 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 4855 | bool is_device_locked() |
| 4856 | { |
| 4857 | return device.is_unlocked ? false:true; |
| 4858 | } |
| 4859 | |
Monika Singh | 32a09f7 | 2018-03-14 13:08:29 +0530 | [diff] [blame] | 4860 | bool is_verity_enforcing() |
| 4861 | { |
| 4862 | return device.verity_mode ? true:false; |
| 4863 | } |
| 4864 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 4865 | /* register commands and variables for fastboot */ |
| 4866 | void aboot_fastboot_register_commands(void) |
| 4867 | { |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 4868 | int i; |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 4869 | char hw_platform_buf[MAX_RSP_SIZE]; |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 4870 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 4871 | struct fastboot_cmd_desc cmd_list[] = { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4872 | /* By default the enabled list is empty. */ |
| 4873 | {"", NULL}, |
| 4874 | /* move commands enclosed within the below ifndef to here |
| 4875 | * if they need to be enabled in user build. |
| 4876 | */ |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 4877 | #ifndef DISABLE_FASTBOOT_CMDS |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4878 | /* Register the following commands only for non-user builds */ |
| 4879 | {"flash:", cmd_flash}, |
| 4880 | {"erase:", cmd_erase}, |
| 4881 | {"boot", cmd_boot}, |
| 4882 | {"continue", cmd_continue}, |
| 4883 | {"reboot", cmd_reboot}, |
| 4884 | {"reboot-bootloader", cmd_reboot_bootloader}, |
| 4885 | {"oem unlock", cmd_oem_unlock}, |
| 4886 | {"oem unlock-go", cmd_oem_unlock_go}, |
| 4887 | {"oem lock", cmd_oem_lock}, |
| 4888 | {"flashing unlock", cmd_oem_unlock}, |
| 4889 | {"flashing lock", cmd_oem_lock}, |
| 4890 | {"flashing lock_critical", cmd_flashing_lock_critical}, |
| 4891 | {"flashing unlock_critical", cmd_flashing_unlock_critical}, |
| 4892 | {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability}, |
| 4893 | {"oem device-info", cmd_oem_devinfo}, |
| 4894 | {"preflash", cmd_preflash}, |
| 4895 | {"oem enable-charger-screen", cmd_oem_enable_charger_screen}, |
| 4896 | {"oem disable-charger-screen", cmd_oem_disable_charger_screen}, |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 4897 | {"oem off-mode-charge", cmd_oem_off_mode_charger}, |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4898 | {"oem select-display-panel", cmd_oem_select_display_panel}, |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4899 | {"set_active",cmd_set_active}, |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 4900 | #if UNITTEST_FW_SUPPORT |
Channagoud Kadabi | d5ddf48 | 2015-09-28 10:31:35 -0700 | [diff] [blame] | 4901 | {"oem run-tests", cmd_oem_runtests}, |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 4902 | #endif |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 4903 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4904 | }; |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 4905 | |
| 4906 | int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]); |
| 4907 | for (i = 1; i < fastboot_cmds_count; i++) |
| 4908 | fastboot_register(cmd_list[i].name,cmd_list[i].cb); |
| 4909 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 4910 | /* publish variables and their values */ |
| 4911 | fastboot_publish("product", TARGET(BOARD)); |
| 4912 | fastboot_publish("kernel", "lk"); |
| 4913 | fastboot_publish("serialno", sn_buf); |
| 4914 | |
Mayank Grover | a64dfbe | 2018-05-17 14:06:34 +0530 | [diff] [blame] | 4915 | /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/ |
| 4916 | snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version()); |
| 4917 | fastboot_publish("hw-revision", soc_version_str); |
| 4918 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 4919 | /* |
| 4920 | * partition info is supported only for emmc partitions |
| 4921 | * Calling this for NAND prints some error messages which |
| 4922 | * is harmless but misleading. Avoid calling this for NAND |
| 4923 | * devices. |
| 4924 | */ |
| 4925 | if (target_is_emmc_boot()) |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 4926 | publish_getvar_partition_info(part_info, partition_get_partition_count()); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 4927 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4928 | if (partition_multislot_is_supported()) |
| 4929 | publish_getvar_multislot_vars(); |
| 4930 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 4931 | /* Max download size supported */ |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 4932 | snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", |
| 4933 | target_get_max_flash_size()); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 4934 | fastboot_publish("max-download-size", (const char *) max_download_size); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 4935 | /* Is the charger screen check enabled */ |
| 4936 | snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d", |
| 4937 | device.charger_screen_enabled); |
| 4938 | fastboot_publish("charger-screen-enabled", |
| 4939 | (const char *) charger_screen_enabled); |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 4940 | fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled); |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 4941 | snprintf(panel_display_mode, MAX_RSP_SIZE, "%s", |
| 4942 | device.display_panel); |
| 4943 | fastboot_publish("display-panel", |
| 4944 | (const char *) panel_display_mode); |
Mukesh Ojha | bb6e7f3 | 2018-03-02 15:04:06 +0530 | [diff] [blame] | 4945 | |
| 4946 | if (target_is_emmc_boot()) |
| 4947 | { |
| 4948 | mmc_blocksize = mmc_get_device_blocksize(); |
| 4949 | } |
| 4950 | else |
| 4951 | { |
| 4952 | mmc_blocksize = flash_block_size(); |
| 4953 | } |
| 4954 | snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize); |
| 4955 | fastboot_publish("erase-block-size", (const char *) block_size_string); |
| 4956 | fastboot_publish("logical-block-size", (const char *) block_size_string); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 4957 | #if PRODUCT_IOT |
| 4958 | get_bootloader_version_iot(&bootloader_version_string); |
| 4959 | fastboot_publish("version-bootloader", (const char *) bootloader_version_string); |
| 4960 | |
| 4961 | /* Version baseband is n/a for apq iot devices */ |
| 4962 | fastboot_publish("version-baseband", "N/A"); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 4963 | #else |
Channagoud Kadabi | aafbbfc | 2015-03-04 17:52:56 -0800 | [diff] [blame] | 4964 | fastboot_publish("version-bootloader", (const char *) device.bootloader_version); |
| 4965 | fastboot_publish("version-baseband", (const char *) device.radio_version); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 4966 | #endif |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 4967 | fastboot_publish("secure", is_secure_boot_enable()? "yes":"no"); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 4968 | fastboot_publish("unlocked", device.is_unlocked ? "yes":"no"); |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 4969 | smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf)); |
| 4970 | snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf, |
| 4971 | target_is_emmc_boot()? "eMMC":"UFS"); |
| 4972 | fastboot_publish("variant", (const char *) get_variant); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 4973 | #if CHECK_BAT_VOLTAGE |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 4974 | update_battery_status(); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 4975 | fastboot_publish("battery-voltage", (const char *) battery_voltage); |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 4976 | fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 4977 | #endif |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 4978 | } |
| 4979 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 4980 | void aboot_init(const struct app_descriptor *app) |
| 4981 | { |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 4982 | unsigned reboot_mode = 0; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4983 | int boot_err_type = 0; |
| 4984 | int boot_slot = INVALID; |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 4985 | |
lijuang | 3983173 | 2016-01-08 17:49:02 +0800 | [diff] [blame] | 4986 | /* Initialise wdog to catch early lk crashes */ |
| 4987 | #if WDOG_SUPPORT |
| 4988 | msm_wdog_init(); |
| 4989 | #endif |
| 4990 | |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 4991 | /* Setup page size information for nv storage */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4992 | if (target_is_emmc_boot()) |
| 4993 | { |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 4994 | page_size = mmc_page_size(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4995 | page_mask = page_size - 1; |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 4996 | mmc_blocksize = mmc_get_device_blocksize(); |
| 4997 | mmc_blocksize_mask = mmc_blocksize - 1; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4998 | } |
| 4999 | else |
| 5000 | { |
| 5001 | page_size = flash_page_size(); |
| 5002 | page_mask = page_size - 1; |
| 5003 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 5004 | ASSERT((MEMBASE + MEMSIZE) > MEMBASE); |
| 5005 | |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5006 | read_device_info(&device); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5007 | read_allow_oem_unlock(&device); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 5008 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5009 | /* Detect multi-slot support */ |
| 5010 | if (partition_multislot_is_supported()) |
| 5011 | { |
| 5012 | boot_slot = partition_find_active_slot(); |
| 5013 | if (boot_slot == INVALID) |
| 5014 | { |
| 5015 | boot_into_fastboot = true; |
| 5016 | dprintf(INFO, "Active Slot: (INVALID)\n"); |
| 5017 | } |
| 5018 | else |
| 5019 | { |
| 5020 | /* Setting the state of system to boot active slot */ |
| 5021 | partition_mark_active_slot(boot_slot); |
| 5022 | dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot)); |
| 5023 | } |
| 5024 | } |
| 5025 | |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 5026 | /* Display splash screen if enabled */ |
| 5027 | #if DISPLAY_SPLASH_SCREEN |
lijuang | 99c02d8 | 2015-02-13 19:04:34 +0800 | [diff] [blame] | 5028 | #if NO_ALARM_DISPLAY |
| 5029 | if (!check_alarm_boot()) { |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 5030 | #endif |
lijuang | 99c02d8 | 2015-02-13 19:04:34 +0800 | [diff] [blame] | 5031 | dprintf(SPEW, "Display Init: Start\n"); |
Ajay Singh Parmar | a7865a8 | 2015-04-16 21:27:52 -0700 | [diff] [blame] | 5032 | #if DISPLAY_HDMI_PRIMARY |
| 5033 | if (!strlen(device.display_panel)) |
| 5034 | strlcpy(device.display_panel, DISPLAY_PANEL_HDMI, |
| 5035 | sizeof(device.display_panel)); |
| 5036 | #endif |
Channagoud Kadabi | 90869ce | 2015-04-27 11:15:14 -0700 | [diff] [blame] | 5037 | #if ENABLE_WBC |
Channagoud Kadabi | d801ac7 | 2015-08-26 11:21:51 -0700 | [diff] [blame] | 5038 | /* Wait if the display shutdown is in progress */ |
| 5039 | while(pm_app_display_shutdown_in_prgs()); |
Channagoud Kadabi | 90869ce | 2015-04-27 11:15:14 -0700 | [diff] [blame] | 5040 | if (!pm_appsbl_display_init_done()) |
| 5041 | target_display_init(device.display_panel); |
| 5042 | else |
| 5043 | display_image_on_screen(); |
| 5044 | #else |
lijuang | 99c02d8 | 2015-02-13 19:04:34 +0800 | [diff] [blame] | 5045 | target_display_init(device.display_panel); |
Channagoud Kadabi | 90869ce | 2015-04-27 11:15:14 -0700 | [diff] [blame] | 5046 | #endif |
lijuang | 99c02d8 | 2015-02-13 19:04:34 +0800 | [diff] [blame] | 5047 | dprintf(SPEW, "Display Init: Done\n"); |
| 5048 | #if NO_ALARM_DISPLAY |
| 5049 | } |
| 5050 | #endif |
| 5051 | #endif |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 5052 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 5053 | target_serialno((unsigned char *) sn_buf); |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 5054 | dprintf(SPEW,"serial number: %s\n",sn_buf); |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 5055 | |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 5056 | memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE); |
| 5057 | |
Matthew Qin | defd556 | 2014-07-11 18:02:40 +0800 | [diff] [blame] | 5058 | /* |
| 5059 | * Check power off reason if user force reset, |
| 5060 | * if yes phone will do normal boot. |
| 5061 | */ |
| 5062 | if (is_user_force_reset()) |
| 5063 | goto normal_boot; |
| 5064 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5065 | /* Check if we should do something other than booting up */ |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 5066 | if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN)) |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 5067 | { |
Pavel Nedev | a4c9d3a | 2013-05-15 14:42:34 +0300 | [diff] [blame] | 5068 | dprintf(ALWAYS,"dload mode key sequence detected\n"); |
lijuang | 395b5e6 | 2015-11-19 17:39:44 +0800 | [diff] [blame] | 5069 | reboot_device(EMERGENCY_DLOAD); |
| 5070 | dprintf(CRITICAL,"Failed to reboot into dload mode\n"); |
| 5071 | |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 5072 | boot_into_fastboot = true; |
| 5073 | } |
| 5074 | if (!boot_into_fastboot) |
| 5075 | { |
| 5076 | if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP)) |
| 5077 | boot_into_recovery = 1; |
| 5078 | if (!boot_into_recovery && |
| 5079 | (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN))) |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5080 | boot_into_fastboot = true; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 5081 | } |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5082 | #if NO_KEYPAD_DRIVER |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 5083 | if (fastboot_trigger()) |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5084 | boot_into_fastboot = true; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5085 | #endif |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 5086 | |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5087 | #if USE_PON_REBOOT_REG |
| 5088 | reboot_mode = check_hard_reboot_mode(); |
| 5089 | #else |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 5090 | reboot_mode = check_reboot_mode(); |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5091 | #endif |
| 5092 | if (reboot_mode == RECOVERY_MODE) |
| 5093 | { |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 5094 | boot_into_recovery = 1; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5095 | } |
| 5096 | else if(reboot_mode == FASTBOOT_MODE) |
| 5097 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5098 | boot_into_fastboot = true; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5099 | } |
| 5100 | else if(reboot_mode == ALARM_BOOT) |
| 5101 | { |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 5102 | boot_reason_alarm = true; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5103 | } |
Monika Singh | b0db4b8 | 2018-09-26 12:18:02 +0530 | [diff] [blame] | 5104 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 5105 | else if (VB_M <= target_get_vb_version()) |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5106 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5107 | if (reboot_mode == DM_VERITY_ENFORCING) |
| 5108 | { |
| 5109 | device.verity_mode = 1; |
| 5110 | write_device_info(&device); |
| 5111 | } |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 5112 | #if ENABLE_VB_ATTEST |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5113 | else if (reboot_mode == DM_VERITY_EIO) |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 5114 | #else |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5115 | else if (reboot_mode == DM_VERITY_LOGGING) |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 5116 | #endif |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5117 | { |
| 5118 | device.verity_mode = 0; |
| 5119 | write_device_info(&device); |
| 5120 | } |
| 5121 | else if (reboot_mode == DM_VERITY_KEYSCLEAR) |
| 5122 | { |
| 5123 | if(send_delete_keys_to_tz()) |
| 5124 | ASSERT(0); |
| 5125 | } |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5126 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 5127 | #endif |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 5128 | |
Matthew Qin | defd556 | 2014-07-11 18:02:40 +0800 | [diff] [blame] | 5129 | normal_boot: |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5130 | if (!boot_into_fastboot) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5131 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5132 | if (target_is_emmc_boot()) |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5133 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5134 | if(emmc_recovery_init()) |
| 5135 | dprintf(ALWAYS,"error in emmc_recovery_init\n"); |
| 5136 | if(target_use_signed_kernel()) |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5137 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5138 | if((device.is_unlocked) || (device.is_tampered)) |
| 5139 | { |
| 5140 | #ifdef TZ_TAMPER_FUSE |
Monika Singh | 9825746 | 2018-06-06 11:28:49 +0530 | [diff] [blame] | 5141 | set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE); |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5142 | #endif |
| 5143 | #if USE_PCOM_SECBOOT |
| 5144 | set_tamper_flag(device.is_tampered); |
| 5145 | #endif |
| 5146 | } |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5147 | } |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 5148 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5149 | retry_boot: |
| 5150 | /* Trying to boot active partition */ |
| 5151 | if (partition_multislot_is_supported()) |
| 5152 | { |
| 5153 | boot_slot = partition_find_boot_slot(); |
| 5154 | partition_mark_active_slot(boot_slot); |
| 5155 | if (boot_slot == INVALID) |
| 5156 | goto fastboot; |
| 5157 | } |
| 5158 | |
| 5159 | boot_err_type = boot_linux_from_mmc(); |
| 5160 | switch (boot_err_type) |
| 5161 | { |
| 5162 | case ERR_INVALID_PAGE_SIZE: |
| 5163 | case ERR_DT_PARSE: |
| 5164 | case ERR_ABOOT_ADDR_OVERLAP: |
Mayank Grover | 10cabbe | 2017-10-30 19:11:05 +0530 | [diff] [blame] | 5165 | case ERR_INVALID_BOOT_MAGIC: |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5166 | if(partition_multislot_is_supported()) |
Mayank Grover | 10cabbe | 2017-10-30 19:11:05 +0530 | [diff] [blame] | 5167 | { |
| 5168 | /* |
| 5169 | * Deactivate current slot, as it failed to |
| 5170 | * boot, and retry next slot. |
| 5171 | */ |
| 5172 | partition_deactivate_slot(boot_slot); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5173 | goto retry_boot; |
Mayank Grover | 10cabbe | 2017-10-30 19:11:05 +0530 | [diff] [blame] | 5174 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5175 | else |
| 5176 | break; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5177 | default: |
| 5178 | break; |
| 5179 | /* going to fastboot menu */ |
| 5180 | } |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5181 | } |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5182 | else |
| 5183 | { |
| 5184 | recovery_init(); |
| 5185 | #if USE_PCOM_SECBOOT |
| 5186 | if((device.is_unlocked) || (device.is_tampered)) |
| 5187 | set_tamper_flag(device.is_tampered); |
| 5188 | #endif |
| 5189 | boot_linux_from_flash(); |
| 5190 | } |
| 5191 | dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting " |
| 5192 | "to fastboot mode.\n"); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5193 | } |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5194 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5195 | fastboot: |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5196 | /* We are here means regular boot did not happen. Start fastboot. */ |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 5197 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5198 | /* register aboot specific fastboot commands */ |
| 5199 | aboot_fastboot_register_commands(); |
Amol Jadi | 57abe4c | 2011-05-24 15:47:27 -0700 | [diff] [blame] | 5200 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5201 | /* dump partition table for debug info */ |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 5202 | partition_dump(); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5203 | |
| 5204 | /* initialize and start fastboot */ |
Saranya Chidura | 41b3453 | 2018-10-16 12:29:52 +0530 | [diff] [blame] | 5205 | #if !VERIFIED_BOOT_2 |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5206 | fastboot_init(target_get_scratch_address(), target_get_max_flash_size()); |
Saranya Chidura | 41b3453 | 2018-10-16 12:29:52 +0530 | [diff] [blame] | 5207 | #else |
| 5208 | /* Add salt buffer offset at start of image address to copy VB salt */ |
| 5209 | fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()), |
| 5210 | SUB_SALT_BUFF_OFFSET(target_get_max_flash_size())); |
| 5211 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5212 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 5213 | display_fastboot_menu(); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5214 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 5215 | } |
| 5216 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 5217 | uint32_t get_page_size() |
| 5218 | { |
| 5219 | return page_size; |
| 5220 | } |
| 5221 | |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 5222 | /* |
| 5223 | * Calculated and save hash (SHA256) for non-signed boot image. |
| 5224 | * |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5225 | * @param image_addr - Boot image address |
| 5226 | * @param image_size - Size of the boot image |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 5227 | * |
| 5228 | * @return int - 0 on success, negative value on failure. |
| 5229 | */ |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5230 | 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] | 5231 | { |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5232 | unsigned int digest[8]; |
| 5233 | #if IMAGE_VERIF_ALGO_SHA1 |
| 5234 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1; |
| 5235 | #else |
| 5236 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256; |
| 5237 | #endif |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 5238 | |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5239 | target_crypto_init_params(); |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 5240 | 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] | 5241 | |
| 5242 | save_kernel_hash_cmd(digest); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5243 | 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] | 5244 | |
| 5245 | return 0; |
| 5246 | } |
| 5247 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5248 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 5249 | APP_START(aboot) |
| 5250 | .init = aboot_init, |
| 5251 | APP_END |