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