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