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; |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 106 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 107 | void *prodinfo_buf; |
| 108 | #endif |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 109 | void write_device_info_mmc(device_info *dev); |
| 110 | void write_device_info_flash(device_info *dev); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 111 | 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] | 112 | static int aboot_frp_unlock(char *pname, void *data, unsigned sz); |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 113 | static inline uint64_t validate_partition_size(); |
Parth Dixit | 54ac3bb | 2017-03-07 15:52:48 +0530 | [diff] [blame] | 114 | bool pwr_key_is_pressed = false; |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 115 | static bool is_systemd_present=false; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 116 | static void publish_getvar_multislot_vars(); |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 117 | /* fastboot command function pointer */ |
| 118 | typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 119 | bool get_perm_attr_status(); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 120 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 121 | void write_prod_info(prod_info *dev); |
| 122 | #endif |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 123 | |
| 124 | struct fastboot_cmd_desc { |
| 125 | char * name; |
| 126 | fastboot_cmd_fn cb; |
| 127 | }; |
| 128 | |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 129 | #define EXPAND(NAME) #NAME |
| 130 | #define TARGET(NAME) EXPAND(NAME) |
Brian Swetland | 2defe16 | 2009-08-18 14:35:59 -0700 | [diff] [blame] | 131 | |
Ajay Singh Parmar | a7865a8 | 2015-04-16 21:27:52 -0700 | [diff] [blame] | 132 | #define DISPLAY_PANEL_HDMI "hdmi" |
| 133 | |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 134 | #ifdef MEMBASE |
| 135 | #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE)) |
| 136 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 137 | #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000 |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 138 | #endif |
| 139 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 140 | #ifndef MEMSIZE |
| 141 | #define MEMSIZE 1024*1024 |
| 142 | #endif |
| 143 | |
| 144 | #define MAX_TAGS_SIZE 1024 |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 145 | #define PLL_CODES_OFFSET 4096 |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 146 | /* make 4096 as default size to ensure EFS,EXT4's erasing */ |
| 147 | #define DEFAULT_ERASE_SIZE 4096 |
Ujwal Patel | c0b0a25 | 2015-08-16 14:05:35 -0700 | [diff] [blame] | 148 | #define MAX_PANEL_BUF_SIZE 196 |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 149 | #define FOOTER_SIZE 16384 |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 150 | |
Dhaval Patel | f83d73b | 2014-06-23 16:24:37 -0700 | [diff] [blame] | 151 | #define DISPLAY_DEFAULT_PREFIX "mdss_mdp" |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 152 | #define BOOT_DEV_MAX_LEN 64 |
Sundarajan Srinivasan | 595b71e | 2013-11-05 12:44:34 -0800 | [diff] [blame] | 153 | |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 154 | #define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false |
| 155 | |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 156 | #define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX |
| 157 | |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 158 | //Size of the header that is used in case the boot image has |
| 159 | //a uncompressed kernel + appended dtb |
| 160 | #define PATCHED_KERNEL_HEADER_SIZE 20 |
| 161 | |
| 162 | //String used to determine if the boot image has |
| 163 | //a uncompressed kernel + appended dtb |
| 164 | #define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG" |
| 165 | |
Sridhar Parasuram | 7bd4aaf | 2015-02-12 11:14:38 -0800 | [diff] [blame] | 166 | #if USE_BOOTDEV_CMDLINE |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 167 | static const char *emmc_cmdline = " androidboot.bootdevice="; |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 168 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 169 | static const char *emmc_cmdline = " androidboot.emmc=true"; |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 170 | #endif |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 171 | static const char *dynamic_bootdev_cmdline = |
| 172 | " androidboot.boot_devices=soc/"; |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 173 | static const char *usb_sn_cmdline = " androidboot.serialno="; |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 174 | static const char *androidboot_mode = " androidboot.mode="; |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 175 | |
| 176 | static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target"; |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 177 | static const char *alarmboot_cmdline = " androidboot.alarmboot=true"; |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 178 | static const char *loglevel = " quiet"; |
Ajay Dudani | ca3a33c | 2011-11-18 08:31:40 -0800 | [diff] [blame] | 179 | static const char *battchg_pause = " androidboot.mode=charger"; |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 180 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 181 | static const char *cust_sn_cmdline = " androidboot.customer_serialno="; |
| 182 | static const char *factory_sn_cmdline = " androidboot.factory_serialno="; |
| 183 | static const char *UsbAdbEnable = " androidboot.adb_enable=1"; |
| 184 | #endif |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 185 | static const char *auth_kernel = " androidboot.authorized_kernel=true"; |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 186 | static const char *secondary_gpt_enable = " gpt"; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 187 | #ifdef MDTP_SUPPORT |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 188 | static const char *mdtp_activated_flag = " mdtp"; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 189 | #endif |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 190 | static const char *baseband_apq = " androidboot.baseband=apq"; |
| 191 | static const char *baseband_msm = " androidboot.baseband=msm"; |
| 192 | static const char *baseband_csfb = " androidboot.baseband=csfb"; |
| 193 | static const char *baseband_svlte2a = " androidboot.baseband=svlte2a"; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 194 | static const char *baseband_mdm = " androidboot.baseband=mdm"; |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 195 | static const char *baseband_mdm2 = " androidboot.baseband=mdm2"; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 196 | static const char *baseband_sglte = " androidboot.baseband=sglte"; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 197 | static const char *baseband_dsda = " androidboot.baseband=dsda"; |
| 198 | static const char *baseband_dsda2 = " androidboot.baseband=dsda2"; |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 199 | static const char *baseband_sglte2 = " androidboot.baseband=sglte2"; |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 200 | static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1"; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 201 | static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr"; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 202 | static const char *androidboot_slot_suffix = " androidboot.slot_suffix="; |
| 203 | static const char *skip_ramfs = " skip_initramfs"; |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 204 | |
| 205 | #if HIBERNATION_SUPPORT |
| 206 | static const char *resume = " resume=/dev/mmcblk0p"; |
| 207 | #endif |
| 208 | |
Sourabh Banerjee | 6c7153c | 2018-03-20 01:21:57 +0530 | [diff] [blame] | 209 | #ifdef INIT_BIN_LE |
| 210 | static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE; |
| 211 | #else |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 212 | static const char *sys_path_cmdline = " rootwait ro init=/init"; |
Sourabh Banerjee | 6c7153c | 2018-03-20 01:21:57 +0530 | [diff] [blame] | 213 | #endif |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 214 | |
| 215 | #if VERITY_LE |
| 216 | static const char *verity_dev = " root=/dev/dm-0"; |
| 217 | static const char *verity_system_part = " dm=\"system"; |
| 218 | static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p"; |
| 219 | #else |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 220 | static const char *sys_path = " root=/dev/mmcblk0p"; |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 221 | |
| 222 | #define MAX_DTBO_IDX_STR 64 |
| 223 | static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx="; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 224 | |
| 225 | #define MAX_DTB_IDX_STR MAX_DTBO_IDX_STR |
| 226 | static const char *android_boot_dtb_idx = " androidboot.dtb_idx="; |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 227 | #endif |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 228 | |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 229 | #include <platform/gpio.h> |
| 230 | static const char *PCBA_STAGE_0 = " androidboot.pcbastage=EP0"; |
| 231 | static const char *PCBA_STAGE_1 = " androidboot.pcbastage=EP1"; |
| 232 | static const char *PCBA_STAGE_2 = " androidboot.pcbastage=EP2"; |
| 233 | static const char *PCBA_STAGE_3 = " androidboot.pcbastage=FP"; |
| 234 | static const char *PCBA_STAGE_4 = " androidboot.pcbastage=MP"; |
michaellin | af8857a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 235 | static const char *PCBA_STAGE_5 = " androidboot.pcbastage=MP-8903MB_001"; |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 236 | static const char *PCBA_STAGE_F = " androidboot.pcbastage=Reserved"; |
| 237 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 238 | #if VERIFIED_BOOT |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 239 | static const char *verity_mode = " androidboot.veritymode="; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 240 | static const char *verified_state= " androidboot.verifiedbootstate="; |
Parth Dixit | a5715a0 | 2015-10-29 12:25:10 +0530 | [diff] [blame] | 241 | static const char *keymaster_v1= " androidboot.keymaster=1"; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 242 | //indexed based on enum values, green is 0 by default |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 243 | |
| 244 | struct verified_boot_verity_mode vbvm[] = |
| 245 | { |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 246 | #if ENABLE_VB_ATTEST |
| 247 | {false, "eio"}, |
| 248 | #else |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 249 | {false, "logging"}, |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 250 | #endif |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 251 | {true, "enforcing"}, |
| 252 | }; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 253 | struct verified_boot_state_name vbsn[] = |
| 254 | { |
| 255 | {GREEN, "green"}, |
| 256 | {ORANGE, "orange"}, |
| 257 | {YELLOW,"yellow"}, |
| 258 | {RED,"red" }, |
| 259 | }; |
| 260 | #endif |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 261 | /*As per spec delay wait time before shutdown in Red state*/ |
| 262 | #define DELAY_WAIT 30000 |
Ashish Bhimanpalliwar | 8cb34fd | 2020-11-12 19:26:41 +0530 | [diff] [blame] | 263 | static unsigned page_size = BOARD_KERNEL_PAGESIZE; |
| 264 | |
| 265 | uint32_t kernel_hdr_page_size() |
| 266 | { |
| 267 | return page_size; |
| 268 | } |
| 269 | |
| 270 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 271 | static unsigned page_mask = 0; |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 272 | static unsigned mmc_blocksize = 0; |
| 273 | static unsigned mmc_blocksize_mask = 0; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 274 | static char ffbm_mode_string[FFBM_MODE_BUF_SIZE]; |
| 275 | static bool boot_into_ffbm; |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 276 | static char *target_boot_params = NULL; |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 277 | static bool boot_reason_alarm; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 278 | static bool devinfo_present = true; |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 279 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 280 | static bool prodinfo_present = true; |
| 281 | #endif |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 282 | bool boot_into_fastboot = false; |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 283 | static uint32_t dt_size = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 284 | static char *vbcmdline; |
| 285 | static bootinfo info = {0}; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 286 | static void *recovery_dtbo_buf = NULL; |
| 287 | static uint32_t recovery_dtbo_size = 0; |
| 288 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 289 | /* Assuming unauthorized kernel image by default */ |
| 290 | static int auth_kernel_img = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 291 | static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}}; |
| 292 | |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 293 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 294 | static prod_info prod = {PRODINFO_MAGIC, {0}, {0}, 0}; |
| 295 | #endif |
| 296 | |
jhchen | a8a15dd | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 297 | #ifdef ENABLE_FUSE_CHECK |
| 298 | static const char *fuse_blown = " androidboot.oem.color=red"; |
| 299 | static const char *fuse_not_blown = " androidboot.oem.color=brown"; |
| 300 | #endif |
| 301 | |
| 302 | #ifdef ENABLE_FUSE_CHECK |
| 303 | int is_fused = 0; |
| 304 | #endif |
| 305 | |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 306 | static bool is_allow_unlock = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 307 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 308 | static char frp_ptns[2][8] = {"config","frp"}; |
| 309 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 310 | static const char *critical_flash_allowed_ptn[] = { |
| 311 | "aboot", |
| 312 | "rpm", |
| 313 | "tz", |
| 314 | "sbl", |
| 315 | "sdi", |
| 316 | "sbl1", |
| 317 | "xbl", |
| 318 | "hyp", |
| 319 | "pmic", |
| 320 | "bootloader", |
| 321 | "devinfo", |
| 322 | "partition"}; |
| 323 | |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 324 | static const char *VirtualAbCriticalPartitions[] = { |
| 325 | "misc", |
| 326 | "metadata", |
| 327 | "userdata"}; |
| 328 | |
| 329 | static bool CheckVirtualAbCriticalPartition (const char *PartitionName); |
| 330 | |
Rahul Shahare | e7baede | 2021-02-02 13:23:57 +0530 | [diff] [blame] | 331 | static const char *VabSnapshotMergeStatus[] = { |
| 332 | "none", |
| 333 | "unknown", |
| 334 | "snapshotted", |
| 335 | "merging", |
| 336 | "cancelled"}; |
| 337 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 338 | struct atag_ptbl_entry |
| 339 | { |
| 340 | char name[16]; |
| 341 | unsigned offset; |
| 342 | unsigned size; |
| 343 | unsigned flags; |
| 344 | }; |
| 345 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 346 | /* |
| 347 | * Partition info, required to be published |
| 348 | * for fastboot |
| 349 | */ |
| 350 | struct getvar_partition_info { |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 351 | char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */ |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 352 | char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */ |
| 353 | char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */ |
| 354 | char size_response[MAX_RSP_SIZE]; /* fastboot response for size */ |
| 355 | char type_response[MAX_RSP_SIZE]; /* fastboot response for type */ |
| 356 | }; |
| 357 | |
| 358 | /* |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 359 | * Update the part_type_known for known paritions types. |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 360 | */ |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 361 | #define RAW_STR "raw" |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 362 | #define EXT_STR "ext4" |
| 363 | #define F2FS_STR "f2fs" |
| 364 | |
| 365 | #define FS_SUPERBLOCK_OFFSET 0x400 |
| 366 | #define EXT_MAGIC 0xEF53 |
| 367 | #define EXT_MAGIC_OFFSET_SB 0x38 |
| 368 | #define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number |
| 369 | #define F2FS_MAGIC_OFFSET_SB 0x0 |
| 370 | |
| 371 | typedef enum fs_signature_type { |
| 372 | EXT_FS_SIGNATURE = 1, |
| 373 | EXT_F2FS_SIGNATURE = 2, |
| 374 | NO_FS = -1 |
| 375 | } fs_signature_type; |
| 376 | |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 377 | struct getvar_partition_info part_info[NUM_PARTITIONS]; |
| 378 | struct getvar_partition_info part_type_known[] = |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 379 | { |
Mayank Grover | 4992021 | 2018-02-09 18:15:55 +0530 | [diff] [blame] | 380 | { "system" , "partition-size:", "partition-type:", "", "ext4" }, |
| 381 | { "userdata" , "partition-size:", "partition-type:", "", "ext4" }, |
| 382 | { "cache" , "partition-size:", "partition-type:", "", "ext4" }, |
| 383 | { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" }, |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 384 | }; |
| 385 | |
| 386 | char max_download_size[MAX_RSP_SIZE]; |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 387 | char charger_screen_enabled[MAX_RSP_SIZE]; |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 388 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 389 | char cust_sn_buf[PRODINFO_MAX_SSN_LEN + 1]; |
| 390 | char factory_sn_buf[PRODINFO_MAX_SSN_LEN + 1]; |
| 391 | char AdbEnable[MAX_RSP_SIZE]; |
| 392 | #endif |
| 393 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 394 | char sn_buf[PRODINFO_MAX_ISN_LEN + 1]; |
| 395 | #else |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 396 | char sn_buf[13]; |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 397 | #endif |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 398 | char display_panel_buf[MAX_PANEL_BUF_SIZE]; |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 399 | char panel_display_mode[MAX_RSP_SIZE]; |
Mayank Grover | a64dfbe | 2018-05-17 14:06:34 +0530 | [diff] [blame] | 400 | char soc_version_str[MAX_RSP_SIZE]; |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 401 | char block_size_string[MAX_RSP_SIZE]; |
Rahul Shahare | e7baede | 2021-02-02 13:23:57 +0530 | [diff] [blame] | 402 | static char SnapshotMergeState[MAX_RSP_SIZE]; |
Mukesh Ojha | bb6e7f3 | 2018-03-02 15:04:06 +0530 | [diff] [blame] | 403 | #if PRODUCT_IOT |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 404 | |
| 405 | /* For IOT we are using custom version */ |
| 406 | #define PRODUCT_IOT_VERSION "IOT001" |
| 407 | char bootloader_version_string[MAX_RSP_SIZE]; |
| 408 | #endif |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 409 | |
| 410 | #if CHECK_BAT_VOLTAGE |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 411 | char battery_voltage[MAX_RSP_SIZE]; |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 412 | char battery_soc_ok [MAX_RSP_SIZE]; |
| 413 | #endif |
| 414 | |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 415 | char get_variant[MAX_RSP_SIZE]; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 416 | |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 417 | extern int emmc_recovery_init(void); |
| 418 | |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 419 | #if NO_KEYPAD_DRIVER |
| 420 | extern int fastboot_trigger(void); |
| 421 | #endif |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 422 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 423 | 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] | 424 | { |
| 425 | /* overwrite the destination of specified for the project */ |
Channagoud Kadabi | 7042fa3 | 2013-04-26 16:44:14 -0700 | [diff] [blame] | 426 | #ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 427 | if (is_arm64) |
| 428 | hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR; |
| 429 | else |
| 430 | hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR; |
Channagoud Kadabi | 7042fa3 | 2013-04-26 16:44:14 -0700 | [diff] [blame] | 431 | hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR; |
| 432 | hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR; |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 433 | #endif |
| 434 | } |
| 435 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 436 | static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn) |
| 437 | { |
| 438 | struct atag_ptbl_entry atag_ptn; |
| 439 | |
| 440 | memcpy(atag_ptn.name, ptn->name, 16); |
| 441 | atag_ptn.name[15] = '\0'; |
| 442 | atag_ptn.offset = ptn->start; |
| 443 | atag_ptn.size = ptn->length; |
| 444 | atag_ptn.flags = ptn->flags; |
| 445 | memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry)); |
| 446 | *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned); |
| 447 | } |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 448 | #ifdef VERIFIED_BOOT_2 |
| 449 | void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz) |
| 450 | { |
| 451 | int index = 0; |
| 452 | char *vbm_img_buf = NULL; |
| 453 | unsigned long long ptn = 0; |
| 454 | unsigned long long ptn_size = 0; |
| 455 | |
| 456 | /* Immediately return if dtbo is not supported */ |
| 457 | index = partition_get_index("vbmeta"); |
| 458 | ptn = partition_get_offset(index); |
| 459 | if(!ptn) |
| 460 | { |
| 461 | dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n"); |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | ptn_size = partition_get_size(index); |
| 466 | if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF) |
| 467 | { |
| 468 | dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n"); |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE)); |
| 473 | if (!vbm_img_buf) |
| 474 | { |
| 475 | dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n"); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | mmc_set_lun(partition_get_lun(index)); |
| 480 | if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size)) |
| 481 | { |
| 482 | dprintf(CRITICAL, "ERROR: vbmeta read failure\n"); |
| 483 | free(vbm_img_buf); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | *vbmeta_image_buf = vbm_img_buf; |
| 488 | *vbmeta_image_sz = (uint32_t)ptn_size; |
| 489 | return; |
| 490 | } |
| 491 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 492 | |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 493 | #if CHECK_BAT_VOLTAGE |
| 494 | void update_battery_status(void) |
| 495 | { |
| 496 | snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage()); |
| 497 | snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no"); |
| 498 | } |
| 499 | #endif |
| 500 | |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 501 | uint32_t GetPcbaVariant(void) |
| 502 | { |
| 503 | uint8_t GPIO_97=10; |
| 504 | uint8_t GPIO_98=10; |
| 505 | uint8_t GPIO_99=10; |
| 506 | |
| 507 | uint8_t pcba_stage = 0; |
| 508 | |
| 509 | // Config GPIO |
| 510 | gpio_tlmm_config(97, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE); |
| 511 | gpio_tlmm_config(98, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE); |
| 512 | gpio_tlmm_config(99, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE); |
| 513 | |
| 514 | /* Wait for the gpio config to take effect - debounce time */ |
| 515 | thread_sleep(10); |
| 516 | |
| 517 | /* Get status of GPIO */ |
| 518 | GPIO_97 = gpio_status(97); |
| 519 | GPIO_98 = gpio_status(98); |
| 520 | GPIO_99 = gpio_status(99); |
| 521 | |
| 522 | pcba_stage = (GPIO_99 << 2) + (GPIO_98 << 1) + GPIO_97; |
| 523 | dprintf(ALWAYS, "pcba_stage status: %u\n", pcba_stage); |
| 524 | // GPIO_99 GPIO_98 GPIO_97 |
| 525 | // 0 0 0 EP0 |
| 526 | // 0 0 1 EP1 |
| 527 | // 0 1 0 EP2 |
| 528 | // 0 1 1 PP |
michaellin | af8857a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 529 | // 0 1 1 FP(8901MB-007)/PP |
| 530 | // 1 0 0 MP(8901MB-008) |
| 531 | // 1 0 1 MP-8903MB_001 |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 532 | return pcba_stage; |
| 533 | } |
| 534 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 535 | unsigned char *update_cmdline(const char * cmdline) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 536 | { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 537 | int cmdline_len = 0; |
| 538 | int have_cmdline = 0; |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 539 | unsigned char *cmdline_final = NULL; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 540 | int pause_at_bootup = 0; |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 541 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 542 | int AdbEnable = 0; |
| 543 | #endif |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 544 | bool warm_boot = false; |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 545 | bool gpt_exists = partition_gpt_exists(); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 546 | int have_target_boot_params = 0; |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 547 | char *boot_dev_buf = NULL; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 548 | #ifdef MDTP_SUPPORT |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 549 | bool is_mdtp_activated = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 550 | #endif |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 551 | int current_active_slot = INVALID; |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 552 | int system_ptn_index = -1; |
| 553 | unsigned int lun = 0; |
| 554 | char lun_char_base = 'a'; |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 555 | #if VERITY_LE |
| 556 | int syspath_buflen = strlen(verity_dev) |
| 557 | + strlen(verity_system_part) + (sizeof(char) * 2) + 2 |
| 558 | + strlen(verity_params) + sizeof(int) + 2; |
| 559 | #else |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 560 | int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/ |
| 561 | char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0"; |
| 562 | int dtbo_idx = INVALID_PTN; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 563 | char dtb_idx_str[MAX_DTB_IDX_STR] = "\0"; |
| 564 | int dtb_idx = INVALID_PTN; |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 565 | #endif |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 566 | char syspath_buf[syspath_buflen]; |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 567 | #if HIBERNATION_SUPPORT |
| 568 | int resume_buflen = strlen(resume) + sizeof(int) + 2; |
| 569 | char resume_buf[resume_buflen]; |
| 570 | int swap_ptn_index = INVALID_PTN; |
| 571 | #endif |
| 572 | |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 573 | #if VERIFIED_BOOT |
| 574 | uint32_t boot_state = RED; |
| 575 | #endif |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 576 | |
| 577 | #if USE_LE_SYSTEMD |
| 578 | is_systemd_present=true; |
| 579 | #endif |
| 580 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 581 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 582 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 583 | { |
| 584 | boot_state = boot_verify_get_state(); |
| 585 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 586 | #endif |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 587 | |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 588 | #ifdef MDTP_SUPPORT |
| 589 | mdtp_activated(&is_mdtp_activated); |
| 590 | #endif /* MDTP_SUPPORT */ |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 591 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 592 | if (cmdline && cmdline[0]) { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 593 | cmdline_len = strlen(cmdline); |
| 594 | have_cmdline = 1; |
| 595 | } |
| 596 | if (target_is_emmc_boot()) { |
| 597 | cmdline_len += strlen(emmc_cmdline); |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 598 | boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN); |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 599 | if (!boot_dev_buf) { |
| 600 | dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n"); |
| 601 | } else { |
lijuang | d28587b | 2021-04-27 15:54:05 +0800 | [diff] [blame] | 602 | 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] | 603 | #if USE_BOOTDEV_CMDLINE |
| 604 | cmdline_len += strlen(boot_dev_buf); |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 605 | #endif |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 606 | if (target_dynamic_partition_supported()) { |
| 607 | cmdline_len += strlen(dynamic_bootdev_cmdline); |
| 608 | cmdline_len += strlen(boot_dev_buf); |
| 609 | } |
| 610 | } |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 611 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 612 | |
| 613 | cmdline_len += strlen(usb_sn_cmdline); |
| 614 | cmdline_len += strlen(sn_buf); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 615 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 616 | cmdline_len += strlen(cust_sn_cmdline); |
| 617 | cmdline_len += strlen(cust_sn_buf); |
| 618 | cmdline_len += strlen(factory_sn_cmdline); |
| 619 | cmdline_len += strlen(factory_sn_buf); |
| 620 | #endif |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 621 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 622 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 623 | if (VB_M <= target_get_vb_version()) |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 624 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 625 | cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name); |
| 626 | if ((device.verity_mode != 0 ) && (device.verity_mode != 1)) |
| 627 | { |
| 628 | dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n"); |
| 629 | ASSERT(0); |
| 630 | } |
| 631 | cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name); |
| 632 | cmdline_len += strlen(keymaster_v1); |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 633 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 634 | #endif |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 635 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 636 | |
| 637 | if (vbcmdline != NULL) { |
| 638 | dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n", |
| 639 | strlen(vbcmdline)); |
| 640 | cmdline_len += strlen(vbcmdline); |
| 641 | } |
| 642 | |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 643 | if (boot_into_recovery && gpt_exists) |
| 644 | cmdline_len += strlen(secondary_gpt_enable); |
| 645 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 646 | #ifdef MDTP_SUPPORT |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 647 | if(is_mdtp_activated) |
| 648 | cmdline_len += strlen(mdtp_activated_flag); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 649 | #endif |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 650 | if (boot_into_ffbm) { |
| 651 | cmdline_len += strlen(androidboot_mode); |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 652 | |
| 653 | if(is_systemd_present) |
| 654 | cmdline_len += strlen(systemd_ffbm_mode); |
| 655 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 656 | cmdline_len += strlen(ffbm_mode_string); |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 657 | /* reduce kernel console messages to speed-up boot */ |
| 658 | cmdline_len += strlen(loglevel); |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 659 | } else if (boot_reason_alarm) { |
| 660 | cmdline_len += strlen(alarmboot_cmdline); |
Zhenhua Huang | 431dafa | 2015-06-30 16:13:37 +0800 | [diff] [blame] | 661 | } else if ((target_build_variant_user() || device.charger_screen_enabled) |
lijuang | 266b17d | 2018-08-17 18:53:42 +0800 | [diff] [blame] | 662 | && target_pause_for_battery_charge() && !boot_into_recovery) { |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 663 | pause_at_bootup = 1; |
| 664 | cmdline_len += strlen(battchg_pause); |
| 665 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 666 | |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 667 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 668 | if (prod.is_adb_enabled) { |
| 669 | dprintf(CRITICAL, "Device will enable adb\n"); |
| 670 | |
| 671 | prod.is_adb_enabled = 0; |
| 672 | write_prod_info(&prod); |
| 673 | |
| 674 | AdbEnable = 1; |
| 675 | cmdline_len += strlen(UsbAdbEnable); |
| 676 | } |
| 677 | #endif |
| 678 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 679 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 680 | cmdline_len += strlen(auth_kernel); |
| 681 | } |
| 682 | |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 683 | if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" : |
| 684 | "system", |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 685 | &target_boot_params) == 0) { |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 686 | have_target_boot_params = 1; |
| 687 | cmdline_len += strlen(target_boot_params); |
| 688 | } |
| 689 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 690 | /* Determine correct androidboot.baseband to use */ |
| 691 | switch(target_baseband()) |
| 692 | { |
| 693 | case BASEBAND_APQ: |
| 694 | cmdline_len += strlen(baseband_apq); |
| 695 | break; |
| 696 | |
| 697 | case BASEBAND_MSM: |
| 698 | cmdline_len += strlen(baseband_msm); |
| 699 | break; |
| 700 | |
| 701 | case BASEBAND_CSFB: |
| 702 | cmdline_len += strlen(baseband_csfb); |
| 703 | break; |
| 704 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 705 | case BASEBAND_SVLTE2A: |
| 706 | cmdline_len += strlen(baseband_svlte2a); |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 707 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 708 | |
| 709 | case BASEBAND_MDM: |
| 710 | cmdline_len += strlen(baseband_mdm); |
| 711 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 712 | |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 713 | case BASEBAND_MDM2: |
| 714 | cmdline_len += strlen(baseband_mdm2); |
| 715 | break; |
| 716 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 717 | case BASEBAND_SGLTE: |
| 718 | cmdline_len += strlen(baseband_sglte); |
| 719 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 720 | |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 721 | case BASEBAND_SGLTE2: |
| 722 | cmdline_len += strlen(baseband_sglte2); |
| 723 | break; |
| 724 | |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 725 | case BASEBAND_DSDA: |
| 726 | cmdline_len += strlen(baseband_dsda); |
| 727 | break; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 728 | |
| 729 | case BASEBAND_DSDA2: |
| 730 | cmdline_len += strlen(baseband_dsda2); |
| 731 | break; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 732 | case BASEBAND_APQ_NOWGR: |
| 733 | cmdline_len += strlen(baseband_apq_nowgr); |
| 734 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 735 | } |
| 736 | |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 737 | switch(GetPcbaVariant()) |
| 738 | { |
| 739 | case 0: // EP0 |
| 740 | cmdline_len += strlen(PCBA_STAGE_0); |
| 741 | break; |
| 742 | |
| 743 | case 1: // EP1 |
| 744 | cmdline_len += strlen(PCBA_STAGE_1); |
| 745 | break; |
| 746 | |
| 747 | case 2: // EP2 |
| 748 | cmdline_len += strlen(PCBA_STAGE_2); |
| 749 | break; |
| 750 | |
michaellin | af8857a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 751 | case 3: // FP(8901MB-007)/PP |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 752 | cmdline_len += strlen(PCBA_STAGE_3); |
| 753 | break; |
| 754 | |
michaellin | af8857a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 755 | case 4: // MP(8901MB-008) |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 756 | cmdline_len += strlen(PCBA_STAGE_4); |
| 757 | break; |
| 758 | |
michaellin | af8857a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 759 | case 5: // MP-8903MB_001 |
| 760 | cmdline_len += strlen(PCBA_STAGE_5); |
| 761 | break; |
| 762 | |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 763 | default:// Reserved |
| 764 | cmdline_len += strlen(PCBA_STAGE_F); |
| 765 | break; |
| 766 | } |
| 767 | |
Vladislav Levenetz | 3cd4524 | 2017-04-05 08:58:07 +0300 | [diff] [blame] | 768 | #if ENABLE_DISPLAY |
Lijuan Gao | 4a5b832 | 2014-07-24 10:38:42 +0800 | [diff] [blame] | 769 | if (cmdline) { |
| 770 | if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) && |
Padmanabhan Komanduru | bccbcdc | 2015-06-30 16:19:24 +0530 | [diff] [blame] | 771 | target_display_panel_node(display_panel_buf, |
| 772 | MAX_PANEL_BUF_SIZE) && |
Lijuan Gao | 4a5b832 | 2014-07-24 10:38:42 +0800 | [diff] [blame] | 773 | strlen(display_panel_buf)) { |
| 774 | cmdline_len += strlen(display_panel_buf); |
| 775 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 776 | } |
Vladislav Levenetz | 3cd4524 | 2017-04-05 08:58:07 +0300 | [diff] [blame] | 777 | #endif |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 778 | |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 779 | if (target_warm_boot()) { |
| 780 | warm_boot = true; |
| 781 | cmdline_len += strlen(warmboot_cmdline); |
| 782 | } |
| 783 | |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 784 | if (target_uses_system_as_root() || |
| 785 | partition_multislot_is_supported()) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 786 | { |
| 787 | current_active_slot = partition_find_active_slot(); |
| 788 | cmdline_len += (strlen(androidboot_slot_suffix)+ |
| 789 | strlen(SUFFIX_SLOT(current_active_slot))); |
| 790 | |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 791 | system_ptn_index = partition_get_index("system"); |
| 792 | if (platform_boot_dev_isemmc()) |
| 793 | { |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 794 | #if VERITY_LE |
| 795 | /* |
| 796 | Condition 4: Verity and A/B both enabled |
| 797 | Eventual command line looks like: |
| 798 | ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ... |
| 799 | ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>" |
| 800 | */ |
| 801 | snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"", |
| 802 | verity_dev, |
| 803 | verity_system_part, suffix_slot[current_active_slot], |
| 804 | verity_params, system_ptn_index + 1); |
| 805 | #else |
| 806 | /* |
| 807 | Condition 5: A/B enabled, but verity disabled |
| 808 | Eventual command line looks like: |
| 809 | ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ... |
| 810 | ... root=/dev/mmcblk0p<NN> ... |
| 811 | */ |
| 812 | snprintf(syspath_buf, syspath_buflen, " %s%d", |
| 813 | sys_path, system_ptn_index + 1); |
| 814 | #endif |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 815 | } |
| 816 | else |
| 817 | { |
| 818 | lun = partition_get_lun(system_ptn_index); |
| 819 | snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d", |
| 820 | lun_char_base + lun, |
| 821 | partition_get_index_in_lun("system", lun)); |
| 822 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 823 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 824 | #ifndef VERIFIED_BOOT_2 |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 825 | cmdline_len += strlen(syspath_buf); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 826 | #endif |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | if (target_uses_system_as_root() || |
| 830 | partition_multislot_is_supported()) |
| 831 | { |
| 832 | cmdline_len += strlen(sys_path_cmdline); |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 833 | |
| 834 | /* For dynamic partition, support skip skip_initramfs */ |
| 835 | if (!target_dynamic_partition_supported() && |
| 836 | !boot_into_recovery) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 837 | cmdline_len += strlen(skip_ramfs); |
| 838 | } |
| 839 | |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 840 | #if HIBERNATION_SUPPORT |
| 841 | if (platform_boot_dev_isemmc()) |
| 842 | { |
| 843 | swap_ptn_index = partition_get_index("swap"); |
| 844 | if (swap_ptn_index != INVALID_PTN) |
| 845 | { |
| 846 | snprintf(resume_buf, resume_buflen, |
| 847 | " %s%d", resume, |
| 848 | (swap_ptn_index + 1)); |
| 849 | cmdline_len += strlen(resume_buf); |
| 850 | } |
| 851 | else |
| 852 | { |
| 853 | dprintf(INFO, "WARNING: swap partition not found\n"); |
| 854 | } |
| 855 | } |
| 856 | #endif |
| 857 | |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 858 | #if TARGET_CMDLINE_SUPPORT |
| 859 | char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF); |
| 860 | int target_cmd_line_len; |
| 861 | ASSERT(target_cmdline_buf); |
| 862 | target_cmd_line_len = target_update_cmdline(target_cmdline_buf); |
| 863 | cmdline_len += target_cmd_line_len; |
| 864 | #endif |
| 865 | |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 866 | #if !VERITY_LE |
| 867 | dtbo_idx = get_dtbo_idx (); |
| 868 | if (dtbo_idx != INVALID_PTN) { |
| 869 | snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d", |
| 870 | android_boot_dtbo_idx, dtbo_idx); |
| 871 | cmdline_len += strlen (dtbo_idx_str); |
| 872 | } |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 873 | |
| 874 | dtb_idx = get_dtb_idx (); |
| 875 | if (dtb_idx != INVALID_PTN) { |
| 876 | snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d", |
| 877 | android_boot_dtb_idx, dtb_idx); |
| 878 | cmdline_len += strlen (dtb_idx_str); |
| 879 | } |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 880 | #endif |
| 881 | |
jhchen | a8a15dd | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 882 | #ifdef ENABLE_FUSE_CHECK |
| 883 | { |
| 884 | int hw_key_status = readl(0xA01D0); |
| 885 | is_fused = (hw_key_status & 0x2) >> 1; |
| 886 | dprintf(CRITICAL, "hw_key_status = 0x%x, is_fused=%d\n", hw_key_status, is_fused); |
| 887 | if (is_fused) { |
| 888 | cmdline_len += strlen(fuse_blown); |
| 889 | } else { |
| 890 | cmdline_len += strlen(fuse_not_blown); |
| 891 | } |
| 892 | } |
| 893 | #endif |
| 894 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 895 | if (cmdline_len > 0) { |
| 896 | const char *src; |
Maria Yu | 52254c0 | 2014-07-04 16:14:54 +0800 | [diff] [blame] | 897 | unsigned char *dst; |
| 898 | |
| 899 | cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3)); |
| 900 | ASSERT(cmdline_final != NULL); |
vijay kumar | 287bb54 | 2015-09-29 13:01:52 +0530 | [diff] [blame] | 901 | memset((void *)cmdline_final, 0, sizeof(*cmdline_final)); |
Maria Yu | 52254c0 | 2014-07-04 16:14:54 +0800 | [diff] [blame] | 902 | dst = cmdline_final; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 903 | |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 904 | /* Save start ptr for debug print */ |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 905 | if (have_cmdline) { |
| 906 | src = cmdline; |
| 907 | while ((*dst++ = *src++)); |
| 908 | } |
| 909 | if (target_is_emmc_boot()) { |
| 910 | src = emmc_cmdline; |
| 911 | if (have_cmdline) --dst; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 912 | have_cmdline = 1; |
| 913 | while ((*dst++ = *src++)); |
Sridhar Parasuram | 7bd4aaf | 2015-02-12 11:14:38 -0800 | [diff] [blame] | 914 | #if USE_BOOTDEV_CMDLINE |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 915 | src = boot_dev_buf; |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 916 | if (have_cmdline && |
| 917 | boot_dev_buf) { |
| 918 | --dst; |
| 919 | while ((*dst++ = *src++)); |
| 920 | } |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 921 | #endif |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 922 | /* Dynamic partition append boot_devices */ |
| 923 | if (target_dynamic_partition_supported() && |
| 924 | boot_dev_buf) { |
| 925 | src = dynamic_bootdev_cmdline; |
| 926 | if (have_cmdline) --dst; |
| 927 | while ((*dst++ = *src++)); |
| 928 | src = boot_dev_buf; |
| 929 | if (have_cmdline) --dst; |
| 930 | while ((*dst++ = *src++)); |
| 931 | } |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 932 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 933 | |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 934 | switch(GetPcbaVariant()) |
| 935 | { |
| 936 | case 0: |
| 937 | src = PCBA_STAGE_0; |
| 938 | if (have_cmdline) --dst; |
| 939 | while ((*dst++ = *src++)); |
| 940 | break; |
| 941 | case 1: |
| 942 | src = PCBA_STAGE_1; |
| 943 | if (have_cmdline) --dst; |
| 944 | while ((*dst++ = *src++)); |
| 945 | break; |
| 946 | case 2: |
| 947 | src = PCBA_STAGE_2; |
| 948 | if (have_cmdline) --dst; |
| 949 | while ((*dst++ = *src++)); |
| 950 | break; |
| 951 | case 3: |
| 952 | src = PCBA_STAGE_3; |
| 953 | if (have_cmdline) --dst; |
| 954 | while ((*dst++ = *src++)); |
| 955 | break; |
| 956 | case 4: |
| 957 | src = PCBA_STAGE_4; |
| 958 | if (have_cmdline) --dst; |
| 959 | while ((*dst++ = *src++)); |
| 960 | break; |
michaellin | af8857a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 961 | case 5: |
| 962 | src = PCBA_STAGE_5; |
| 963 | if (have_cmdline) --dst; |
| 964 | while ((*dst++ = *src++)); |
| 965 | break; |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 966 | default: |
| 967 | src = PCBA_STAGE_F; |
| 968 | if (have_cmdline) --dst; |
| 969 | while ((*dst++ = *src++)); |
| 970 | break; |
| 971 | } |
| 972 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 973 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 974 | if (VB_M <= target_get_vb_version()) |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 975 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 976 | src = verified_state; |
| 977 | if(have_cmdline) --dst; |
| 978 | have_cmdline = 1; |
| 979 | while ((*dst++ = *src++)); |
| 980 | src = vbsn[boot_state].name; |
| 981 | if(have_cmdline) --dst; |
| 982 | while ((*dst++ = *src++)); |
| 983 | |
| 984 | if ((device.verity_mode != 0 ) && (device.verity_mode != 1)) |
| 985 | { |
| 986 | dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n"); |
| 987 | ASSERT(0); |
| 988 | } |
| 989 | src = verity_mode; |
| 990 | if(have_cmdline) --dst; |
| 991 | while ((*dst++ = *src++)); |
| 992 | src = vbvm[device.verity_mode].name; |
| 993 | if(have_cmdline) -- dst; |
| 994 | while ((*dst++ = *src++)); |
| 995 | src = keymaster_v1; |
| 996 | if(have_cmdline) --dst; |
| 997 | while ((*dst++ = *src++)); |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 998 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 999 | #endif |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1000 | |
| 1001 | if (vbcmdline != NULL) { |
| 1002 | src = vbcmdline; |
| 1003 | if (have_cmdline) --dst; |
| 1004 | while ((*dst++ = *src++)); |
| 1005 | } |
| 1006 | |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 1007 | src = usb_sn_cmdline; |
| 1008 | if (have_cmdline) --dst; |
| 1009 | have_cmdline = 1; |
| 1010 | while ((*dst++ = *src++)); |
| 1011 | src = sn_buf; |
| 1012 | if (have_cmdline) --dst; |
| 1013 | have_cmdline = 1; |
| 1014 | while ((*dst++ = *src++)); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 1015 | |
| 1016 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 1017 | src = cust_sn_cmdline; |
| 1018 | if (have_cmdline) --dst; |
| 1019 | have_cmdline = 1; |
| 1020 | while ((*dst++ = *src++)); |
| 1021 | src = cust_sn_buf; |
| 1022 | if (have_cmdline) --dst; |
| 1023 | have_cmdline = 1; |
| 1024 | while ((*dst++ = *src++)); |
| 1025 | src = factory_sn_cmdline; |
| 1026 | if (have_cmdline) --dst; |
| 1027 | have_cmdline = 1; |
| 1028 | while ((*dst++ = *src++)); |
| 1029 | src = factory_sn_buf; |
| 1030 | if (have_cmdline) --dst; |
| 1031 | have_cmdline = 1; |
| 1032 | while ((*dst++ = *src++)); |
| 1033 | #endif |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 1034 | if (warm_boot) { |
| 1035 | if (have_cmdline) --dst; |
| 1036 | src = warmboot_cmdline; |
| 1037 | while ((*dst++ = *src++)); |
| 1038 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 1039 | |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 1040 | if (boot_into_recovery && gpt_exists) { |
| 1041 | src = secondary_gpt_enable; |
| 1042 | if (have_cmdline) --dst; |
| 1043 | while ((*dst++ = *src++)); |
| 1044 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1045 | #ifdef MDTP_SUPPORT |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 1046 | if (is_mdtp_activated) { |
| 1047 | src = mdtp_activated_flag; |
| 1048 | if (have_cmdline) --dst; |
| 1049 | while ((*dst++ = *src++)); |
| 1050 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1051 | #endif |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 1052 | if (boot_into_ffbm) { |
| 1053 | src = androidboot_mode; |
| 1054 | if (have_cmdline) --dst; |
| 1055 | while ((*dst++ = *src++)); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 1056 | src = ffbm_mode_string; |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 1057 | if (have_cmdline) --dst; |
| 1058 | while ((*dst++ = *src++)); |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 1059 | |
| 1060 | if(is_systemd_present) { |
| 1061 | src = systemd_ffbm_mode; |
| 1062 | if (have_cmdline) --dst; |
| 1063 | while ((*dst++ = *src++)); |
| 1064 | } |
| 1065 | |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 1066 | src = loglevel; |
| 1067 | if (have_cmdline) --dst; |
| 1068 | while ((*dst++ = *src++)); |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 1069 | } else if (boot_reason_alarm) { |
| 1070 | src = alarmboot_cmdline; |
| 1071 | if (have_cmdline) --dst; |
| 1072 | while ((*dst++ = *src++)); |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 1073 | } else if (pause_at_bootup) { |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 1074 | src = battchg_pause; |
| 1075 | if (have_cmdline) --dst; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 1076 | while ((*dst++ = *src++)); |
| 1077 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 1078 | |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 1079 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 1080 | if (AdbEnable) { |
| 1081 | src = UsbAdbEnable; |
| 1082 | if (have_cmdline) --dst; |
| 1083 | while ((*dst++ = *src++)); |
| 1084 | } |
| 1085 | #endif |
| 1086 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1087 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 1088 | src = auth_kernel; |
| 1089 | if (have_cmdline) --dst; |
| 1090 | while ((*dst++ = *src++)); |
| 1091 | } |
| 1092 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 1093 | switch(target_baseband()) |
| 1094 | { |
| 1095 | case BASEBAND_APQ: |
| 1096 | src = baseband_apq; |
| 1097 | if (have_cmdline) --dst; |
| 1098 | while ((*dst++ = *src++)); |
| 1099 | break; |
| 1100 | |
| 1101 | case BASEBAND_MSM: |
| 1102 | src = baseband_msm; |
| 1103 | if (have_cmdline) --dst; |
| 1104 | while ((*dst++ = *src++)); |
| 1105 | break; |
| 1106 | |
| 1107 | case BASEBAND_CSFB: |
| 1108 | src = baseband_csfb; |
| 1109 | if (have_cmdline) --dst; |
| 1110 | while ((*dst++ = *src++)); |
| 1111 | break; |
| 1112 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 1113 | case BASEBAND_SVLTE2A: |
| 1114 | src = baseband_svlte2a; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 1115 | if (have_cmdline) --dst; |
| 1116 | while ((*dst++ = *src++)); |
| 1117 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 1118 | |
| 1119 | case BASEBAND_MDM: |
| 1120 | src = baseband_mdm; |
| 1121 | if (have_cmdline) --dst; |
| 1122 | while ((*dst++ = *src++)); |
| 1123 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 1124 | |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 1125 | case BASEBAND_MDM2: |
| 1126 | src = baseband_mdm2; |
| 1127 | if (have_cmdline) --dst; |
| 1128 | while ((*dst++ = *src++)); |
| 1129 | break; |
| 1130 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 1131 | case BASEBAND_SGLTE: |
| 1132 | src = baseband_sglte; |
| 1133 | if (have_cmdline) --dst; |
| 1134 | while ((*dst++ = *src++)); |
| 1135 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 1136 | |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 1137 | case BASEBAND_SGLTE2: |
| 1138 | src = baseband_sglte2; |
| 1139 | if (have_cmdline) --dst; |
| 1140 | while ((*dst++ = *src++)); |
| 1141 | break; |
| 1142 | |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 1143 | case BASEBAND_DSDA: |
| 1144 | src = baseband_dsda; |
| 1145 | if (have_cmdline) --dst; |
| 1146 | while ((*dst++ = *src++)); |
| 1147 | break; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 1148 | |
| 1149 | case BASEBAND_DSDA2: |
| 1150 | src = baseband_dsda2; |
| 1151 | if (have_cmdline) --dst; |
| 1152 | while ((*dst++ = *src++)); |
| 1153 | break; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 1154 | case BASEBAND_APQ_NOWGR: |
| 1155 | src = baseband_apq_nowgr; |
| 1156 | if (have_cmdline) --dst; |
| 1157 | while ((*dst++ = *src++)); |
| 1158 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 1159 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 1160 | |
| 1161 | if (strlen(display_panel_buf)) { |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 1162 | src = display_panel_buf; |
| 1163 | if (have_cmdline) --dst; |
| 1164 | while ((*dst++ = *src++)); |
| 1165 | } |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 1166 | |
| 1167 | if (have_target_boot_params) { |
| 1168 | if (have_cmdline) --dst; |
| 1169 | src = target_boot_params; |
| 1170 | while ((*dst++ = *src++)); |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 1171 | free(target_boot_params); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 1172 | } |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 1173 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1174 | if (partition_multislot_is_supported() && have_cmdline) |
| 1175 | { |
| 1176 | src = androidboot_slot_suffix; |
| 1177 | --dst; |
| 1178 | while ((*dst++ = *src++)); |
| 1179 | --dst; |
| 1180 | src = SUFFIX_SLOT(current_active_slot); |
| 1181 | while ((*dst++ = *src++)); |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1182 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1183 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1184 | |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1185 | /* |
| 1186 | * System-As-Root behaviour, system.img should contain both |
| 1187 | * system content and ramdisk content, and should be mounted at |
| 1188 | * root(a/b). |
| 1189 | * Apending skip_ramfs for non a/b builds which use, system as root. |
| 1190 | */ |
| 1191 | if ((target_uses_system_as_root() || |
| 1192 | partition_multislot_is_supported()) && |
| 1193 | have_cmdline) |
| 1194 | { |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 1195 | if (!target_dynamic_partition_supported() && |
| 1196 | !boot_into_recovery) |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1197 | { |
| 1198 | src = skip_ramfs; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1199 | --dst; |
| 1200 | while ((*dst++ = *src++)); |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | src = sys_path_cmdline; |
| 1204 | --dst; |
| 1205 | while ((*dst++ = *src++)); |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 1206 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1207 | #ifndef VERIFIED_BOOT_2 |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1208 | src = syspath_buf; |
| 1209 | --dst; |
| 1210 | while ((*dst++ = *src++)); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1211 | #endif |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1212 | } |
| 1213 | |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 1214 | #if HIBERNATION_SUPPORT |
| 1215 | if (swap_ptn_index != INVALID_PTN) |
| 1216 | { |
| 1217 | src = resume_buf; |
| 1218 | --dst; |
| 1219 | while ((*dst++ = *src++)); |
| 1220 | } |
| 1221 | #endif |
| 1222 | |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 1223 | #if TARGET_CMDLINE_SUPPORT |
| 1224 | if (target_cmdline_buf && target_cmd_line_len) |
| 1225 | { |
| 1226 | if (have_cmdline) --dst; |
| 1227 | src = target_cmdline_buf; |
| 1228 | while((*dst++ = *src++)); |
| 1229 | free(target_cmdline_buf); |
| 1230 | } |
| 1231 | #endif |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 1232 | |
| 1233 | #if !VERITY_LE |
| 1234 | if (dtbo_idx != INVALID_PTN) { |
| 1235 | src = dtbo_idx_str; |
| 1236 | --dst; |
| 1237 | while ((*dst++ = *src++)); |
| 1238 | } |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1239 | |
| 1240 | if (dtb_idx != INVALID_PTN) { |
| 1241 | src = dtb_idx_str; |
| 1242 | --dst; |
| 1243 | while ((*dst++ = *src++)); |
| 1244 | } |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 1245 | #endif |
jhchen | a8a15dd | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 1246 | |
| 1247 | #ifdef ENABLE_FUSE_CHECK |
| 1248 | if (is_fused) { |
| 1249 | src = fuse_blown; |
| 1250 | if (have_cmdline) --dst; |
| 1251 | while ((*dst++ = *src++)); |
| 1252 | } else { |
| 1253 | src = fuse_not_blown; |
| 1254 | if (have_cmdline) --dst; |
| 1255 | while ((*dst++ = *src++)); |
| 1256 | } |
| 1257 | #endif |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1258 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 1259 | |
| 1260 | |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 1261 | if (boot_dev_buf) |
| 1262 | free(boot_dev_buf); |
| 1263 | |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 1264 | if (cmdline_final) |
| 1265 | dprintf(INFO, "cmdline: %s\n", cmdline_final); |
| 1266 | else |
| 1267 | dprintf(INFO, "cmdline is NULL\n"); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1268 | return cmdline_final; |
| 1269 | } |
| 1270 | |
| 1271 | unsigned *atag_core(unsigned *ptr) |
| 1272 | { |
| 1273 | /* CORE */ |
| 1274 | *ptr++ = 2; |
| 1275 | *ptr++ = 0x54410001; |
| 1276 | |
| 1277 | return ptr; |
| 1278 | |
| 1279 | } |
| 1280 | |
| 1281 | unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk, |
| 1282 | unsigned ramdisk_size) |
| 1283 | { |
| 1284 | if (ramdisk_size) { |
| 1285 | *ptr++ = 4; |
| 1286 | *ptr++ = 0x54420005; |
| 1287 | *ptr++ = (unsigned)ramdisk; |
| 1288 | *ptr++ = ramdisk_size; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1291 | return ptr; |
| 1292 | } |
| 1293 | |
| 1294 | unsigned *atag_ptable(unsigned **ptr_addr) |
| 1295 | { |
| 1296 | int i; |
| 1297 | struct ptable *ptable; |
| 1298 | |
| 1299 | if ((ptable = flash_get_ptable()) && (ptable->count != 0)) { |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1300 | *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) / |
| 1301 | sizeof(unsigned))); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1302 | *(*ptr_addr)++ = 0x4d534d70; |
| 1303 | for (i = 0; i < ptable->count; ++i) |
| 1304 | ptentry_to_tag(ptr_addr, ptable_get(ptable, i)); |
| 1305 | } |
| 1306 | |
| 1307 | return (*ptr_addr); |
| 1308 | } |
| 1309 | |
| 1310 | unsigned *atag_cmdline(unsigned *ptr, const char *cmdline) |
| 1311 | { |
| 1312 | int cmdline_length = 0; |
| 1313 | int n; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1314 | char *dest; |
| 1315 | |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1316 | cmdline_length = strlen((const char*)cmdline); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1317 | n = (cmdline_length + 4) & (~3); |
| 1318 | |
| 1319 | *ptr++ = (n / 4) + 2; |
| 1320 | *ptr++ = 0x54410009; |
| 1321 | dest = (char *) ptr; |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1322 | while ((*dest++ = *cmdline++)); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1323 | ptr += (n / 4); |
| 1324 | |
| 1325 | return ptr; |
| 1326 | } |
| 1327 | |
| 1328 | unsigned *atag_end(unsigned *ptr) |
| 1329 | { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1330 | /* END */ |
| 1331 | *ptr++ = 0; |
| 1332 | *ptr++ = 0; |
| 1333 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1334 | return ptr; |
| 1335 | } |
| 1336 | |
| 1337 | void generate_atags(unsigned *ptr, const char *cmdline, |
| 1338 | void *ramdisk, unsigned ramdisk_size) |
| 1339 | { |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 1340 | unsigned *orig_ptr = ptr; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1341 | ptr = atag_core(ptr); |
| 1342 | ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size); |
| 1343 | ptr = target_atag_mem(ptr); |
| 1344 | |
| 1345 | /* Skip NAND partition ATAGS for eMMC boot */ |
| 1346 | if (!target_is_emmc_boot()){ |
| 1347 | ptr = atag_ptable(&ptr); |
| 1348 | } |
| 1349 | |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 1350 | /* |
| 1351 | * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned |
| 1352 | * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes |
| 1353 | */ |
Mayank Grover | bc8a2ef | 2018-02-23 18:03:36 +0530 | [diff] [blame] | 1354 | if (!cmdline) |
| 1355 | return; |
| 1356 | |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 1357 | if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) { |
| 1358 | ptr = atag_cmdline(ptr, cmdline); |
| 1359 | ptr = atag_end(ptr); |
| 1360 | } |
| 1361 | else { |
| 1362 | dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n"); |
| 1363 | ASSERT(0); |
| 1364 | } |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1367 | typedef void entry_func_ptr(unsigned, unsigned, unsigned*); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1368 | void boot_linux(void *kernel, unsigned *tags, |
| 1369 | const char *cmdline, unsigned machtype, |
| 1370 | void *ramdisk, unsigned ramdisk_size) |
| 1371 | { |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1372 | unsigned char *final_cmdline; |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1373 | #if DEVICE_TREE |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1374 | int ret = 0; |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1375 | #endif |
| 1376 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1377 | void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel)); |
Deepa Dinamani | 25a9f76 | 2012-11-30 15:57:15 -0800 | [diff] [blame] | 1378 | uint32_t tags_phys = PA((addr_t)tags); |
vijay kumar | 1a50a64 | 2015-11-16 12:41:15 +0530 | [diff] [blame] | 1379 | struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel))); |
Deepa Dinamani | 25a9f76 | 2012-11-30 15:57:15 -0800 | [diff] [blame] | 1380 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1381 | ramdisk = (void *)PA((addr_t)ramdisk); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1382 | |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1383 | final_cmdline = update_cmdline((const char*)cmdline); |
| 1384 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1385 | #if DEVICE_TREE |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1386 | dprintf(INFO, "Updating device tree: start\n"); |
| 1387 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1388 | /* Update the Device Tree */ |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1389 | 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] | 1390 | if(ret) |
| 1391 | { |
| 1392 | dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n"); |
| 1393 | ASSERT(0); |
| 1394 | } |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1395 | dprintf(INFO, "Updating device tree: done\n"); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1396 | #else |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1397 | /* Generating the Atags */ |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1398 | generate_atags(tags, final_cmdline, ramdisk, ramdisk_size); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1399 | #endif |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1400 | |
Monika Singh | 0ec6db8 | 2019-07-18 16:57:58 +0530 | [diff] [blame] | 1401 | #if VERIFIED_BOOT |
Monika Singh | b0db4b8 | 2018-09-26 12:18:02 +0530 | [diff] [blame] | 1402 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1403 | { |
| 1404 | if (device.verity_mode == 0) { |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1405 | #if FBCON_DISPLAY_MSG |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1406 | #if ENABLE_VB_ATTEST |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1407 | display_bootverify_menu(DISPLAY_MENU_EIO); |
| 1408 | wait_for_users_action(); |
| 1409 | if(!pwr_key_is_pressed) |
| 1410 | shutdown_device(); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1411 | #else |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1412 | display_bootverify_menu(DISPLAY_MENU_LOGGING); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1413 | #endif |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1414 | wait_for_users_action(); |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1415 | #else |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1416 | dprintf(CRITICAL, |
| 1417 | "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n"); |
| 1418 | mdelay(5000); |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1419 | #endif |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1420 | } |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1421 | } |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1422 | #endif |
| 1423 | |
| 1424 | #if VERIFIED_BOOT |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 1425 | /* Write protect the device info */ |
Channagoud Kadabi | 3bd9d1e | 2015-05-05 16:18:20 -0700 | [diff] [blame] | 1426 | 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] | 1427 | { |
| 1428 | dprintf(INFO, "Failed to write protect dev info\n"); |
| 1429 | ASSERT(0); |
| 1430 | } |
| 1431 | #endif |
| 1432 | |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 1433 | /* Turn off splash screen if enabled */ |
| 1434 | #if DISPLAY_SPLASH_SCREEN |
| 1435 | target_display_shutdown(); |
| 1436 | #endif |
| 1437 | |
Veera Sundaram Sankaran | 67ea093 | 2015-09-25 10:09:30 -0700 | [diff] [blame] | 1438 | /* Perform target specific cleanup */ |
| 1439 | target_uninit(); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1440 | free_verified_boot_resource(&info); |
| 1441 | if (final_cmdline) |
| 1442 | free(final_cmdline); |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 1443 | |
Deepa Dinamani | 33734bc | 2013-03-06 12:16:06 -0800 | [diff] [blame] | 1444 | 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] | 1445 | entry, ramdisk, ramdisk_size, (void *)tags_phys); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1446 | |
| 1447 | enter_critical_section(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1448 | |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 1449 | /* do any platform specific cleanup before kernel entry */ |
| 1450 | platform_uninit(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1451 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1452 | arch_disable_cache(UCACHE); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1453 | |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 1454 | #if ARM_WITH_MMU |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1455 | arch_disable_mmu(); |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 1456 | #endif |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 1457 | bs_set_timestamp(BS_KERNEL_ENTRY); |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1458 | |
| 1459 | if (IS_ARM64(kptr)) |
| 1460 | /* Jump to a 64bit kernel */ |
| 1461 | scm_elexec_call((paddr_t)kernel, tags_phys); |
| 1462 | else |
| 1463 | /* Jump to a 32bit kernel */ |
| 1464 | entry(0, machtype, (unsigned*)tags_phys); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1465 | } |
| 1466 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1467 | /* Function to check if the memory address range falls within the aboot |
| 1468 | * boundaries. |
| 1469 | * start: Start of the memory region |
| 1470 | * size: Size of the memory region |
| 1471 | */ |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 1472 | int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1473 | { |
| 1474 | /* Check for boundary conditions. */ |
Sundarajan Srinivasan | ce2a0ea | 2013-12-16 17:02:56 -0800 | [diff] [blame] | 1475 | if ((UINT_MAX - start) < size) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1476 | return -1; |
| 1477 | |
| 1478 | /* Check for memory overlap. */ |
| 1479 | if ((start < MEMBASE) && ((start + size) <= MEMBASE)) |
| 1480 | return 0; |
Channagoud Kadabi | 9414391 | 2013-10-15 12:53:52 -0700 | [diff] [blame] | 1481 | else if (start >= (MEMBASE + MEMSIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1482 | return 0; |
| 1483 | else |
| 1484 | return -1; |
| 1485 | } |
| 1486 | |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 1487 | /* Function to check if the memory address range falls beyond ddr region. |
| 1488 | * start: Start of the memory region |
| 1489 | * size: Size of the memory region |
| 1490 | */ |
| 1491 | int check_ddr_addr_range_bound(uintptr_t start, uint32_t size) |
| 1492 | { |
| 1493 | uintptr_t ddr_pa_start_addr = PA(get_ddr_start()); |
| 1494 | uint64_t ddr_size = smem_get_ddr_size(); |
| 1495 | uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size; |
| 1496 | uintptr_t pa_start_addr = PA(start); |
| 1497 | |
| 1498 | /* Check for boundary conditions. */ |
| 1499 | if ((UINT_MAX - start) < size) |
| 1500 | return -1; |
| 1501 | |
| 1502 | /* Check if memory range is beyond the ddr range. */ |
| 1503 | if (pa_start_addr < ddr_pa_start_addr || |
| 1504 | pa_start_addr >= (ddr_pa_end_addr) || |
| 1505 | (pa_start_addr + size) > ddr_pa_end_addr) |
| 1506 | return -1; |
| 1507 | else |
| 1508 | return 0; |
| 1509 | } |
| 1510 | |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1511 | 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] | 1512 | |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1513 | int getimage(void **image_buffer, uint32_t *imgsize, |
| 1514 | const char *imgname) |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1515 | { |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1516 | uint32_t loadedindex; |
| 1517 | if (image_buffer == NULL || imgsize == NULL || |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1518 | imgname == NULL) { |
| 1519 | dprintf(CRITICAL, "getimage: invalid parameters\n"); |
| 1520 | return -1; |
| 1521 | } |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1522 | for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) { |
| 1523 | if (!strncmp(info.images[loadedindex].name, imgname, |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1524 | strlen(imgname))) { |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1525 | *image_buffer = info.images[loadedindex].image_buffer; |
| 1526 | *imgsize = info.images[loadedindex].imgsize; |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 1527 | dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n", |
| 1528 | info.images[loadedindex].name, |
| 1529 | info.images[loadedindex].imgsize); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1530 | return 0; |
| 1531 | } |
| 1532 | } |
| 1533 | return -1; |
| 1534 | } |
| 1535 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1536 | static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size) |
| 1537 | { |
| 1538 | int ret; |
Channagoud Kadabi | a8c623f | 2015-01-13 14:48:48 -0800 | [diff] [blame] | 1539 | |
| 1540 | #if !VERIFIED_BOOT |
Sundarajan Srinivasan | ce54d6e | 2013-11-11 12:45:00 -0800 | [diff] [blame] | 1541 | #if IMAGE_VERIF_ALGO_SHA1 |
| 1542 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1; |
| 1543 | #else |
| 1544 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256; |
| 1545 | #endif |
Channagoud Kadabi | a8c623f | 2015-01-13 14:48:48 -0800 | [diff] [blame] | 1546 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1547 | |
| 1548 | /* Assume device is rooted at this time. */ |
| 1549 | device.is_tampered = 1; |
| 1550 | |
| 1551 | dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size); |
| 1552 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1553 | #if VERIFIED_BOOT |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 1554 | uint32_t bootstate; |
| 1555 | if(boot_into_recovery && |
Mayank Grover | 68fbf0d | 2017-10-24 14:13:39 +0530 | [diff] [blame] | 1556 | (!partition_multislot_is_supported())) |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 1557 | { |
| 1558 | ret = boot_verify_image((unsigned char *)bootimg_addr, |
| 1559 | bootimg_size, "/recovery", &bootstate); |
| 1560 | } |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1561 | else |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 1562 | { |
| 1563 | ret = boot_verify_image((unsigned char *)bootimg_addr, |
| 1564 | bootimg_size, "/boot", &bootstate); |
| 1565 | } |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1566 | boot_verify_print_state(); |
| 1567 | #else |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1568 | ret = image_verify((unsigned char *)bootimg_addr, |
| 1569 | (unsigned char *)(bootimg_addr + bootimg_size), |
| 1570 | bootimg_size, |
Sundarajan Srinivasan | ce54d6e | 2013-11-11 12:45:00 -0800 | [diff] [blame] | 1571 | auth_algo); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1572 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1573 | dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret); |
| 1574 | |
| 1575 | if (ret) |
| 1576 | { |
| 1577 | /* Authorized kernel */ |
| 1578 | device.is_tampered = 0; |
Sundarajan Srinivasan | 3fb21f1 | 2013-09-16 18:36:15 -0700 | [diff] [blame] | 1579 | auth_kernel_img = 1; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 1582 | #ifdef MDTP_SUPPORT |
| 1583 | { |
| 1584 | /* Verify MDTP lock. |
| 1585 | * For boot & recovery partitions, use aboot's verification result. |
| 1586 | */ |
| 1587 | mdtp_ext_partition_verification_t ext_partition; |
| 1588 | ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT; |
| 1589 | ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID; |
| 1590 | ext_partition.page_size = 0; /* Not needed since already validated */ |
| 1591 | ext_partition.image_addr = 0; /* Not needed since already validated */ |
| 1592 | ext_partition.image_size = 0; /* Not needed since already validated */ |
| 1593 | ext_partition.sig_avail = FALSE; /* Not needed since already validated */ |
| 1594 | mdtp_fwlock_verify_lock(&ext_partition); |
| 1595 | } |
| 1596 | #endif /* MDTP_SUPPORT */ |
| 1597 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1598 | #if USE_PCOM_SECBOOT |
| 1599 | set_tamper_flag(device.is_tampered); |
| 1600 | #endif |
| 1601 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1602 | #if VERIFIED_BOOT |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1603 | switch(boot_verify_get_state()) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1604 | { |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1605 | case RED: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1606 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 1607 | display_bootverify_menu(DISPLAY_MENU_RED); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1608 | #if ENABLE_VB_ATTEST |
| 1609 | mdelay(DELAY_WAIT); |
| 1610 | shutdown_device(); |
| 1611 | #else |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1612 | wait_for_users_action(); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1613 | #endif |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1614 | #else |
| 1615 | dprintf(CRITICAL, |
| 1616 | "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n"); |
| 1617 | mdelay(5000); |
| 1618 | #endif |
| 1619 | |
| 1620 | break; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1621 | case YELLOW: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1622 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 1623 | display_bootverify_menu(DISPLAY_MENU_YELLOW); |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1624 | wait_for_users_action(); |
| 1625 | #else |
| 1626 | dprintf(CRITICAL, |
| 1627 | "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n"); |
| 1628 | mdelay(5000); |
| 1629 | #endif |
| 1630 | break; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1631 | default: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1632 | break; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1633 | } |
| 1634 | #endif |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1635 | #if !VERIFIED_BOOT |
Unnati Gandhi | 1be0475 | 2015-03-27 19:41:53 +0530 | [diff] [blame] | 1636 | if(device.is_tampered) |
| 1637 | { |
| 1638 | write_device_info_mmc(&device); |
| 1639 | #ifdef TZ_TAMPER_FUSE |
Monika Singh | 9825746 | 2018-06-06 11:28:49 +0530 | [diff] [blame] | 1640 | set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE); |
Unnati Gandhi | 1be0475 | 2015-03-27 19:41:53 +0530 | [diff] [blame] | 1641 | #endif |
| 1642 | #ifdef ASSERT_ON_TAMPER |
| 1643 | dprintf(CRITICAL, "Device is tampered. Asserting..\n"); |
| 1644 | ASSERT(0); |
| 1645 | #endif |
| 1646 | } |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1647 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1650 | static bool check_format_bit() |
| 1651 | { |
| 1652 | bool ret = false; |
| 1653 | int index; |
| 1654 | uint64_t offset; |
| 1655 | struct boot_selection_info *in = NULL; |
| 1656 | char *buf = NULL; |
| 1657 | |
| 1658 | index = partition_get_index("bootselect"); |
| 1659 | if (index == INVALID_PTN) |
| 1660 | { |
| 1661 | dprintf(INFO, "Unable to locate /bootselect partition\n"); |
| 1662 | return ret; |
| 1663 | } |
| 1664 | offset = partition_get_offset(index); |
| 1665 | if(!offset) |
| 1666 | { |
| 1667 | dprintf(INFO, "partition /bootselect doesn't exist\n"); |
| 1668 | return ret; |
| 1669 | } |
| 1670 | buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE)); |
Mayank Grover | 4886040 | 2016-11-29 12:34:53 +0530 | [diff] [blame] | 1671 | mmc_set_lun(partition_get_lun(index)); |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1672 | ASSERT(buf); |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1673 | if (mmc_read(offset, (uint32_t *)buf, page_size)) |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1674 | { |
| 1675 | dprintf(INFO, "mmc read failure /bootselect %d\n", page_size); |
| 1676 | free(buf); |
| 1677 | return ret; |
| 1678 | } |
| 1679 | in = (struct boot_selection_info *) buf; |
| 1680 | if ((in->signature == BOOTSELECT_SIGNATURE) && |
| 1681 | (in->version == BOOTSELECT_VERSION)) { |
| 1682 | if ((in->state_info & BOOTSELECT_FORMAT) && |
| 1683 | !(in->state_info & BOOTSELECT_FACTORY)) |
| 1684 | ret = true; |
| 1685 | } else { |
| 1686 | dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n", |
| 1687 | in->signature, in->version); |
| 1688 | ASSERT(0); |
| 1689 | } |
| 1690 | free(buf); |
| 1691 | return ret; |
| 1692 | } |
| 1693 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1694 | void boot_verifier_init() |
| 1695 | { |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1696 | uint32_t boot_state; |
| 1697 | /* Check if device unlock */ |
| 1698 | if(device.is_unlocked) |
| 1699 | { |
| 1700 | boot_verify_send_event(DEV_UNLOCK); |
| 1701 | boot_verify_print_state(); |
| 1702 | dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n"); |
| 1703 | return; |
| 1704 | } |
| 1705 | else |
| 1706 | { |
| 1707 | boot_verify_send_event(BOOT_INIT); |
| 1708 | } |
| 1709 | |
| 1710 | /* Initialize keystore */ |
| 1711 | boot_state = boot_verify_keystore_init(); |
| 1712 | if(boot_state == YELLOW) |
| 1713 | { |
| 1714 | boot_verify_print_state(); |
| 1715 | dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n"); |
| 1716 | } |
| 1717 | } |
| 1718 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1719 | /* Function to return recovery appended dtbo buffer info */ |
| 1720 | void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf) |
| 1721 | { |
| 1722 | *dtbo_size = recovery_dtbo_size; |
| 1723 | *dtbo_buf = recovery_dtbo_buf; |
| 1724 | return; |
| 1725 | } |
| 1726 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1727 | int boot_linux_from_mmc(void) |
| 1728 | { |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1729 | boot_img_hdr *hdr = (void*) buf; |
| 1730 | boot_img_hdr *uhdr; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1731 | unsigned offset = 0; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 1732 | int rcode; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1733 | unsigned long long ptn = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1734 | int index = INVALID_PTN; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1735 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1736 | unsigned char *image_addr = 0; |
| 1737 | unsigned kernel_actual; |
| 1738 | unsigned ramdisk_actual; |
| 1739 | unsigned imagesize_actual; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1740 | unsigned second_actual = 0; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1741 | void * image_buf = NULL; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1742 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1743 | unsigned int dtb_size = 0; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1744 | unsigned dtb_image_size = 0; |
| 1745 | uint32_t dtb_image_offset = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1746 | unsigned int out_len = 0; |
| 1747 | unsigned int out_avai_len = 0; |
| 1748 | unsigned char *out_addr = NULL; |
| 1749 | uint32_t dtb_offset = 0; |
| 1750 | unsigned char *kernel_start_addr = NULL; |
| 1751 | unsigned int kernel_size = 0; |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 1752 | unsigned int patched_kernel_hdr_size = 0; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1753 | uint64_t image_size = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1754 | int rc; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1755 | #if VERIFIED_BOOT_2 |
| 1756 | int status; |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 1757 | void *dtbo_image_buf = NULL; |
| 1758 | uint32_t dtbo_image_sz = 0; |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 1759 | void *vbmeta_image_buf = NULL; |
| 1760 | uint32_t vbmeta_image_sz = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1761 | #endif |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1762 | char *ptn_name = NULL; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1763 | #if DEVICE_TREE |
| 1764 | struct dt_table *table; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1765 | struct dt_entry dt_entry; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1766 | unsigned dt_table_offset; |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1767 | uint32_t dt_actual; |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 1768 | uint32_t dt_hdr_size; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1769 | unsigned char *best_match_dt_addr = NULL; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1770 | #endif |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1771 | struct kernel64_hdr *kptr = NULL; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1772 | int current_active_slot = INVALID; |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1773 | |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1774 | if (check_format_bit()) |
| 1775 | boot_into_recovery = 1; |
| 1776 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 1777 | if (!boot_into_recovery) { |
| 1778 | memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string)); |
| 1779 | rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string)); |
| 1780 | if (rcode <= 0) { |
| 1781 | boot_into_ffbm = false; |
| 1782 | if (rcode < 0) |
| 1783 | dprintf(CRITICAL,"failed to get ffbm cookie"); |
| 1784 | } else |
| 1785 | boot_into_ffbm = true; |
| 1786 | } else |
| 1787 | boot_into_ffbm = false; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1788 | uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1789 | if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 1790 | dprintf(INFO, "Unified boot method!\n"); |
| 1791 | hdr = uhdr; |
| 1792 | goto unified_boot; |
| 1793 | } |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1794 | |
| 1795 | /* For a/b recovery image code is on boot partition. |
| 1796 | If we support multislot, always use boot partition. */ |
| 1797 | if (boot_into_recovery && |
Mayank Grover | 8ab4a76 | 2019-04-25 17:23:34 +0530 | [diff] [blame] | 1798 | ((!partition_multislot_is_supported()) || |
| 1799 | (target_dynamic_partition_supported()))) |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1800 | ptn_name = "recovery"; |
| 1801 | else |
| 1802 | ptn_name = "boot"; |
| 1803 | |
| 1804 | index = partition_get_index(ptn_name); |
| 1805 | ptn = partition_get_offset(index); |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1806 | image_size = partition_get_size(index); |
| 1807 | if(ptn == 0 || image_size == 0) { |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1808 | dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name); |
| 1809 | return -1; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1810 | } |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1811 | |
Channagoud Kadabi | ef0547c | 2015-02-10 12:57:38 -0800 | [diff] [blame] | 1812 | /* Set Lun for boot & recovery partitions */ |
| 1813 | mmc_set_lun(partition_get_lun(index)); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1814 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1815 | if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) { |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1816 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 1817 | return -1; |
| 1818 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1819 | |
| 1820 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 1821 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1822 | return ERR_INVALID_BOOT_MAGIC; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1823 | } |
| 1824 | |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 1825 | if (hdr->page_size && (hdr->page_size != page_size)) { |
vijay kumar | 2e21b3a | 2014-06-26 17:40:15 +0530 | [diff] [blame] | 1826 | |
| 1827 | if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) { |
| 1828 | dprintf(CRITICAL, "ERROR: Invalid page size\n"); |
| 1829 | return -1; |
| 1830 | } |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 1831 | page_size = hdr->page_size; |
| 1832 | page_mask = page_size - 1; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1835 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 1836 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1837 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1838 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1839 | image_addr = (unsigned char *)target_get_scratch_address(); |
Mayank Grover | 6fd3679 | 2018-09-10 13:26:58 +0530 | [diff] [blame] | 1840 | #if VERIFIED_BOOT_2 |
| 1841 | /* Create hole in start of image for VB salt to copy */ |
| 1842 | image_addr += SALT_BUFF_OFFSET; |
| 1843 | #endif |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 1844 | memcpy(image_addr, (void *)buf, page_size); |
| 1845 | |
| 1846 | /* ensure commandline is terminated */ |
| 1847 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1848 | |
| 1849 | #if DEVICE_TREE |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 1850 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 1851 | dt_size = hdr->dt_size; |
Mayank Grover | 5a50258 | 2018-09-12 11:24:49 +0530 | [diff] [blame] | 1852 | #else |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1853 | dprintf(INFO, "BootImage Header: %d\n", hdr->header_version); |
Mayank Grover | 5a50258 | 2018-09-12 11:24:49 +0530 | [diff] [blame] | 1854 | #endif |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1855 | |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 1856 | dt_actual = ROUND_TO_PAGE(dt_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1857 | 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] | 1858 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__); |
| 1859 | return -1; |
| 1860 | } |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1861 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1862 | #else |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1863 | 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] | 1864 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__); |
| 1865 | return -1; |
| 1866 | } |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1867 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1868 | #endif |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1869 | dtb_image_size = hdr->kernel_size; |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1870 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1871 | #ifdef OSVERSION_IN_BOOTIMAGE |
| 1872 | /* If header version is ONE and booting into recovery, |
| 1873 | dtbo is appended with recovery image. |
| 1874 | Doing following: |
| 1875 | * Validating the recovery offset and size. |
| 1876 | * Extracting recovery dtbo to be used as dtbo. |
| 1877 | */ |
| 1878 | if (boot_into_recovery && |
| 1879 | hdr->header_version == BOOT_HEADER_VERSION_ONE) |
| 1880 | { |
| 1881 | struct boot_img_hdr_v1 *hdr1 = |
| 1882 | (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr)); |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1883 | unsigned int recovery_dtbo_actual = 0; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1884 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1885 | recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask); |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1886 | if ((hdr1->header_size != |
| 1887 | sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr))) |
| 1888 | { |
| 1889 | dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size); |
| 1890 | return -1; |
| 1891 | } |
| 1892 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1893 | if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF) |
| 1894 | { |
| 1895 | 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] | 1896 | MAX_SUPPORTED_DTBO_IMG_BUF); |
| 1897 | return -1; |
| 1898 | } |
| 1899 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1900 | if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual)) |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1901 | { |
| 1902 | dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__); |
| 1903 | return -1; |
| 1904 | } |
| 1905 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1906 | if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) { |
| 1907 | dprintf(CRITICAL, |
| 1908 | "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__); |
| 1909 | return -1; |
| 1910 | } |
| 1911 | |
| 1912 | if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size) |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1913 | { |
| 1914 | dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx," |
| 1915 | " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n", |
| 1916 | hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size); |
| 1917 | return -1; |
| 1918 | } |
| 1919 | |
| 1920 | recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr); |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1921 | recovery_dtbo_size = recovery_dtbo_actual; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1922 | imagesize_actual += recovery_dtbo_size; |
| 1923 | |
| 1924 | dprintf(SPEW, "Header version: %d\n", hdr->header_version); |
| 1925 | dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size); |
| 1926 | dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset); |
| 1927 | |
| 1928 | } |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1929 | |
| 1930 | if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) { |
| 1931 | struct boot_img_hdr_v1 *hdr1 = |
| 1932 | (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr)); |
| 1933 | struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *) |
| 1934 | (image_addr + sizeof(boot_img_hdr) + |
| 1935 | BOOT_IMAGE_HEADER_V2_OFFSET); |
| 1936 | unsigned int recovery_dtbo_actual = 0; |
| 1937 | |
lijuang | 0b17ba2 | 2019-09-04 14:20:47 +0800 | [diff] [blame] | 1938 | recovery_dtbo_actual = |
| 1939 | ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask); |
| 1940 | imagesize_actual += recovery_dtbo_actual; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1941 | |
| 1942 | imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask); |
| 1943 | |
| 1944 | |
| 1945 | dtb_image_offset = page_size + patched_kernel_hdr_size + |
| 1946 | kernel_actual + ramdisk_actual + second_actual + |
| 1947 | recovery_dtbo_actual; |
| 1948 | |
| 1949 | dprintf(SPEW, "Header version: %d\n", hdr->header_version); |
| 1950 | dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset); |
| 1951 | } |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1952 | #endif |
| 1953 | |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1954 | |
Saranya Chidura | b1aaf23 | 2018-11-19 15:47:50 +0530 | [diff] [blame] | 1955 | /* Validate the boot/recovery image size is within the bounds of partition size */ |
| 1956 | if (imagesize_actual > image_size) { |
| 1957 | dprintf(CRITICAL, "Image size is greater than partition size.\n"); |
| 1958 | return -1; |
| 1959 | } |
| 1960 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1961 | #if VERIFIED_BOOT |
| 1962 | boot_verifier_init(); |
| 1963 | #endif |
| 1964 | |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 1965 | #if VERIFIED_BOOT_2 |
| 1966 | /* read full partition if device is unlocked */ |
| 1967 | if (device.is_unlocked) |
| 1968 | imagesize_actual = image_size; |
| 1969 | #endif |
| 1970 | |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 1971 | if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual)) |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1972 | { |
| 1973 | dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n"); |
| 1974 | return -1; |
| 1975 | } |
| 1976 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1977 | /* |
| 1978 | * Update loading flow of bootimage to support compressed/uncompressed |
| 1979 | * bootimage on both 64bit and 32bit platform. |
| 1980 | * 1. Load bootimage from emmc partition onto DDR. |
| 1981 | * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel |
| 1982 | * 3. Check kernel header and update kernel load addr for 64bit and 32bit |
| 1983 | * platform accordingly. |
| 1984 | * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data. |
| 1985 | */ |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1986 | if (partition_multislot_is_supported()) |
| 1987 | { |
| 1988 | current_active_slot = partition_find_active_slot(); |
| 1989 | dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n", |
| 1990 | imagesize_actual, SUFFIX_SLOT(current_active_slot)); |
| 1991 | } |
| 1992 | else |
| 1993 | { |
| 1994 | dprintf(INFO, "Loading (%s) image (%d): start\n", |
| 1995 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
| 1996 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1997 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
| 1998 | |
Gaurav Nebhwani | 43e2a46 | 2016-03-17 21:32:56 +0530 | [diff] [blame] | 1999 | if ((target_get_max_flash_size() - page_size) < imagesize_actual) |
| 2000 | { |
| 2001 | dprintf(CRITICAL, "booimage size is greater than DDR can hold\n"); |
| 2002 | return -1; |
| 2003 | } |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 2004 | offset = page_size; |
| 2005 | /* Read image without signature and header*/ |
| 2006 | 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] | 2007 | { |
| 2008 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 2009 | return -1; |
| 2010 | } |
| 2011 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 2012 | if (partition_multislot_is_supported()) |
| 2013 | { |
| 2014 | dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n", |
| 2015 | imagesize_actual, SUFFIX_SLOT(current_active_slot)); |
| 2016 | } |
| 2017 | else |
| 2018 | { |
| 2019 | dprintf(INFO, "Loading (%s) image (%d): done\n", |
Channagoud Kadabi | f0705b5 | 2015-08-20 14:16:08 -0700 | [diff] [blame] | 2020 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
| 2021 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 2022 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2023 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
| 2024 | |
| 2025 | /* Authenticate Kernel */ |
| 2026 | dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n", |
| 2027 | (int) target_use_signed_kernel(), |
| 2028 | device.is_unlocked, |
| 2029 | device.is_tampered); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2030 | #if VERIFIED_BOOT_2 |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 2031 | /* if device is unlocked skip reading signature, as full partition is read */ |
| 2032 | if (!device.is_unlocked) |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2033 | { |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 2034 | offset = imagesize_actual; |
| 2035 | if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size)) |
| 2036 | { |
| 2037 | dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n"); |
| 2038 | return -1; |
| 2039 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2040 | |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 2041 | /* Read signature */ |
| 2042 | if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size)) |
| 2043 | { |
| 2044 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 2045 | return -1; |
| 2046 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2047 | } |
| 2048 | |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2049 | /* load and validate dtbo partition */ |
| 2050 | load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz); |
| 2051 | |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 2052 | /* load vbmeta partition */ |
| 2053 | load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz); |
| 2054 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2055 | memset(&info, 0, sizeof(bootinfo)); |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2056 | |
| 2057 | /* Pass loaded boot image passed */ |
Mayank Grover | 6fd3679 | 2018-09-10 13:26:58 +0530 | [diff] [blame] | 2058 | info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr); |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2059 | info.images[IMG_BOOT].imgsize = imagesize_actual; |
| 2060 | info.images[IMG_BOOT].name = ptn_name; |
| 2061 | ++info.num_loaded_images; |
| 2062 | |
| 2063 | /* Pass loaded dtbo image */ |
| 2064 | if (dtbo_image_buf != NULL) { |
Mayank Grover | 6fd3679 | 2018-09-10 13:26:58 +0530 | [diff] [blame] | 2065 | info.images[IMG_DTBO].image_buffer = |
| 2066 | SUB_SALT_BUFF_OFFSET(dtbo_image_buf); |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2067 | info.images[IMG_DTBO].imgsize = dtbo_image_sz; |
| 2068 | info.images[IMG_DTBO].name = "dtbo"; |
| 2069 | ++info.num_loaded_images; |
| 2070 | } |
| 2071 | |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 2072 | /* Pass loaded vbmeta image */ |
| 2073 | if (vbmeta_image_buf != NULL) { |
| 2074 | info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf; |
| 2075 | info.images[IMG_VBMETA].imgsize = vbmeta_image_sz; |
| 2076 | info.images[IMG_VBMETA].name = "vbmeta"; |
| 2077 | ++info.num_loaded_images; |
| 2078 | } |
| 2079 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2080 | info.header_version = hdr->header_version; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2081 | info.multi_slot_boot = partition_multislot_is_supported(); |
| 2082 | info.bootreason_alarm = boot_reason_alarm; |
| 2083 | info.bootinto_recovery = boot_into_recovery; |
| 2084 | status = load_image_and_auth(&info); |
| 2085 | if(status) |
| 2086 | return -1; |
| 2087 | |
| 2088 | vbcmdline = info.vbcmdline; |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 2089 | |
| 2090 | /* Free the buffer allocated to vbmeta post verification */ |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 2091 | if (vbmeta_image_buf != NULL) { |
| 2092 | free(vbmeta_image_buf); |
| 2093 | --info.num_loaded_images; |
| 2094 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2095 | #else |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 2096 | /* Change the condition a little bit to include the test framework support. |
| 2097 | * We would never reach this point if device is in fastboot mode, even if we did |
| 2098 | * that means we are in test mode, so execute kernel authentication part for the |
| 2099 | * tests */ |
Parth Dixit | cb0c608 | 2015-12-30 15:01:13 +0530 | [diff] [blame] | 2100 | if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled()) |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2101 | { |
| 2102 | offset = imagesize_actual; |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 2103 | 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] | 2104 | { |
| 2105 | dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n"); |
| 2106 | return -1; |
| 2107 | } |
| 2108 | |
| 2109 | /* Read signature */ |
| 2110 | if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size)) |
| 2111 | { |
| 2112 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 2113 | return -1; |
| 2114 | } |
| 2115 | |
| 2116 | verify_signed_bootimg((uint32_t)image_addr, imagesize_actual); |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 2117 | /* The purpose of our test is done here */ |
Parth Dixit | cb0c608 | 2015-12-30 15:01:13 +0530 | [diff] [blame] | 2118 | if(is_test_mode_enabled() && auth_kernel_img) |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 2119 | return 0; |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2120 | } else { |
| 2121 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 2122 | #ifdef TZ_SAVE_KERNEL_HASH |
| 2123 | aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual); |
| 2124 | #endif /* TZ_SAVE_KERNEL_HASH */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 2125 | |
| 2126 | #ifdef MDTP_SUPPORT |
| 2127 | { |
| 2128 | /* Verify MDTP lock. |
| 2129 | * For boot & recovery partitions, MDTP will use boot_verifier APIs, |
| 2130 | * since verification was skipped in aboot. The signature is not part of the loaded image. |
| 2131 | */ |
| 2132 | mdtp_ext_partition_verification_t ext_partition; |
| 2133 | ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT; |
| 2134 | ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET; |
| 2135 | ext_partition.page_size = page_size; |
| 2136 | ext_partition.image_addr = (uint32)image_addr; |
| 2137 | ext_partition.image_size = imagesize_actual; |
| 2138 | ext_partition.sig_avail = FALSE; |
| 2139 | mdtp_fwlock_verify_lock(&ext_partition); |
| 2140 | } |
| 2141 | #endif /* MDTP_SUPPORT */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2142 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2143 | #endif |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2144 | |
Sridhar Parasuram | 00bfedb | 2015-05-26 14:21:27 -0700 | [diff] [blame] | 2145 | #if VERIFIED_BOOT |
taozhang | 93088bd | 2016-11-16 15:50:46 +0800 | [diff] [blame] | 2146 | if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm)) |
Reut Zysman | ba8a9d5 | 2016-02-18 11:44:04 +0200 | [diff] [blame] | 2147 | { |
| 2148 | #if FBCON_DISPLAY_MSG |
| 2149 | display_bootverify_menu(DISPLAY_MENU_ORANGE); |
| 2150 | wait_for_users_action(); |
| 2151 | #else |
| 2152 | dprintf(CRITICAL, |
| 2153 | "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n"); |
| 2154 | mdelay(5000); |
| 2155 | #endif |
| 2156 | } |
| 2157 | #endif |
| 2158 | |
| 2159 | #if VERIFIED_BOOT |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2160 | if (VB_M == target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 2161 | { |
| 2162 | /* set boot and system versions. */ |
| 2163 | set_os_version((unsigned char *)image_addr); |
| 2164 | // send root of trust |
| 2165 | if(!send_rot_command((uint32_t)device.is_unlocked)) |
| 2166 | ASSERT(0); |
| 2167 | } |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 2168 | #endif |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2169 | /* |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2170 | * Check if the kernel image is a gzip package. If yes, need to decompress it. |
| 2171 | * If not, continue booting. |
| 2172 | */ |
| 2173 | if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size)) |
| 2174 | { |
| 2175 | out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size); |
| 2176 | out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size; |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2177 | #if VERIFIED_BOOT_2 |
| 2178 | if (dtbo_image_sz) |
| 2179 | out_avai_len -= DTBO_IMG_BUF; |
| 2180 | #endif |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2181 | dprintf(INFO, "decompressing kernel image: start\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2182 | rc = decompress((unsigned char *)(image_addr + page_size), |
| 2183 | hdr->kernel_size, out_addr, out_avai_len, |
| 2184 | &dtb_offset, &out_len); |
| 2185 | if (rc) |
| 2186 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2187 | dprintf(CRITICAL, "decompressing kernel image failed!!!\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2188 | ASSERT(0); |
| 2189 | } |
| 2190 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2191 | dprintf(INFO, "decompressing kernel image: done\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2192 | kptr = (struct kernel64_hdr *)out_addr; |
| 2193 | kernel_start_addr = out_addr; |
| 2194 | kernel_size = out_len; |
| 2195 | } else { |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 2196 | dprintf(INFO, "Uncpmpressed kernel in use\n"); |
| 2197 | if (!strncmp((char*)(image_addr + page_size), |
| 2198 | PATCHED_KERNEL_MAGIC, |
| 2199 | sizeof(PATCHED_KERNEL_MAGIC) - 1)) { |
| 2200 | dprintf(INFO, "Patched kernel detected\n"); |
| 2201 | kptr = (struct kernel64_hdr *)(image_addr + page_size + |
| 2202 | PATCHED_KERNEL_HEADER_SIZE); |
| 2203 | //The size of the kernel is stored at start of kernel image + 16 |
| 2204 | //The dtb would start just after the kernel |
| 2205 | dtb_offset = *((uint32_t*)((unsigned char*) |
| 2206 | (image_addr + page_size + |
| 2207 | sizeof(PATCHED_KERNEL_MAGIC) - |
| 2208 | 1))); |
| 2209 | //The actual kernel starts after the 20 byte header. |
| 2210 | kernel_start_addr = (unsigned char*)(image_addr + |
| 2211 | page_size + PATCHED_KERNEL_HEADER_SIZE); |
| 2212 | kernel_size = hdr->kernel_size; |
| 2213 | patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE; |
| 2214 | } else { |
| 2215 | dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n"); |
| 2216 | kptr = (struct kernel64_hdr *)(image_addr + page_size); |
| 2217 | kernel_start_addr = (unsigned char *)(image_addr + page_size); |
| 2218 | kernel_size = hdr->kernel_size; |
| 2219 | } |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | /* |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2223 | * Update the kernel/ramdisk/tags address if the boot image header |
| 2224 | * has default values, these default values come from mkbootimg when |
| 2225 | * the boot image is flashed using fastboot flash:raw |
| 2226 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2227 | update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr)); |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2228 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2229 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 2230 | hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr)); |
| 2231 | hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr)); |
| 2232 | hdr->tags_addr = VA((addr_t)(hdr->tags_addr)); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2233 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2234 | kernel_size = ROUND_TO_PAGE(kernel_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2235 | /* Check if the addresses in the header are valid. */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2236 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) || |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2237 | check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) || |
| 2238 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) || |
| 2239 | check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2240 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2241 | dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2242 | return -1; |
| 2243 | } |
| 2244 | |
| 2245 | #ifndef DEVICE_TREE |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2246 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) || |
| 2247 | check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2248 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2249 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2250 | return -1; |
| 2251 | } |
| 2252 | #endif |
| 2253 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2254 | /* Move kernel, ramdisk and device tree to correct address */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2255 | memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2256 | 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] | 2257 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2258 | #if DEVICE_TREE |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 2259 | if(dt_size) { |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2260 | dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual); |
| 2261 | table = (struct dt_table*) dt_table_offset; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2262 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2263 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
| 2264 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 2265 | return -1; |
| 2266 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2267 | |
P.V. Phani Kumar | 8291676 | 2016-03-09 09:20:19 +0530 | [diff] [blame] | 2268 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 2269 | goes beyound hdr->dt_size*/ |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 2270 | 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] | 2271 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 2272 | return -1; |
| 2273 | } |
| 2274 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2275 | /* Find index of device tree within device tree table */ |
| 2276 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
| 2277 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 2278 | return -1; |
| 2279 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2280 | |
P.V. Phani Kumar | 3c33320 | 2016-03-09 11:54:37 +0530 | [diff] [blame] | 2281 | if(dt_entry.offset > (UINT_MAX - dt_entry.size)) { |
| 2282 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2283 | return -1; |
| 2284 | } |
| 2285 | |
| 2286 | /* Ensure we are not overshooting dt_size with the dt_entry selected */ |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 2287 | if ((dt_entry.offset + dt_entry.size) > dt_size) { |
P.V. Phani Kumar | 3c33320 | 2016-03-09 11:54:37 +0530 | [diff] [blame] | 2288 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2289 | return -1; |
| 2290 | } |
| 2291 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2292 | if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size)) |
| 2293 | { |
| 2294 | unsigned int compressed_size = 0; |
| 2295 | out_addr += out_len; |
| 2296 | out_avai_len -= out_len; |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2297 | dprintf(INFO, "decompressing dtb: start\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2298 | rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset, |
| 2299 | dt_entry.size, out_addr, out_avai_len, |
| 2300 | &compressed_size, &dtb_size); |
| 2301 | if (rc) |
| 2302 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2303 | dprintf(CRITICAL, "decompressing dtb failed!!!\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2304 | ASSERT(0); |
| 2305 | } |
| 2306 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2307 | dprintf(INFO, "decompressing dtb: done\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2308 | best_match_dt_addr = out_addr; |
| 2309 | } else { |
| 2310 | best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset; |
| 2311 | dtb_size = dt_entry.size; |
| 2312 | } |
| 2313 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2314 | /* Validate and Read device device tree in the tags_addr */ |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2315 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) || |
| 2316 | check_ddr_addr_range_bound(hdr->tags_addr, dtb_size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2317 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2318 | dprintf(CRITICAL, "Device tree addresses are not valid\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2319 | return -1; |
| 2320 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2321 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2322 | memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2323 | } else { |
| 2324 | /* Validate the tags_addr */ |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2325 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) || |
| 2326 | check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual)) |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 2327 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2328 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 2329 | return -1; |
| 2330 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2331 | /* |
| 2332 | * If appended dev tree is found, update the atags with |
| 2333 | * memory address to the DTB appended location on RAM. |
| 2334 | * Else update with the atags address in the kernel header |
Mayank Grover | 1ed3e8b | 2018-09-19 15:16:50 +0530 | [diff] [blame] | 2335 | * |
| 2336 | * Make sure everything from scratch address is read before next step! |
| 2337 | * 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] | 2338 | */ |
| 2339 | void *dtb; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 2340 | image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size); |
| 2341 | |
Nagireddy Annem | 0d60bb6 | 2019-08-16 19:16:00 +0530 | [diff] [blame] | 2342 | #ifdef OSVERSION_IN_BOOTIMAGE |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 2343 | if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) { |
| 2344 | |
| 2345 | image_buf = (void*)(image_addr); |
| 2346 | dtb_offset = dtb_image_offset; |
| 2347 | dtb_image_size = imagesize_actual; |
| 2348 | } |
Nagireddy Annem | 0d60bb6 | 2019-08-16 19:16:00 +0530 | [diff] [blame] | 2349 | #endif |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 2350 | |
| 2351 | dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset, |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 2352 | (void *)hdr->tags_addr); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2353 | if (!dtb) { |
| 2354 | dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n"); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 2355 | return -1; |
| 2356 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2357 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2358 | #endif |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2359 | |
Stanimir Varbanov | 69ec546 | 2013-07-18 18:17:42 +0300 | [diff] [blame] | 2360 | if (boot_into_recovery && !device.is_unlocked && !device.is_tampered) |
| 2361 | target_load_ssd_keystore(); |
| 2362 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2363 | unified_boot: |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2364 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 2365 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Dima Zavin | bd3daa0 | 2013-03-26 11:06:44 -0700 | [diff] [blame] | 2366 | (const char *)hdr->cmdline, board_machtype(), |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2367 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 2368 | |
| 2369 | return 0; |
| 2370 | } |
| 2371 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2372 | int boot_linux_from_flash(void) |
| 2373 | { |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2374 | boot_img_hdr *hdr = (void*) buf; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2375 | struct ptentry *ptn; |
| 2376 | struct ptable *ptable; |
| 2377 | unsigned offset = 0; |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 2378 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2379 | unsigned char *image_addr = 0; |
| 2380 | unsigned kernel_actual; |
| 2381 | unsigned ramdisk_actual; |
| 2382 | unsigned imagesize_actual; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2383 | unsigned second_actual = 0; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2384 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2385 | #if DEVICE_TREE |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2386 | struct dt_table *table = NULL; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 2387 | struct dt_entry dt_entry; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2388 | unsigned dt_table_offset; |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 2389 | uint32_t dt_actual; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2390 | uint32_t dt_hdr_size = 0; |
Vivek Kumar | 07bd086 | 2018-04-10 14:19:23 +0530 | [diff] [blame] | 2391 | uint32_t dtb_offset = 0; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2392 | unsigned int dtb_size = 0; |
| 2393 | unsigned char *best_match_dt_addr = NULL; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2394 | #endif |
| 2395 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 2396 | if (target_is_emmc_boot()) { |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2397 | hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 2398 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 2399 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
| 2400 | return -1; |
| 2401 | } |
| 2402 | goto continue_boot; |
| 2403 | } |
| 2404 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2405 | ptable = flash_get_ptable(); |
| 2406 | if (ptable == NULL) { |
| 2407 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 2408 | return -1; |
| 2409 | } |
| 2410 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 2411 | if(!boot_into_recovery) |
| 2412 | { |
| 2413 | ptn = ptable_find(ptable, "boot"); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2414 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 2415 | if (ptn == NULL) { |
| 2416 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 2417 | return -1; |
| 2418 | } |
| 2419 | } |
| 2420 | else |
| 2421 | { |
| 2422 | ptn = ptable_find(ptable, "recovery"); |
| 2423 | if (ptn == NULL) { |
| 2424 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 2425 | return -1; |
| 2426 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2427 | } |
| 2428 | |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2429 | /* Read boot.img header from flash */ |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 2430 | if (flash_read(ptn, offset, buf, page_size)) { |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2431 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 2432 | return -1; |
| 2433 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2434 | |
| 2435 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 2436 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2437 | return -1; |
| 2438 | } |
| 2439 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 2440 | if (hdr->page_size != page_size) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 2441 | 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] | 2442 | return -1; |
| 2443 | } |
| 2444 | |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 2445 | image_addr = (unsigned char *)target_get_scratch_address(); |
| 2446 | memcpy(image_addr, (void *)buf, page_size); |
vijay kumar | 287bb54 | 2015-09-29 13:01:52 +0530 | [diff] [blame] | 2447 | |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2448 | /* |
| 2449 | * Update the kernel/ramdisk/tags address if the boot image header |
| 2450 | * has default values, these default values come from mkbootimg when |
| 2451 | * the boot image is flashed using fastboot flash:raw |
| 2452 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2453 | update_ker_tags_rdisk_addr(hdr, false); |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2454 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2455 | /* Get virtual addresses since the hdr saves physical addresses. */ |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2456 | hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr)); |
| 2457 | hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr)); |
| 2458 | hdr->tags_addr = VA((addr_t)(hdr->tags_addr)); |
| 2459 | |
| 2460 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 2461 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2462 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2463 | |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 2464 | /* ensure commandline is terminated */ |
| 2465 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 2466 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2467 | /* Check if the addresses in the header are valid. */ |
| 2468 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) || |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2469 | check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) || |
| 2470 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) || |
| 2471 | check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2472 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2473 | dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2474 | return -1; |
| 2475 | } |
| 2476 | |
| 2477 | #ifndef DEVICE_TREE |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2478 | 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] | 2479 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n"); |
| 2480 | return -1; |
| 2481 | } |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2482 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2483 | |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2484 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) || |
| 2485 | check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE)) |
Vijay Kumar Pendoti | d3ed20e | 2016-09-20 00:34:46 +0530 | [diff] [blame] | 2486 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2487 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
Vijay Kumar Pendoti | d3ed20e | 2016-09-20 00:34:46 +0530 | [diff] [blame] | 2488 | return -1; |
| 2489 | } |
| 2490 | #else |
| 2491 | |
| 2492 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 2493 | dt_size = hdr->dt_size; |
| 2494 | #endif |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2495 | dt_actual = ROUND_TO_PAGE(dt_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2496 | 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] | 2497 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n"); |
| 2498 | return -1; |
| 2499 | } |
| 2500 | |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2501 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2502 | |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2503 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) || |
| 2504 | check_ddr_addr_range_bound(hdr->tags_addr, dt_size)) |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2505 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2506 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2507 | return -1; |
| 2508 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2509 | #endif |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2510 | |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2511 | /* Read full boot.img from flash */ |
| 2512 | dprintf(INFO, "Loading (%s) image (%d): start\n", |
| 2513 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
| 2514 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
| 2515 | |
| 2516 | if (UINT_MAX - page_size < imagesize_actual) |
| 2517 | { |
| 2518 | dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__); |
| 2519 | return -1; |
| 2520 | } |
| 2521 | |
| 2522 | /*Check the availability of RAM before reading boot image + max signature length from flash*/ |
| 2523 | if (target_get_max_flash_size() < (imagesize_actual + page_size)) |
| 2524 | { |
| 2525 | dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n"); |
| 2526 | return -1; |
| 2527 | } |
| 2528 | |
| 2529 | offset = page_size; |
| 2530 | /* Read image without signature and header */ |
| 2531 | if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size)) |
| 2532 | { |
| 2533 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 2534 | return -1; |
| 2535 | } |
| 2536 | |
| 2537 | dprintf(INFO, "Loading (%s) image (%d): done\n", |
| 2538 | (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual); |
| 2539 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
| 2540 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2541 | /* Authenticate Kernel */ |
Deepa Dinamani | 23b60d4 | 2013-06-24 18:10:52 -0700 | [diff] [blame] | 2542 | if(target_use_signed_kernel() && (!device.is_unlocked)) |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2543 | { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2544 | offset = imagesize_actual; |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2545 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2546 | /* Read signature */ |
| 2547 | if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size)) |
| 2548 | { |
| 2549 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2550 | return -1; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2551 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2552 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 2553 | verify_signed_bootimg((uint32_t)image_addr, imagesize_actual); |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2554 | } |
| 2555 | offset = page_size; |
| 2556 | if(hdr->second_size != 0) { |
| 2557 | if (UINT_MAX - offset < second_actual) |
| 2558 | { |
| 2559 | dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__); |
| 2560 | return -1; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2561 | } |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2562 | offset += second_actual; |
| 2563 | /* Second image loading not implemented. */ |
| 2564 | ASSERT(0); |
| 2565 | } |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2566 | /* Move kernel and ramdisk to correct address */ |
| 2567 | memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size); |
| 2568 | memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size); |
| 2569 | |
| 2570 | #if DEVICE_TREE |
| 2571 | if(dt_size != 0) { |
| 2572 | |
| 2573 | dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual); |
| 2574 | |
| 2575 | table = (struct dt_table*) dt_table_offset; |
| 2576 | |
| 2577 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
| 2578 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 2579 | return -1; |
| 2580 | } |
| 2581 | |
| 2582 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 2583 | goes beyound hdr->dt_size*/ |
| 2584 | if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) { |
| 2585 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 2586 | return -1; |
| 2587 | } |
| 2588 | |
| 2589 | /* Find index of device tree within device tree table */ |
| 2590 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
| 2591 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 2592 | return -1; |
| 2593 | } |
| 2594 | |
| 2595 | /* Validate and Read device device tree in the "tags_add */ |
| 2596 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) || |
| 2597 | check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size)) |
| 2598 | { |
| 2599 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
| 2600 | return -1; |
| 2601 | } |
| 2602 | |
| 2603 | if(dt_entry.offset > (UINT_MAX - dt_entry.size)) { |
| 2604 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2605 | return -1; |
| 2606 | } |
| 2607 | |
| 2608 | /* Ensure we are not overshooting dt_size with the dt_entry selected */ |
| 2609 | if ((dt_entry.offset + dt_entry.size) > dt_size) { |
| 2610 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2611 | return -1; |
| 2612 | } |
| 2613 | |
| 2614 | best_match_dt_addr = (unsigned char *)table + dt_entry.offset; |
| 2615 | dtb_size = dt_entry.size; |
| 2616 | memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size); |
Vivek Kumar | 07bd086 | 2018-04-10 14:19:23 +0530 | [diff] [blame] | 2617 | |
| 2618 | } else { |
| 2619 | /* Validate the tags_addr */ |
| 2620 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) || |
| 2621 | check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual)) |
| 2622 | { |
| 2623 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
| 2624 | return -1; |
| 2625 | } |
| 2626 | /* |
| 2627 | * If appended dev tree is found, update the atags with |
| 2628 | * memory address to the DTB appended location on RAM. |
| 2629 | * Else update with the atags address in the kernel header |
Mayank Grover | 1ed3e8b | 2018-09-19 15:16:50 +0530 | [diff] [blame] | 2630 | * |
| 2631 | * Make sure everything from scratch address is read before next step! |
| 2632 | * 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] | 2633 | */ |
| 2634 | void *dtb = NULL; |
| 2635 | dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr); |
| 2636 | if (!dtb) { |
| 2637 | dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n"); |
| 2638 | return -1; |
| 2639 | } |
| 2640 | } |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2641 | #endif |
| 2642 | if(target_use_signed_kernel() && (!device.is_unlocked)) |
| 2643 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2644 | /* Make sure everything from scratch address is read before next step!*/ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2645 | if(device.is_tampered) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2646 | { |
| 2647 | write_device_info_flash(&device); |
| 2648 | } |
Channagoud Kadabi | 5c86fe3 | 2012-02-16 10:58:48 +0530 | [diff] [blame] | 2649 | #if USE_PCOM_SECBOOT |
| 2650 | set_tamper_flag(device.is_tampered); |
| 2651 | #endif |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2652 | } |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 2653 | continue_boot: |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2654 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2655 | /* TODO: create/pass atags to kernel */ |
| 2656 | |
Ajay Dudani | e28a607 | 2011-07-01 13:59:46 -0700 | [diff] [blame] | 2657 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Dima Zavin | bd3daa0 | 2013-03-26 11:06:44 -0700 | [diff] [blame] | 2658 | (const char *)hdr->cmdline, board_machtype(), |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2659 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 2660 | |
| 2661 | return 0; |
| 2662 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2663 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2664 | void write_device_info_mmc(device_info *dev) |
| 2665 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2666 | unsigned long long ptn = 0; |
| 2667 | unsigned long long size; |
| 2668 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2669 | uint8_t lun = 0; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2670 | uint32_t ret = 0; |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2671 | uint32_t device_info_sz = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2672 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2673 | if (devinfo_present) |
| 2674 | index = partition_get_index("devinfo"); |
| 2675 | else |
| 2676 | index = partition_get_index("aboot"); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 2677 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2678 | ptn = partition_get_offset(index); |
| 2679 | if(ptn == 0) |
| 2680 | { |
| 2681 | return; |
| 2682 | } |
| 2683 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2684 | lun = partition_get_lun(index); |
| 2685 | mmc_set_lun(lun); |
| 2686 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2687 | size = partition_get_size(index); |
| 2688 | |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2689 | device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info), |
| 2690 | mmc_blocksize_mask); |
| 2691 | if (device_info_sz == UINT_MAX) |
| 2692 | { |
| 2693 | dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n"); |
| 2694 | return; |
| 2695 | } |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 2696 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2697 | if (devinfo_present) |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2698 | ret = mmc_write(ptn, device_info_sz, (void *)info_buf); |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2699 | else |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2700 | 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] | 2701 | if (ret) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2702 | { |
| 2703 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 2704 | ASSERT(0); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2705 | } |
| 2706 | } |
| 2707 | |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2708 | void read_device_info_mmc(struct device_info *info) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2709 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2710 | unsigned long long ptn = 0; |
| 2711 | unsigned long long size; |
| 2712 | int index = INVALID_PTN; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2713 | uint32_t ret = 0; |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2714 | uint32_t device_info_sz = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2715 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2716 | if ((index = partition_get_index("devinfo")) < 0) |
| 2717 | { |
| 2718 | devinfo_present = false; |
| 2719 | index = partition_get_index("aboot"); |
| 2720 | } |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 2721 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2722 | ptn = partition_get_offset(index); |
| 2723 | if(ptn == 0) |
| 2724 | { |
| 2725 | return; |
| 2726 | } |
| 2727 | |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 2728 | mmc_set_lun(partition_get_lun(index)); |
| 2729 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2730 | size = partition_get_size(index); |
| 2731 | |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2732 | device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info), |
| 2733 | mmc_blocksize_mask); |
| 2734 | if (device_info_sz == UINT_MAX) |
| 2735 | { |
| 2736 | dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n"); |
| 2737 | return; |
| 2738 | } |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 2739 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2740 | if (devinfo_present) |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2741 | ret = mmc_read(ptn, (void *)info_buf, device_info_sz); |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2742 | else |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2743 | 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] | 2744 | if (ret) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2745 | { |
| 2746 | dprintf(CRITICAL, "ERROR: Cannot read device info\n"); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 2747 | ASSERT(0); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2748 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2749 | } |
| 2750 | |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 2751 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 2752 | void write_prod_info_mmc(prod_info *dev) |
| 2753 | { |
| 2754 | unsigned long long ptn = 0; |
| 2755 | unsigned long long size; |
| 2756 | int index = INVALID_PTN; |
| 2757 | uint32_t blocksize; |
| 2758 | uint8_t lun = 0; |
| 2759 | uint32_t ret = 0; |
| 2760 | |
| 2761 | if (prodinfo_present) |
| 2762 | index = partition_get_index("prodinfo"); |
| 2763 | else |
| 2764 | index = partition_get_index("aboot"); |
| 2765 | |
| 2766 | ptn = partition_get_offset(index); |
| 2767 | if(ptn == 0) |
| 2768 | { |
| 2769 | return; |
| 2770 | } |
| 2771 | |
| 2772 | lun = partition_get_lun(index); |
| 2773 | mmc_set_lun(lun); |
| 2774 | |
| 2775 | size = partition_get_size(index); |
| 2776 | |
| 2777 | blocksize = mmc_get_device_blocksize(); |
| 2778 | |
| 2779 | if (prodinfo_present) |
| 2780 | ret = mmc_write(ptn, blocksize, (void *)prodinfo_buf); |
| 2781 | else |
| 2782 | ret = mmc_write((ptn + size - blocksize), blocksize, (void *)prodinfo_buf); |
| 2783 | if (ret) |
| 2784 | { |
| 2785 | dprintf(CRITICAL, "ERROR: Cannot write prod info\n"); |
| 2786 | ASSERT(0); |
| 2787 | } |
| 2788 | } |
| 2789 | |
| 2790 | void read_prod_info_mmc(struct prod_info *info) |
| 2791 | { |
| 2792 | unsigned long long ptn = 0; |
| 2793 | unsigned long long size; |
| 2794 | int index = INVALID_PTN; |
| 2795 | uint32_t blocksize; |
| 2796 | uint32_t ret = 0; |
| 2797 | |
| 2798 | if ((index = partition_get_index("prodinfo")) < 0) |
| 2799 | { |
| 2800 | prodinfo_present = false; |
| 2801 | index = partition_get_index("aboot"); |
| 2802 | } |
| 2803 | |
| 2804 | ptn = partition_get_offset(index); |
| 2805 | if(ptn == 0) |
| 2806 | { |
| 2807 | return; |
| 2808 | } |
| 2809 | |
| 2810 | mmc_set_lun(partition_get_lun(index)); |
| 2811 | |
| 2812 | size = partition_get_size(index); |
| 2813 | |
| 2814 | blocksize = mmc_get_device_blocksize(); |
| 2815 | |
| 2816 | if (prodinfo_present) |
| 2817 | ret = mmc_read(ptn, (void *)prodinfo_buf, blocksize); |
| 2818 | else |
| 2819 | ret = mmc_read((ptn + size - blocksize), (void *)prodinfo_buf, blocksize); |
| 2820 | if (ret) |
| 2821 | { |
| 2822 | dprintf(CRITICAL, "ERROR: Cannot read prod info\n"); |
| 2823 | ASSERT(0); |
| 2824 | } |
| 2825 | } |
| 2826 | #endif |
| 2827 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2828 | void write_device_info_flash(device_info *dev) |
| 2829 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2830 | 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] | 2831 | struct ptentry *ptn; |
| 2832 | struct ptable *ptable; |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2833 | if(info == NULL) |
| 2834 | { |
| 2835 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 2836 | ASSERT(0); |
| 2837 | } |
| 2838 | info_buf = info; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2839 | ptable = flash_get_ptable(); |
| 2840 | if (ptable == NULL) |
| 2841 | { |
| 2842 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 2843 | return; |
| 2844 | } |
| 2845 | |
| 2846 | ptn = ptable_find(ptable, "devinfo"); |
| 2847 | if (ptn == NULL) |
| 2848 | { |
Smita Ghosh | 670c8b8 | 2015-05-07 09:30:03 -0700 | [diff] [blame] | 2849 | dprintf(CRITICAL, "ERROR: No devinfo partition found\n"); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2850 | return; |
| 2851 | } |
| 2852 | |
Mayank Grover | dedbc89 | 2017-10-24 13:41:34 +0530 | [diff] [blame] | 2853 | memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2854 | memcpy(info, dev, sizeof(device_info)); |
| 2855 | |
| 2856 | if (flash_write(ptn, 0, (void *)info_buf, page_size)) |
| 2857 | { |
| 2858 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 2859 | return; |
| 2860 | } |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2861 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2862 | } |
| 2863 | |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2864 | static int read_allow_oem_unlock(device_info *dev) |
| 2865 | { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2866 | unsigned offset; |
| 2867 | int index; |
| 2868 | unsigned long long ptn; |
| 2869 | unsigned long long ptn_size; |
| 2870 | unsigned blocksize = mmc_get_device_blocksize(); |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2871 | STACKBUF_DMA_ALIGN(buf, blocksize); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2872 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2873 | index = partition_get_index(frp_ptns[0]); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2874 | if (index == INVALID_PTN) |
| 2875 | { |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2876 | index = partition_get_index(frp_ptns[1]); |
| 2877 | if (index == INVALID_PTN) |
| 2878 | { |
| 2879 | dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]); |
| 2880 | return -1; |
| 2881 | } |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | ptn = partition_get_offset(index); |
| 2885 | ptn_size = partition_get_size(index); |
| 2886 | offset = ptn_size - blocksize; |
| 2887 | |
Mayank Grover | 4886040 | 2016-11-29 12:34:53 +0530 | [diff] [blame] | 2888 | /* Set Lun for partition */ |
| 2889 | mmc_set_lun(partition_get_lun(index)); |
| 2890 | |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2891 | if (mmc_read(ptn + offset, (void *)buf, blocksize)) |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2892 | { |
| 2893 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 2894 | return -1; |
| 2895 | } |
| 2896 | |
| 2897 | /*is_allow_unlock is a bool value stored at the LSB of last byte*/ |
| 2898 | is_allow_unlock = buf[blocksize-1] & 0x01; |
| 2899 | return 0; |
| 2900 | } |
| 2901 | |
| 2902 | static int write_allow_oem_unlock(bool allow_unlock) |
| 2903 | { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2904 | unsigned offset; |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2905 | int index; |
| 2906 | unsigned long long ptn; |
| 2907 | unsigned long long ptn_size; |
| 2908 | unsigned blocksize = mmc_get_device_blocksize(); |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2909 | STACKBUF_DMA_ALIGN(buf, blocksize); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2910 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2911 | index = partition_get_index(frp_ptns[0]); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2912 | if (index == INVALID_PTN) |
| 2913 | { |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2914 | index = partition_get_index(frp_ptns[1]); |
| 2915 | if (index == INVALID_PTN) |
| 2916 | { |
| 2917 | dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]); |
| 2918 | return -1; |
| 2919 | } |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | ptn = partition_get_offset(index); |
| 2923 | ptn_size = partition_get_size(index); |
| 2924 | offset = ptn_size - blocksize; |
Mayank Grover | 4886040 | 2016-11-29 12:34:53 +0530 | [diff] [blame] | 2925 | mmc_set_lun(partition_get_lun(index)); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2926 | |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2927 | if (mmc_read(ptn + offset, (void *)buf, blocksize)) |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2928 | { |
| 2929 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 2930 | return -1; |
| 2931 | } |
| 2932 | |
| 2933 | /*is_allow_unlock is a bool value stored at the LSB of last byte*/ |
| 2934 | buf[blocksize-1] = allow_unlock; |
| 2935 | if (mmc_write(ptn + offset, blocksize, buf)) |
| 2936 | { |
| 2937 | dprintf(CRITICAL, "Writing MMC failed\n"); |
| 2938 | return -1; |
| 2939 | } |
| 2940 | |
| 2941 | return 0; |
| 2942 | } |
| 2943 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2944 | void read_device_info_flash(device_info *dev) |
| 2945 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2946 | 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] | 2947 | struct ptentry *ptn; |
| 2948 | struct ptable *ptable; |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2949 | if(info == NULL) |
| 2950 | { |
| 2951 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 2952 | ASSERT(0); |
| 2953 | } |
| 2954 | info_buf = info; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2955 | ptable = flash_get_ptable(); |
| 2956 | if (ptable == NULL) |
| 2957 | { |
| 2958 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 2959 | return; |
| 2960 | } |
| 2961 | |
| 2962 | ptn = ptable_find(ptable, "devinfo"); |
| 2963 | if (ptn == NULL) |
| 2964 | { |
Smita Ghosh | 670c8b8 | 2015-05-07 09:30:03 -0700 | [diff] [blame] | 2965 | dprintf(CRITICAL, "ERROR: No devinfo partition found\n"); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2966 | return; |
| 2967 | } |
| 2968 | |
| 2969 | if (flash_read(ptn, 0, (void *)info_buf, page_size)) |
| 2970 | { |
| 2971 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 2972 | return; |
| 2973 | } |
| 2974 | |
| 2975 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 2976 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2977 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
| 2978 | info->is_unlocked = 0; |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2979 | info->is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2980 | write_device_info_flash(info); |
| 2981 | } |
| 2982 | memcpy(dev, info, sizeof(device_info)); |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2983 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2984 | } |
| 2985 | |
| 2986 | void write_device_info(device_info *dev) |
| 2987 | { |
| 2988 | if(target_is_emmc_boot()) |
| 2989 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2990 | struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 2991 | if(info == NULL) |
| 2992 | { |
| 2993 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 2994 | ASSERT(0); |
| 2995 | } |
| 2996 | info_buf = info; |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2997 | memcpy(info, dev, sizeof(struct device_info)); |
| 2998 | |
| 2999 | #if USE_RPMB_FOR_DEVINFO |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3000 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3001 | is_secure_boot_enable()) { |
| 3002 | if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0) |
| 3003 | ASSERT(0); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 3004 | } |
Channagoud Kadabi | c80cb49 | 2015-04-28 16:08:28 -0700 | [diff] [blame] | 3005 | else |
| 3006 | write_device_info_mmc(info); |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3007 | #else |
| 3008 | write_device_info_mmc(info); |
| 3009 | #endif |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 3010 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3011 | } |
| 3012 | else |
| 3013 | { |
| 3014 | write_device_info_flash(dev); |
| 3015 | } |
| 3016 | } |
| 3017 | |
Monika Singh | 9431646 | 2018-03-15 18:39:01 +0530 | [diff] [blame] | 3018 | int read_rollback_index(uint32_t loc, uint64_t *roll_back_index) |
| 3019 | { |
| 3020 | if (!devinfo_present) { |
| 3021 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3022 | return -EINVAL; |
| 3023 | } |
| 3024 | if (loc >= ARRAY_SIZE(device.rollback_index)) { |
| 3025 | dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n", |
| 3026 | __func__, loc, ARRAY_SIZE(device.rollback_index)); |
| 3027 | ASSERT(0); |
| 3028 | } |
| 3029 | |
| 3030 | *roll_back_index = device.rollback_index[loc]; |
| 3031 | return 0; |
| 3032 | } |
| 3033 | |
| 3034 | int write_rollback_index(uint32_t loc, uint64_t roll_back_index) |
| 3035 | { |
| 3036 | if (!devinfo_present) { |
| 3037 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3038 | return -EINVAL; |
| 3039 | } |
| 3040 | if (loc >= ARRAY_SIZE(device.rollback_index)) { |
| 3041 | dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n", |
| 3042 | __func__, loc, ARRAY_SIZE(device.rollback_index)); |
| 3043 | ASSERT(0); |
| 3044 | } |
| 3045 | |
| 3046 | device.rollback_index[loc] = roll_back_index; |
| 3047 | write_device_info(&device); |
| 3048 | return 0; |
| 3049 | } |
| 3050 | |
Monika Singh | b0fad82 | 2018-03-15 18:50:55 +0530 | [diff] [blame] | 3051 | int store_userkey(uint8_t *user_key, uint32_t user_key_size) |
| 3052 | { |
| 3053 | if (!devinfo_present) { |
| 3054 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3055 | return -EINVAL; |
| 3056 | } |
| 3057 | |
| 3058 | if (user_key_size > ARRAY_SIZE(device.user_public_key)) { |
| 3059 | dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n"); |
| 3060 | return -ENODEV; |
| 3061 | } |
| 3062 | |
| 3063 | memcpy(device.user_public_key, user_key, user_key_size); |
| 3064 | device.user_public_key_length = user_key_size; |
| 3065 | write_device_info(&device); |
| 3066 | return 0; |
| 3067 | } |
| 3068 | |
| 3069 | int erase_userkey() |
| 3070 | { |
| 3071 | if (!devinfo_present) { |
| 3072 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3073 | return -EINVAL; |
| 3074 | } |
| 3075 | memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key)); |
| 3076 | device.user_public_key_length = 0; |
| 3077 | write_device_info(&device); |
| 3078 | return 0; |
| 3079 | } |
| 3080 | |
| 3081 | int get_userkey(uint8_t **user_key, uint32_t *user_key_size) |
| 3082 | { |
| 3083 | if (!devinfo_present) { |
| 3084 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3085 | return -EINVAL; |
| 3086 | } |
| 3087 | *user_key = device.user_public_key; |
| 3088 | *user_key_size = device.user_public_key_length; |
| 3089 | return 0; |
| 3090 | } |
| 3091 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3092 | void read_device_info(device_info *dev) |
| 3093 | { |
| 3094 | if(target_is_emmc_boot()) |
| 3095 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 3096 | struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 3097 | if(info == NULL) |
| 3098 | { |
| 3099 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 3100 | ASSERT(0); |
| 3101 | } |
| 3102 | info_buf = info; |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3103 | |
| 3104 | #if USE_RPMB_FOR_DEVINFO |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3105 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3106 | is_secure_boot_enable()) { |
| 3107 | if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0) |
| 3108 | ASSERT(0); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 3109 | } |
Channagoud Kadabi | c80cb49 | 2015-04-28 16:08:28 -0700 | [diff] [blame] | 3110 | else |
| 3111 | read_device_info_mmc(info); |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3112 | #else |
| 3113 | read_device_info_mmc(info); |
| 3114 | #endif |
| 3115 | |
| 3116 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 3117 | { |
| 3118 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3119 | if (is_secure_boot_enable()) { |
Channagoud Kadabi | 05f78ba | 2015-07-06 11:58:14 -0700 | [diff] [blame] | 3120 | info->is_unlocked = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3121 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3122 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3123 | info->is_unlock_critical = 0; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3124 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3125 | } else { |
Channagoud Kadabi | 2fda409 | 2015-07-07 13:34:11 -0700 | [diff] [blame] | 3126 | info->is_unlocked = 1; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3127 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3128 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3129 | info->is_unlock_critical = 1; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3130 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3131 | } |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3132 | info->is_tampered = 0; |
| 3133 | info->charger_screen_enabled = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3134 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3135 | if (VB_M <= target_get_vb_version()) |
Monika Singh | 1c71a1c | 2019-12-03 12:12:48 +0530 | [diff] [blame] | 3136 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3137 | info->verity_mode = 1; //enforcing by default |
Monika Singh | 1c71a1c | 2019-12-03 12:12:48 +0530 | [diff] [blame] | 3138 | info->user_public_key_length = 0; |
| 3139 | memset(info->rollback_index, 0, sizeof(info->rollback_index)); |
| 3140 | memset(info->user_public_key, 0, sizeof(info->user_public_key)); |
| 3141 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3142 | #endif |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3143 | write_device_info(info); |
| 3144 | } |
jessicatseng | 8e7644c | 2020-04-22 11:13:20 +0800 | [diff] [blame] | 3145 | |
| 3146 | info->charger_screen_enabled = 1; |
| 3147 | write_device_info(info); |
| 3148 | |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3149 | memcpy(dev, info, sizeof(device_info)); |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 3150 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3151 | } |
| 3152 | else |
| 3153 | { |
| 3154 | read_device_info_flash(dev); |
| 3155 | } |
| 3156 | } |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 3157 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 3158 | void write_prod_info(prod_info *dev) |
| 3159 | { |
| 3160 | if(target_is_emmc_boot()) |
| 3161 | { |
| 3162 | struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 3163 | if(info == NULL) |
| 3164 | { |
| 3165 | dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n"); |
| 3166 | ASSERT(0); |
| 3167 | } |
| 3168 | prodinfo_buf = info; |
| 3169 | memcpy(info, dev, sizeof(struct prod_info)); |
| 3170 | |
| 3171 | write_prod_info_mmc(info); |
| 3172 | free(info); |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | void read_prod_info(prod_info *dev) |
| 3177 | { |
| 3178 | if(target_is_emmc_boot()) |
| 3179 | { |
| 3180 | struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 3181 | if(info == NULL) |
| 3182 | { |
| 3183 | dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n"); |
| 3184 | ASSERT(0); |
| 3185 | } |
| 3186 | prodinfo_buf = info; |
| 3187 | |
| 3188 | read_prod_info_mmc(info); |
| 3189 | |
| 3190 | if (memcmp(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE)) |
| 3191 | { |
| 3192 | memcpy(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE); |
| 3193 | memcpy(info->isn, "No_Serial_Number", PRODINFO_MAX_ISN_LEN); |
| 3194 | memcpy(info->ssn, "No_Custer_Serial_Number", PRODINFO_MAX_SSN_LEN); |
| 3195 | info->is_adb_enabled = 0; |
| 3196 | write_prod_info(info); |
| 3197 | } |
| 3198 | memcpy(dev, info, sizeof(prod_info)); |
| 3199 | free(info); |
| 3200 | } |
| 3201 | } |
| 3202 | #endif |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3203 | |
| 3204 | void reset_device_info() |
| 3205 | { |
| 3206 | dprintf(ALWAYS, "reset_device_info called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3207 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3208 | write_device_info(&device); |
| 3209 | } |
| 3210 | |
| 3211 | void set_device_root() |
| 3212 | { |
| 3213 | dprintf(ALWAYS, "set_device_root called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3214 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3215 | write_device_info(&device); |
| 3216 | } |
| 3217 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3218 | /* set device unlock value |
| 3219 | * Must check FRP before call this function |
| 3220 | * Need to wipe data when unlock status changed |
| 3221 | * type 0: oem unlock |
| 3222 | * type 1: unlock critical |
| 3223 | * status 0: unlock as false |
| 3224 | * status 1: lock as true |
| 3225 | */ |
| 3226 | void set_device_unlock_value(int type, bool status) |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 3227 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3228 | if (type == UNLOCK) |
| 3229 | device.is_unlocked = status; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3230 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3231 | else if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3232 | type == UNLOCK_CRITICAL) |
| 3233 | device.is_unlock_critical = status; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3234 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3235 | write_device_info(&device); |
| 3236 | } |
| 3237 | |
| 3238 | static void set_device_unlock(int type, bool status) |
| 3239 | { |
| 3240 | int is_unlocked = -1; |
| 3241 | char response[MAX_RSP_SIZE]; |
| 3242 | |
| 3243 | /* check device unlock status if it is as expected */ |
| 3244 | if (type == UNLOCK) |
| 3245 | is_unlocked = device.is_unlocked; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3246 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3247 | if(VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3248 | type == UNLOCK_CRITICAL) |
| 3249 | { |
| 3250 | is_unlocked = device.is_unlock_critical; |
| 3251 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3252 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3253 | if (is_unlocked == status) { |
| 3254 | snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!")); |
| 3255 | fastboot_info(response); |
| 3256 | fastboot_okay(""); |
| 3257 | return; |
| 3258 | } |
| 3259 | |
| 3260 | /* status is true, it means to unlock device */ |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 3261 | if (status && !is_allow_unlock) { |
| 3262 | fastboot_fail("oem unlock is not allowed"); |
| 3263 | return; |
| 3264 | } |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 3265 | |
ericlin | 4e397c4 | 2020-04-29 14:14:24 +0800 | [diff] [blame] | 3266 | if(target_build_variant_user()){ |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3267 | #if FBCON_DISPLAY_MSG |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 3268 | display_unlock_menu(type, status); |
| 3269 | fastboot_okay(""); |
| 3270 | return; |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3271 | #else |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 3272 | if (status && type == UNLOCK) { |
| 3273 | fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'"); |
| 3274 | return; |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 3275 | } |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 3276 | #endif |
ericlin | 4e397c4 | 2020-04-29 14:14:24 +0800 | [diff] [blame] | 3277 | } |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3278 | |
| 3279 | set_device_unlock_value(type, status); |
| 3280 | |
| 3281 | /* wipe data */ |
| 3282 | struct recovery_message msg; |
Kishor PK | 60a6821 | 2017-05-08 16:55:57 +0530 | [diff] [blame] | 3283 | memset(&msg, 0, sizeof(msg)); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3284 | snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data"); |
| 3285 | write_misc(0, &msg, sizeof(msg)); |
| 3286 | |
| 3287 | fastboot_okay(""); |
ericlin | 4e397c4 | 2020-04-29 14:14:24 +0800 | [diff] [blame] | 3288 | //reboot_device(RECOVERY_MODE); |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 3289 | } |
| 3290 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3291 | static bool critical_flash_allowed(const char * entry) |
| 3292 | { |
| 3293 | uint32_t i = 0; |
| 3294 | if (entry == NULL) |
| 3295 | return false; |
| 3296 | |
| 3297 | for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) { |
| 3298 | if(!strcmp(entry, critical_flash_allowed_ptn[i])) |
| 3299 | return true; |
| 3300 | } |
| 3301 | return false; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | #if DEVICE_TREE |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3305 | int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset) |
| 3306 | { |
| 3307 | uint32 dt_image_offset = 0; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3308 | uint32_t n; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3309 | struct dt_table *table = NULL; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3310 | struct dt_entry dt_entry; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3311 | uint32_t dt_hdr_size = 0; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3312 | unsigned int compressed_size = 0; |
| 3313 | unsigned int dtb_size = 0; |
| 3314 | unsigned int out_avai_len = 0; |
| 3315 | unsigned char *out_addr = NULL; |
| 3316 | unsigned char *best_match_dt_addr = NULL; |
| 3317 | int rc; |
| 3318 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 3319 | boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start); |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3320 | |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 3321 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 3322 | dt_size = hdr->dt_size; |
| 3323 | #endif |
| 3324 | |
| 3325 | if(dt_size != 0) { |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3326 | /* add kernel offset */ |
| 3327 | dt_image_offset += page_size; |
| 3328 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 3329 | dt_image_offset += n; |
| 3330 | |
| 3331 | /* add ramdisk offset */ |
| 3332 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 3333 | dt_image_offset += n; |
| 3334 | |
| 3335 | /* add second offset */ |
| 3336 | if(hdr->second_size != 0) { |
| 3337 | n = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 3338 | dt_image_offset += n; |
| 3339 | } |
| 3340 | |
| 3341 | /* offset now point to start of dt.img */ |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 3342 | table = (struct dt_table*)(boot_image_start + dt_image_offset); |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3343 | |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 3344 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3345 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 3346 | return -1; |
| 3347 | } |
P.V. Phani Kumar | 8291676 | 2016-03-09 09:20:19 +0530 | [diff] [blame] | 3348 | |
| 3349 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 3350 | goes beyound hdr->dt_size*/ |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 3351 | 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] | 3352 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 3353 | return -1; |
| 3354 | } |
| 3355 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 3356 | /* Find index of device tree within device tree table */ |
| 3357 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3358 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 3359 | return -1; |
| 3360 | } |
| 3361 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3362 | best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset; |
| 3363 | if (is_gzip_package(best_match_dt_addr, dt_entry.size)) |
| 3364 | { |
| 3365 | out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset; |
| 3366 | out_avai_len = target_get_max_flash_size() - scratch_offset; |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3367 | dprintf(INFO, "decompressing dtb: start\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3368 | rc = decompress(best_match_dt_addr, |
| 3369 | dt_entry.size, out_addr, out_avai_len, |
| 3370 | &compressed_size, &dtb_size); |
| 3371 | if (rc) |
| 3372 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3373 | dprintf(CRITICAL, "decompressing dtb failed!!!\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3374 | ASSERT(0); |
| 3375 | } |
| 3376 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3377 | dprintf(INFO, "decompressing dtb: done\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3378 | best_match_dt_addr = out_addr; |
| 3379 | } else { |
| 3380 | dtb_size = dt_entry.size; |
| 3381 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3382 | /* Validate and Read device device tree in the "tags_add */ |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3383 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) || |
| 3384 | check_ddr_addr_range_bound(hdr->tags_addr, dtb_size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3385 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3386 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3387 | return -1; |
| 3388 | } |
| 3389 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3390 | /* Read device device tree in the "tags_add */ |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3391 | memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3392 | } else |
| 3393 | return -1; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3394 | |
| 3395 | /* Everything looks fine. Return success. */ |
| 3396 | return 0; |
| 3397 | } |
| 3398 | #endif |
| 3399 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3400 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 3401 | { |
| 3402 | unsigned kernel_actual; |
| 3403 | unsigned ramdisk_actual; |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 3404 | unsigned second_actual; |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3405 | uint32_t image_actual; |
| 3406 | uint32_t dt_actual = 0; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 3407 | boot_img_hdr *hdr = NULL; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3408 | struct kernel64_hdr *kptr = NULL; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3409 | char *ptr = ((char*) data); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3410 | int ret = 0; |
| 3411 | uint8_t dtb_copied = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3412 | unsigned int out_len = 0; |
| 3413 | unsigned int out_avai_len = 0; |
| 3414 | unsigned char *out_addr = NULL; |
| 3415 | uint32_t dtb_offset = 0; |
| 3416 | unsigned char *kernel_start_addr = NULL; |
| 3417 | unsigned int kernel_size = 0; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3418 | unsigned int scratch_offset = 0; |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3419 | #if VERIFIED_BOOT_2 |
| 3420 | void *dtbo_image_buf = NULL; |
| 3421 | uint32_t dtbo_image_sz = 0; |
| 3422 | void *vbmeta_image_buf = NULL; |
| 3423 | uint32_t vbmeta_image_sz = 0; |
| 3424 | #endif |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3425 | #if !VERIFIED_BOOT_2 |
| 3426 | uint32_t sig_actual = 0; |
| 3427 | uint32_t sig_size = 0; |
| 3428 | #ifdef MDTP_SUPPORT |
| 3429 | static bool is_mdtp_activated = 0; |
| 3430 | #endif /* MDTP_SUPPORT */ |
| 3431 | #endif |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3432 | |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3433 | #if FBCON_DISPLAY_MSG |
| 3434 | /* Exit keys' detection thread firstly */ |
| 3435 | exit_menu_keys_detection(); |
| 3436 | #endif |
| 3437 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3438 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Channagoud Kadabi | 6d5375e | 2015-06-23 17:15:42 -0700 | [diff] [blame] | 3439 | if(target_build_variant_user() && !device.is_unlocked) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3440 | { |
| 3441 | fastboot_fail("unlock device to use this command"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3442 | goto boot_failed; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3443 | } |
| 3444 | #endif |
| 3445 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3446 | if (sz < sizeof(hdr)) { |
| 3447 | fastboot_fail("invalid bootimage header"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3448 | goto boot_failed; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3449 | } |
| 3450 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 3451 | hdr = (boot_img_hdr *)data; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3452 | |
| 3453 | /* ensure commandline is terminated */ |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3454 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3455 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3456 | if(target_is_emmc_boot() && hdr->page_size) { |
| 3457 | page_size = hdr->page_size; |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 3458 | page_mask = page_size - 1; |
| 3459 | } |
| 3460 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3461 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 3462 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 3463 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3464 | #if DEVICE_TREE |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 3465 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 3466 | dt_size = hdr->dt_size; |
| 3467 | #endif |
| 3468 | dt_actual = ROUND_TO_PAGE(dt_size, page_mask); |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3469 | #endif |
| 3470 | |
| 3471 | image_actual = ADD_OF(page_size, kernel_actual); |
| 3472 | image_actual = ADD_OF(image_actual, ramdisk_actual); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 3473 | image_actual = ADD_OF(image_actual, second_actual); |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3474 | image_actual = ADD_OF(image_actual, dt_actual); |
| 3475 | |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3476 | /* Checking to prevent oob access in read_der_message_length */ |
| 3477 | if (image_actual > sz) { |
| 3478 | fastboot_fail("bootimage header fields are invalid"); |
| 3479 | goto boot_failed; |
| 3480 | } |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3481 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3482 | #if VERIFIED_BOOT_2 |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 3483 | /* Pass size of boot partition, as imgsize, to avoid |
| 3484 | read fewer bytes error */ |
| 3485 | image_actual = partition_get_size(partition_get_index("boot")); |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3486 | |
| 3487 | /* load and validate dtbo partition */ |
| 3488 | load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz); |
| 3489 | |
| 3490 | /* load vbmeta partition */ |
| 3491 | load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz); |
| 3492 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3493 | memset(&info, 0, sizeof(bootinfo)); |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3494 | |
| 3495 | info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data); |
| 3496 | info.images[IMG_BOOT].imgsize = image_actual; |
| 3497 | info.images[IMG_BOOT].name = "boot"; |
| 3498 | ++info.num_loaded_images; |
| 3499 | |
| 3500 | /* Pass loaded dtbo image */ |
| 3501 | if (dtbo_image_buf != NULL) { |
| 3502 | info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf); |
| 3503 | info.images[IMG_DTBO].imgsize = dtbo_image_sz; |
| 3504 | info.images[IMG_DTBO].name = "dtbo"; |
| 3505 | ++info.num_loaded_images; |
| 3506 | } |
| 3507 | |
| 3508 | /* Pass loaded vbmeta image */ |
| 3509 | if (vbmeta_image_buf != NULL) { |
| 3510 | info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf; |
| 3511 | info.images[IMG_VBMETA].imgsize = vbmeta_image_sz; |
| 3512 | info.images[IMG_VBMETA].name = "vbmeta"; |
| 3513 | ++info.num_loaded_images; |
| 3514 | } |
| 3515 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3516 | info.multi_slot_boot = partition_multislot_is_supported(); |
| 3517 | if (load_image_and_auth(&info)) |
| 3518 | goto boot_failed; |
| 3519 | vbcmdline = info.vbcmdline; |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3520 | |
| 3521 | /* Free the buffer allocated to vbmeta post verification */ |
| 3522 | if (vbmeta_image_buf != NULL) { |
| 3523 | free(vbmeta_image_buf); |
| 3524 | --info.num_loaded_images; |
| 3525 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3526 | #else |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3527 | sig_size = sz - image_actual; |
| 3528 | |
P.V. Phani Kumar | eecfd82 | 2016-03-09 20:09:03 +0530 | [diff] [blame] | 3529 | if (target_use_signed_kernel() && (!device.is_unlocked)) { |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3530 | unsigned chk; |
P.V. Phani Kumar | eecfd82 | 2016-03-09 20:09:03 +0530 | [diff] [blame] | 3531 | /* Calculate the signature length from boot image */ |
| 3532 | sig_actual = read_der_message_length( |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3533 | (unsigned char*)(data + image_actual), sig_size); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3534 | chk = ADD_OF(image_actual, sig_actual); |
Channagoud Kadabi | 3d83901 | 2014-06-26 14:26:39 -0700 | [diff] [blame] | 3535 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3536 | if (chk > sz) { |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3537 | fastboot_fail("bootimage header fields are invalid"); |
| 3538 | goto boot_failed; |
| 3539 | } |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3540 | } |
| 3541 | |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 3542 | // Initialize boot state before trying to verify boot.img |
| 3543 | #if VERIFIED_BOOT |
Channagoud Kadabi | 699466e | 2015-11-03 12:37:42 -0800 | [diff] [blame] | 3544 | boot_verifier_init(); |
Mayank Grover | b337e93 | 2017-01-18 20:00:40 +0530 | [diff] [blame] | 3545 | #endif |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 3546 | /* Handle overflow if the input image size is greater than |
| 3547 | * boot image buffer can hold |
| 3548 | */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3549 | if ((target_get_max_flash_size() - page_size) < image_actual) |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 3550 | { |
| 3551 | fastboot_fail("booimage: size is greater than boot image buffer can hold"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3552 | goto boot_failed; |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 3553 | } |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 3554 | |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3555 | /* Verify the boot image |
| 3556 | * device & page_size are initialized in aboot_init |
| 3557 | */ |
Sridhar Parasuram | 7ae9fbc | 2015-09-17 09:56:30 -0700 | [diff] [blame] | 3558 | if (target_use_signed_kernel() && (!device.is_unlocked)) { |
Channagoud Kadabi | 3d83901 | 2014-06-26 14:26:39 -0700 | [diff] [blame] | 3559 | /* Pass size excluding signature size, otherwise we would try to |
| 3560 | * access signature beyond its length |
| 3561 | */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3562 | verify_signed_bootimg((uint32_t)data, image_actual); |
Sridhar Parasuram | 7ae9fbc | 2015-09-17 09:56:30 -0700 | [diff] [blame] | 3563 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3564 | #ifdef MDTP_SUPPORT |
| 3565 | else |
| 3566 | { |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3567 | /* fastboot boot is not allowed when MDTP is activated */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3568 | mdtp_ext_partition_verification_t ext_partition; |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 3569 | |
| 3570 | if (!is_mdtp_activated) { |
| 3571 | ext_partition.partition = MDTP_PARTITION_NONE; |
| 3572 | mdtp_fwlock_verify_lock(&ext_partition); |
| 3573 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3574 | } |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3575 | |
Amir Kotzer | 7c768c0 | 2016-04-13 09:08:05 +0300 | [diff] [blame] | 3576 | /* If mdtp state cannot be validate, block fastboot boot*/ |
| 3577 | if(mdtp_activated(&is_mdtp_activated)){ |
| 3578 | dprintf(CRITICAL, "mdtp_activated cannot validate state.\n"); |
| 3579 | dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n"); |
| 3580 | goto boot_failed; |
| 3581 | } |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3582 | if(is_mdtp_activated){ |
| 3583 | dprintf(CRITICAL, "fastboot boot command is not available.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3584 | goto boot_failed; |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3585 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3586 | #endif /* MDTP_SUPPORT */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3587 | #endif /* VERIFIED_BOOT_2 else */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3588 | |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 3589 | #if VERIFIED_BOOT |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3590 | if (VB_M == target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3591 | { |
| 3592 | /* set boot and system versions. */ |
| 3593 | set_os_version((unsigned char *)data); |
| 3594 | // send root of trust |
| 3595 | if(!send_rot_command((uint32_t)device.is_unlocked)) |
| 3596 | ASSERT(0); |
| 3597 | } |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 3598 | #endif |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 3599 | /* |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3600 | * Check if the kernel image is a gzip package. If yes, need to decompress it. |
| 3601 | * If not, continue booting. |
| 3602 | */ |
| 3603 | if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size)) |
| 3604 | { |
| 3605 | out_addr = (unsigned char *)target_get_scratch_address(); |
| 3606 | out_addr = (unsigned char *)(out_addr + image_actual + page_size); |
| 3607 | out_avai_len = target_get_max_flash_size() - image_actual - page_size; |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3608 | #if VERIFIED_BOOT_2 |
| 3609 | if (dtbo_image_sz) |
| 3610 | out_avai_len -= DTBO_IMG_BUF; |
| 3611 | #endif |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3612 | dprintf(INFO, "decompressing kernel image: start\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3613 | ret = decompress((unsigned char *)(ptr + page_size), |
| 3614 | hdr->kernel_size, out_addr, out_avai_len, |
| 3615 | &dtb_offset, &out_len); |
| 3616 | if (ret) |
| 3617 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3618 | dprintf(CRITICAL, "decompressing image failed!!!\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3619 | ASSERT(0); |
| 3620 | } |
| 3621 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3622 | dprintf(INFO, "decompressing kernel image: done\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3623 | kptr = (struct kernel64_hdr *)out_addr; |
| 3624 | kernel_start_addr = out_addr; |
| 3625 | kernel_size = out_len; |
| 3626 | } else { |
| 3627 | kptr = (struct kernel64_hdr*)((char *)data + page_size); |
| 3628 | kernel_start_addr = (unsigned char *)((char *)data + page_size); |
| 3629 | kernel_size = hdr->kernel_size; |
| 3630 | } |
| 3631 | |
| 3632 | /* |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 3633 | * Update the kernel/ramdisk/tags address if the boot image header |
| 3634 | * has default values, these default values come from mkbootimg when |
| 3635 | * the boot image is flashed using fastboot flash:raw |
| 3636 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 3637 | update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr)); |
Dima Zavin | 3cadfff | 2013-03-21 14:30:48 -0700 | [diff] [blame] | 3638 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3639 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 3640 | hdr->kernel_addr = VA(hdr->kernel_addr); |
| 3641 | hdr->ramdisk_addr = VA(hdr->ramdisk_addr); |
| 3642 | hdr->tags_addr = VA(hdr->tags_addr); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3643 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3644 | kernel_size = ROUND_TO_PAGE(kernel_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3645 | /* Check if the addresses in the header are valid. */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3646 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) || |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3647 | check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) || |
| 3648 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) || |
| 3649 | check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3650 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3651 | dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3652 | goto boot_failed; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3653 | } |
| 3654 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3655 | #if DEVICE_TREE |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3656 | scratch_offset = image_actual + page_size + out_len; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3657 | /* find correct dtb and copy it to right location */ |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3658 | ret = copy_dtb(data, scratch_offset); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3659 | |
| 3660 | dtb_copied = !ret ? 1 : 0; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3661 | #else |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3662 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) || |
| 3663 | check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3664 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3665 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3666 | goto boot_failed; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3667 | } |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3668 | #endif |
| 3669 | |
| 3670 | /* Load ramdisk & kernel */ |
| 3671 | 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] | 3672 | memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3673 | |
| 3674 | #if DEVICE_TREE |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3675 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) || |
| 3676 | check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual)) |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3677 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3678 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3679 | goto boot_failed; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3680 | } |
| 3681 | |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3682 | /* |
| 3683 | * If dtb is not found look for appended DTB in the kernel. |
| 3684 | * If appended dev tree is found, update the atags with |
| 3685 | * memory address to the DTB appended location on RAM. |
| 3686 | * Else update with the atags address in the kernel header |
| 3687 | */ |
| 3688 | if (!dtb_copied) { |
| 3689 | void *dtb; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3690 | dtb = dev_tree_appended((void*)(ptr + page_size), |
| 3691 | hdr->kernel_size, dtb_offset, |
Dima Zavin | e63e557 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 3692 | (void *)hdr->tags_addr); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3693 | if (!dtb) { |
| 3694 | fastboot_fail("dtb not found"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3695 | goto boot_failed; |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3696 | } |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3697 | } |
| 3698 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3699 | |
| 3700 | fastboot_okay(""); |
Amol Jadi | 7c4316c | 2013-10-07 14:19:26 -0700 | [diff] [blame] | 3701 | fastboot_stop(); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3702 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 3703 | boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr, |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3704 | (const char*) hdr->cmdline, board_machtype(), |
| 3705 | (void*) hdr->ramdisk_addr, hdr->ramdisk_size); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3706 | |
| 3707 | /* fastboot already stop, it's no need to show fastboot menu */ |
| 3708 | return; |
| 3709 | boot_failed: |
| 3710 | #if FBCON_DISPLAY_MSG |
| 3711 | /* revert to fastboot menu if boot failed */ |
| 3712 | display_fastboot_menu(); |
| 3713 | #endif |
| 3714 | return; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3715 | } |
| 3716 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3717 | void cmd_erase_nand(const char *arg, void *data, unsigned sz) |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3718 | { |
| 3719 | struct ptentry *ptn; |
| 3720 | struct ptable *ptable; |
| 3721 | |
| 3722 | ptable = flash_get_ptable(); |
| 3723 | if (ptable == NULL) { |
| 3724 | fastboot_fail("partition table doesn't exist"); |
| 3725 | return; |
| 3726 | } |
| 3727 | |
| 3728 | ptn = ptable_find(ptable, arg); |
| 3729 | if (ptn == NULL) { |
| 3730 | fastboot_fail("unknown partition name"); |
| 3731 | return; |
| 3732 | } |
| 3733 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3734 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 3735 | dprintf(INFO, "erasing avb_custom_key\n"); |
| 3736 | if (erase_userkey()) { |
| 3737 | fastboot_fail("Erasing avb_custom_key failed"); |
| 3738 | } else { |
| 3739 | fastboot_okay(""); |
| 3740 | } |
| 3741 | return; |
| 3742 | } |
| 3743 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3744 | if (flash_erase(ptn)) { |
| 3745 | fastboot_fail("failed to erase partition"); |
| 3746 | return; |
| 3747 | } |
| 3748 | fastboot_okay(""); |
| 3749 | } |
| 3750 | |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3751 | |
| 3752 | void cmd_erase_mmc(const char *arg, void *data, unsigned sz) |
| 3753 | { |
| 3754 | unsigned long long ptn = 0; |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 3755 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3756 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3757 | uint8_t lun = 0; |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 3758 | char *footer = NULL; |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 3759 | char EraseResultStr[MAX_RSP_SIZE] = ""; |
| 3760 | VirtualAbMergeStatus SnapshotMergeStatus; |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3761 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3762 | #if VERIFIED_BOOT |
| 3763 | if(!strcmp(arg, KEYSTORE_PTN_NAME)) |
| 3764 | { |
| 3765 | if(!device.is_unlocked) |
| 3766 | { |
| 3767 | fastboot_fail("unlock device to erase keystore"); |
| 3768 | return; |
| 3769 | } |
| 3770 | } |
| 3771 | #endif |
| 3772 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3773 | index = partition_get_index(arg); |
| 3774 | ptn = partition_get_offset(index); |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 3775 | size = partition_get_size(index); |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 3776 | |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 3777 | if (target_virtual_ab_supported()) { |
| 3778 | if (CheckVirtualAbCriticalPartition (arg)) { |
| 3779 | snprintf(EraseResultStr, MAX_RSP_SIZE,"Erase of %s is not allowed in %s state", |
| 3780 | arg, SnapshotMergeState); |
| 3781 | fastboot_fail(EraseResultStr); |
| 3782 | return; |
| 3783 | } |
| 3784 | SnapshotMergeStatus = GetSnapshotMergeStatus (); |
| 3785 | if (((SnapshotMergeStatus == MERGING) || (SnapshotMergeStatus == SNAPSHOTTED)) && |
| 3786 | !strncmp (arg, "super", strlen ("super"))) { |
| 3787 | |
| 3788 | if(SetSnapshotMergeStatus (CANCELLED)) |
| 3789 | { |
| 3790 | fastboot_fail("Failed to update snapshot state to cancel"); |
| 3791 | return; |
| 3792 | } |
| 3793 | |
| 3794 | //updating fbvar snapshot-merge-state |
| 3795 | snprintf(SnapshotMergeState,strlen(VabSnapshotMergeStatus[NONE_MERGE_STATUS]) + 1, |
| 3796 | "%s", VabSnapshotMergeStatus[NONE_MERGE_STATUS]); |
| 3797 | } |
| 3798 | } |
| 3799 | |
Monika Singh | c4778b7 | 2018-05-16 11:16:42 +0530 | [diff] [blame] | 3800 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 3801 | dprintf(INFO, "erasing avb_custom_key\n"); |
| 3802 | if (erase_userkey()) { |
| 3803 | fastboot_fail("Erasing avb_custom_key failed"); |
| 3804 | } else { |
| 3805 | fastboot_okay(""); |
| 3806 | } |
| 3807 | return; |
| 3808 | } |
| 3809 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3810 | if(ptn == 0) { |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 3811 | fastboot_fail("Partition table doesn't exist\n"); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3812 | return; |
| 3813 | } |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 3814 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3815 | lun = partition_get_lun(index); |
| 3816 | mmc_set_lun(lun); |
| 3817 | |
Sridhar Parasuram | d795712 | 2015-02-27 11:33:40 -0800 | [diff] [blame] | 3818 | if (platform_boot_dev_isemmc()) |
| 3819 | { |
| 3820 | if (mmc_erase_card(ptn, size)) { |
| 3821 | fastboot_fail("failed to erase partition\n"); |
| 3822 | return; |
| 3823 | } |
| 3824 | } else { |
| 3825 | BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE); |
| 3826 | size = partition_get_size(index); |
| 3827 | if (size > DEFAULT_ERASE_SIZE) |
| 3828 | size = DEFAULT_ERASE_SIZE; |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 3829 | |
Sridhar Parasuram | d795712 | 2015-02-27 11:33:40 -0800 | [diff] [blame] | 3830 | /* Simple inefficient version of erase. Just writing |
| 3831 | 0 in first several blocks */ |
| 3832 | if (mmc_write(ptn , size, (unsigned int *)out)) { |
| 3833 | fastboot_fail("failed to erase partition"); |
| 3834 | return; |
| 3835 | } |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 3836 | /*Erase FDE metadata at the userdata footer*/ |
| 3837 | if(!(strncmp(arg, "userdata", 8))) |
| 3838 | { |
| 3839 | footer = memalign(CACHE_LINE, FOOTER_SIZE); |
| 3840 | memset((void *)footer, 0, FOOTER_SIZE); |
| 3841 | |
| 3842 | size = partition_get_size(index); |
| 3843 | |
| 3844 | if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) { |
| 3845 | fastboot_fail("failed to erase userdata footer"); |
| 3846 | free(footer); |
| 3847 | return; |
| 3848 | } |
| 3849 | free(footer); |
| 3850 | } |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3851 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3852 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3853 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3854 | !(strncmp(arg, "userdata", 8)) && |
| 3855 | send_delete_keys_to_tz()) |
Sridhar Parasuram | 32b3066 | 2015-07-10 13:33:22 -0700 | [diff] [blame] | 3856 | ASSERT(0); |
| 3857 | #endif |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3858 | fastboot_okay(""); |
| 3859 | } |
| 3860 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3861 | void cmd_erase(const char *arg, void *data, unsigned sz) |
| 3862 | { |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3863 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 3864 | if (target_build_variant_user()) |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 3865 | { |
Sridhar Parasuram | c32d07d | 2015-07-12 10:57:48 -0700 | [diff] [blame] | 3866 | if(!device.is_unlocked) |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 3867 | { |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 3868 | fastboot_fail("device is locked. Cannot erase"); |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 3869 | return; |
| 3870 | } |
| 3871 | } |
| 3872 | #endif |
| 3873 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3874 | if(target_is_emmc_boot()) |
| 3875 | cmd_erase_mmc(arg, data, sz); |
| 3876 | else |
| 3877 | cmd_erase_nand(arg, data, sz); |
| 3878 | } |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3879 | |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 3880 | /* Get the size from partiton name */ |
| 3881 | static void get_partition_size(const char *arg, char *response) |
| 3882 | { |
| 3883 | uint64_t ptn = 0; |
| 3884 | uint64_t size; |
| 3885 | int index = INVALID_PTN; |
| 3886 | |
| 3887 | index = partition_get_index(arg); |
| 3888 | |
| 3889 | if (index == INVALID_PTN) |
| 3890 | { |
| 3891 | dprintf(CRITICAL, "Invalid partition index\n"); |
| 3892 | return; |
| 3893 | } |
| 3894 | |
| 3895 | ptn = partition_get_offset(index); |
| 3896 | |
| 3897 | if(!ptn) |
| 3898 | { |
| 3899 | dprintf(CRITICAL, "Invalid partition name %s\n", arg); |
| 3900 | return; |
| 3901 | } |
| 3902 | |
| 3903 | size = partition_get_size(index); |
| 3904 | |
| 3905 | snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size); |
| 3906 | return; |
| 3907 | } |
| 3908 | |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3909 | /* Function to check partition type of a partition*/ |
| 3910 | static fs_signature_type |
| 3911 | check_partition_fs_signature(const char *arg) |
| 3912 | { |
| 3913 | fs_signature_type ret = NO_FS; |
| 3914 | int index; |
| 3915 | unsigned long long ptn; |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3916 | char *buffer = memalign(CACHE_LINE, mmc_blocksize); |
| 3917 | uint32_t sb_blk_offset = 0; |
| 3918 | char *sb_buffer = buffer; |
| 3919 | |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3920 | if (!sb_buffer) |
| 3921 | { |
| 3922 | dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n"); |
| 3923 | goto out; |
| 3924 | } |
| 3925 | |
| 3926 | /* Read super block */ |
| 3927 | if ((index = partition_get_index(arg)) < 0) |
| 3928 | { |
| 3929 | dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg); |
| 3930 | goto out; |
| 3931 | } |
| 3932 | ptn = partition_get_offset(index); |
| 3933 | mmc_set_lun(partition_get_lun(index)); |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3934 | sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize); |
| 3935 | |
| 3936 | if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize), |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3937 | (void *)sb_buffer, mmc_blocksize)) |
| 3938 | { |
| 3939 | dprintf(CRITICAL, "ERROR: Failed to read Superblock\n"); |
| 3940 | goto out; |
| 3941 | } |
| 3942 | |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3943 | if (sb_blk_offset == 0) |
| 3944 | sb_buffer += FS_SUPERBLOCK_OFFSET; |
| 3945 | |
| 3946 | if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC) |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3947 | { |
| 3948 | dprintf(SPEW, "%s() Found EXT FS\n", arg); |
| 3949 | ret = EXT_FS_SIGNATURE; |
| 3950 | } |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3951 | else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC) |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3952 | { |
| 3953 | dprintf(SPEW, "%s() Found F2FS FS\n", arg); |
| 3954 | ret = EXT_F2FS_SIGNATURE; |
| 3955 | } |
| 3956 | else |
| 3957 | { |
| 3958 | dprintf(SPEW, "%s() Reverting to default 0x%x\n", |
| 3959 | arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB]))); |
| 3960 | ret = NO_FS; |
| 3961 | } |
| 3962 | |
| 3963 | out: |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3964 | if(buffer) |
| 3965 | free(buffer); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3966 | return ret; |
| 3967 | } |
| 3968 | |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 3969 | /* Function to get partition type */ |
| 3970 | static void get_partition_type(const char *arg, char *response) |
| 3971 | { |
| 3972 | uint n = 0; |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3973 | fs_signature_type fs_signature; |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 3974 | |
| 3975 | if (arg == NULL || |
| 3976 | response == NULL) |
| 3977 | { |
| 3978 | dprintf(CRITICAL, "Invalid input parameter\n"); |
| 3979 | return; |
| 3980 | } |
| 3981 | |
| 3982 | /* By default copy raw to response */ |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 3983 | strlcpy(response, RAW_STR, MAX_RSP_SIZE); |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 3984 | |
| 3985 | /* Mark partiton type for known paritions only */ |
| 3986 | for (n=0; n < ARRAY_SIZE(part_type_known); n++) |
| 3987 | { |
| 3988 | if (!strncmp(part_type_known[n].part_name, arg, strlen(arg))) |
| 3989 | { |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3990 | /* Check partition for FS signature */ |
| 3991 | fs_signature = check_partition_fs_signature(arg); |
| 3992 | switch (fs_signature) |
| 3993 | { |
| 3994 | case EXT_FS_SIGNATURE: |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 3995 | strlcpy(response, EXT_STR, MAX_RSP_SIZE); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3996 | break; |
| 3997 | case EXT_F2FS_SIGNATURE: |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 3998 | strlcpy(response, F2FS_STR, MAX_RSP_SIZE); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3999 | break; |
| 4000 | case NO_FS: |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 4001 | strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4002 | } |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 4003 | } |
| 4004 | } |
| 4005 | return; |
| 4006 | } |
| 4007 | |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4008 | /* |
| 4009 | * Publish the partition type & size info |
| 4010 | * fastboot getvar will publish the required information. |
| 4011 | * fastboot getvar partition_size:<partition_name>: partition size in hex |
| 4012 | * fastboot getvar partition_type:<partition_name>: partition type (ext/fat) |
| 4013 | */ |
| 4014 | static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts) |
| 4015 | { |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 4016 | uint8_t i; |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4017 | static bool published = false; |
| 4018 | struct partition_entry *ptn_entry = |
| 4019 | partition_get_partition_entries(); |
| 4020 | memset(info, 0, sizeof(struct getvar_partition_info)* num_parts); |
| 4021 | |
| 4022 | for (i = 0; i < num_parts; i++) { |
| 4023 | strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE); |
| 4024 | strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE); |
| 4025 | strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE); |
| 4026 | |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 4027 | get_partition_type(info[i].part_name, info[i].type_response); |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4028 | get_partition_size(info[i].part_name, info[i].size_response); |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4029 | if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE) |
| 4030 | { |
| 4031 | dprintf(CRITICAL, "partition size name truncated\n"); |
| 4032 | return; |
| 4033 | } |
| 4034 | if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE) |
| 4035 | { |
| 4036 | dprintf(CRITICAL, "partition type name truncated\n"); |
| 4037 | return; |
| 4038 | } |
| 4039 | |
| 4040 | if (!published) |
| 4041 | { |
| 4042 | /* publish partition size & type info */ |
| 4043 | fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response); |
| 4044 | fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response); |
| 4045 | } |
| 4046 | } |
| 4047 | if (!published) |
| 4048 | published = true; |
| 4049 | } |
| 4050 | |
| 4051 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4052 | void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz) |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 4053 | { |
| 4054 | unsigned long long ptn = 0; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 4055 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 4056 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4057 | char *token = NULL; |
| 4058 | char *pname = NULL; |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 4059 | char *sp; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4060 | uint8_t lun = 0; |
| 4061 | bool lun_set = false; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4062 | int current_active_slot = INVALID; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 4063 | |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 4064 | token = strtok_r((char *)arg, ":", &sp); |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4065 | pname = token; |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 4066 | token = strtok_r(NULL, ":", &sp); |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4067 | if(token) |
| 4068 | { |
| 4069 | lun = atoi(token); |
| 4070 | mmc_set_lun(lun); |
| 4071 | lun_set = true; |
| 4072 | } |
| 4073 | |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4074 | if (pname) |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 4075 | { |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 4076 | if (!strncmp(pname, "frp-unlock", strlen("frp-unlock"))) |
| 4077 | { |
| 4078 | if (!aboot_frp_unlock(pname, data, sz)) |
| 4079 | { |
| 4080 | fastboot_info("FRP unlock successful"); |
| 4081 | fastboot_okay(""); |
| 4082 | } |
| 4083 | else |
| 4084 | fastboot_fail("Secret key is invalid, please update the bootloader with secret key"); |
| 4085 | |
| 4086 | return; |
| 4087 | } |
| 4088 | |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4089 | if (!strcmp(pname, "partition")) |
| 4090 | { |
| 4091 | dprintf(INFO, "Attempt to write partition image.\n"); |
| 4092 | if (write_partition(sz, (unsigned char *) data)) { |
| 4093 | fastboot_fail("failed to write partition"); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 4094 | return; |
| 4095 | } |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4096 | /* Re-publish partition table */ |
| 4097 | publish_getvar_partition_info(part_info, partition_get_partition_count()); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4098 | |
| 4099 | /* Rescan partition table to ensure we have multislot support*/ |
| 4100 | if (partition_scan_for_multislot()) |
| 4101 | { |
| 4102 | current_active_slot = partition_find_active_slot(); |
| 4103 | dprintf(INFO, "Multislot supported: Slot %s active", |
| 4104 | (SUFFIX_SLOT(current_active_slot))); |
| 4105 | } |
| 4106 | partition_mark_active_slot(current_active_slot); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 4107 | } |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4108 | else |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4109 | { |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4110 | #if VERIFIED_BOOT |
| 4111 | if(!strcmp(pname, KEYSTORE_PTN_NAME)) |
| 4112 | { |
| 4113 | if(!device.is_unlocked) |
| 4114 | { |
| 4115 | fastboot_fail("unlock device to flash keystore"); |
| 4116 | return; |
| 4117 | } |
Gaurav Nebhwani | c7313cc | 2015-12-15 22:25:04 +0530 | [diff] [blame] | 4118 | if(!boot_verify_validate_keystore((unsigned char *)data,sz)) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4119 | { |
| 4120 | fastboot_fail("image is not a keystore file"); |
| 4121 | return; |
| 4122 | } |
| 4123 | } |
| 4124 | #endif |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4125 | index = partition_get_index(pname); |
| 4126 | ptn = partition_get_offset(index); |
| 4127 | if(ptn == 0) { |
| 4128 | fastboot_fail("partition table doesn't exist"); |
| 4129 | return; |
| 4130 | } |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4131 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4132 | if (!strncmp(pname, "boot", strlen("boot")) |
| 4133 | || !strcmp(pname, "recovery")) |
| 4134 | { |
Ashish Bhimanpalliwar | 84f7db9 | 2020-09-23 11:59:16 +0530 | [diff] [blame] | 4135 | 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] | 4136 | fastboot_fail("image is not a boot image"); |
| 4137 | return; |
| 4138 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4139 | |
| 4140 | /* Reset multislot_partition attributes in case of flashing boot */ |
| 4141 | if (partition_multislot_is_supported()) |
| 4142 | { |
| 4143 | partition_reset_attributes(index); |
| 4144 | } |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | if(!lun_set) |
| 4148 | { |
| 4149 | lun = partition_get_lun(index); |
| 4150 | mmc_set_lun(lun); |
| 4151 | } |
| 4152 | |
| 4153 | size = partition_get_size(index); |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 4154 | if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) { |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4155 | fastboot_fail("size too large"); |
| 4156 | return; |
| 4157 | } |
| 4158 | else if (mmc_write(ptn , sz, (unsigned int *)data)) { |
| 4159 | fastboot_fail("flash write failure"); |
| 4160 | return; |
| 4161 | } |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 4162 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 4163 | } |
| 4164 | fastboot_okay(""); |
| 4165 | return; |
| 4166 | } |
| 4167 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4168 | void cmd_flash_meta_img(const char *arg, void *data, unsigned sz) |
| 4169 | { |
| 4170 | int i, images; |
| 4171 | meta_header_t *meta_header; |
| 4172 | img_header_entry_t *img_header_entry; |
Parth Dixit | 3e2d303 | 2016-03-04 17:11:52 +0530 | [diff] [blame] | 4173 | /*End of the image address*/ |
| 4174 | uintptr_t data_end; |
| 4175 | |
| 4176 | if( (UINT_MAX - sz) > (uintptr_t)data ) |
| 4177 | data_end = (uintptr_t)data + sz; |
| 4178 | else |
| 4179 | { |
| 4180 | fastboot_fail("Cannot flash: image header corrupt"); |
| 4181 | return; |
| 4182 | } |
| 4183 | |
Ashish Bhimanpalliwar | 21494e1 | 2020-10-07 13:24:03 +0530 | [diff] [blame] | 4184 | 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] | 4185 | { |
| 4186 | fastboot_fail("Cannot flash: image header corrupt"); |
| 4187 | return; |
| 4188 | } |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4189 | |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 4190 | /* If device is locked: |
| 4191 | * Forbid to flash image to avoid the device to bypass the image |
| 4192 | * which with "any" name other than bootloader. Because it maybe |
| 4193 | * a meta package of all partitions. |
| 4194 | */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4195 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 4196 | if (target_build_variant_user()) { |
| 4197 | if (!device.is_unlocked) { |
| 4198 | fastboot_fail("Device is locked, meta image flashing is not allowed"); |
| 4199 | return; |
| 4200 | } |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4201 | |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 4202 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4203 | !device.is_unlock_critical) |
| 4204 | { |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 4205 | fastboot_fail("Device is critical locked, Meta image flashing is not allowed"); |
| 4206 | return; |
| 4207 | } |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 4208 | } |
| 4209 | #endif |
| 4210 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4211 | meta_header = (meta_header_t*) data; |
Ashish Bhimanpalliwar | 21494e1 | 2020-10-07 13:24:03 +0530 | [diff] [blame] | 4212 | 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] | 4213 | { |
| 4214 | fastboot_fail("Cannot flash: image header corrupt"); |
| 4215 | return; |
| 4216 | } |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4217 | img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t)); |
| 4218 | |
| 4219 | images = meta_header->img_hdr_sz / sizeof(img_header_entry_t); |
| 4220 | |
| 4221 | for (i=0; i<images; i++) { |
| 4222 | |
| 4223 | if((img_header_entry[i].ptn_name == NULL) || |
| 4224 | (img_header_entry[i].start_offset == 0) || |
| 4225 | (img_header_entry[i].size == 0)) |
| 4226 | break; |
Kishor PK | 4983150 | 2017-04-21 17:55:43 +0530 | [diff] [blame] | 4227 | if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) { |
| 4228 | fastboot_fail("Integer overflow detected in start_offset of img"); |
| 4229 | break; |
| 4230 | } |
| 4231 | else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) { |
| 4232 | fastboot_fail("Integer overflow detected in size of img"); |
| 4233 | break; |
| 4234 | } |
Parth Dixit | 3e2d303 | 2016-03-04 17:11:52 +0530 | [diff] [blame] | 4235 | if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset |
| 4236 | + img_header_entry[i].size) ) |
| 4237 | { |
| 4238 | fastboot_fail("Cannot flash: image size mismatch"); |
| 4239 | break; |
| 4240 | } |
| 4241 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4242 | cmd_flash_mmc_img(img_header_entry[i].ptn_name, |
| 4243 | (void *) data + img_header_entry[i].start_offset, |
| 4244 | img_header_entry[i].size); |
| 4245 | } |
| 4246 | |
Channagoud Kadabi | aafbbfc | 2015-03-04 17:52:56 -0800 | [diff] [blame] | 4247 | if (!strncmp(arg, "bootloader", strlen("bootloader"))) |
| 4248 | { |
| 4249 | strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN); |
| 4250 | strlcat(device.bootloader_version, "-", MAX_VERSION_LEN); |
| 4251 | strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN); |
| 4252 | } |
| 4253 | else |
| 4254 | { |
| 4255 | strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN); |
| 4256 | strlcat(device.radio_version, "-", MAX_VERSION_LEN); |
| 4257 | strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN); |
| 4258 | } |
| 4259 | |
| 4260 | write_device_info(&device); |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4261 | fastboot_okay(""); |
| 4262 | return; |
| 4263 | } |
| 4264 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4265 | void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz) |
| 4266 | { |
| 4267 | unsigned int chunk; |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4268 | uint64_t chunk_data_sz; |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4269 | uint32_t *fill_buf = NULL; |
| 4270 | uint32_t fill_val; |
Mayank Grover | 4f50bba | 2017-07-19 18:17:08 +0530 | [diff] [blame] | 4271 | uint32_t blk_sz_actual = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4272 | sparse_header_t *sparse_header; |
| 4273 | chunk_header_t *chunk_header; |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 4274 | uint32_t total_blocks = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4275 | unsigned long long ptn = 0; |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 4276 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 4277 | int index = INVALID_PTN; |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 4278 | uint32_t i; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4279 | uint8_t lun = 0; |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4280 | /*End of the sparse image address*/ |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4281 | uintptr_t data_end = (uintptr_t)data + sz; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4282 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 4283 | index = partition_get_index(arg); |
| 4284 | ptn = partition_get_offset(index); |
| 4285 | if(ptn == 0) { |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4286 | fastboot_fail("partition table doesn't exist"); |
| 4287 | return; |
| 4288 | } |
| 4289 | |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 4290 | size = partition_get_size(index); |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 4291 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4292 | lun = partition_get_lun(index); |
| 4293 | mmc_set_lun(lun); |
| 4294 | |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4295 | if (sz < sizeof(sparse_header_t)) { |
| 4296 | fastboot_fail("size too low"); |
| 4297 | return; |
| 4298 | } |
| 4299 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4300 | /* Read and skip over sparse image header */ |
| 4301 | sparse_header = (sparse_header_t *) data; |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4302 | |
Mayank Grover | 48bd9bb | 2017-07-19 12:04:16 +0530 | [diff] [blame] | 4303 | if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){ |
| 4304 | fastboot_fail("Invalid block size\n"); |
| 4305 | return; |
| 4306 | } |
| 4307 | |
vijay kumar | 1321f34 | 2015-03-27 12:13:42 +0530 | [diff] [blame] | 4308 | 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] | 4309 | fastboot_fail("size too large"); |
| 4310 | return; |
| 4311 | } |
| 4312 | |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 4313 | data += sizeof(sparse_header_t); |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4314 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4315 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4316 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 4317 | return; |
| 4318 | } |
| 4319 | |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 4320 | if(sparse_header->file_hdr_sz != sizeof(sparse_header_t)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4321 | { |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 4322 | fastboot_fail("sparse header size mismatch"); |
| 4323 | return; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4324 | } |
| 4325 | |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 4326 | dprintf (SPEW, "=== Sparse Image Header ===\n"); |
| 4327 | dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic); |
| 4328 | dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version); |
| 4329 | dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version); |
| 4330 | dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz); |
| 4331 | dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz); |
| 4332 | dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz); |
| 4333 | dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks); |
| 4334 | dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4335 | |
| 4336 | /* Start processing chunks */ |
| 4337 | for (chunk=0; chunk<sparse_header->total_chunks; chunk++) |
| 4338 | { |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4339 | /* Make sure the total image size does not exceed the partition size */ |
| 4340 | if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) { |
| 4341 | fastboot_fail("size too large"); |
| 4342 | return; |
| 4343 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4344 | /* Read and skip over chunk header */ |
| 4345 | chunk_header = (chunk_header_t *) data; |
| 4346 | data += sizeof(chunk_header_t); |
| 4347 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4348 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4349 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 4350 | return; |
| 4351 | } |
| 4352 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4353 | dprintf (SPEW, "=== Chunk Header ===\n"); |
| 4354 | dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type); |
| 4355 | dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz); |
| 4356 | dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz); |
| 4357 | |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4358 | if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4359 | { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4360 | fastboot_fail("chunk header size mismatch"); |
| 4361 | return; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4362 | } |
| 4363 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4364 | chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz; |
| 4365 | |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4366 | /* Make sure that the chunk size calculated from sparse image does not |
| 4367 | * exceed partition size |
| 4368 | */ |
| 4369 | if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size) |
| 4370 | { |
| 4371 | fastboot_fail("Chunk data size exceeds partition size"); |
| 4372 | return; |
| 4373 | } |
| 4374 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4375 | switch (chunk_header->chunk_type) |
| 4376 | { |
| 4377 | case CHUNK_TYPE_RAW: |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4378 | 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] | 4379 | chunk_data_sz)) |
| 4380 | { |
| 4381 | fastboot_fail("Bogus chunk size for chunk type Raw"); |
| 4382 | return; |
| 4383 | } |
| 4384 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4385 | if (data_end < (uintptr_t)data + chunk_data_sz) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4386 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 4387 | return; |
| 4388 | } |
| 4389 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4390 | /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32 |
| 4391 | otherwise it will return in the line above |
| 4392 | */ |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 4393 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4394 | (uint32_t)chunk_data_sz, |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 4395 | (unsigned int*)data)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4396 | { |
| 4397 | fastboot_fail("flash write failure"); |
| 4398 | return; |
| 4399 | } |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4400 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 4401 | fastboot_fail("Bogus size for RAW chunk type"); |
| 4402 | return; |
| 4403 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4404 | total_blocks += chunk_header->chunk_sz; |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4405 | data += (uint32_t)chunk_data_sz; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4406 | break; |
| 4407 | |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4408 | case CHUNK_TYPE_FILL: |
| 4409 | if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz + |
| 4410 | sizeof(uint32_t))) |
| 4411 | { |
| 4412 | fastboot_fail("Bogus chunk size for chunk type FILL"); |
| 4413 | return; |
| 4414 | } |
| 4415 | |
Mayank Grover | 4f50bba | 2017-07-19 18:17:08 +0530 | [diff] [blame] | 4416 | blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE); |
| 4417 | /* Integer overflow detected */ |
| 4418 | if (blk_sz_actual < sparse_header->blk_sz) |
| 4419 | { |
| 4420 | fastboot_fail("Invalid block size"); |
| 4421 | return; |
| 4422 | } |
| 4423 | |
| 4424 | fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual); |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4425 | if (!fill_buf) |
| 4426 | { |
| 4427 | fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL"); |
| 4428 | return; |
| 4429 | } |
| 4430 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4431 | if (data_end < (uintptr_t)data + sizeof(uint32_t)) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4432 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
Gaurav Nebhwani | 163cbf8 | 2016-03-17 21:21:27 +0530 | [diff] [blame] | 4433 | free(fill_buf); |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4434 | return; |
| 4435 | } |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4436 | fill_val = *(uint32_t *)data; |
| 4437 | data = (char *) data + sizeof(uint32_t); |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4438 | |
| 4439 | for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++) |
| 4440 | { |
| 4441 | fill_buf[i] = fill_val; |
| 4442 | } |
| 4443 | |
Gaurav Nebhwani | e94cbe1 | 2016-03-17 21:16:34 +0530 | [diff] [blame] | 4444 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) |
| 4445 | { |
| 4446 | fastboot_fail("bogus size for chunk FILL type"); |
| 4447 | free(fill_buf); |
| 4448 | return; |
| 4449 | } |
| 4450 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4451 | for (i = 0; i < chunk_header->chunk_sz; i++) |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4452 | { |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4453 | /* Make sure that the data written to partition does not exceed partition size */ |
| 4454 | if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size) |
| 4455 | { |
| 4456 | fastboot_fail("Chunk data size for fill type exceeds partition size"); |
Gaurav Nebhwani | 163cbf8 | 2016-03-17 21:21:27 +0530 | [diff] [blame] | 4457 | free(fill_buf); |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4458 | return; |
| 4459 | } |
| 4460 | |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4461 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
| 4462 | sparse_header->blk_sz, |
| 4463 | fill_buf)) |
| 4464 | { |
| 4465 | fastboot_fail("flash write failure"); |
| 4466 | free(fill_buf); |
| 4467 | return; |
| 4468 | } |
| 4469 | |
| 4470 | total_blocks++; |
| 4471 | } |
| 4472 | |
| 4473 | free(fill_buf); |
| 4474 | break; |
| 4475 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4476 | case CHUNK_TYPE_DONT_CARE: |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4477 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 4478 | fastboot_fail("bogus size for chunk DONT CARE type"); |
| 4479 | return; |
| 4480 | } |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 4481 | total_blocks += chunk_header->chunk_sz; |
| 4482 | break; |
| 4483 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4484 | case CHUNK_TYPE_CRC: |
| 4485 | if(chunk_header->total_sz != sparse_header->chunk_hdr_sz) |
| 4486 | { |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4487 | fastboot_fail("Bogus chunk size for chunk type CRC"); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4488 | return; |
| 4489 | } |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4490 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 4491 | fastboot_fail("bogus size for chunk CRC type"); |
| 4492 | return; |
| 4493 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4494 | total_blocks += chunk_header->chunk_sz; |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4495 | if ((uintptr_t)data > UINT_MAX - chunk_data_sz) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4496 | fastboot_fail("integer overflow occured"); |
| 4497 | return; |
| 4498 | } |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4499 | data += (uint32_t)chunk_data_sz; |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4500 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4501 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 4502 | return; |
| 4503 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4504 | break; |
| 4505 | |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 4506 | default: |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4507 | dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4508 | fastboot_fail("Unknown chunk type"); |
| 4509 | return; |
| 4510 | } |
| 4511 | } |
| 4512 | |
Ajay Dudani | 0c6927b | 2011-05-18 11:12:16 -0700 | [diff] [blame] | 4513 | dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n", |
| 4514 | total_blocks, sparse_header->total_blks); |
| 4515 | |
| 4516 | if(total_blocks != sparse_header->total_blks) |
| 4517 | { |
| 4518 | fastboot_fail("sparse image write failure"); |
| 4519 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4520 | |
| 4521 | fastboot_okay(""); |
| 4522 | return; |
| 4523 | } |
| 4524 | |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 4525 | static bool CheckVirtualAbCriticalPartition (const char *PartitionName) |
| 4526 | { |
| 4527 | VirtualAbMergeStatus SnapshotMergeStatus; |
| 4528 | uint32 Iter = 0; |
| 4529 | |
| 4530 | SnapshotMergeStatus = GetSnapshotMergeStatus (); |
| 4531 | if ((SnapshotMergeStatus == MERGING || SnapshotMergeStatus == SNAPSHOTTED)) { |
| 4532 | for (Iter = 0; Iter < ARRAY_SIZE (VirtualAbCriticalPartitions); Iter++) { |
| 4533 | if (!strncmp (PartitionName, VirtualAbCriticalPartitions[Iter], |
| 4534 | strlen (VirtualAbCriticalPartitions[Iter]))) |
| 4535 | return true; |
| 4536 | } |
| 4537 | } |
| 4538 | return false; |
| 4539 | } |
| 4540 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4541 | void cmd_flash_mmc(const char *arg, void *data, unsigned sz) |
| 4542 | { |
| 4543 | sparse_header_t *sparse_header; |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4544 | meta_header_t *meta_header; |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 4545 | VirtualAbMergeStatus SnapshotMergeStatus; |
| 4546 | char FlashResultStr[MAX_RSP_SIZE] = ""; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4547 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 4548 | #ifdef SSD_ENABLE |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 4549 | /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */ |
| 4550 | unsigned int *magic_number = (unsigned int *) data; |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4551 | int ret=0; |
| 4552 | uint32 major_version=0; |
| 4553 | uint32 minor_version=0; |
| 4554 | |
| 4555 | ret = scm_svc_version(&major_version,&minor_version); |
| 4556 | if(!ret) |
| 4557 | { |
| 4558 | if(major_version >= 2) |
| 4559 | { |
Amir Samuelov | bb65ce0 | 2013-05-05 12:20:18 +0300 | [diff] [blame] | 4560 | if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") ) |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4561 | { |
| 4562 | ret = encrypt_scm((uint32 **) &data, &sz); |
| 4563 | if (ret != 0) { |
| 4564 | dprintf(CRITICAL, "ERROR: Encryption Failure\n"); |
| 4565 | return; |
| 4566 | } |
| 4567 | |
Amir Samuelov | bb65ce0 | 2013-05-05 12:20:18 +0300 | [diff] [blame] | 4568 | /* Protect only for SSD */ |
| 4569 | if (!strcmp(arg, "ssd")) { |
| 4570 | ret = scm_protect_keystore((uint32 *) data, sz); |
| 4571 | if (ret != 0) { |
| 4572 | dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n"); |
| 4573 | return; |
| 4574 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4575 | } |
| 4576 | } |
| 4577 | else |
| 4578 | { |
| 4579 | ret = decrypt_scm_v2((uint32 **) &data, &sz); |
| 4580 | if(ret != 0) |
| 4581 | { |
| 4582 | dprintf(CRITICAL,"ERROR: Decryption Failure\n"); |
| 4583 | return; |
| 4584 | } |
| 4585 | } |
| 4586 | } |
| 4587 | else |
| 4588 | { |
| 4589 | if (magic_number[0] == DECRYPT_MAGIC_0 && |
| 4590 | magic_number[1] == DECRYPT_MAGIC_1) |
| 4591 | { |
| 4592 | ret = decrypt_scm((uint32 **) &data, &sz); |
| 4593 | if (ret != 0) { |
| 4594 | dprintf(CRITICAL, "ERROR: Invalid secure image\n"); |
| 4595 | return; |
| 4596 | } |
| 4597 | } |
| 4598 | else if (magic_number[0] == ENCRYPT_MAGIC_0 && |
| 4599 | magic_number[1] == ENCRYPT_MAGIC_1) |
| 4600 | { |
| 4601 | ret = encrypt_scm((uint32 **) &data, &sz); |
| 4602 | if (ret != 0) { |
| 4603 | dprintf(CRITICAL, "ERROR: Encryption Failure\n"); |
| 4604 | return; |
| 4605 | } |
| 4606 | } |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 4607 | } |
| 4608 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4609 | else |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 4610 | { |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4611 | dprintf(CRITICAL,"INVALID SVC Version\n"); |
| 4612 | return; |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 4613 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4614 | #endif /* SSD_ENABLE */ |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 4615 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4616 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 4617 | if (target_build_variant_user()) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4618 | { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4619 | /* if device is locked: |
| 4620 | * common partition will not allow to be flashed |
| 4621 | * critical partition will allow to flash image. |
| 4622 | */ |
| 4623 | if(!device.is_unlocked && !critical_flash_allowed(arg)) { |
| 4624 | fastboot_fail("Partition flashing is not allowed"); |
| 4625 | return; |
| 4626 | } |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4627 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4628 | /* if device critical is locked: |
| 4629 | * common partition will allow to be flashed |
| 4630 | * critical partition will not allow to flash image. |
| 4631 | */ |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 4632 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4633 | !device.is_unlock_critical && |
| 4634 | critical_flash_allowed(arg)) { |
| 4635 | fastboot_fail("Critical partition flashing is not allowed"); |
| 4636 | return; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4637 | } |
| 4638 | } |
| 4639 | #endif |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 4640 | |
| 4641 | if (target_virtual_ab_supported()) |
| 4642 | { |
| 4643 | if (CheckVirtualAbCriticalPartition (arg)) { |
| 4644 | snprintf(FlashResultStr, MAX_RSP_SIZE,"Flashing of %s is not allowed in %s state", |
| 4645 | arg, SnapshotMergeState); |
| 4646 | fastboot_fail(FlashResultStr); |
| 4647 | return; |
| 4648 | } |
| 4649 | |
| 4650 | SnapshotMergeStatus = GetSnapshotMergeStatus (); |
| 4651 | if (((SnapshotMergeStatus == MERGING) || (SnapshotMergeStatus == SNAPSHOTTED)) && |
| 4652 | !strncmp (arg, "super", strlen ("super"))) { |
| 4653 | if(SetSnapshotMergeStatus (CANCELLED)) |
| 4654 | { |
| 4655 | fastboot_fail("Failed to update snapshot state to cancel"); |
| 4656 | return; |
| 4657 | } |
| 4658 | |
| 4659 | //updating fbvar snapshot-merge-state |
| 4660 | snprintf(SnapshotMergeState,strlen(VabSnapshotMergeStatus[NONE_MERGE_STATUS]) + 1, |
| 4661 | "%s", VabSnapshotMergeStatus[NONE_MERGE_STATUS]); |
| 4662 | } |
| 4663 | } |
| 4664 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4665 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 4666 | dprintf(INFO, "flashing avb_custom_key\n"); |
| 4667 | if (store_userkey(data, sz)) { |
| 4668 | fastboot_fail("Flashing avb_custom_key failed"); |
| 4669 | } else { |
| 4670 | fastboot_okay(""); |
| 4671 | } |
| 4672 | return; |
| 4673 | } |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4674 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 4675 | sparse_header = (sparse_header_t *) data; |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4676 | meta_header = (meta_header_t *) data; |
| 4677 | if (sparse_header->magic == SPARSE_HEADER_MAGIC) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4678 | cmd_flash_mmc_sparse_img(arg, data, sz); |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4679 | else if (meta_header->magic == META_HEADER_MAGIC) |
| 4680 | cmd_flash_meta_img(arg, data, sz); |
| 4681 | else |
| 4682 | cmd_flash_mmc_img(arg, data, sz); |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 4683 | |
| 4684 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 4685 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4686 | (!strncmp(arg, "system", 6)) && |
| 4687 | !device.verity_mode) |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 4688 | // reset dm_verity mode to enforcing |
| 4689 | device.verity_mode = 1; |
| 4690 | write_device_info(&device); |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 4691 | #endif |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 4692 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4693 | return; |
| 4694 | } |
| 4695 | |
Tanya Brokhman | 84eaaf6 | 2015-02-24 21:57:06 +0200 | [diff] [blame] | 4696 | void cmd_updatevol(const char *vol_name, void *data, unsigned sz) |
| 4697 | { |
| 4698 | struct ptentry *sys_ptn; |
| 4699 | struct ptable *ptable; |
| 4700 | |
| 4701 | ptable = flash_get_ptable(); |
| 4702 | if (ptable == NULL) { |
| 4703 | fastboot_fail("partition table doesn't exist"); |
| 4704 | return; |
| 4705 | } |
| 4706 | |
| 4707 | sys_ptn = ptable_find(ptable, "system"); |
| 4708 | if (sys_ptn == NULL) { |
| 4709 | fastboot_fail("system partition not found"); |
| 4710 | return; |
| 4711 | } |
| 4712 | |
| 4713 | sz = ROUND_TO_PAGE(sz, page_mask); |
| 4714 | if (update_ubi_vol(sys_ptn, vol_name, data, sz)) |
| 4715 | fastboot_fail("update_ubi_vol failed"); |
| 4716 | else |
| 4717 | fastboot_okay(""); |
| 4718 | } |
| 4719 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 4720 | void cmd_flash_nand(const char *arg, void *data, unsigned sz) |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4721 | { |
| 4722 | struct ptentry *ptn; |
| 4723 | struct ptable *ptable; |
| 4724 | unsigned extra = 0; |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 4725 | uint64_t partition_size = 0; |
Vijay Kumar Pendoti | c6f22a7 | 2017-05-19 22:44:34 +0530 | [diff] [blame] | 4726 | unsigned bytes_to_round_page = 0; |
| 4727 | unsigned rounded_size = 0; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4728 | |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4729 | if((uintptr_t)data > (UINT_MAX - sz)) { |
| 4730 | fastboot_fail("Cannot flash: image header corrupt"); |
| 4731 | return; |
| 4732 | } |
| 4733 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4734 | ptable = flash_get_ptable(); |
| 4735 | if (ptable == NULL) { |
| 4736 | fastboot_fail("partition table doesn't exist"); |
| 4737 | return; |
| 4738 | } |
| 4739 | |
| 4740 | ptn = ptable_find(ptable, arg); |
| 4741 | if (ptn == NULL) { |
Tanya Brokhman | 84eaaf6 | 2015-02-24 21:57:06 +0200 | [diff] [blame] | 4742 | dprintf(INFO, "unknown partition name (%s). Trying updatevol\n", |
| 4743 | arg); |
| 4744 | cmd_updatevol(arg, data, sz); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4745 | return; |
| 4746 | } |
| 4747 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4748 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 4749 | dprintf(INFO, "flashing avb_custom_key\n"); |
| 4750 | if (store_userkey(data, sz)) { |
| 4751 | fastboot_fail("Flashing avb_custom_key failed"); |
| 4752 | } else { |
| 4753 | fastboot_okay(""); |
| 4754 | } |
| 4755 | return; |
| 4756 | } |
| 4757 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4758 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4759 | if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) { |
| 4760 | dprintf(INFO, "Verified the BOOT_MAGIC in image header \n"); |
| 4761 | } else { |
| 4762 | fastboot_fail("Image is not a boot image"); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4763 | return; |
| 4764 | } |
| 4765 | } |
| 4766 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 4767 | if (!strcmp(ptn->name, "system") |
Deepa Dinamani | 13e32c4 | 2012-03-12 14:34:17 -0700 | [diff] [blame] | 4768 | || !strcmp(ptn->name, "userdata") |
| 4769 | || !strcmp(ptn->name, "persist") |
Sundarajan Srinivasan | b063a85 | 2013-11-19 14:02:27 -0800 | [diff] [blame] | 4770 | || !strcmp(ptn->name, "recoveryfs") |
Sundarajan Srinivasan | 595b71e | 2013-11-05 12:44:34 -0800 | [diff] [blame] | 4771 | || !strcmp(ptn->name, "modem")) |
Tanya Brokhman | 1c94f1a | 2015-02-15 09:05:03 +0200 | [diff] [blame] | 4772 | extra = 1; |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4773 | else { |
Vijay Kumar Pendoti | c6f22a7 | 2017-05-19 22:44:34 +0530 | [diff] [blame] | 4774 | rounded_size = ROUNDUP(sz, page_size); |
| 4775 | bytes_to_round_page = rounded_size - sz; |
| 4776 | if (bytes_to_round_page) { |
| 4777 | if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) { |
| 4778 | fastboot_fail("Integer overflow detected"); |
| 4779 | return; |
| 4780 | } |
| 4781 | if (((uintptr_t)data + sz + bytes_to_round_page) > |
| 4782 | ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) { |
| 4783 | fastboot_fail("Buffer size is not aligned to page_size"); |
| 4784 | return; |
| 4785 | } |
| 4786 | else { |
| 4787 | memset(data + sz, 0, bytes_to_round_page); |
| 4788 | sz = rounded_size; |
| 4789 | } |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4790 | } |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 4791 | } |
| 4792 | |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4793 | /*Checking partition_size for the possible integer overflow */ |
| 4794 | partition_size = validate_partition_size(ptn); |
| 4795 | |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 4796 | if (sz > partition_size) { |
| 4797 | fastboot_fail("Image size too large"); |
| 4798 | return; |
| 4799 | } |
| 4800 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4801 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
Mayank Grover | 6cde935 | 2017-06-06 18:45:23 +0530 | [diff] [blame] | 4802 | if ((sz > UBI_EC_HDR_SIZE) && |
| 4803 | (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) { |
Tanya Brokhman | 1c94f1a | 2015-02-15 09:05:03 +0200 | [diff] [blame] | 4804 | if (flash_ubi_img(ptn, data, sz)) { |
| 4805 | fastboot_fail("flash write failure"); |
| 4806 | return; |
| 4807 | } |
| 4808 | } else { |
| 4809 | if (flash_write(ptn, extra, data, sz)) { |
| 4810 | fastboot_fail("flash write failure"); |
| 4811 | return; |
| 4812 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4813 | } |
| 4814 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 4815 | fastboot_okay(""); |
| 4816 | } |
| 4817 | |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4818 | |
| 4819 | static inline uint64_t validate_partition_size(struct ptentry *ptn) |
| 4820 | { |
| 4821 | if (ptn->length && flash_num_pages_per_blk() && page_size) { |
| 4822 | if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) { |
| 4823 | return ptn->length * flash_num_pages_per_blk() * page_size; |
| 4824 | } |
| 4825 | } |
| 4826 | return 0; |
| 4827 | } |
| 4828 | |
| 4829 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 4830 | void cmd_flash(const char *arg, void *data, unsigned sz) |
| 4831 | { |
| 4832 | if(target_is_emmc_boot()) |
| 4833 | cmd_flash_mmc(arg, data, sz); |
| 4834 | else |
| 4835 | cmd_flash_nand(arg, data, sz); |
| 4836 | } |
| 4837 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4838 | void cmd_continue(const char *arg, void *data, unsigned sz) |
| 4839 | { |
| 4840 | fastboot_okay(""); |
Amol Jadi | 7c4316c | 2013-10-07 14:19:26 -0700 | [diff] [blame] | 4841 | fastboot_stop(); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4842 | |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4843 | if (target_is_emmc_boot()) |
| 4844 | { |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 4845 | #if FBCON_DISPLAY_MSG |
lijuang | de34d50 | 2016-02-26 16:04:50 +0800 | [diff] [blame] | 4846 | /* Exit keys' detection thread firstly */ |
| 4847 | exit_menu_keys_detection(); |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 4848 | #endif |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4849 | boot_linux_from_mmc(); |
| 4850 | } |
| 4851 | else |
| 4852 | { |
| 4853 | boot_linux_from_flash(); |
| 4854 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4855 | } |
| 4856 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 4857 | void cmd_reboot(const char *arg, void *data, unsigned sz) |
| 4858 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4859 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 4860 | fastboot_okay(""); |
| 4861 | reboot_device(0); |
| 4862 | } |
| 4863 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4864 | void cmd_set_active(const char *arg, void *data, unsigned sz) |
| 4865 | { |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4866 | char *p, *sp = NULL; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4867 | unsigned i,current_active_slot; |
| 4868 | const char *current_slot_suffix; |
| 4869 | |
| 4870 | if (!partition_multislot_is_supported()) |
| 4871 | { |
| 4872 | fastboot_fail("Command not supported"); |
| 4873 | return; |
| 4874 | } |
| 4875 | |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 4876 | if (target_virtual_ab_supported()) { |
| 4877 | if (GetSnapshotMergeStatus () == MERGING) { |
| 4878 | fastboot_fail ("Slot Change is not allowed in merging state"); |
| 4879 | return; |
| 4880 | } |
| 4881 | } |
| 4882 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4883 | if (arg) |
| 4884 | { |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4885 | p = strtok_r((char *)arg, ":", &sp); |
Mayank Grover | 5eb5f69 | 2017-07-19 20:16:04 +0530 | [diff] [blame] | 4886 | if (p) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4887 | { |
| 4888 | current_active_slot = partition_find_active_slot(); |
| 4889 | |
| 4890 | /* Check if trying to make curent slot active */ |
| 4891 | current_slot_suffix = SUFFIX_SLOT(current_active_slot); |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4892 | current_slot_suffix = strtok_r((char *)current_slot_suffix, |
| 4893 | (char *)suffix_delimiter, &sp); |
| 4894 | |
Mayank Grover | 5eb5f69 | 2017-07-19 20:16:04 +0530 | [diff] [blame] | 4895 | if (current_slot_suffix && |
| 4896 | !strncmp(p, current_slot_suffix, strlen(current_slot_suffix))) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4897 | { |
| 4898 | fastboot_okay("Slot already set active"); |
| 4899 | return; |
| 4900 | } |
| 4901 | else |
| 4902 | { |
| 4903 | for (i = 0; i < AB_SUPPORTED_SLOTS; i++) |
| 4904 | { |
| 4905 | current_slot_suffix = SUFFIX_SLOT(i); |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4906 | current_slot_suffix = strtok_r((char *)current_slot_suffix, |
| 4907 | (char *)suffix_delimiter, &sp); |
Mayank Grover | 5eb5f69 | 2017-07-19 20:16:04 +0530 | [diff] [blame] | 4908 | if (current_slot_suffix && |
| 4909 | !strncmp(p, current_slot_suffix, strlen(current_slot_suffix))) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4910 | { |
lijuang | 8b03e5f | 2019-07-12 18:16:19 +0800 | [diff] [blame] | 4911 | partition_switch_slots(current_active_slot, i, true); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4912 | publish_getvar_multislot_vars(); |
| 4913 | fastboot_okay(""); |
| 4914 | return; |
| 4915 | } |
| 4916 | } |
| 4917 | } |
| 4918 | } |
| 4919 | } |
| 4920 | fastboot_fail("Invalid slot suffix."); |
| 4921 | return; |
| 4922 | } |
| 4923 | |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 4924 | #if DYNAMIC_PARTITION_SUPPORT |
| 4925 | void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz) |
| 4926 | { |
| 4927 | dprintf(INFO, "rebooting the device - userspace fastboot\n"); |
| 4928 | if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) { |
| 4929 | dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n"); |
| 4930 | fastboot_fail("Failed to update recovery command"); |
| 4931 | return; |
| 4932 | } |
| 4933 | fastboot_okay(""); |
| 4934 | reboot_device(REBOOT_MODE_UNKNOWN); |
| 4935 | |
| 4936 | //shouldn't come here. |
| 4937 | dprintf(CRITICAL, "ERROR: Failed to reboot device\n"); |
| 4938 | return; |
| 4939 | } |
| 4940 | |
| 4941 | void cmd_reboot_recovery(const char *arg, void *data, unsigned sz) |
| 4942 | { |
| 4943 | dprintf(INFO, "rebooting the device - recovery\n"); |
| 4944 | if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) { |
| 4945 | dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n"); |
| 4946 | fastboot_fail("Failed to update recovery command"); |
| 4947 | return; |
| 4948 | } |
| 4949 | fastboot_okay(""); |
| 4950 | reboot_device(REBOOT_MODE_UNKNOWN); |
| 4951 | |
| 4952 | //shouldn't come here. |
| 4953 | dprintf(CRITICAL, "ERROR: Failed to reboot device\n"); |
| 4954 | return; |
| 4955 | } |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 4956 | |
| 4957 | #ifdef VIRTUAL_AB_OTA |
| 4958 | void CmdUpdateSnapshot(const char *arg, void *data, unsigned sz) |
| 4959 | { |
| 4960 | char *command = NULL; |
| 4961 | const char *delim = ":"; |
| 4962 | char *sp; |
| 4963 | |
| 4964 | if (arg) { |
| 4965 | command = strtok_r((char *)arg, delim, &sp); |
| 4966 | if (command) { |
| 4967 | command++; |
| 4968 | |
| 4969 | if(!strncmp (command, "merge", AsciiStrLen ("merge"))) { |
| 4970 | if (GetSnapshotMergeStatus () == MERGING) { |
| 4971 | cmd_reboot_fastboot(Arg, Data, Size); |
| 4972 | } |
| 4973 | FastbootOkay (""); |
| 4974 | return; |
| 4975 | } |
| 4976 | else if (!strncmp (Command, "cancel", AsciiStrLen ("cancel"))) { |
| 4977 | if(!device.is_unlocked) { |
| 4978 | fastboot_fail ("Snapshot Cancel is not allowed in Lock State"); |
| 4979 | return; |
| 4980 | } |
| 4981 | |
| 4982 | if (SetSnapshotMergeStatus (CANCELLED)) |
| 4983 | { |
| 4984 | fastboot_fail("Failed to update snapshot state to cancel"); |
| 4985 | return; |
| 4986 | } |
| 4987 | |
| 4988 | //updating fbvar snapshot-merge-state |
| 4989 | snprintf(SnapshotMergeState, strlen(VabSnapshotMergeStatus[NONE_MERGE_STATUS]) + 1, |
| 4990 | "%s", VabSnapshotMergeStatus[NONE_MERGE_STATUS]); |
| 4991 | fastboot_okay(""); |
| 4992 | return; |
| 4993 | } |
| 4994 | } |
| 4995 | } |
| 4996 | fastboot_fail("Invalid snapshot-update command"); |
| 4997 | return; |
| 4998 | } |
| 4999 | #endif |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 5000 | #endif |
| 5001 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 5002 | void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz) |
| 5003 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5004 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 5005 | fastboot_okay(""); |
| 5006 | reboot_device(FASTBOOT_MODE); |
| 5007 | } |
| 5008 | |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5009 | void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size) |
| 5010 | { |
| 5011 | dprintf(INFO, "Enabling charger screen check\n"); |
| 5012 | device.charger_screen_enabled = 1; |
| 5013 | write_device_info(&device); |
| 5014 | fastboot_okay(""); |
| 5015 | } |
| 5016 | |
| 5017 | void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size) |
| 5018 | { |
| 5019 | dprintf(INFO, "Disabling charger screen check\n"); |
| 5020 | device.charger_screen_enabled = 0; |
| 5021 | write_device_info(&device); |
| 5022 | fastboot_okay(""); |
| 5023 | } |
| 5024 | |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5025 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5026 | void CmdOemEnableAdb(const char *arg, void *data, unsigned size) |
| 5027 | { |
| 5028 | dprintf(INFO, "Enabling Adb\n"); |
| 5029 | prod.is_adb_enabled = 1; |
| 5030 | write_prod_info(&prod); |
| 5031 | fastboot_okay(""); |
| 5032 | } |
| 5033 | #endif |
| 5034 | |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5035 | void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size) |
| 5036 | { |
| 5037 | char *p = NULL; |
| 5038 | const char *delim = " \t\n\r"; |
Parth Dixit | 407f15b | 2016-01-07 14:47:37 +0530 | [diff] [blame] | 5039 | char *sp; |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5040 | |
| 5041 | if (arg) { |
Parth Dixit | 407f15b | 2016-01-07 14:47:37 +0530 | [diff] [blame] | 5042 | p = strtok_r((char *)arg, delim, &sp); |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5043 | if (p) { |
| 5044 | if (!strncmp(p, "0", 1)) { |
| 5045 | device.charger_screen_enabled = 0; |
| 5046 | } else if (!strncmp(p, "1", 1)) { |
| 5047 | device.charger_screen_enabled = 1; |
| 5048 | } |
| 5049 | } |
| 5050 | } |
| 5051 | |
| 5052 | /* update charger_screen_enabled value for getvar |
| 5053 | * command |
| 5054 | */ |
| 5055 | snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d", |
| 5056 | device.charger_screen_enabled); |
| 5057 | |
| 5058 | write_device_info(&device); |
| 5059 | fastboot_okay(""); |
| 5060 | } |
| 5061 | |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 5062 | void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size) |
| 5063 | { |
| 5064 | dprintf(INFO, "Selecting display panel %s\n", arg); |
| 5065 | if (arg) |
| 5066 | strlcpy(device.display_panel, arg, |
| 5067 | sizeof(device.display_panel)); |
| 5068 | write_device_info(&device); |
| 5069 | fastboot_okay(""); |
| 5070 | } |
| 5071 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 5072 | void cmd_oem_unlock(const char *arg, void *data, unsigned sz) |
| 5073 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5074 | set_device_unlock(UNLOCK, TRUE); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5075 | } |
| 5076 | |
| 5077 | void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz) |
| 5078 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5079 | if(!device.is_unlocked) { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5080 | if(!is_allow_unlock) { |
| 5081 | fastboot_fail("oem unlock is not allowed"); |
| 5082 | return; |
| 5083 | } |
| 5084 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5085 | set_device_unlock_value(UNLOCK, TRUE); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5086 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5087 | /* wipe data */ |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5088 | struct recovery_message msg; |
Kishor PK | 60a6821 | 2017-05-08 16:55:57 +0530 | [diff] [blame] | 5089 | memset(&msg, 0, sizeof(msg)); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5090 | snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data"); |
| 5091 | write_misc(0, &msg, sizeof(msg)); |
| 5092 | |
| 5093 | fastboot_okay(""); |
| 5094 | reboot_device(RECOVERY_MODE); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 5095 | } |
| 5096 | fastboot_okay(""); |
| 5097 | } |
| 5098 | |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 5099 | static int aboot_frp_unlock(char *pname, void *data, unsigned sz) |
| 5100 | { |
Mayank Grover | 70f8b0e | 2017-05-17 11:49:00 +0530 | [diff] [blame] | 5101 | int ret=1; |
| 5102 | bool authentication_success=false; |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 5103 | |
Mayank Grover | 70f8b0e | 2017-05-17 11:49:00 +0530 | [diff] [blame] | 5104 | /* |
| 5105 | Authentication method not implemented. |
| 5106 | |
| 5107 | OEM to implement, authentication system which on successful validataion, |
| 5108 | calls write_allow_oem_unlock() with is_allow_unlock. |
| 5109 | */ |
| 5110 | #if 0 |
| 5111 | authentication_success = oem_specific_auth_mthd(); |
| 5112 | #endif |
| 5113 | |
| 5114 | if (authentication_success) |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 5115 | { |
Mayank Grover | 70f8b0e | 2017-05-17 11:49:00 +0530 | [diff] [blame] | 5116 | is_allow_unlock = true; |
| 5117 | write_allow_oem_unlock(is_allow_unlock); |
| 5118 | ret = 0; |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 5119 | } |
| 5120 | return ret; |
| 5121 | } |
| 5122 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 5123 | void cmd_oem_lock(const char *arg, void *data, unsigned sz) |
| 5124 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5125 | set_device_unlock(UNLOCK, FALSE); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 5126 | } |
| 5127 | |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5128 | void cmd_oem_devinfo(const char *arg, void *data, unsigned sz) |
| 5129 | { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5130 | char response[MAX_RSP_SIZE]; |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5131 | snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false")); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5132 | fastboot_info(response); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5133 | snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false")); |
| 5134 | fastboot_info(response); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 5135 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 5136 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5137 | { |
| 5138 | snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", |
| 5139 | (device.is_unlock_critical ? "true" : "false")); |
| 5140 | fastboot_info(response); |
| 5141 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 5142 | #endif |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5143 | 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] | 5144 | fastboot_info(response); |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 5145 | snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel)); |
| 5146 | fastboot_info(response); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5147 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5148 | snprintf(response, sizeof(response), "\tAdb enabled: %s", prod.is_adb_enabled ? "true" : "false"); |
| 5149 | fastboot_info(response); |
| 5150 | #endif |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5151 | fastboot_okay(""); |
| 5152 | } |
| 5153 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5154 | void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz) |
| 5155 | { |
| 5156 | char response[MAX_RSP_SIZE]; |
| 5157 | snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock); |
| 5158 | fastboot_info(response); |
| 5159 | fastboot_okay(""); |
| 5160 | } |
| 5161 | |
| 5162 | void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz) |
| 5163 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5164 | set_device_unlock(UNLOCK_CRITICAL, FALSE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5165 | } |
| 5166 | |
| 5167 | void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz) |
| 5168 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5169 | set_device_unlock(UNLOCK_CRITICAL, TRUE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5170 | } |
| 5171 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 5172 | void cmd_preflash(const char *arg, void *data, unsigned sz) |
| 5173 | { |
| 5174 | fastboot_okay(""); |
| 5175 | } |
| 5176 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5177 | static uint8_t logo_header[LOGO_IMG_HEADER_SIZE]; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5178 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5179 | int splash_screen_check_header(logo_img_header *header) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5180 | { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5181 | if (memcmp(header->magic, LOGO_IMG_MAGIC, 8)) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5182 | return -1; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5183 | if (header->width == 0 || header->height == 0) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5184 | return -1; |
Ashish Bhimanpalliwar | 36f9f00 | 2021-04-14 13:50:52 +0530 | [diff] [blame] | 5185 | if (((header->blocks == 0) || (header->blocks > UINT_MAX/512))) { |
Ashish Bhimanpalliwar | eab60c6 | 2020-10-09 16:31:11 +0530 | [diff] [blame] | 5186 | return -1; |
| 5187 | } |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5188 | return 0; |
| 5189 | } |
| 5190 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5191 | int splash_screen_flash() |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5192 | { |
| 5193 | struct ptentry *ptn; |
| 5194 | struct ptable *ptable; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5195 | struct logo_img_header *header; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5196 | struct fbcon_config *fb_display = NULL; |
Channagoud Kadabi | b3ccf5c | 2014-12-03 12:39:29 -0800 | [diff] [blame] | 5197 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5198 | ptable = flash_get_ptable(); |
| 5199 | if (ptable == NULL) { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5200 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 5201 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5202 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5203 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5204 | ptn = ptable_find(ptable, "splash"); |
| 5205 | if (ptn == NULL) { |
| 5206 | dprintf(CRITICAL, "ERROR: splash Partition not found\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5207 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5208 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5209 | if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5210 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5211 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5212 | } |
| 5213 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5214 | header = (struct logo_img_header *)logo_header; |
| 5215 | if (splash_screen_check_header(header)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5216 | dprintf(CRITICAL, "ERROR: Boot image header invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5217 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | fb_display = fbcon_display(); |
| 5221 | if (fb_display) { |
Ashish Bhimanpalliwar | eab60c6 | 2020-10-09 16:31:11 +0530 | [diff] [blame] | 5222 | if (header->type && |
| 5223 | ((header->blocks * 512) <= (fb_display->width * |
| 5224 | fb_display->height * (fb_display->bpp / 8)))) { |
raghavendra ambadas | 554f4fd | 2018-10-09 15:50:40 +0530 | [diff] [blame] | 5225 | /* RLE24 compressed data */ |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5226 | uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET; |
| 5227 | |
| 5228 | /* if the logo is full-screen size, remove "fbcon_clear()" */ |
| 5229 | if ((header->width != fb_display->width) |
| 5230 | || (header->height != fb_display->height)) |
| 5231 | fbcon_clear(); |
| 5232 | |
| 5233 | if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0, |
| 5234 | (uint32_t *)base, |
| 5235 | (header->blocks * 512))) { |
| 5236 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 5237 | return -1; |
| 5238 | } |
| 5239 | fbcon_extract_to_screen(header, base); |
| 5240 | return 0; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5241 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5242 | |
Veera Sundaram Sankaran | c33416f | 2015-09-28 10:25:55 -0700 | [diff] [blame] | 5243 | if ((header->width > fb_display->width) || (header->height > fb_display->height)) { |
| 5244 | 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] | 5245 | return -1; |
| 5246 | } |
| 5247 | |
Vineet Bajaj | 99291ed | 2014-09-09 12:29:46 +0530 | [diff] [blame] | 5248 | uint8_t *base = (uint8_t *) fb_display->base; |
Sachin Bhayare | 619e9e4 | 2017-05-15 13:10:31 +0530 | [diff] [blame] | 5249 | uint32_t fb_size = ROUNDUP(fb_display->width * |
| 5250 | fb_display->height * |
| 5251 | (fb_display->bpp / 8), 4096); |
| 5252 | uint32_t splash_size = ((((header->width * header->height * |
| 5253 | fb_display->bpp/8) + 511) >> 9) << 9); |
| 5254 | |
Ashish Bhimanpalliwar | 36f9f00 | 2021-04-14 13:50:52 +0530 | [diff] [blame] | 5255 | if ((header->height * header->width * (fb_display->bpp/8)) > (header->blocks * 512)) { |
| 5256 | dprintf(CRITICAL, "ERROR: Splash image size invalid\n"); |
| 5257 | return -1; |
| 5258 | } |
| 5259 | |
Sachin Bhayare | 619e9e4 | 2017-05-15 13:10:31 +0530 | [diff] [blame] | 5260 | if (splash_size > fb_size) { |
| 5261 | dprintf(CRITICAL, "ERROR: Splash image size invalid\n"); |
| 5262 | return -1; |
| 5263 | } |
| 5264 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5265 | if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0, |
| 5266 | (uint32_t *)base, |
| 5267 | ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5268 | fbcon_clear(); |
Vineet Bajaj | 99291ed | 2014-09-09 12:29:46 +0530 | [diff] [blame] | 5269 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5270 | return -1; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5271 | } |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5272 | } |
| 5273 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5274 | return 0; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5275 | } |
| 5276 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5277 | int splash_screen_mmc() |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5278 | { |
| 5279 | int index = INVALID_PTN; |
| 5280 | unsigned long long ptn = 0; |
| 5281 | struct fbcon_config *fb_display = NULL; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5282 | struct logo_img_header *header; |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5283 | uint32_t blocksize, realsize, readsize; |
| 5284 | uint8_t *base; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5285 | |
| 5286 | index = partition_get_index("splash"); |
| 5287 | if (index == 0) { |
| 5288 | dprintf(CRITICAL, "ERROR: splash Partition table not found\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5289 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5290 | } |
| 5291 | |
| 5292 | ptn = partition_get_offset(index); |
| 5293 | if (ptn == 0) { |
| 5294 | dprintf(CRITICAL, "ERROR: splash Partition invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5295 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5296 | } |
| 5297 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5298 | mmc_set_lun(partition_get_lun(index)); |
| 5299 | |
| 5300 | blocksize = mmc_get_device_blocksize(); |
| 5301 | if (blocksize == 0) { |
| 5302 | dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n"); |
| 5303 | return -1; |
| 5304 | } |
| 5305 | |
| 5306 | fb_display = fbcon_display(); |
Channagoud Kadabi | daf10a6 | 2015-07-22 11:51:55 -0700 | [diff] [blame] | 5307 | if (!fb_display) |
| 5308 | { |
| 5309 | dprintf(CRITICAL, "ERROR: fb config is not allocated\n"); |
| 5310 | return -1; |
| 5311 | } |
| 5312 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5313 | base = (uint8_t *) fb_display->base; |
| 5314 | |
jialongjhan | 2769d20 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5315 | if (mmc_read(ptn , (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5316 | dprintf(CRITICAL, "ERROR: Cannot read splash image header\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5317 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5318 | } |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5319 | header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5320 | if (splash_screen_check_header(header)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5321 | dprintf(CRITICAL, "ERROR: Splash image header invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5322 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5323 | } |
| 5324 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5325 | if (fb_display) { |
Ashish Bhimanpalliwar | eab60c6 | 2020-10-09 16:31:11 +0530 | [diff] [blame] | 5326 | if (header->type && |
Ashish Bhimanpalliwar | ce39f48 | 2020-10-06 15:27:37 +0530 | [diff] [blame] | 5327 | (((UINT_MAX - LOGO_IMG_HEADER_SIZE) / 512) >= header->blocks) && |
raghavendra ambadas | 554f4fd | 2018-10-09 15:50:40 +0530 | [diff] [blame] | 5328 | ((header->blocks * 512) <= (fb_display->width * |
| 5329 | fb_display->height * (fb_display->bpp / 8)))) { |
| 5330 | /* 1 RLE24 compressed data */ |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5331 | base += LOGO_IMG_OFFSET; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5332 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5333 | realsize = header->blocks * 512; |
| 5334 | readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize; |
| 5335 | |
| 5336 | /* if the logo is not full-screen size, clean screen */ |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5337 | if ((header->width != fb_display->width) |
| 5338 | || (header->height != fb_display->height)) |
| 5339 | fbcon_clear(); |
| 5340 | |
Sachin Bhayare | 619e9e4 | 2017-05-15 13:10:31 +0530 | [diff] [blame] | 5341 | uint32_t fb_size = ROUNDUP(fb_display->width * |
| 5342 | fb_display->height * |
| 5343 | (fb_display->bpp / 8), 4096); |
| 5344 | |
| 5345 | if (readsize > fb_size) { |
| 5346 | dprintf(CRITICAL, "ERROR: Splash image size invalid\n"); |
| 5347 | return -1; |
| 5348 | } |
| 5349 | |
jialongjhan | 2769d20 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5350 | if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5351 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 5352 | return -1; |
| 5353 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5354 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5355 | fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE)); |
| 5356 | } else { /* 2 Raw BGR data */ |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5357 | |
Veera Sundaram Sankaran | c33416f | 2015-09-28 10:25:55 -0700 | [diff] [blame] | 5358 | if ((header->width > fb_display->width) || (header->height > fb_display->height)) { |
| 5359 | 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] | 5360 | return -1; |
| 5361 | } |
| 5362 | |
| 5363 | realsize = header->width * header->height * fb_display->bpp / 8; |
| 5364 | readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize; |
Ashish Bhimanpalliwar | 36f9f00 | 2021-04-14 13:50:52 +0530 | [diff] [blame] | 5365 | if (realsize > (header->blocks * 512)) { |
| 5366 | dprintf(CRITICAL, "Logo Size error\n"); |
| 5367 | return -1; |
| 5368 | } |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5369 | |
| 5370 | if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */ |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 5371 | 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] | 5372 | fbcon_clear(); |
| 5373 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 5374 | return -1; |
| 5375 | } |
| 5376 | } else { |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 5377 | if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize , |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5378 | (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) { |
| 5379 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 5380 | return -1; |
| 5381 | } |
| 5382 | memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize); |
| 5383 | } |
| 5384 | } |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5385 | } |
| 5386 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5387 | return 0; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5388 | } |
| 5389 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5390 | int fetch_image_from_partition() |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5391 | { |
| 5392 | if (target_is_emmc_boot()) { |
| 5393 | return splash_screen_mmc(); |
| 5394 | } else { |
| 5395 | return splash_screen_flash(); |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5396 | } |
| 5397 | } |
| 5398 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5399 | void publish_getvar_multislot_vars() |
| 5400 | { |
| 5401 | int i,count; |
| 5402 | static bool published = false; |
| 5403 | static char slot_count[MAX_RSP_SIZE]; |
| 5404 | static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS]; |
| 5405 | static char active_slot_suffix[MAX_RSP_SIZE]; |
| 5406 | static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE]; |
| 5407 | static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE]; |
| 5408 | const char *tmp; |
| 5409 | char tmpbuff[MAX_GET_VAR_NAME_SIZE]; |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5410 | signed active_slt; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5411 | |
| 5412 | if (!published) |
| 5413 | { |
| 5414 | /* Update slot meta info */ |
| 5415 | count = partition_fill_partition_meta(has_slot_pname, has_slot_reply, |
| 5416 | partition_get_partition_count()); |
| 5417 | for(i=0; i<count; i++) |
| 5418 | { |
| 5419 | memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE); |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5420 | snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s", |
| 5421 | has_slot_pname[i]); |
| 5422 | strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5423 | fastboot_publish(has_slot_pname[i], has_slot_reply[i]); |
| 5424 | } |
| 5425 | |
| 5426 | for (i=0; i<AB_SUPPORTED_SLOTS; i++) |
| 5427 | { |
| 5428 | tmp = SUFFIX_SLOT(i); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5429 | tmp++; // to remove "_" from slot_suffix. |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5430 | snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable), |
| 5431 | "slot-unbootable:%s", tmp); |
| 5432 | snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active), |
| 5433 | "slot-active:%s", tmp); |
| 5434 | snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful), |
| 5435 | "slot-success:%s", tmp); |
| 5436 | snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count), |
| 5437 | "slot-retry-count:%s", tmp); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5438 | fastboot_publish(slot_info[i].slot_is_unbootable, |
| 5439 | slot_info[i].slot_is_unbootable_rsp); |
| 5440 | fastboot_publish(slot_info[i].slot_is_active, |
| 5441 | slot_info[i].slot_is_active_rsp); |
| 5442 | fastboot_publish(slot_info[i].slot_is_succesful, |
| 5443 | slot_info[i].slot_is_succesful_rsp); |
| 5444 | fastboot_publish(slot_info[i].slot_retry_count, |
| 5445 | slot_info[i].slot_retry_count_rsp); |
| 5446 | } |
| 5447 | fastboot_publish("current-slot", active_slot_suffix); |
| 5448 | snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS); |
| 5449 | fastboot_publish("slot-count", slot_count); |
| 5450 | published = true; |
| 5451 | } |
| 5452 | |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5453 | active_slt = partition_find_active_slot(); |
| 5454 | if (active_slt != INVALID) |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5455 | { |
| 5456 | tmp = SUFFIX_SLOT(active_slt); |
| 5457 | tmp++; // to remove "_" from slot_suffix. |
| 5458 | snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp); |
| 5459 | } |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5460 | else |
| 5461 | strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix)); |
| 5462 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5463 | /* Update partition meta information */ |
| 5464 | partition_fill_slot_meta(slot_info); |
| 5465 | return; |
| 5466 | } |
| 5467 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5468 | void get_product_name(unsigned char *buf) |
| 5469 | { |
| 5470 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD)); |
| 5471 | return; |
| 5472 | } |
| 5473 | |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5474 | #if PRODUCT_IOT |
| 5475 | void get_bootloader_version_iot(unsigned char *buf) |
| 5476 | { |
| 5477 | if (buf != NULL) |
| 5478 | { |
| 5479 | strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN); |
| 5480 | strlcat(buf, "-", MAX_VERSION_LEN); |
| 5481 | strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN); |
| 5482 | } |
| 5483 | return; |
| 5484 | } |
| 5485 | #endif |
| 5486 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5487 | void get_bootloader_version(unsigned char *buf) |
| 5488 | { |
jhchen | a8a15dd | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 5489 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5490 | return; |
| 5491 | } |
| 5492 | |
| 5493 | void get_baseband_version(unsigned char *buf) |
| 5494 | { |
jhchen | a8a15dd | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 5495 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5496 | return; |
| 5497 | } |
| 5498 | |
Monika Singh | 32a09f7 | 2018-03-14 13:08:29 +0530 | [diff] [blame] | 5499 | bool is_device_locked_critical() |
| 5500 | { |
| 5501 | return device.is_unlock_critical ? false:true; |
| 5502 | } |
| 5503 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5504 | bool is_device_locked() |
| 5505 | { |
| 5506 | return device.is_unlocked ? false:true; |
| 5507 | } |
| 5508 | |
Monika Singh | 32a09f7 | 2018-03-14 13:08:29 +0530 | [diff] [blame] | 5509 | bool is_verity_enforcing() |
| 5510 | { |
| 5511 | return device.verity_mode ? true:false; |
| 5512 | } |
| 5513 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5514 | /* register commands and variables for fastboot */ |
| 5515 | void aboot_fastboot_register_commands(void) |
| 5516 | { |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5517 | int i; |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 5518 | char hw_platform_buf[MAX_RSP_SIZE]; |
Rahul Shahare | e7baede | 2021-02-02 13:23:57 +0530 | [diff] [blame] | 5519 | VirtualAbMergeStatus SnapshotMergeStatus; |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5520 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5521 | struct fastboot_cmd_desc cmd_list[] = { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5522 | /* By default the enabled list is empty. */ |
| 5523 | {"", NULL}, |
| 5524 | /* move commands enclosed within the below ifndef to here |
| 5525 | * if they need to be enabled in user build. |
| 5526 | */ |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5527 | #ifndef DISABLE_FASTBOOT_CMDS |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5528 | /* Register the following commands only for non-user builds */ |
| 5529 | {"flash:", cmd_flash}, |
| 5530 | {"erase:", cmd_erase}, |
| 5531 | {"boot", cmd_boot}, |
| 5532 | {"continue", cmd_continue}, |
| 5533 | {"reboot", cmd_reboot}, |
| 5534 | {"reboot-bootloader", cmd_reboot_bootloader}, |
| 5535 | {"oem unlock", cmd_oem_unlock}, |
| 5536 | {"oem unlock-go", cmd_oem_unlock_go}, |
| 5537 | {"oem lock", cmd_oem_lock}, |
| 5538 | {"flashing unlock", cmd_oem_unlock}, |
| 5539 | {"flashing lock", cmd_oem_lock}, |
| 5540 | {"flashing lock_critical", cmd_flashing_lock_critical}, |
| 5541 | {"flashing unlock_critical", cmd_flashing_unlock_critical}, |
| 5542 | {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability}, |
| 5543 | {"oem device-info", cmd_oem_devinfo}, |
| 5544 | {"preflash", cmd_preflash}, |
| 5545 | {"oem enable-charger-screen", cmd_oem_enable_charger_screen}, |
| 5546 | {"oem disable-charger-screen", cmd_oem_disable_charger_screen}, |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5547 | {"oem off-mode-charge", cmd_oem_off_mode_charger}, |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5548 | {"oem select-display-panel", cmd_oem_select_display_panel}, |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 5549 | {"set_active",cmd_set_active}, |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 5550 | #if DYNAMIC_PARTITION_SUPPORT |
| 5551 | {"reboot-fastboot",cmd_reboot_fastboot}, |
| 5552 | {"reboot-recovery",cmd_reboot_recovery}, |
| 5553 | #endif |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 5554 | #ifdef VIRTUAL_AB_OTA |
| 5555 | {"snapshot-update", CmdUpdateSnapshot}, |
| 5556 | #endif |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 5557 | #if UNITTEST_FW_SUPPORT |
Channagoud Kadabi | d5ddf48 | 2015-09-28 10:31:35 -0700 | [diff] [blame] | 5558 | {"oem run-tests", cmd_oem_runtests}, |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 5559 | #endif |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5560 | #endif |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5561 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5562 | {"oem adb_enable", CmdOemEnableAdb}, |
| 5563 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5564 | }; |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5565 | |
| 5566 | int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]); |
| 5567 | for (i = 1; i < fastboot_cmds_count; i++) |
| 5568 | fastboot_register(cmd_list[i].name,cmd_list[i].cb); |
| 5569 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5570 | /* publish variables and their values */ |
| 5571 | fastboot_publish("product", TARGET(BOARD)); |
| 5572 | fastboot_publish("kernel", "lk"); |
| 5573 | fastboot_publish("serialno", sn_buf); |
| 5574 | |
Mayank Grover | a64dfbe | 2018-05-17 14:06:34 +0530 | [diff] [blame] | 5575 | /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/ |
| 5576 | snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version()); |
| 5577 | fastboot_publish("hw-revision", soc_version_str); |
| 5578 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5579 | /* |
| 5580 | * partition info is supported only for emmc partitions |
| 5581 | * Calling this for NAND prints some error messages which |
| 5582 | * is harmless but misleading. Avoid calling this for NAND |
| 5583 | * devices. |
| 5584 | */ |
| 5585 | if (target_is_emmc_boot()) |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5586 | publish_getvar_partition_info(part_info, partition_get_partition_count()); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5587 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5588 | if (partition_multislot_is_supported()) |
| 5589 | publish_getvar_multislot_vars(); |
| 5590 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5591 | /* Max download size supported */ |
lijuang | 2fbed91 | 2019-07-12 14:15:05 +0800 | [diff] [blame] | 5592 | #if !VERIFIED_BOOT_2 |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5593 | snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", |
| 5594 | target_get_max_flash_size()); |
lijuang | 2fbed91 | 2019-07-12 14:15:05 +0800 | [diff] [blame] | 5595 | #else |
| 5596 | snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", |
| 5597 | SUB_SALT_BUFF_OFFSET(target_get_max_flash_size())); |
| 5598 | #endif |
| 5599 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5600 | fastboot_publish("max-download-size", (const char *) max_download_size); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5601 | /* Is the charger screen check enabled */ |
| 5602 | snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d", |
| 5603 | device.charger_screen_enabled); |
| 5604 | fastboot_publish("charger-screen-enabled", |
| 5605 | (const char *) charger_screen_enabled); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5606 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5607 | read_prod_info(&prod); |
| 5608 | snprintf(AdbEnable, MAX_RSP_SIZE, "%d", |
| 5609 | prod.is_adb_enabled); |
| 5610 | fastboot_publish("adb-enabled", |
| 5611 | (const char *) AdbEnable); |
| 5612 | #endif |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5613 | fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled); |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 5614 | snprintf(panel_display_mode, MAX_RSP_SIZE, "%s", |
| 5615 | device.display_panel); |
| 5616 | fastboot_publish("display-panel", |
| 5617 | (const char *) panel_display_mode); |
Mukesh Ojha | bb6e7f3 | 2018-03-02 15:04:06 +0530 | [diff] [blame] | 5618 | |
| 5619 | if (target_is_emmc_boot()) |
| 5620 | { |
| 5621 | mmc_blocksize = mmc_get_device_blocksize(); |
| 5622 | } |
| 5623 | else |
| 5624 | { |
| 5625 | mmc_blocksize = flash_block_size(); |
| 5626 | } |
| 5627 | snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize); |
| 5628 | fastboot_publish("erase-block-size", (const char *) block_size_string); |
| 5629 | fastboot_publish("logical-block-size", (const char *) block_size_string); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5630 | #if PRODUCT_IOT |
| 5631 | get_bootloader_version_iot(&bootloader_version_string); |
| 5632 | fastboot_publish("version-bootloader", (const char *) bootloader_version_string); |
| 5633 | |
| 5634 | /* Version baseband is n/a for apq iot devices */ |
| 5635 | fastboot_publish("version-baseband", "N/A"); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5636 | #else |
Channagoud Kadabi | aafbbfc | 2015-03-04 17:52:56 -0800 | [diff] [blame] | 5637 | fastboot_publish("version-bootloader", (const char *) device.bootloader_version); |
| 5638 | fastboot_publish("version-baseband", (const char *) device.radio_version); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5639 | #endif |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 5640 | fastboot_publish("secure", is_secure_boot_enable()? "yes":"no"); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5641 | fastboot_publish("unlocked", device.is_unlocked ? "yes":"no"); |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 5642 | smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf)); |
| 5643 | snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf, |
| 5644 | target_is_emmc_boot()? "eMMC":"UFS"); |
| 5645 | fastboot_publish("variant", (const char *) get_variant); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 5646 | #if CHECK_BAT_VOLTAGE |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 5647 | update_battery_status(); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 5648 | fastboot_publish("battery-voltage", (const char *) battery_voltage); |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 5649 | fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 5650 | #endif |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 5651 | if (target_dynamic_partition_supported()) |
| 5652 | fastboot_publish("is-userspace", "no"); |
Rahul Shahare | e7baede | 2021-02-02 13:23:57 +0530 | [diff] [blame] | 5653 | |
| 5654 | if (target_virtual_ab_supported()) { |
| 5655 | SnapshotMergeStatus = GetSnapshotMergeStatus (); |
| 5656 | |
| 5657 | switch (SnapshotMergeStatus) { |
| 5658 | case SNAPSHOTTED: |
| 5659 | SnapshotMergeStatus = SNAPSHOTTED; |
| 5660 | break; |
| 5661 | case MERGING: |
| 5662 | SnapshotMergeStatus = MERGING; |
| 5663 | break; |
| 5664 | default: |
| 5665 | SnapshotMergeStatus = NONE_MERGE_STATUS; |
| 5666 | break; |
| 5667 | } |
| 5668 | |
| 5669 | snprintf(SnapshotMergeState, |
| 5670 | strlen(VabSnapshotMergeStatus[SnapshotMergeStatus]) + 1, |
| 5671 | "%s", VabSnapshotMergeStatus[SnapshotMergeStatus]); |
| 5672 | fastboot_publish("snapshot-update-state", SnapshotMergeState); |
| 5673 | } |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5674 | } |
| 5675 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 5676 | void aboot_init(const struct app_descriptor *app) |
| 5677 | { |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 5678 | unsigned reboot_mode = 0; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5679 | int boot_err_type = 0; |
| 5680 | int boot_slot = INVALID; |
jessicatseng | 43b1e35 | 2020-05-26 13:17:56 +0800 | [diff] [blame] | 5681 | int vbat = 0; |
| 5682 | char boot_vbat[MAX_RSP_SIZE]; |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 5683 | |
lijuang | 3983173 | 2016-01-08 17:49:02 +0800 | [diff] [blame] | 5684 | /* Initialise wdog to catch early lk crashes */ |
| 5685 | #if WDOG_SUPPORT |
| 5686 | msm_wdog_init(); |
| 5687 | #endif |
| 5688 | |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 5689 | /* Setup page size information for nv storage */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5690 | if (target_is_emmc_boot()) |
| 5691 | { |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 5692 | page_size = mmc_page_size(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5693 | page_mask = page_size - 1; |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 5694 | mmc_blocksize = mmc_get_device_blocksize(); |
| 5695 | mmc_blocksize_mask = mmc_blocksize - 1; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5696 | } |
| 5697 | else |
| 5698 | { |
| 5699 | page_size = flash_page_size(); |
| 5700 | page_mask = page_size - 1; |
| 5701 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 5702 | ASSERT((MEMBASE + MEMSIZE) > MEMBASE); |
| 5703 | |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5704 | read_device_info(&device); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5705 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5706 | read_prod_info(&prod); |
| 5707 | #endif |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5708 | read_allow_oem_unlock(&device); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 5709 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5710 | /* Detect multi-slot support */ |
| 5711 | if (partition_multislot_is_supported()) |
| 5712 | { |
| 5713 | boot_slot = partition_find_active_slot(); |
| 5714 | if (boot_slot == INVALID) |
| 5715 | { |
| 5716 | boot_into_fastboot = true; |
| 5717 | dprintf(INFO, "Active Slot: (INVALID)\n"); |
| 5718 | } |
| 5719 | else |
| 5720 | { |
| 5721 | /* Setting the state of system to boot active slot */ |
| 5722 | partition_mark_active_slot(boot_slot); |
| 5723 | dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot)); |
| 5724 | } |
| 5725 | } |
| 5726 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 5727 | target_serialno((unsigned char *) sn_buf); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5728 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5729 | dprintf(CRITICAL,"serial number: %s\n",sn_buf); |
| 5730 | #else |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 5731 | dprintf(SPEW,"serial number: %s\n",sn_buf); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5732 | #endif |
| 5733 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5734 | read_prod_info(&prod); |
| 5735 | snprintf((char *)cust_sn_buf, PRODINFO_MAX_SSN_LEN + 1, "%s", prod.ssn); |
| 5736 | dprintf(CRITICAL,"customer serial number: %s\n", cust_sn_buf); |
| 5737 | snprintf((char *)factory_sn_buf, PRODINFO_MAX_ISN_LEN + 1, "%s", prod.isn); |
| 5738 | dprintf(CRITICAL,"factory serial number: %s\n", factory_sn_buf); |
| 5739 | #endif |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 5740 | memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE); |
| 5741 | |
Matthew Qin | defd556 | 2014-07-11 18:02:40 +0800 | [diff] [blame] | 5742 | /* |
| 5743 | * Check power off reason if user force reset, |
| 5744 | * if yes phone will do normal boot. |
| 5745 | */ |
| 5746 | if (is_user_force_reset()) |
| 5747 | goto normal_boot; |
| 5748 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5749 | /* Check if we should do something other than booting up */ |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 5750 | if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN)) |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 5751 | { |
Pavel Nedev | a4c9d3a | 2013-05-15 14:42:34 +0300 | [diff] [blame] | 5752 | dprintf(ALWAYS,"dload mode key sequence detected\n"); |
lijuang | 395b5e6 | 2015-11-19 17:39:44 +0800 | [diff] [blame] | 5753 | reboot_device(EMERGENCY_DLOAD); |
| 5754 | dprintf(CRITICAL,"Failed to reboot into dload mode\n"); |
| 5755 | |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 5756 | boot_into_fastboot = true; |
| 5757 | } |
| 5758 | if (!boot_into_fastboot) |
| 5759 | { |
| 5760 | if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP)) |
| 5761 | boot_into_recovery = 1; |
| 5762 | if (!boot_into_recovery && |
| 5763 | (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN))) |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5764 | boot_into_fastboot = true; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 5765 | } |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5766 | #if NO_KEYPAD_DRIVER |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 5767 | if (fastboot_trigger()) |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5768 | boot_into_fastboot = true; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5769 | #endif |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 5770 | |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5771 | #if USE_PON_REBOOT_REG |
| 5772 | reboot_mode = check_hard_reboot_mode(); |
| 5773 | #else |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 5774 | reboot_mode = check_reboot_mode(); |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5775 | #endif |
| 5776 | if (reboot_mode == RECOVERY_MODE) |
| 5777 | { |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 5778 | boot_into_recovery = 1; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5779 | } |
| 5780 | else if(reboot_mode == FASTBOOT_MODE) |
| 5781 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5782 | boot_into_fastboot = true; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5783 | } |
| 5784 | else if(reboot_mode == ALARM_BOOT) |
| 5785 | { |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 5786 | boot_reason_alarm = true; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5787 | } |
Monika Singh | b0db4b8 | 2018-09-26 12:18:02 +0530 | [diff] [blame] | 5788 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 5789 | else if (VB_M <= target_get_vb_version()) |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5790 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5791 | if (reboot_mode == DM_VERITY_ENFORCING) |
| 5792 | { |
| 5793 | device.verity_mode = 1; |
| 5794 | write_device_info(&device); |
| 5795 | } |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 5796 | #if ENABLE_VB_ATTEST |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5797 | else if (reboot_mode == DM_VERITY_EIO) |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 5798 | #else |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5799 | else if (reboot_mode == DM_VERITY_LOGGING) |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 5800 | #endif |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5801 | { |
| 5802 | device.verity_mode = 0; |
| 5803 | write_device_info(&device); |
| 5804 | } |
| 5805 | else if (reboot_mode == DM_VERITY_KEYSCLEAR) |
| 5806 | { |
| 5807 | if(send_delete_keys_to_tz()) |
| 5808 | ASSERT(0); |
| 5809 | } |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5810 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 5811 | #endif |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 5812 | |
jialongjhan | 2769d20 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5813 | /* Display splash screen if enabled */ |
| 5814 | #if DISPLAY_SPLASH_SCREEN |
| 5815 | #if NO_ALARM_DISPLAY |
| 5816 | if (!check_alarm_boot()) { |
| 5817 | #endif |
| 5818 | dprintf(SPEW, "Display Init: Start\n"); |
| 5819 | #if DISPLAY_HDMI_PRIMARY |
| 5820 | if (!strlen(device.display_panel)) |
| 5821 | strlcpy(device.display_panel, DISPLAY_PANEL_HDMI, |
| 5822 | sizeof(device.display_panel)); |
| 5823 | #endif |
| 5824 | #if ENABLE_WBC |
| 5825 | /* Wait if the display shutdown is in progress */ |
| 5826 | while(pm_app_display_shutdown_in_prgs()); |
| 5827 | if (!pm_appsbl_display_init_done()) |
| 5828 | target_display_init(device.display_panel); |
| 5829 | else |
| 5830 | display_image_on_screen(); |
| 5831 | #else |
| 5832 | target_display_init(device.display_panel); |
| 5833 | #endif |
| 5834 | dprintf(SPEW, "Display Init: Done\n"); |
| 5835 | #if NO_ALARM_DISPLAY |
| 5836 | } |
| 5837 | #endif |
| 5838 | #endif |
| 5839 | |
| 5840 | |
Matthew Qin | defd556 | 2014-07-11 18:02:40 +0800 | [diff] [blame] | 5841 | normal_boot: |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5842 | if (!boot_into_fastboot) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5843 | { |
jessicatseng | 43b1e35 | 2020-05-26 13:17:56 +0800 | [diff] [blame] | 5844 | if(!target_pause_for_battery_charge()) |
| 5845 | { |
| 5846 | vbat = target_get_battery_voltage(); |
| 5847 | snprintf(boot_vbat, MAX_RSP_SIZE, "%d", vbat); |
| 5848 | dprintf(CRITICAL,"battery_voltage: %s\n", boot_vbat); |
| 5849 | if(vbat < 3500000) |
| 5850 | { |
| 5851 | display_lowbattery_image_on_screen(); |
| 5852 | //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 Start |
| 5853 | msm_display_flush(); |
| 5854 | //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 End |
| 5855 | dprintf(CRITICAL,"Low battery, cannot boot up...\n"); |
| 5856 | mdelay(3000); |
| 5857 | shutdown_device(); |
| 5858 | } |
| 5859 | } |
| 5860 | |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5861 | if (target_is_emmc_boot()) |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5862 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5863 | if(emmc_recovery_init()) |
| 5864 | dprintf(ALWAYS,"error in emmc_recovery_init\n"); |
| 5865 | if(target_use_signed_kernel()) |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5866 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5867 | if((device.is_unlocked) || (device.is_tampered)) |
| 5868 | { |
| 5869 | #ifdef TZ_TAMPER_FUSE |
Monika Singh | 9825746 | 2018-06-06 11:28:49 +0530 | [diff] [blame] | 5870 | set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE); |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5871 | #endif |
| 5872 | #if USE_PCOM_SECBOOT |
| 5873 | set_tamper_flag(device.is_tampered); |
| 5874 | #endif |
| 5875 | } |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5876 | } |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 5877 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5878 | retry_boot: |
| 5879 | /* Trying to boot active partition */ |
| 5880 | if (partition_multislot_is_supported()) |
| 5881 | { |
| 5882 | boot_slot = partition_find_boot_slot(); |
| 5883 | partition_mark_active_slot(boot_slot); |
| 5884 | if (boot_slot == INVALID) |
| 5885 | goto fastboot; |
| 5886 | } |
| 5887 | |
| 5888 | boot_err_type = boot_linux_from_mmc(); |
| 5889 | switch (boot_err_type) |
| 5890 | { |
| 5891 | case ERR_INVALID_PAGE_SIZE: |
| 5892 | case ERR_DT_PARSE: |
| 5893 | case ERR_ABOOT_ADDR_OVERLAP: |
Mayank Grover | 10cabbe | 2017-10-30 19:11:05 +0530 | [diff] [blame] | 5894 | case ERR_INVALID_BOOT_MAGIC: |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5895 | if(partition_multislot_is_supported()) |
Mayank Grover | 10cabbe | 2017-10-30 19:11:05 +0530 | [diff] [blame] | 5896 | { |
| 5897 | /* |
| 5898 | * Deactivate current slot, as it failed to |
| 5899 | * boot, and retry next slot. |
| 5900 | */ |
| 5901 | partition_deactivate_slot(boot_slot); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5902 | goto retry_boot; |
Mayank Grover | 10cabbe | 2017-10-30 19:11:05 +0530 | [diff] [blame] | 5903 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5904 | else |
| 5905 | break; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5906 | default: |
| 5907 | break; |
| 5908 | /* going to fastboot menu */ |
| 5909 | } |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5910 | } |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5911 | else |
| 5912 | { |
| 5913 | recovery_init(); |
| 5914 | #if USE_PCOM_SECBOOT |
| 5915 | if((device.is_unlocked) || (device.is_tampered)) |
| 5916 | set_tamper_flag(device.is_tampered); |
| 5917 | #endif |
| 5918 | boot_linux_from_flash(); |
| 5919 | } |
| 5920 | dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting " |
| 5921 | "to fastboot mode.\n"); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5922 | } |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5923 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5924 | fastboot: |
jialongjhan | 2769d20 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5925 | |
| 5926 | mdelay(1000); |
| 5927 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5928 | /* We are here means regular boot did not happen. Start fastboot. */ |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 5929 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5930 | /* register aboot specific fastboot commands */ |
| 5931 | aboot_fastboot_register_commands(); |
Amol Jadi | 57abe4c | 2011-05-24 15:47:27 -0700 | [diff] [blame] | 5932 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5933 | /* dump partition table for debug info */ |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 5934 | partition_dump(); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5935 | |
| 5936 | /* initialize and start fastboot */ |
Saranya Chidura | 41b3453 | 2018-10-16 12:29:52 +0530 | [diff] [blame] | 5937 | #if !VERIFIED_BOOT_2 |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5938 | fastboot_init(target_get_scratch_address(), target_get_max_flash_size()); |
Saranya Chidura | 41b3453 | 2018-10-16 12:29:52 +0530 | [diff] [blame] | 5939 | #else |
| 5940 | /* Add salt buffer offset at start of image address to copy VB salt */ |
| 5941 | fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()), |
| 5942 | SUB_SALT_BUFF_OFFSET(target_get_max_flash_size())); |
| 5943 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5944 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 5945 | display_fastboot_menu(); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5946 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 5947 | } |
| 5948 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 5949 | uint32_t get_page_size() |
| 5950 | { |
| 5951 | return page_size; |
| 5952 | } |
| 5953 | |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 5954 | /* |
| 5955 | * Calculated and save hash (SHA256) for non-signed boot image. |
| 5956 | * |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5957 | * @param image_addr - Boot image address |
| 5958 | * @param image_size - Size of the boot image |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 5959 | * |
| 5960 | * @return int - 0 on success, negative value on failure. |
| 5961 | */ |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5962 | 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] | 5963 | { |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5964 | unsigned int digest[8]; |
| 5965 | #if IMAGE_VERIF_ALGO_SHA1 |
| 5966 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1; |
| 5967 | #else |
| 5968 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256; |
| 5969 | #endif |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 5970 | |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5971 | target_crypto_init_params(); |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 5972 | 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] | 5973 | |
| 5974 | save_kernel_hash_cmd(digest); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5975 | 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] | 5976 | |
| 5977 | return 0; |
| 5978 | } |
| 5979 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5980 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 5981 | APP_START(aboot) |
| 5982 | .init = aboot_init, |
| 5983 | APP_END |