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" |
tracychui | 792d2ad | 2020-06-15 10:50:49 +0800 | [diff] [blame^] | 98 | #include <mmu.h> |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 99 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 100 | extern bool target_use_signed_kernel(void); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 101 | extern void platform_uninit(void); |
Channagoud Kadabi | 33defe2 | 2013-06-18 18:35:40 -0700 | [diff] [blame] | 102 | extern void target_uninit(void); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 103 | extern int get_target_boot_params(const char *cmdline, const char *part, |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 104 | char **buf); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 105 | |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 106 | void *info_buf; |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 107 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 108 | void *prodinfo_buf; |
| 109 | #endif |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 110 | void write_device_info_mmc(device_info *dev); |
| 111 | void write_device_info_flash(device_info *dev); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 112 | 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] | 113 | static int aboot_frp_unlock(char *pname, void *data, unsigned sz); |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 114 | static inline uint64_t validate_partition_size(); |
Parth Dixit | 54ac3bb | 2017-03-07 15:52:48 +0530 | [diff] [blame] | 115 | bool pwr_key_is_pressed = false; |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 116 | static bool is_systemd_present=false; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 117 | static void publish_getvar_multislot_vars(); |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 118 | /* fastboot command function pointer */ |
| 119 | typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 120 | bool get_perm_attr_status(); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 121 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 122 | void write_prod_info(prod_info *dev); |
| 123 | #endif |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 124 | |
| 125 | struct fastboot_cmd_desc { |
| 126 | char * name; |
| 127 | fastboot_cmd_fn cb; |
| 128 | }; |
| 129 | |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 130 | #define EXPAND(NAME) #NAME |
| 131 | #define TARGET(NAME) EXPAND(NAME) |
Brian Swetland | 2defe16 | 2009-08-18 14:35:59 -0700 | [diff] [blame] | 132 | |
Ajay Singh Parmar | a7865a8 | 2015-04-16 21:27:52 -0700 | [diff] [blame] | 133 | #define DISPLAY_PANEL_HDMI "hdmi" |
| 134 | |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 135 | #ifdef MEMBASE |
| 136 | #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE)) |
| 137 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 138 | #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000 |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 139 | #endif |
| 140 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 141 | #ifndef MEMSIZE |
| 142 | #define MEMSIZE 1024*1024 |
| 143 | #endif |
| 144 | |
| 145 | #define MAX_TAGS_SIZE 1024 |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 146 | #define PLL_CODES_OFFSET 4096 |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 147 | /* make 4096 as default size to ensure EFS,EXT4's erasing */ |
| 148 | #define DEFAULT_ERASE_SIZE 4096 |
Ujwal Patel | c0b0a25 | 2015-08-16 14:05:35 -0700 | [diff] [blame] | 149 | #define MAX_PANEL_BUF_SIZE 196 |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 150 | #define FOOTER_SIZE 16384 |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 151 | |
Dhaval Patel | f83d73b | 2014-06-23 16:24:37 -0700 | [diff] [blame] | 152 | #define DISPLAY_DEFAULT_PREFIX "mdss_mdp" |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 153 | #define BOOT_DEV_MAX_LEN 64 |
Sundarajan Srinivasan | 595b71e | 2013-11-05 12:44:34 -0800 | [diff] [blame] | 154 | |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 155 | #define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false |
| 156 | |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 157 | #define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX |
| 158 | |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 159 | //Size of the header that is used in case the boot image has |
| 160 | //a uncompressed kernel + appended dtb |
| 161 | #define PATCHED_KERNEL_HEADER_SIZE 20 |
| 162 | |
| 163 | //String used to determine if the boot image has |
| 164 | //a uncompressed kernel + appended dtb |
| 165 | #define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG" |
| 166 | |
Sridhar Parasuram | 7bd4aaf | 2015-02-12 11:14:38 -0800 | [diff] [blame] | 167 | #if USE_BOOTDEV_CMDLINE |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 168 | static const char *emmc_cmdline = " androidboot.bootdevice="; |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 169 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 170 | static const char *emmc_cmdline = " androidboot.emmc=true"; |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 171 | #endif |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 172 | static const char *dynamic_bootdev_cmdline = |
| 173 | " androidboot.boot_devices=soc/"; |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 174 | static const char *usb_sn_cmdline = " androidboot.serialno="; |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 175 | static const char *androidboot_mode = " androidboot.mode="; |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 176 | |
| 177 | static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target"; |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 178 | static const char *alarmboot_cmdline = " androidboot.alarmboot=true"; |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 179 | static const char *loglevel = " quiet"; |
Ajay Dudani | ca3a33c | 2011-11-18 08:31:40 -0800 | [diff] [blame] | 180 | static const char *battchg_pause = " androidboot.mode=charger"; |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 181 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 182 | static const char *cust_sn_cmdline = " androidboot.customer_serialno="; |
| 183 | static const char *factory_sn_cmdline = " androidboot.factory_serialno="; |
| 184 | static const char *UsbAdbEnable = " androidboot.adb_enable=1"; |
| 185 | #endif |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 186 | static const char *auth_kernel = " androidboot.authorized_kernel=true"; |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 187 | static const char *secondary_gpt_enable = " gpt"; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 188 | #ifdef MDTP_SUPPORT |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 189 | static const char *mdtp_activated_flag = " mdtp"; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 190 | #endif |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 191 | static const char *baseband_apq = " androidboot.baseband=apq"; |
| 192 | static const char *baseband_msm = " androidboot.baseband=msm"; |
| 193 | static const char *baseband_csfb = " androidboot.baseband=csfb"; |
| 194 | static const char *baseband_svlte2a = " androidboot.baseband=svlte2a"; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 195 | static const char *baseband_mdm = " androidboot.baseband=mdm"; |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 196 | static const char *baseband_mdm2 = " androidboot.baseband=mdm2"; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 197 | static const char *baseband_sglte = " androidboot.baseband=sglte"; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 198 | static const char *baseband_dsda = " androidboot.baseband=dsda"; |
| 199 | static const char *baseband_dsda2 = " androidboot.baseband=dsda2"; |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 200 | static const char *baseband_sglte2 = " androidboot.baseband=sglte2"; |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 201 | static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1"; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 202 | static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr"; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 203 | static const char *androidboot_slot_suffix = " androidboot.slot_suffix="; |
| 204 | static const char *skip_ramfs = " skip_initramfs"; |
tracychui | 792d2ad | 2020-06-15 10:50:49 +0800 | [diff] [blame^] | 205 | static const char *memory_config_3G = " androidboot.memory_config=32GB,3GB"; |
| 206 | static const char *memory_config_4G = " androidboot.memory_config=64GB,4GB"; |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 207 | |
| 208 | #if HIBERNATION_SUPPORT |
| 209 | static const char *resume = " resume=/dev/mmcblk0p"; |
| 210 | #endif |
| 211 | |
Sourabh Banerjee | 6c7153c | 2018-03-20 01:21:57 +0530 | [diff] [blame] | 212 | #ifdef INIT_BIN_LE |
| 213 | static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE; |
| 214 | #else |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 215 | static const char *sys_path_cmdline = " rootwait ro init=/init"; |
Sourabh Banerjee | 6c7153c | 2018-03-20 01:21:57 +0530 | [diff] [blame] | 216 | #endif |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 217 | |
| 218 | #if VERITY_LE |
| 219 | static const char *verity_dev = " root=/dev/dm-0"; |
| 220 | static const char *verity_system_part = " dm=\"system"; |
| 221 | static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p"; |
| 222 | #else |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 223 | static const char *sys_path = " root=/dev/mmcblk0p"; |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 224 | |
| 225 | #define MAX_DTBO_IDX_STR 64 |
| 226 | static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx="; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 227 | |
| 228 | #define MAX_DTB_IDX_STR MAX_DTBO_IDX_STR |
| 229 | static const char *android_boot_dtb_idx = " androidboot.dtb_idx="; |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 230 | #endif |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 231 | |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 232 | #include <platform/gpio.h> |
| 233 | static const char *PCBA_STAGE_0 = " androidboot.pcbastage=EP0"; |
| 234 | static const char *PCBA_STAGE_1 = " androidboot.pcbastage=EP1"; |
| 235 | static const char *PCBA_STAGE_2 = " androidboot.pcbastage=EP2"; |
| 236 | static const char *PCBA_STAGE_3 = " androidboot.pcbastage=FP"; |
| 237 | static const char *PCBA_STAGE_4 = " androidboot.pcbastage=MP"; |
michaellin | af8857a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 238 | static const char *PCBA_STAGE_5 = " androidboot.pcbastage=MP-8903MB_001"; |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 239 | static const char *PCBA_STAGE_F = " androidboot.pcbastage=Reserved"; |
| 240 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 241 | #if VERIFIED_BOOT |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 242 | static const char *verity_mode = " androidboot.veritymode="; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 243 | static const char *verified_state= " androidboot.verifiedbootstate="; |
Parth Dixit | a5715a0 | 2015-10-29 12:25:10 +0530 | [diff] [blame] | 244 | static const char *keymaster_v1= " androidboot.keymaster=1"; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 245 | //indexed based on enum values, green is 0 by default |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 246 | |
| 247 | struct verified_boot_verity_mode vbvm[] = |
| 248 | { |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 249 | #if ENABLE_VB_ATTEST |
| 250 | {false, "eio"}, |
| 251 | #else |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 252 | {false, "logging"}, |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 253 | #endif |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 254 | {true, "enforcing"}, |
| 255 | }; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 256 | struct verified_boot_state_name vbsn[] = |
| 257 | { |
| 258 | {GREEN, "green"}, |
| 259 | {ORANGE, "orange"}, |
| 260 | {YELLOW,"yellow"}, |
| 261 | {RED,"red" }, |
| 262 | }; |
| 263 | #endif |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 264 | /*As per spec delay wait time before shutdown in Red state*/ |
| 265 | #define DELAY_WAIT 30000 |
Ashish Bhimanpalliwar | 8cb34fd | 2020-11-12 19:26:41 +0530 | [diff] [blame] | 266 | static unsigned page_size = BOARD_KERNEL_PAGESIZE; |
| 267 | |
| 268 | uint32_t kernel_hdr_page_size() |
| 269 | { |
| 270 | return page_size; |
| 271 | } |
| 272 | |
| 273 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 274 | static unsigned page_mask = 0; |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 275 | static unsigned mmc_blocksize = 0; |
| 276 | static unsigned mmc_blocksize_mask = 0; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 277 | static char ffbm_mode_string[FFBM_MODE_BUF_SIZE]; |
| 278 | static bool boot_into_ffbm; |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 279 | static char *target_boot_params = NULL; |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 280 | static bool boot_reason_alarm; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 281 | static bool devinfo_present = true; |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 282 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 283 | static bool prodinfo_present = true; |
| 284 | #endif |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 285 | bool boot_into_fastboot = false; |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 286 | static uint32_t dt_size = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 287 | static char *vbcmdline; |
| 288 | static bootinfo info = {0}; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 289 | static void *recovery_dtbo_buf = NULL; |
| 290 | static uint32_t recovery_dtbo_size = 0; |
| 291 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 292 | /* Assuming unauthorized kernel image by default */ |
| 293 | static int auth_kernel_img = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 294 | static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}}; |
| 295 | |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 296 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 297 | static prod_info prod = {PRODINFO_MAGIC, {0}, {0}, 0}; |
| 298 | #endif |
| 299 | |
jhchen | a8a15dd | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 300 | #ifdef ENABLE_FUSE_CHECK |
| 301 | static const char *fuse_blown = " androidboot.oem.color=red"; |
| 302 | static const char *fuse_not_blown = " androidboot.oem.color=brown"; |
| 303 | #endif |
| 304 | |
| 305 | #ifdef ENABLE_FUSE_CHECK |
| 306 | int is_fused = 0; |
| 307 | #endif |
| 308 | |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 309 | static bool is_allow_unlock = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 310 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 311 | static char frp_ptns[2][8] = {"config","frp"}; |
| 312 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 313 | static const char *critical_flash_allowed_ptn[] = { |
| 314 | "aboot", |
| 315 | "rpm", |
| 316 | "tz", |
| 317 | "sbl", |
| 318 | "sdi", |
| 319 | "sbl1", |
| 320 | "xbl", |
| 321 | "hyp", |
| 322 | "pmic", |
| 323 | "bootloader", |
| 324 | "devinfo", |
| 325 | "partition"}; |
| 326 | |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 327 | static const char *VirtualAbCriticalPartitions[] = { |
| 328 | "misc", |
| 329 | "metadata", |
| 330 | "userdata"}; |
| 331 | |
| 332 | static bool CheckVirtualAbCriticalPartition (const char *PartitionName); |
| 333 | |
Rahul Shahare | e7baede | 2021-02-02 13:23:57 +0530 | [diff] [blame] | 334 | static const char *VabSnapshotMergeStatus[] = { |
| 335 | "none", |
| 336 | "unknown", |
| 337 | "snapshotted", |
| 338 | "merging", |
| 339 | "cancelled"}; |
| 340 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 341 | struct atag_ptbl_entry |
| 342 | { |
| 343 | char name[16]; |
| 344 | unsigned offset; |
| 345 | unsigned size; |
| 346 | unsigned flags; |
| 347 | }; |
| 348 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 349 | /* |
| 350 | * Partition info, required to be published |
| 351 | * for fastboot |
| 352 | */ |
| 353 | struct getvar_partition_info { |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 354 | char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */ |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 355 | char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */ |
| 356 | char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */ |
| 357 | char size_response[MAX_RSP_SIZE]; /* fastboot response for size */ |
| 358 | char type_response[MAX_RSP_SIZE]; /* fastboot response for type */ |
| 359 | }; |
| 360 | |
| 361 | /* |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 362 | * Update the part_type_known for known paritions types. |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 363 | */ |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 364 | #define RAW_STR "raw" |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 365 | #define EXT_STR "ext4" |
| 366 | #define F2FS_STR "f2fs" |
| 367 | |
| 368 | #define FS_SUPERBLOCK_OFFSET 0x400 |
| 369 | #define EXT_MAGIC 0xEF53 |
| 370 | #define EXT_MAGIC_OFFSET_SB 0x38 |
| 371 | #define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number |
| 372 | #define F2FS_MAGIC_OFFSET_SB 0x0 |
| 373 | |
| 374 | typedef enum fs_signature_type { |
| 375 | EXT_FS_SIGNATURE = 1, |
| 376 | EXT_F2FS_SIGNATURE = 2, |
| 377 | NO_FS = -1 |
| 378 | } fs_signature_type; |
| 379 | |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 380 | struct getvar_partition_info part_info[NUM_PARTITIONS]; |
| 381 | struct getvar_partition_info part_type_known[] = |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 382 | { |
Mayank Grover | 4992021 | 2018-02-09 18:15:55 +0530 | [diff] [blame] | 383 | { "system" , "partition-size:", "partition-type:", "", "ext4" }, |
| 384 | { "userdata" , "partition-size:", "partition-type:", "", "ext4" }, |
| 385 | { "cache" , "partition-size:", "partition-type:", "", "ext4" }, |
| 386 | { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" }, |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | char max_download_size[MAX_RSP_SIZE]; |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 390 | char charger_screen_enabled[MAX_RSP_SIZE]; |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 391 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 392 | char cust_sn_buf[PRODINFO_MAX_SSN_LEN + 1]; |
| 393 | char factory_sn_buf[PRODINFO_MAX_SSN_LEN + 1]; |
| 394 | char AdbEnable[MAX_RSP_SIZE]; |
| 395 | #endif |
| 396 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 397 | char sn_buf[PRODINFO_MAX_ISN_LEN + 1]; |
| 398 | #else |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 399 | char sn_buf[13]; |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 400 | #endif |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 401 | char display_panel_buf[MAX_PANEL_BUF_SIZE]; |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 402 | char panel_display_mode[MAX_RSP_SIZE]; |
Mayank Grover | a64dfbe | 2018-05-17 14:06:34 +0530 | [diff] [blame] | 403 | char soc_version_str[MAX_RSP_SIZE]; |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 404 | char block_size_string[MAX_RSP_SIZE]; |
Rahul Shahare | e7baede | 2021-02-02 13:23:57 +0530 | [diff] [blame] | 405 | static char SnapshotMergeState[MAX_RSP_SIZE]; |
Mukesh Ojha | bb6e7f3 | 2018-03-02 15:04:06 +0530 | [diff] [blame] | 406 | #if PRODUCT_IOT |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 407 | |
| 408 | /* For IOT we are using custom version */ |
| 409 | #define PRODUCT_IOT_VERSION "IOT001" |
| 410 | char bootloader_version_string[MAX_RSP_SIZE]; |
| 411 | #endif |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 412 | |
| 413 | #if CHECK_BAT_VOLTAGE |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 414 | char battery_voltage[MAX_RSP_SIZE]; |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 415 | char battery_soc_ok [MAX_RSP_SIZE]; |
| 416 | #endif |
| 417 | |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 418 | char get_variant[MAX_RSP_SIZE]; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 419 | |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 420 | extern int emmc_recovery_init(void); |
| 421 | |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 422 | #if NO_KEYPAD_DRIVER |
| 423 | extern int fastboot_trigger(void); |
| 424 | #endif |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 425 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 426 | 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] | 427 | { |
| 428 | /* overwrite the destination of specified for the project */ |
Channagoud Kadabi | 7042fa3 | 2013-04-26 16:44:14 -0700 | [diff] [blame] | 429 | #ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 430 | if (is_arm64) |
| 431 | hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR; |
| 432 | else |
| 433 | hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR; |
Channagoud Kadabi | 7042fa3 | 2013-04-26 16:44:14 -0700 | [diff] [blame] | 434 | hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR; |
| 435 | hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR; |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 436 | #endif |
| 437 | } |
| 438 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 439 | static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn) |
| 440 | { |
| 441 | struct atag_ptbl_entry atag_ptn; |
| 442 | |
| 443 | memcpy(atag_ptn.name, ptn->name, 16); |
| 444 | atag_ptn.name[15] = '\0'; |
| 445 | atag_ptn.offset = ptn->start; |
| 446 | atag_ptn.size = ptn->length; |
| 447 | atag_ptn.flags = ptn->flags; |
| 448 | memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry)); |
| 449 | *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned); |
| 450 | } |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 451 | #ifdef VERIFIED_BOOT_2 |
| 452 | void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz) |
| 453 | { |
| 454 | int index = 0; |
| 455 | char *vbm_img_buf = NULL; |
| 456 | unsigned long long ptn = 0; |
| 457 | unsigned long long ptn_size = 0; |
| 458 | |
| 459 | /* Immediately return if dtbo is not supported */ |
| 460 | index = partition_get_index("vbmeta"); |
| 461 | ptn = partition_get_offset(index); |
| 462 | if(!ptn) |
| 463 | { |
| 464 | dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n"); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | ptn_size = partition_get_size(index); |
| 469 | if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF) |
| 470 | { |
| 471 | dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n"); |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE)); |
| 476 | if (!vbm_img_buf) |
| 477 | { |
| 478 | dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n"); |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | mmc_set_lun(partition_get_lun(index)); |
| 483 | if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size)) |
| 484 | { |
| 485 | dprintf(CRITICAL, "ERROR: vbmeta read failure\n"); |
| 486 | free(vbm_img_buf); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | *vbmeta_image_buf = vbm_img_buf; |
| 491 | *vbmeta_image_sz = (uint32_t)ptn_size; |
| 492 | return; |
| 493 | } |
| 494 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 495 | |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 496 | #if CHECK_BAT_VOLTAGE |
| 497 | void update_battery_status(void) |
| 498 | { |
| 499 | snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage()); |
| 500 | snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no"); |
| 501 | } |
| 502 | #endif |
| 503 | |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 504 | uint32_t GetPcbaVariant(void) |
| 505 | { |
| 506 | uint8_t GPIO_97=10; |
| 507 | uint8_t GPIO_98=10; |
| 508 | uint8_t GPIO_99=10; |
| 509 | |
| 510 | uint8_t pcba_stage = 0; |
| 511 | |
| 512 | // Config GPIO |
| 513 | gpio_tlmm_config(97, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE); |
| 514 | gpio_tlmm_config(98, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE); |
| 515 | gpio_tlmm_config(99, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE); |
| 516 | |
| 517 | /* Wait for the gpio config to take effect - debounce time */ |
| 518 | thread_sleep(10); |
| 519 | |
| 520 | /* Get status of GPIO */ |
| 521 | GPIO_97 = gpio_status(97); |
| 522 | GPIO_98 = gpio_status(98); |
| 523 | GPIO_99 = gpio_status(99); |
| 524 | |
| 525 | pcba_stage = (GPIO_99 << 2) + (GPIO_98 << 1) + GPIO_97; |
| 526 | dprintf(ALWAYS, "pcba_stage status: %u\n", pcba_stage); |
| 527 | // GPIO_99 GPIO_98 GPIO_97 |
| 528 | // 0 0 0 EP0 |
| 529 | // 0 0 1 EP1 |
| 530 | // 0 1 0 EP2 |
| 531 | // 0 1 1 PP |
michaellin | af8857a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 532 | // 0 1 1 FP(8901MB-007)/PP |
| 533 | // 1 0 0 MP(8901MB-008) |
| 534 | // 1 0 1 MP-8903MB_001 |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 535 | return pcba_stage; |
| 536 | } |
| 537 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 538 | unsigned char *update_cmdline(const char * cmdline) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 539 | { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 540 | int cmdline_len = 0; |
| 541 | int have_cmdline = 0; |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 542 | unsigned char *cmdline_final = NULL; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 543 | int pause_at_bootup = 0; |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 544 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 545 | int AdbEnable = 0; |
| 546 | #endif |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 547 | bool warm_boot = false; |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 548 | bool gpt_exists = partition_gpt_exists(); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 549 | int have_target_boot_params = 0; |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 550 | char *boot_dev_buf = NULL; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 551 | #ifdef MDTP_SUPPORT |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 552 | bool is_mdtp_activated = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 553 | #endif |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 554 | int current_active_slot = INVALID; |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 555 | int system_ptn_index = -1; |
| 556 | unsigned int lun = 0; |
| 557 | char lun_char_base = 'a'; |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 558 | #if VERITY_LE |
| 559 | int syspath_buflen = strlen(verity_dev) |
| 560 | + strlen(verity_system_part) + (sizeof(char) * 2) + 2 |
| 561 | + strlen(verity_params) + sizeof(int) + 2; |
| 562 | #else |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 563 | int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/ |
| 564 | char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0"; |
| 565 | int dtbo_idx = INVALID_PTN; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 566 | char dtb_idx_str[MAX_DTB_IDX_STR] = "\0"; |
| 567 | int dtb_idx = INVALID_PTN; |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 568 | #endif |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 569 | char syspath_buf[syspath_buflen]; |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 570 | #if HIBERNATION_SUPPORT |
| 571 | int resume_buflen = strlen(resume) + sizeof(int) + 2; |
| 572 | char resume_buf[resume_buflen]; |
| 573 | int swap_ptn_index = INVALID_PTN; |
| 574 | #endif |
| 575 | |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 576 | #if VERIFIED_BOOT |
| 577 | uint32_t boot_state = RED; |
| 578 | #endif |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 579 | |
| 580 | #if USE_LE_SYSTEMD |
| 581 | is_systemd_present=true; |
| 582 | #endif |
| 583 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 584 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 585 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 586 | { |
| 587 | boot_state = boot_verify_get_state(); |
| 588 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 589 | #endif |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 590 | |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 591 | #ifdef MDTP_SUPPORT |
| 592 | mdtp_activated(&is_mdtp_activated); |
| 593 | #endif /* MDTP_SUPPORT */ |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 594 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 595 | if (cmdline && cmdline[0]) { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 596 | cmdline_len = strlen(cmdline); |
| 597 | have_cmdline = 1; |
| 598 | } |
| 599 | if (target_is_emmc_boot()) { |
| 600 | cmdline_len += strlen(emmc_cmdline); |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 601 | boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN); |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 602 | if (!boot_dev_buf) { |
| 603 | dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n"); |
| 604 | } else { |
lijuang | d28587b | 2021-04-27 15:54:05 +0800 | [diff] [blame] | 605 | 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] | 606 | #if USE_BOOTDEV_CMDLINE |
| 607 | cmdline_len += strlen(boot_dev_buf); |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 608 | #endif |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 609 | if (target_dynamic_partition_supported()) { |
| 610 | cmdline_len += strlen(dynamic_bootdev_cmdline); |
| 611 | cmdline_len += strlen(boot_dev_buf); |
| 612 | } |
| 613 | } |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 614 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 615 | |
| 616 | cmdline_len += strlen(usb_sn_cmdline); |
| 617 | cmdline_len += strlen(sn_buf); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 618 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 619 | cmdline_len += strlen(cust_sn_cmdline); |
| 620 | cmdline_len += strlen(cust_sn_buf); |
| 621 | cmdline_len += strlen(factory_sn_cmdline); |
| 622 | cmdline_len += strlen(factory_sn_buf); |
| 623 | #endif |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 624 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 625 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 626 | if (VB_M <= target_get_vb_version()) |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 627 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 628 | cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name); |
| 629 | if ((device.verity_mode != 0 ) && (device.verity_mode != 1)) |
| 630 | { |
| 631 | dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n"); |
| 632 | ASSERT(0); |
| 633 | } |
| 634 | cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name); |
| 635 | cmdline_len += strlen(keymaster_v1); |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 636 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 637 | #endif |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 638 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 639 | |
| 640 | if (vbcmdline != NULL) { |
| 641 | dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n", |
| 642 | strlen(vbcmdline)); |
| 643 | cmdline_len += strlen(vbcmdline); |
| 644 | } |
| 645 | |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 646 | if (boot_into_recovery && gpt_exists) |
| 647 | cmdline_len += strlen(secondary_gpt_enable); |
| 648 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 649 | #ifdef MDTP_SUPPORT |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 650 | if(is_mdtp_activated) |
| 651 | cmdline_len += strlen(mdtp_activated_flag); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 652 | #endif |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 653 | if (boot_into_ffbm) { |
| 654 | cmdline_len += strlen(androidboot_mode); |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 655 | |
| 656 | if(is_systemd_present) |
| 657 | cmdline_len += strlen(systemd_ffbm_mode); |
| 658 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 659 | cmdline_len += strlen(ffbm_mode_string); |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 660 | /* reduce kernel console messages to speed-up boot */ |
| 661 | cmdline_len += strlen(loglevel); |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 662 | } else if (boot_reason_alarm) { |
| 663 | cmdline_len += strlen(alarmboot_cmdline); |
Zhenhua Huang | 431dafa | 2015-06-30 16:13:37 +0800 | [diff] [blame] | 664 | } else if ((target_build_variant_user() || device.charger_screen_enabled) |
lijuang | 266b17d | 2018-08-17 18:53:42 +0800 | [diff] [blame] | 665 | && target_pause_for_battery_charge() && !boot_into_recovery) { |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 666 | pause_at_bootup = 1; |
| 667 | cmdline_len += strlen(battchg_pause); |
| 668 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 669 | |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 670 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 671 | if (prod.is_adb_enabled) { |
| 672 | dprintf(CRITICAL, "Device will enable adb\n"); |
| 673 | |
| 674 | prod.is_adb_enabled = 0; |
| 675 | write_prod_info(&prod); |
| 676 | |
| 677 | AdbEnable = 1; |
| 678 | cmdline_len += strlen(UsbAdbEnable); |
| 679 | } |
| 680 | #endif |
| 681 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 682 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 683 | cmdline_len += strlen(auth_kernel); |
| 684 | } |
| 685 | |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 686 | if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" : |
| 687 | "system", |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 688 | &target_boot_params) == 0) { |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 689 | have_target_boot_params = 1; |
| 690 | cmdline_len += strlen(target_boot_params); |
| 691 | } |
| 692 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 693 | /* Determine correct androidboot.baseband to use */ |
| 694 | switch(target_baseband()) |
| 695 | { |
| 696 | case BASEBAND_APQ: |
| 697 | cmdline_len += strlen(baseband_apq); |
| 698 | break; |
| 699 | |
| 700 | case BASEBAND_MSM: |
| 701 | cmdline_len += strlen(baseband_msm); |
| 702 | break; |
| 703 | |
| 704 | case BASEBAND_CSFB: |
| 705 | cmdline_len += strlen(baseband_csfb); |
| 706 | break; |
| 707 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 708 | case BASEBAND_SVLTE2A: |
| 709 | cmdline_len += strlen(baseband_svlte2a); |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 710 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 711 | |
| 712 | case BASEBAND_MDM: |
| 713 | cmdline_len += strlen(baseband_mdm); |
| 714 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 715 | |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 716 | case BASEBAND_MDM2: |
| 717 | cmdline_len += strlen(baseband_mdm2); |
| 718 | break; |
| 719 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 720 | case BASEBAND_SGLTE: |
| 721 | cmdline_len += strlen(baseband_sglte); |
| 722 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 723 | |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 724 | case BASEBAND_SGLTE2: |
| 725 | cmdline_len += strlen(baseband_sglte2); |
| 726 | break; |
| 727 | |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 728 | case BASEBAND_DSDA: |
| 729 | cmdline_len += strlen(baseband_dsda); |
| 730 | break; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 731 | |
| 732 | case BASEBAND_DSDA2: |
| 733 | cmdline_len += strlen(baseband_dsda2); |
| 734 | break; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 735 | case BASEBAND_APQ_NOWGR: |
| 736 | cmdline_len += strlen(baseband_apq_nowgr); |
| 737 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 738 | } |
| 739 | |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 740 | switch(GetPcbaVariant()) |
| 741 | { |
| 742 | case 0: // EP0 |
| 743 | cmdline_len += strlen(PCBA_STAGE_0); |
| 744 | break; |
| 745 | |
| 746 | case 1: // EP1 |
| 747 | cmdline_len += strlen(PCBA_STAGE_1); |
| 748 | break; |
| 749 | |
| 750 | case 2: // EP2 |
| 751 | cmdline_len += strlen(PCBA_STAGE_2); |
| 752 | break; |
| 753 | |
michaellin | af8857a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 754 | case 3: // FP(8901MB-007)/PP |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 755 | cmdline_len += strlen(PCBA_STAGE_3); |
| 756 | break; |
| 757 | |
michaellin | af8857a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 758 | case 4: // MP(8901MB-008) |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 759 | cmdline_len += strlen(PCBA_STAGE_4); |
| 760 | break; |
| 761 | |
michaellin | af8857a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 762 | case 5: // MP-8903MB_001 |
| 763 | cmdline_len += strlen(PCBA_STAGE_5); |
| 764 | break; |
| 765 | |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 766 | default:// Reserved |
| 767 | cmdline_len += strlen(PCBA_STAGE_F); |
| 768 | break; |
| 769 | } |
| 770 | |
tracychui | 792d2ad | 2020-06-15 10:50:49 +0800 | [diff] [blame^] | 771 | if(smem_get_ddr_size() == MEM_3GB ) |
| 772 | { |
| 773 | cmdline_len += strlen(memory_config_3G); |
| 774 | } |
| 775 | else |
| 776 | { |
| 777 | cmdline_len += strlen(memory_config_4G); |
| 778 | } |
| 779 | |
Vladislav Levenetz | 3cd4524 | 2017-04-05 08:58:07 +0300 | [diff] [blame] | 780 | #if ENABLE_DISPLAY |
Lijuan Gao | 4a5b832 | 2014-07-24 10:38:42 +0800 | [diff] [blame] | 781 | if (cmdline) { |
| 782 | if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) && |
Padmanabhan Komanduru | bccbcdc | 2015-06-30 16:19:24 +0530 | [diff] [blame] | 783 | target_display_panel_node(display_panel_buf, |
| 784 | MAX_PANEL_BUF_SIZE) && |
Lijuan Gao | 4a5b832 | 2014-07-24 10:38:42 +0800 | [diff] [blame] | 785 | strlen(display_panel_buf)) { |
| 786 | cmdline_len += strlen(display_panel_buf); |
| 787 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 788 | } |
Vladislav Levenetz | 3cd4524 | 2017-04-05 08:58:07 +0300 | [diff] [blame] | 789 | #endif |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 790 | |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 791 | if (target_warm_boot()) { |
| 792 | warm_boot = true; |
| 793 | cmdline_len += strlen(warmboot_cmdline); |
| 794 | } |
| 795 | |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 796 | if (target_uses_system_as_root() || |
| 797 | partition_multislot_is_supported()) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 798 | { |
| 799 | current_active_slot = partition_find_active_slot(); |
| 800 | cmdline_len += (strlen(androidboot_slot_suffix)+ |
| 801 | strlen(SUFFIX_SLOT(current_active_slot))); |
| 802 | |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 803 | system_ptn_index = partition_get_index("system"); |
| 804 | if (platform_boot_dev_isemmc()) |
| 805 | { |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 806 | #if VERITY_LE |
| 807 | /* |
| 808 | Condition 4: Verity and A/B both enabled |
| 809 | Eventual command line looks like: |
| 810 | ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ... |
| 811 | ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>" |
| 812 | */ |
| 813 | snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"", |
| 814 | verity_dev, |
| 815 | verity_system_part, suffix_slot[current_active_slot], |
| 816 | verity_params, system_ptn_index + 1); |
| 817 | #else |
| 818 | /* |
| 819 | Condition 5: A/B enabled, but verity disabled |
| 820 | Eventual command line looks like: |
| 821 | ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ... |
| 822 | ... root=/dev/mmcblk0p<NN> ... |
| 823 | */ |
| 824 | snprintf(syspath_buf, syspath_buflen, " %s%d", |
| 825 | sys_path, system_ptn_index + 1); |
| 826 | #endif |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 827 | } |
| 828 | else |
| 829 | { |
| 830 | lun = partition_get_lun(system_ptn_index); |
| 831 | snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d", |
| 832 | lun_char_base + lun, |
| 833 | partition_get_index_in_lun("system", lun)); |
| 834 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 835 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 836 | #ifndef VERIFIED_BOOT_2 |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 837 | cmdline_len += strlen(syspath_buf); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 838 | #endif |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | if (target_uses_system_as_root() || |
| 842 | partition_multislot_is_supported()) |
| 843 | { |
| 844 | cmdline_len += strlen(sys_path_cmdline); |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 845 | |
| 846 | /* For dynamic partition, support skip skip_initramfs */ |
| 847 | if (!target_dynamic_partition_supported() && |
| 848 | !boot_into_recovery) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 849 | cmdline_len += strlen(skip_ramfs); |
| 850 | } |
| 851 | |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 852 | #if HIBERNATION_SUPPORT |
| 853 | if (platform_boot_dev_isemmc()) |
| 854 | { |
| 855 | swap_ptn_index = partition_get_index("swap"); |
| 856 | if (swap_ptn_index != INVALID_PTN) |
| 857 | { |
| 858 | snprintf(resume_buf, resume_buflen, |
| 859 | " %s%d", resume, |
| 860 | (swap_ptn_index + 1)); |
| 861 | cmdline_len += strlen(resume_buf); |
| 862 | } |
| 863 | else |
| 864 | { |
| 865 | dprintf(INFO, "WARNING: swap partition not found\n"); |
| 866 | } |
| 867 | } |
| 868 | #endif |
| 869 | |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 870 | #if TARGET_CMDLINE_SUPPORT |
| 871 | char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF); |
| 872 | int target_cmd_line_len; |
| 873 | ASSERT(target_cmdline_buf); |
| 874 | target_cmd_line_len = target_update_cmdline(target_cmdline_buf); |
| 875 | cmdline_len += target_cmd_line_len; |
| 876 | #endif |
| 877 | |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 878 | #if !VERITY_LE |
| 879 | dtbo_idx = get_dtbo_idx (); |
| 880 | if (dtbo_idx != INVALID_PTN) { |
| 881 | snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d", |
| 882 | android_boot_dtbo_idx, dtbo_idx); |
| 883 | cmdline_len += strlen (dtbo_idx_str); |
| 884 | } |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 885 | |
| 886 | dtb_idx = get_dtb_idx (); |
| 887 | if (dtb_idx != INVALID_PTN) { |
| 888 | snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d", |
| 889 | android_boot_dtb_idx, dtb_idx); |
| 890 | cmdline_len += strlen (dtb_idx_str); |
| 891 | } |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 892 | #endif |
| 893 | |
jhchen | a8a15dd | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 894 | #ifdef ENABLE_FUSE_CHECK |
| 895 | { |
| 896 | int hw_key_status = readl(0xA01D0); |
| 897 | is_fused = (hw_key_status & 0x2) >> 1; |
| 898 | dprintf(CRITICAL, "hw_key_status = 0x%x, is_fused=%d\n", hw_key_status, is_fused); |
| 899 | if (is_fused) { |
| 900 | cmdline_len += strlen(fuse_blown); |
| 901 | } else { |
| 902 | cmdline_len += strlen(fuse_not_blown); |
| 903 | } |
| 904 | } |
| 905 | #endif |
| 906 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 907 | if (cmdline_len > 0) { |
| 908 | const char *src; |
Maria Yu | 52254c0 | 2014-07-04 16:14:54 +0800 | [diff] [blame] | 909 | unsigned char *dst; |
| 910 | |
| 911 | cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3)); |
| 912 | ASSERT(cmdline_final != NULL); |
vijay kumar | 287bb54 | 2015-09-29 13:01:52 +0530 | [diff] [blame] | 913 | memset((void *)cmdline_final, 0, sizeof(*cmdline_final)); |
Maria Yu | 52254c0 | 2014-07-04 16:14:54 +0800 | [diff] [blame] | 914 | dst = cmdline_final; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 915 | |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 916 | /* Save start ptr for debug print */ |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 917 | if (have_cmdline) { |
| 918 | src = cmdline; |
| 919 | while ((*dst++ = *src++)); |
| 920 | } |
| 921 | if (target_is_emmc_boot()) { |
| 922 | src = emmc_cmdline; |
| 923 | if (have_cmdline) --dst; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 924 | have_cmdline = 1; |
| 925 | while ((*dst++ = *src++)); |
Sridhar Parasuram | 7bd4aaf | 2015-02-12 11:14:38 -0800 | [diff] [blame] | 926 | #if USE_BOOTDEV_CMDLINE |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 927 | src = boot_dev_buf; |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 928 | if (have_cmdline && |
| 929 | boot_dev_buf) { |
| 930 | --dst; |
| 931 | while ((*dst++ = *src++)); |
| 932 | } |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 933 | #endif |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 934 | /* Dynamic partition append boot_devices */ |
| 935 | if (target_dynamic_partition_supported() && |
| 936 | boot_dev_buf) { |
| 937 | src = dynamic_bootdev_cmdline; |
| 938 | if (have_cmdline) --dst; |
| 939 | while ((*dst++ = *src++)); |
| 940 | src = boot_dev_buf; |
| 941 | if (have_cmdline) --dst; |
| 942 | while ((*dst++ = *src++)); |
| 943 | } |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 944 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 945 | |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 946 | switch(GetPcbaVariant()) |
| 947 | { |
| 948 | case 0: |
| 949 | src = PCBA_STAGE_0; |
| 950 | if (have_cmdline) --dst; |
| 951 | while ((*dst++ = *src++)); |
| 952 | break; |
| 953 | case 1: |
| 954 | src = PCBA_STAGE_1; |
| 955 | if (have_cmdline) --dst; |
| 956 | while ((*dst++ = *src++)); |
| 957 | break; |
| 958 | case 2: |
| 959 | src = PCBA_STAGE_2; |
| 960 | if (have_cmdline) --dst; |
| 961 | while ((*dst++ = *src++)); |
| 962 | break; |
| 963 | case 3: |
| 964 | src = PCBA_STAGE_3; |
| 965 | if (have_cmdline) --dst; |
| 966 | while ((*dst++ = *src++)); |
| 967 | break; |
| 968 | case 4: |
| 969 | src = PCBA_STAGE_4; |
| 970 | if (have_cmdline) --dst; |
| 971 | while ((*dst++ = *src++)); |
| 972 | break; |
michaellin | af8857a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 973 | case 5: |
| 974 | src = PCBA_STAGE_5; |
| 975 | if (have_cmdline) --dst; |
| 976 | while ((*dst++ = *src++)); |
| 977 | break; |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 978 | default: |
| 979 | src = PCBA_STAGE_F; |
| 980 | if (have_cmdline) --dst; |
| 981 | while ((*dst++ = *src++)); |
| 982 | break; |
| 983 | } |
tracychui | 792d2ad | 2020-06-15 10:50:49 +0800 | [diff] [blame^] | 984 | if(smem_get_ddr_size() == MEM_3GB ) |
| 985 | { |
| 986 | src = memory_config_3G; |
| 987 | if (have_cmdline) --dst; |
| 988 | while ((*dst++ = *src++)); |
| 989 | } |
| 990 | else |
| 991 | { |
| 992 | src = memory_config_4G; |
| 993 | if (have_cmdline) --dst; |
| 994 | while ((*dst++ = *src++)); |
| 995 | } |
michaellin | 6be1f39 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 996 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 997 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 998 | if (VB_M <= target_get_vb_version()) |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 999 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1000 | src = verified_state; |
| 1001 | if(have_cmdline) --dst; |
| 1002 | have_cmdline = 1; |
| 1003 | while ((*dst++ = *src++)); |
| 1004 | src = vbsn[boot_state].name; |
| 1005 | if(have_cmdline) --dst; |
| 1006 | while ((*dst++ = *src++)); |
| 1007 | |
| 1008 | if ((device.verity_mode != 0 ) && (device.verity_mode != 1)) |
| 1009 | { |
| 1010 | dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n"); |
| 1011 | ASSERT(0); |
| 1012 | } |
| 1013 | src = verity_mode; |
| 1014 | if(have_cmdline) --dst; |
| 1015 | while ((*dst++ = *src++)); |
| 1016 | src = vbvm[device.verity_mode].name; |
| 1017 | if(have_cmdline) -- dst; |
| 1018 | while ((*dst++ = *src++)); |
| 1019 | src = keymaster_v1; |
| 1020 | if(have_cmdline) --dst; |
| 1021 | while ((*dst++ = *src++)); |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 1022 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 1023 | #endif |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1024 | |
| 1025 | if (vbcmdline != NULL) { |
| 1026 | src = vbcmdline; |
| 1027 | if (have_cmdline) --dst; |
| 1028 | while ((*dst++ = *src++)); |
| 1029 | } |
| 1030 | |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 1031 | src = usb_sn_cmdline; |
| 1032 | if (have_cmdline) --dst; |
| 1033 | have_cmdline = 1; |
| 1034 | while ((*dst++ = *src++)); |
| 1035 | src = sn_buf; |
| 1036 | if (have_cmdline) --dst; |
| 1037 | have_cmdline = 1; |
| 1038 | while ((*dst++ = *src++)); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 1039 | |
| 1040 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 1041 | src = cust_sn_cmdline; |
| 1042 | if (have_cmdline) --dst; |
| 1043 | have_cmdline = 1; |
| 1044 | while ((*dst++ = *src++)); |
| 1045 | src = cust_sn_buf; |
| 1046 | if (have_cmdline) --dst; |
| 1047 | have_cmdline = 1; |
| 1048 | while ((*dst++ = *src++)); |
| 1049 | src = factory_sn_cmdline; |
| 1050 | if (have_cmdline) --dst; |
| 1051 | have_cmdline = 1; |
| 1052 | while ((*dst++ = *src++)); |
| 1053 | src = factory_sn_buf; |
| 1054 | if (have_cmdline) --dst; |
| 1055 | have_cmdline = 1; |
| 1056 | while ((*dst++ = *src++)); |
| 1057 | #endif |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 1058 | if (warm_boot) { |
| 1059 | if (have_cmdline) --dst; |
| 1060 | src = warmboot_cmdline; |
| 1061 | while ((*dst++ = *src++)); |
| 1062 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 1063 | |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 1064 | if (boot_into_recovery && gpt_exists) { |
| 1065 | src = secondary_gpt_enable; |
| 1066 | if (have_cmdline) --dst; |
| 1067 | while ((*dst++ = *src++)); |
| 1068 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1069 | #ifdef MDTP_SUPPORT |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 1070 | if (is_mdtp_activated) { |
| 1071 | src = mdtp_activated_flag; |
| 1072 | if (have_cmdline) --dst; |
| 1073 | while ((*dst++ = *src++)); |
| 1074 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1075 | #endif |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 1076 | if (boot_into_ffbm) { |
| 1077 | src = androidboot_mode; |
| 1078 | if (have_cmdline) --dst; |
| 1079 | while ((*dst++ = *src++)); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 1080 | src = ffbm_mode_string; |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 1081 | if (have_cmdline) --dst; |
| 1082 | while ((*dst++ = *src++)); |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 1083 | |
| 1084 | if(is_systemd_present) { |
| 1085 | src = systemd_ffbm_mode; |
| 1086 | if (have_cmdline) --dst; |
| 1087 | while ((*dst++ = *src++)); |
| 1088 | } |
| 1089 | |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 1090 | src = loglevel; |
| 1091 | if (have_cmdline) --dst; |
| 1092 | while ((*dst++ = *src++)); |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 1093 | } else if (boot_reason_alarm) { |
| 1094 | src = alarmboot_cmdline; |
| 1095 | if (have_cmdline) --dst; |
| 1096 | while ((*dst++ = *src++)); |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 1097 | } else if (pause_at_bootup) { |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 1098 | src = battchg_pause; |
| 1099 | if (have_cmdline) --dst; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 1100 | while ((*dst++ = *src++)); |
| 1101 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 1102 | |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 1103 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 1104 | if (AdbEnable) { |
| 1105 | src = UsbAdbEnable; |
| 1106 | if (have_cmdline) --dst; |
| 1107 | while ((*dst++ = *src++)); |
| 1108 | } |
| 1109 | #endif |
| 1110 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1111 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 1112 | src = auth_kernel; |
| 1113 | if (have_cmdline) --dst; |
| 1114 | while ((*dst++ = *src++)); |
| 1115 | } |
| 1116 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 1117 | switch(target_baseband()) |
| 1118 | { |
| 1119 | case BASEBAND_APQ: |
| 1120 | src = baseband_apq; |
| 1121 | if (have_cmdline) --dst; |
| 1122 | while ((*dst++ = *src++)); |
| 1123 | break; |
| 1124 | |
| 1125 | case BASEBAND_MSM: |
| 1126 | src = baseband_msm; |
| 1127 | if (have_cmdline) --dst; |
| 1128 | while ((*dst++ = *src++)); |
| 1129 | break; |
| 1130 | |
| 1131 | case BASEBAND_CSFB: |
| 1132 | src = baseband_csfb; |
| 1133 | if (have_cmdline) --dst; |
| 1134 | while ((*dst++ = *src++)); |
| 1135 | break; |
| 1136 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 1137 | case BASEBAND_SVLTE2A: |
| 1138 | src = baseband_svlte2a; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 1139 | if (have_cmdline) --dst; |
| 1140 | while ((*dst++ = *src++)); |
| 1141 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 1142 | |
| 1143 | case BASEBAND_MDM: |
| 1144 | src = baseband_mdm; |
| 1145 | if (have_cmdline) --dst; |
| 1146 | while ((*dst++ = *src++)); |
| 1147 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 1148 | |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 1149 | case BASEBAND_MDM2: |
| 1150 | src = baseband_mdm2; |
| 1151 | if (have_cmdline) --dst; |
| 1152 | while ((*dst++ = *src++)); |
| 1153 | break; |
| 1154 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 1155 | case BASEBAND_SGLTE: |
| 1156 | src = baseband_sglte; |
| 1157 | if (have_cmdline) --dst; |
| 1158 | while ((*dst++ = *src++)); |
| 1159 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 1160 | |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 1161 | case BASEBAND_SGLTE2: |
| 1162 | src = baseband_sglte2; |
| 1163 | if (have_cmdline) --dst; |
| 1164 | while ((*dst++ = *src++)); |
| 1165 | break; |
| 1166 | |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 1167 | case BASEBAND_DSDA: |
| 1168 | src = baseband_dsda; |
| 1169 | if (have_cmdline) --dst; |
| 1170 | while ((*dst++ = *src++)); |
| 1171 | break; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 1172 | |
| 1173 | case BASEBAND_DSDA2: |
| 1174 | src = baseband_dsda2; |
| 1175 | if (have_cmdline) --dst; |
| 1176 | while ((*dst++ = *src++)); |
| 1177 | break; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 1178 | case BASEBAND_APQ_NOWGR: |
| 1179 | src = baseband_apq_nowgr; |
| 1180 | if (have_cmdline) --dst; |
| 1181 | while ((*dst++ = *src++)); |
| 1182 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 1183 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 1184 | |
| 1185 | if (strlen(display_panel_buf)) { |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 1186 | src = display_panel_buf; |
| 1187 | if (have_cmdline) --dst; |
| 1188 | while ((*dst++ = *src++)); |
| 1189 | } |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 1190 | |
| 1191 | if (have_target_boot_params) { |
| 1192 | if (have_cmdline) --dst; |
| 1193 | src = target_boot_params; |
| 1194 | while ((*dst++ = *src++)); |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 1195 | free(target_boot_params); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 1196 | } |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 1197 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1198 | if (partition_multislot_is_supported() && have_cmdline) |
| 1199 | { |
| 1200 | src = androidboot_slot_suffix; |
| 1201 | --dst; |
| 1202 | while ((*dst++ = *src++)); |
| 1203 | --dst; |
| 1204 | src = SUFFIX_SLOT(current_active_slot); |
| 1205 | while ((*dst++ = *src++)); |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1206 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1207 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1208 | |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1209 | /* |
| 1210 | * System-As-Root behaviour, system.img should contain both |
| 1211 | * system content and ramdisk content, and should be mounted at |
| 1212 | * root(a/b). |
| 1213 | * Apending skip_ramfs for non a/b builds which use, system as root. |
| 1214 | */ |
| 1215 | if ((target_uses_system_as_root() || |
| 1216 | partition_multislot_is_supported()) && |
| 1217 | have_cmdline) |
| 1218 | { |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 1219 | if (!target_dynamic_partition_supported() && |
| 1220 | !boot_into_recovery) |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1221 | { |
| 1222 | src = skip_ramfs; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1223 | --dst; |
| 1224 | while ((*dst++ = *src++)); |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | src = sys_path_cmdline; |
| 1228 | --dst; |
| 1229 | while ((*dst++ = *src++)); |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 1230 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1231 | #ifndef VERIFIED_BOOT_2 |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1232 | src = syspath_buf; |
| 1233 | --dst; |
| 1234 | while ((*dst++ = *src++)); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1235 | #endif |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1236 | } |
| 1237 | |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 1238 | #if HIBERNATION_SUPPORT |
| 1239 | if (swap_ptn_index != INVALID_PTN) |
| 1240 | { |
| 1241 | src = resume_buf; |
| 1242 | --dst; |
| 1243 | while ((*dst++ = *src++)); |
| 1244 | } |
| 1245 | #endif |
| 1246 | |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 1247 | #if TARGET_CMDLINE_SUPPORT |
| 1248 | if (target_cmdline_buf && target_cmd_line_len) |
| 1249 | { |
| 1250 | if (have_cmdline) --dst; |
| 1251 | src = target_cmdline_buf; |
| 1252 | while((*dst++ = *src++)); |
| 1253 | free(target_cmdline_buf); |
| 1254 | } |
| 1255 | #endif |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 1256 | |
| 1257 | #if !VERITY_LE |
| 1258 | if (dtbo_idx != INVALID_PTN) { |
| 1259 | src = dtbo_idx_str; |
| 1260 | --dst; |
| 1261 | while ((*dst++ = *src++)); |
| 1262 | } |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1263 | |
| 1264 | if (dtb_idx != INVALID_PTN) { |
| 1265 | src = dtb_idx_str; |
| 1266 | --dst; |
| 1267 | while ((*dst++ = *src++)); |
| 1268 | } |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 1269 | #endif |
jhchen | a8a15dd | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 1270 | |
| 1271 | #ifdef ENABLE_FUSE_CHECK |
| 1272 | if (is_fused) { |
| 1273 | src = fuse_blown; |
| 1274 | if (have_cmdline) --dst; |
| 1275 | while ((*dst++ = *src++)); |
| 1276 | } else { |
| 1277 | src = fuse_not_blown; |
| 1278 | if (have_cmdline) --dst; |
| 1279 | while ((*dst++ = *src++)); |
| 1280 | } |
| 1281 | #endif |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1282 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 1283 | |
| 1284 | |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 1285 | if (boot_dev_buf) |
| 1286 | free(boot_dev_buf); |
| 1287 | |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 1288 | if (cmdline_final) |
| 1289 | dprintf(INFO, "cmdline: %s\n", cmdline_final); |
| 1290 | else |
| 1291 | dprintf(INFO, "cmdline is NULL\n"); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1292 | return cmdline_final; |
| 1293 | } |
| 1294 | |
| 1295 | unsigned *atag_core(unsigned *ptr) |
| 1296 | { |
| 1297 | /* CORE */ |
| 1298 | *ptr++ = 2; |
| 1299 | *ptr++ = 0x54410001; |
| 1300 | |
| 1301 | return ptr; |
| 1302 | |
| 1303 | } |
| 1304 | |
| 1305 | unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk, |
| 1306 | unsigned ramdisk_size) |
| 1307 | { |
| 1308 | if (ramdisk_size) { |
| 1309 | *ptr++ = 4; |
| 1310 | *ptr++ = 0x54420005; |
| 1311 | *ptr++ = (unsigned)ramdisk; |
| 1312 | *ptr++ = ramdisk_size; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1313 | } |
| 1314 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1315 | return ptr; |
| 1316 | } |
| 1317 | |
| 1318 | unsigned *atag_ptable(unsigned **ptr_addr) |
| 1319 | { |
| 1320 | int i; |
| 1321 | struct ptable *ptable; |
| 1322 | |
| 1323 | if ((ptable = flash_get_ptable()) && (ptable->count != 0)) { |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1324 | *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) / |
| 1325 | sizeof(unsigned))); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1326 | *(*ptr_addr)++ = 0x4d534d70; |
| 1327 | for (i = 0; i < ptable->count; ++i) |
| 1328 | ptentry_to_tag(ptr_addr, ptable_get(ptable, i)); |
| 1329 | } |
| 1330 | |
| 1331 | return (*ptr_addr); |
| 1332 | } |
| 1333 | |
| 1334 | unsigned *atag_cmdline(unsigned *ptr, const char *cmdline) |
| 1335 | { |
| 1336 | int cmdline_length = 0; |
| 1337 | int n; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1338 | char *dest; |
| 1339 | |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1340 | cmdline_length = strlen((const char*)cmdline); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1341 | n = (cmdline_length + 4) & (~3); |
| 1342 | |
| 1343 | *ptr++ = (n / 4) + 2; |
| 1344 | *ptr++ = 0x54410009; |
| 1345 | dest = (char *) ptr; |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1346 | while ((*dest++ = *cmdline++)); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1347 | ptr += (n / 4); |
| 1348 | |
| 1349 | return ptr; |
| 1350 | } |
| 1351 | |
| 1352 | unsigned *atag_end(unsigned *ptr) |
| 1353 | { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1354 | /* END */ |
| 1355 | *ptr++ = 0; |
| 1356 | *ptr++ = 0; |
| 1357 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1358 | return ptr; |
| 1359 | } |
| 1360 | |
| 1361 | void generate_atags(unsigned *ptr, const char *cmdline, |
| 1362 | void *ramdisk, unsigned ramdisk_size) |
| 1363 | { |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 1364 | unsigned *orig_ptr = ptr; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1365 | ptr = atag_core(ptr); |
| 1366 | ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size); |
| 1367 | ptr = target_atag_mem(ptr); |
| 1368 | |
| 1369 | /* Skip NAND partition ATAGS for eMMC boot */ |
| 1370 | if (!target_is_emmc_boot()){ |
| 1371 | ptr = atag_ptable(&ptr); |
| 1372 | } |
| 1373 | |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 1374 | /* |
| 1375 | * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned |
| 1376 | * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes |
| 1377 | */ |
Mayank Grover | bc8a2ef | 2018-02-23 18:03:36 +0530 | [diff] [blame] | 1378 | if (!cmdline) |
| 1379 | return; |
| 1380 | |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 1381 | if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) { |
| 1382 | ptr = atag_cmdline(ptr, cmdline); |
| 1383 | ptr = atag_end(ptr); |
| 1384 | } |
| 1385 | else { |
| 1386 | dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n"); |
| 1387 | ASSERT(0); |
| 1388 | } |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1391 | typedef void entry_func_ptr(unsigned, unsigned, unsigned*); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1392 | void boot_linux(void *kernel, unsigned *tags, |
| 1393 | const char *cmdline, unsigned machtype, |
| 1394 | void *ramdisk, unsigned ramdisk_size) |
| 1395 | { |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1396 | unsigned char *final_cmdline; |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1397 | #if DEVICE_TREE |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1398 | int ret = 0; |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1399 | #endif |
| 1400 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1401 | void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel)); |
Deepa Dinamani | 25a9f76 | 2012-11-30 15:57:15 -0800 | [diff] [blame] | 1402 | uint32_t tags_phys = PA((addr_t)tags); |
vijay kumar | 1a50a64 | 2015-11-16 12:41:15 +0530 | [diff] [blame] | 1403 | struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel))); |
Deepa Dinamani | 25a9f76 | 2012-11-30 15:57:15 -0800 | [diff] [blame] | 1404 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1405 | ramdisk = (void *)PA((addr_t)ramdisk); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1406 | |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1407 | final_cmdline = update_cmdline((const char*)cmdline); |
| 1408 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1409 | #if DEVICE_TREE |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1410 | dprintf(INFO, "Updating device tree: start\n"); |
| 1411 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1412 | /* Update the Device Tree */ |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1413 | 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] | 1414 | if(ret) |
| 1415 | { |
| 1416 | dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n"); |
| 1417 | ASSERT(0); |
| 1418 | } |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1419 | dprintf(INFO, "Updating device tree: done\n"); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1420 | #else |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1421 | /* Generating the Atags */ |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1422 | generate_atags(tags, final_cmdline, ramdisk, ramdisk_size); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1423 | #endif |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1424 | |
Monika Singh | 0ec6db8 | 2019-07-18 16:57:58 +0530 | [diff] [blame] | 1425 | #if VERIFIED_BOOT |
Monika Singh | b0db4b8 | 2018-09-26 12:18:02 +0530 | [diff] [blame] | 1426 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1427 | { |
| 1428 | if (device.verity_mode == 0) { |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1429 | #if FBCON_DISPLAY_MSG |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1430 | #if ENABLE_VB_ATTEST |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1431 | display_bootverify_menu(DISPLAY_MENU_EIO); |
| 1432 | wait_for_users_action(); |
| 1433 | if(!pwr_key_is_pressed) |
| 1434 | shutdown_device(); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1435 | #else |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1436 | display_bootverify_menu(DISPLAY_MENU_LOGGING); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1437 | #endif |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1438 | wait_for_users_action(); |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1439 | #else |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1440 | dprintf(CRITICAL, |
| 1441 | "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n"); |
| 1442 | mdelay(5000); |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1443 | #endif |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1444 | } |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1445 | } |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1446 | #endif |
| 1447 | |
| 1448 | #if VERIFIED_BOOT |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 1449 | /* Write protect the device info */ |
Channagoud Kadabi | 3bd9d1e | 2015-05-05 16:18:20 -0700 | [diff] [blame] | 1450 | 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] | 1451 | { |
| 1452 | dprintf(INFO, "Failed to write protect dev info\n"); |
| 1453 | ASSERT(0); |
| 1454 | } |
| 1455 | #endif |
| 1456 | |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 1457 | /* Turn off splash screen if enabled */ |
| 1458 | #if DISPLAY_SPLASH_SCREEN |
| 1459 | target_display_shutdown(); |
| 1460 | #endif |
| 1461 | |
Veera Sundaram Sankaran | 67ea093 | 2015-09-25 10:09:30 -0700 | [diff] [blame] | 1462 | /* Perform target specific cleanup */ |
| 1463 | target_uninit(); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1464 | free_verified_boot_resource(&info); |
| 1465 | if (final_cmdline) |
| 1466 | free(final_cmdline); |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 1467 | |
Deepa Dinamani | 33734bc | 2013-03-06 12:16:06 -0800 | [diff] [blame] | 1468 | 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] | 1469 | entry, ramdisk, ramdisk_size, (void *)tags_phys); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1470 | |
| 1471 | enter_critical_section(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1472 | |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 1473 | /* do any platform specific cleanup before kernel entry */ |
| 1474 | platform_uninit(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1475 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1476 | arch_disable_cache(UCACHE); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1477 | |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 1478 | #if ARM_WITH_MMU |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1479 | arch_disable_mmu(); |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 1480 | #endif |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 1481 | bs_set_timestamp(BS_KERNEL_ENTRY); |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1482 | |
| 1483 | if (IS_ARM64(kptr)) |
| 1484 | /* Jump to a 64bit kernel */ |
| 1485 | scm_elexec_call((paddr_t)kernel, tags_phys); |
| 1486 | else |
| 1487 | /* Jump to a 32bit kernel */ |
| 1488 | entry(0, machtype, (unsigned*)tags_phys); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1489 | } |
| 1490 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1491 | /* Function to check if the memory address range falls within the aboot |
| 1492 | * boundaries. |
| 1493 | * start: Start of the memory region |
| 1494 | * size: Size of the memory region |
| 1495 | */ |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 1496 | int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1497 | { |
| 1498 | /* Check for boundary conditions. */ |
Sundarajan Srinivasan | ce2a0ea | 2013-12-16 17:02:56 -0800 | [diff] [blame] | 1499 | if ((UINT_MAX - start) < size) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1500 | return -1; |
| 1501 | |
| 1502 | /* Check for memory overlap. */ |
| 1503 | if ((start < MEMBASE) && ((start + size) <= MEMBASE)) |
| 1504 | return 0; |
Channagoud Kadabi | 9414391 | 2013-10-15 12:53:52 -0700 | [diff] [blame] | 1505 | else if (start >= (MEMBASE + MEMSIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1506 | return 0; |
| 1507 | else |
| 1508 | return -1; |
| 1509 | } |
| 1510 | |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 1511 | /* Function to check if the memory address range falls beyond ddr region. |
| 1512 | * start: Start of the memory region |
| 1513 | * size: Size of the memory region |
| 1514 | */ |
| 1515 | int check_ddr_addr_range_bound(uintptr_t start, uint32_t size) |
| 1516 | { |
| 1517 | uintptr_t ddr_pa_start_addr = PA(get_ddr_start()); |
| 1518 | uint64_t ddr_size = smem_get_ddr_size(); |
| 1519 | uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size; |
| 1520 | uintptr_t pa_start_addr = PA(start); |
| 1521 | |
| 1522 | /* Check for boundary conditions. */ |
| 1523 | if ((UINT_MAX - start) < size) |
| 1524 | return -1; |
| 1525 | |
| 1526 | /* Check if memory range is beyond the ddr range. */ |
| 1527 | if (pa_start_addr < ddr_pa_start_addr || |
| 1528 | pa_start_addr >= (ddr_pa_end_addr) || |
| 1529 | (pa_start_addr + size) > ddr_pa_end_addr) |
| 1530 | return -1; |
| 1531 | else |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1535 | 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] | 1536 | |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1537 | int getimage(void **image_buffer, uint32_t *imgsize, |
| 1538 | const char *imgname) |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1539 | { |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1540 | uint32_t loadedindex; |
| 1541 | if (image_buffer == NULL || imgsize == NULL || |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1542 | imgname == NULL) { |
| 1543 | dprintf(CRITICAL, "getimage: invalid parameters\n"); |
| 1544 | return -1; |
| 1545 | } |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1546 | for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) { |
| 1547 | if (!strncmp(info.images[loadedindex].name, imgname, |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1548 | strlen(imgname))) { |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1549 | *image_buffer = info.images[loadedindex].image_buffer; |
| 1550 | *imgsize = info.images[loadedindex].imgsize; |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 1551 | dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n", |
| 1552 | info.images[loadedindex].name, |
| 1553 | info.images[loadedindex].imgsize); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1554 | return 0; |
| 1555 | } |
| 1556 | } |
| 1557 | return -1; |
| 1558 | } |
| 1559 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1560 | static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size) |
| 1561 | { |
| 1562 | int ret; |
Channagoud Kadabi | a8c623f | 2015-01-13 14:48:48 -0800 | [diff] [blame] | 1563 | |
| 1564 | #if !VERIFIED_BOOT |
Sundarajan Srinivasan | ce54d6e | 2013-11-11 12:45:00 -0800 | [diff] [blame] | 1565 | #if IMAGE_VERIF_ALGO_SHA1 |
| 1566 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1; |
| 1567 | #else |
| 1568 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256; |
| 1569 | #endif |
Channagoud Kadabi | a8c623f | 2015-01-13 14:48:48 -0800 | [diff] [blame] | 1570 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1571 | |
| 1572 | /* Assume device is rooted at this time. */ |
| 1573 | device.is_tampered = 1; |
| 1574 | |
| 1575 | dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size); |
| 1576 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1577 | #if VERIFIED_BOOT |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 1578 | uint32_t bootstate; |
| 1579 | if(boot_into_recovery && |
Mayank Grover | 68fbf0d | 2017-10-24 14:13:39 +0530 | [diff] [blame] | 1580 | (!partition_multislot_is_supported())) |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 1581 | { |
| 1582 | ret = boot_verify_image((unsigned char *)bootimg_addr, |
| 1583 | bootimg_size, "/recovery", &bootstate); |
| 1584 | } |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1585 | else |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 1586 | { |
| 1587 | ret = boot_verify_image((unsigned char *)bootimg_addr, |
| 1588 | bootimg_size, "/boot", &bootstate); |
| 1589 | } |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1590 | boot_verify_print_state(); |
| 1591 | #else |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1592 | ret = image_verify((unsigned char *)bootimg_addr, |
| 1593 | (unsigned char *)(bootimg_addr + bootimg_size), |
| 1594 | bootimg_size, |
Sundarajan Srinivasan | ce54d6e | 2013-11-11 12:45:00 -0800 | [diff] [blame] | 1595 | auth_algo); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1596 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1597 | dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret); |
| 1598 | |
| 1599 | if (ret) |
| 1600 | { |
| 1601 | /* Authorized kernel */ |
| 1602 | device.is_tampered = 0; |
Sundarajan Srinivasan | 3fb21f1 | 2013-09-16 18:36:15 -0700 | [diff] [blame] | 1603 | auth_kernel_img = 1; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1604 | } |
| 1605 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 1606 | #ifdef MDTP_SUPPORT |
| 1607 | { |
| 1608 | /* Verify MDTP lock. |
| 1609 | * For boot & recovery partitions, use aboot's verification result. |
| 1610 | */ |
| 1611 | mdtp_ext_partition_verification_t ext_partition; |
| 1612 | ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT; |
| 1613 | ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID; |
| 1614 | ext_partition.page_size = 0; /* Not needed since already validated */ |
| 1615 | ext_partition.image_addr = 0; /* Not needed since already validated */ |
| 1616 | ext_partition.image_size = 0; /* Not needed since already validated */ |
| 1617 | ext_partition.sig_avail = FALSE; /* Not needed since already validated */ |
| 1618 | mdtp_fwlock_verify_lock(&ext_partition); |
| 1619 | } |
| 1620 | #endif /* MDTP_SUPPORT */ |
| 1621 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1622 | #if USE_PCOM_SECBOOT |
| 1623 | set_tamper_flag(device.is_tampered); |
| 1624 | #endif |
| 1625 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1626 | #if VERIFIED_BOOT |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1627 | switch(boot_verify_get_state()) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1628 | { |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1629 | case RED: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1630 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 1631 | display_bootverify_menu(DISPLAY_MENU_RED); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1632 | #if ENABLE_VB_ATTEST |
| 1633 | mdelay(DELAY_WAIT); |
| 1634 | shutdown_device(); |
| 1635 | #else |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1636 | wait_for_users_action(); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1637 | #endif |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1638 | #else |
| 1639 | dprintf(CRITICAL, |
| 1640 | "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n"); |
| 1641 | mdelay(5000); |
| 1642 | #endif |
| 1643 | |
| 1644 | break; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1645 | case YELLOW: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1646 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 1647 | display_bootverify_menu(DISPLAY_MENU_YELLOW); |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1648 | wait_for_users_action(); |
| 1649 | #else |
| 1650 | dprintf(CRITICAL, |
| 1651 | "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n"); |
| 1652 | mdelay(5000); |
| 1653 | #endif |
| 1654 | break; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1655 | default: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1656 | break; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1657 | } |
| 1658 | #endif |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1659 | #if !VERIFIED_BOOT |
Unnati Gandhi | 1be0475 | 2015-03-27 19:41:53 +0530 | [diff] [blame] | 1660 | if(device.is_tampered) |
| 1661 | { |
| 1662 | write_device_info_mmc(&device); |
| 1663 | #ifdef TZ_TAMPER_FUSE |
Monika Singh | 9825746 | 2018-06-06 11:28:49 +0530 | [diff] [blame] | 1664 | set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE); |
Unnati Gandhi | 1be0475 | 2015-03-27 19:41:53 +0530 | [diff] [blame] | 1665 | #endif |
| 1666 | #ifdef ASSERT_ON_TAMPER |
| 1667 | dprintf(CRITICAL, "Device is tampered. Asserting..\n"); |
| 1668 | ASSERT(0); |
| 1669 | #endif |
| 1670 | } |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1671 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1674 | static bool check_format_bit() |
| 1675 | { |
| 1676 | bool ret = false; |
| 1677 | int index; |
| 1678 | uint64_t offset; |
| 1679 | struct boot_selection_info *in = NULL; |
| 1680 | char *buf = NULL; |
| 1681 | |
| 1682 | index = partition_get_index("bootselect"); |
| 1683 | if (index == INVALID_PTN) |
| 1684 | { |
| 1685 | dprintf(INFO, "Unable to locate /bootselect partition\n"); |
| 1686 | return ret; |
| 1687 | } |
| 1688 | offset = partition_get_offset(index); |
| 1689 | if(!offset) |
| 1690 | { |
| 1691 | dprintf(INFO, "partition /bootselect doesn't exist\n"); |
| 1692 | return ret; |
| 1693 | } |
| 1694 | buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE)); |
Mayank Grover | 4886040 | 2016-11-29 12:34:53 +0530 | [diff] [blame] | 1695 | mmc_set_lun(partition_get_lun(index)); |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1696 | ASSERT(buf); |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1697 | if (mmc_read(offset, (uint32_t *)buf, page_size)) |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1698 | { |
| 1699 | dprintf(INFO, "mmc read failure /bootselect %d\n", page_size); |
| 1700 | free(buf); |
| 1701 | return ret; |
| 1702 | } |
| 1703 | in = (struct boot_selection_info *) buf; |
| 1704 | if ((in->signature == BOOTSELECT_SIGNATURE) && |
| 1705 | (in->version == BOOTSELECT_VERSION)) { |
| 1706 | if ((in->state_info & BOOTSELECT_FORMAT) && |
| 1707 | !(in->state_info & BOOTSELECT_FACTORY)) |
| 1708 | ret = true; |
| 1709 | } else { |
| 1710 | dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n", |
| 1711 | in->signature, in->version); |
| 1712 | ASSERT(0); |
| 1713 | } |
| 1714 | free(buf); |
| 1715 | return ret; |
| 1716 | } |
| 1717 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1718 | void boot_verifier_init() |
| 1719 | { |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1720 | uint32_t boot_state; |
| 1721 | /* Check if device unlock */ |
| 1722 | if(device.is_unlocked) |
| 1723 | { |
| 1724 | boot_verify_send_event(DEV_UNLOCK); |
| 1725 | boot_verify_print_state(); |
| 1726 | dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n"); |
| 1727 | return; |
| 1728 | } |
| 1729 | else |
| 1730 | { |
| 1731 | boot_verify_send_event(BOOT_INIT); |
| 1732 | } |
| 1733 | |
| 1734 | /* Initialize keystore */ |
| 1735 | boot_state = boot_verify_keystore_init(); |
| 1736 | if(boot_state == YELLOW) |
| 1737 | { |
| 1738 | boot_verify_print_state(); |
| 1739 | dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n"); |
| 1740 | } |
| 1741 | } |
| 1742 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1743 | /* Function to return recovery appended dtbo buffer info */ |
| 1744 | void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf) |
| 1745 | { |
| 1746 | *dtbo_size = recovery_dtbo_size; |
| 1747 | *dtbo_buf = recovery_dtbo_buf; |
| 1748 | return; |
| 1749 | } |
| 1750 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1751 | int boot_linux_from_mmc(void) |
| 1752 | { |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1753 | boot_img_hdr *hdr = (void*) buf; |
| 1754 | boot_img_hdr *uhdr; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1755 | unsigned offset = 0; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 1756 | int rcode; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1757 | unsigned long long ptn = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1758 | int index = INVALID_PTN; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1759 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1760 | unsigned char *image_addr = 0; |
| 1761 | unsigned kernel_actual; |
| 1762 | unsigned ramdisk_actual; |
| 1763 | unsigned imagesize_actual; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1764 | unsigned second_actual = 0; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1765 | void * image_buf = NULL; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1766 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1767 | unsigned int dtb_size = 0; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1768 | unsigned dtb_image_size = 0; |
| 1769 | uint32_t dtb_image_offset = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1770 | unsigned int out_len = 0; |
| 1771 | unsigned int out_avai_len = 0; |
| 1772 | unsigned char *out_addr = NULL; |
| 1773 | uint32_t dtb_offset = 0; |
| 1774 | unsigned char *kernel_start_addr = NULL; |
| 1775 | unsigned int kernel_size = 0; |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 1776 | unsigned int patched_kernel_hdr_size = 0; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1777 | uint64_t image_size = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1778 | int rc; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1779 | #if VERIFIED_BOOT_2 |
| 1780 | int status; |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 1781 | void *dtbo_image_buf = NULL; |
| 1782 | uint32_t dtbo_image_sz = 0; |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 1783 | void *vbmeta_image_buf = NULL; |
| 1784 | uint32_t vbmeta_image_sz = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1785 | #endif |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1786 | char *ptn_name = NULL; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1787 | #if DEVICE_TREE |
| 1788 | struct dt_table *table; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1789 | struct dt_entry dt_entry; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1790 | unsigned dt_table_offset; |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1791 | uint32_t dt_actual; |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 1792 | uint32_t dt_hdr_size; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1793 | unsigned char *best_match_dt_addr = NULL; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1794 | #endif |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1795 | struct kernel64_hdr *kptr = NULL; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1796 | int current_active_slot = INVALID; |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1797 | |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1798 | if (check_format_bit()) |
| 1799 | boot_into_recovery = 1; |
| 1800 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 1801 | if (!boot_into_recovery) { |
| 1802 | memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string)); |
| 1803 | rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string)); |
| 1804 | if (rcode <= 0) { |
| 1805 | boot_into_ffbm = false; |
| 1806 | if (rcode < 0) |
| 1807 | dprintf(CRITICAL,"failed to get ffbm cookie"); |
| 1808 | } else |
| 1809 | boot_into_ffbm = true; |
| 1810 | } else |
| 1811 | boot_into_ffbm = false; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1812 | uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1813 | if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 1814 | dprintf(INFO, "Unified boot method!\n"); |
| 1815 | hdr = uhdr; |
| 1816 | goto unified_boot; |
| 1817 | } |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1818 | |
| 1819 | /* For a/b recovery image code is on boot partition. |
| 1820 | If we support multislot, always use boot partition. */ |
| 1821 | if (boot_into_recovery && |
Mayank Grover | 8ab4a76 | 2019-04-25 17:23:34 +0530 | [diff] [blame] | 1822 | ((!partition_multislot_is_supported()) || |
| 1823 | (target_dynamic_partition_supported()))) |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1824 | ptn_name = "recovery"; |
| 1825 | else |
| 1826 | ptn_name = "boot"; |
| 1827 | |
| 1828 | index = partition_get_index(ptn_name); |
| 1829 | ptn = partition_get_offset(index); |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1830 | image_size = partition_get_size(index); |
| 1831 | if(ptn == 0 || image_size == 0) { |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1832 | dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name); |
| 1833 | return -1; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1834 | } |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1835 | |
Channagoud Kadabi | ef0547c | 2015-02-10 12:57:38 -0800 | [diff] [blame] | 1836 | /* Set Lun for boot & recovery partitions */ |
| 1837 | mmc_set_lun(partition_get_lun(index)); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1838 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1839 | if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) { |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1840 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 1841 | return -1; |
| 1842 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1843 | |
| 1844 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 1845 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1846 | return ERR_INVALID_BOOT_MAGIC; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 1849 | if (hdr->page_size && (hdr->page_size != page_size)) { |
vijay kumar | 2e21b3a | 2014-06-26 17:40:15 +0530 | [diff] [blame] | 1850 | |
| 1851 | if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) { |
| 1852 | dprintf(CRITICAL, "ERROR: Invalid page size\n"); |
| 1853 | return -1; |
| 1854 | } |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 1855 | page_size = hdr->page_size; |
| 1856 | page_mask = page_size - 1; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1859 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 1860 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1861 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1862 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1863 | image_addr = (unsigned char *)target_get_scratch_address(); |
Mayank Grover | 6fd3679 | 2018-09-10 13:26:58 +0530 | [diff] [blame] | 1864 | #if VERIFIED_BOOT_2 |
| 1865 | /* Create hole in start of image for VB salt to copy */ |
| 1866 | image_addr += SALT_BUFF_OFFSET; |
| 1867 | #endif |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 1868 | memcpy(image_addr, (void *)buf, page_size); |
| 1869 | |
| 1870 | /* ensure commandline is terminated */ |
| 1871 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1872 | |
| 1873 | #if DEVICE_TREE |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 1874 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 1875 | dt_size = hdr->dt_size; |
Mayank Grover | 5a50258 | 2018-09-12 11:24:49 +0530 | [diff] [blame] | 1876 | #else |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1877 | dprintf(INFO, "BootImage Header: %d\n", hdr->header_version); |
Mayank Grover | 5a50258 | 2018-09-12 11:24:49 +0530 | [diff] [blame] | 1878 | #endif |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1879 | |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 1880 | dt_actual = ROUND_TO_PAGE(dt_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1881 | 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] | 1882 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__); |
| 1883 | return -1; |
| 1884 | } |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1885 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1886 | #else |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1887 | 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] | 1888 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__); |
| 1889 | return -1; |
| 1890 | } |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1891 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1892 | #endif |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1893 | dtb_image_size = hdr->kernel_size; |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1894 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1895 | #ifdef OSVERSION_IN_BOOTIMAGE |
| 1896 | /* If header version is ONE and booting into recovery, |
| 1897 | dtbo is appended with recovery image. |
| 1898 | Doing following: |
| 1899 | * Validating the recovery offset and size. |
| 1900 | * Extracting recovery dtbo to be used as dtbo. |
| 1901 | */ |
| 1902 | if (boot_into_recovery && |
| 1903 | hdr->header_version == BOOT_HEADER_VERSION_ONE) |
| 1904 | { |
| 1905 | struct boot_img_hdr_v1 *hdr1 = |
| 1906 | (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr)); |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1907 | unsigned int recovery_dtbo_actual = 0; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1908 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1909 | recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask); |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1910 | if ((hdr1->header_size != |
| 1911 | sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr))) |
| 1912 | { |
| 1913 | dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size); |
| 1914 | return -1; |
| 1915 | } |
| 1916 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1917 | if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF) |
| 1918 | { |
| 1919 | 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] | 1920 | MAX_SUPPORTED_DTBO_IMG_BUF); |
| 1921 | return -1; |
| 1922 | } |
| 1923 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1924 | if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual)) |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1925 | { |
| 1926 | dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__); |
| 1927 | return -1; |
| 1928 | } |
| 1929 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1930 | if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) { |
| 1931 | dprintf(CRITICAL, |
| 1932 | "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__); |
| 1933 | return -1; |
| 1934 | } |
| 1935 | |
| 1936 | if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size) |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1937 | { |
| 1938 | dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx," |
| 1939 | " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n", |
| 1940 | hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size); |
| 1941 | return -1; |
| 1942 | } |
| 1943 | |
| 1944 | recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr); |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1945 | recovery_dtbo_size = recovery_dtbo_actual; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1946 | imagesize_actual += recovery_dtbo_size; |
| 1947 | |
| 1948 | dprintf(SPEW, "Header version: %d\n", hdr->header_version); |
| 1949 | dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size); |
| 1950 | dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset); |
| 1951 | |
| 1952 | } |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1953 | |
| 1954 | if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) { |
| 1955 | struct boot_img_hdr_v1 *hdr1 = |
| 1956 | (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr)); |
| 1957 | struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *) |
| 1958 | (image_addr + sizeof(boot_img_hdr) + |
| 1959 | BOOT_IMAGE_HEADER_V2_OFFSET); |
| 1960 | unsigned int recovery_dtbo_actual = 0; |
| 1961 | |
lijuang | 0b17ba2 | 2019-09-04 14:20:47 +0800 | [diff] [blame] | 1962 | recovery_dtbo_actual = |
| 1963 | ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask); |
| 1964 | imagesize_actual += recovery_dtbo_actual; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1965 | |
| 1966 | imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask); |
| 1967 | |
| 1968 | |
| 1969 | dtb_image_offset = page_size + patched_kernel_hdr_size + |
| 1970 | kernel_actual + ramdisk_actual + second_actual + |
| 1971 | recovery_dtbo_actual; |
| 1972 | |
| 1973 | dprintf(SPEW, "Header version: %d\n", hdr->header_version); |
| 1974 | dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset); |
| 1975 | } |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1976 | #endif |
| 1977 | |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1978 | |
Saranya Chidura | b1aaf23 | 2018-11-19 15:47:50 +0530 | [diff] [blame] | 1979 | /* Validate the boot/recovery image size is within the bounds of partition size */ |
| 1980 | if (imagesize_actual > image_size) { |
| 1981 | dprintf(CRITICAL, "Image size is greater than partition size.\n"); |
| 1982 | return -1; |
| 1983 | } |
| 1984 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1985 | #if VERIFIED_BOOT |
| 1986 | boot_verifier_init(); |
| 1987 | #endif |
| 1988 | |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 1989 | #if VERIFIED_BOOT_2 |
| 1990 | /* read full partition if device is unlocked */ |
| 1991 | if (device.is_unlocked) |
| 1992 | imagesize_actual = image_size; |
| 1993 | #endif |
| 1994 | |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 1995 | if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual)) |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1996 | { |
| 1997 | dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n"); |
| 1998 | return -1; |
| 1999 | } |
| 2000 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2001 | /* |
| 2002 | * Update loading flow of bootimage to support compressed/uncompressed |
| 2003 | * bootimage on both 64bit and 32bit platform. |
| 2004 | * 1. Load bootimage from emmc partition onto DDR. |
| 2005 | * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel |
| 2006 | * 3. Check kernel header and update kernel load addr for 64bit and 32bit |
| 2007 | * platform accordingly. |
| 2008 | * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data. |
| 2009 | */ |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 2010 | if (partition_multislot_is_supported()) |
| 2011 | { |
| 2012 | current_active_slot = partition_find_active_slot(); |
| 2013 | dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n", |
| 2014 | imagesize_actual, SUFFIX_SLOT(current_active_slot)); |
| 2015 | } |
| 2016 | else |
| 2017 | { |
| 2018 | dprintf(INFO, "Loading (%s) image (%d): start\n", |
| 2019 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
| 2020 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2021 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
| 2022 | |
Gaurav Nebhwani | 43e2a46 | 2016-03-17 21:32:56 +0530 | [diff] [blame] | 2023 | if ((target_get_max_flash_size() - page_size) < imagesize_actual) |
| 2024 | { |
| 2025 | dprintf(CRITICAL, "booimage size is greater than DDR can hold\n"); |
| 2026 | return -1; |
| 2027 | } |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 2028 | offset = page_size; |
| 2029 | /* Read image without signature and header*/ |
| 2030 | 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] | 2031 | { |
| 2032 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 2033 | return -1; |
| 2034 | } |
| 2035 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 2036 | if (partition_multislot_is_supported()) |
| 2037 | { |
| 2038 | dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n", |
| 2039 | imagesize_actual, SUFFIX_SLOT(current_active_slot)); |
| 2040 | } |
| 2041 | else |
| 2042 | { |
| 2043 | dprintf(INFO, "Loading (%s) image (%d): done\n", |
Channagoud Kadabi | f0705b5 | 2015-08-20 14:16:08 -0700 | [diff] [blame] | 2044 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
| 2045 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 2046 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2047 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
| 2048 | |
| 2049 | /* Authenticate Kernel */ |
| 2050 | dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n", |
| 2051 | (int) target_use_signed_kernel(), |
| 2052 | device.is_unlocked, |
| 2053 | device.is_tampered); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2054 | #if VERIFIED_BOOT_2 |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 2055 | /* if device is unlocked skip reading signature, as full partition is read */ |
| 2056 | if (!device.is_unlocked) |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2057 | { |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 2058 | offset = imagesize_actual; |
| 2059 | if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size)) |
| 2060 | { |
| 2061 | dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n"); |
| 2062 | return -1; |
| 2063 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2064 | |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 2065 | /* Read signature */ |
| 2066 | if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size)) |
| 2067 | { |
| 2068 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 2069 | return -1; |
| 2070 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2071 | } |
| 2072 | |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2073 | /* load and validate dtbo partition */ |
| 2074 | load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz); |
| 2075 | |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 2076 | /* load vbmeta partition */ |
| 2077 | load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz); |
| 2078 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2079 | memset(&info, 0, sizeof(bootinfo)); |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2080 | |
| 2081 | /* Pass loaded boot image passed */ |
Mayank Grover | 6fd3679 | 2018-09-10 13:26:58 +0530 | [diff] [blame] | 2082 | info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr); |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2083 | info.images[IMG_BOOT].imgsize = imagesize_actual; |
| 2084 | info.images[IMG_BOOT].name = ptn_name; |
| 2085 | ++info.num_loaded_images; |
| 2086 | |
| 2087 | /* Pass loaded dtbo image */ |
| 2088 | if (dtbo_image_buf != NULL) { |
Mayank Grover | 6fd3679 | 2018-09-10 13:26:58 +0530 | [diff] [blame] | 2089 | info.images[IMG_DTBO].image_buffer = |
| 2090 | SUB_SALT_BUFF_OFFSET(dtbo_image_buf); |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2091 | info.images[IMG_DTBO].imgsize = dtbo_image_sz; |
| 2092 | info.images[IMG_DTBO].name = "dtbo"; |
| 2093 | ++info.num_loaded_images; |
| 2094 | } |
| 2095 | |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 2096 | /* Pass loaded vbmeta image */ |
| 2097 | if (vbmeta_image_buf != NULL) { |
| 2098 | info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf; |
| 2099 | info.images[IMG_VBMETA].imgsize = vbmeta_image_sz; |
| 2100 | info.images[IMG_VBMETA].name = "vbmeta"; |
| 2101 | ++info.num_loaded_images; |
| 2102 | } |
| 2103 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2104 | info.header_version = hdr->header_version; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2105 | info.multi_slot_boot = partition_multislot_is_supported(); |
| 2106 | info.bootreason_alarm = boot_reason_alarm; |
| 2107 | info.bootinto_recovery = boot_into_recovery; |
| 2108 | status = load_image_and_auth(&info); |
| 2109 | if(status) |
| 2110 | return -1; |
| 2111 | |
| 2112 | vbcmdline = info.vbcmdline; |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 2113 | |
| 2114 | /* Free the buffer allocated to vbmeta post verification */ |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 2115 | if (vbmeta_image_buf != NULL) { |
| 2116 | free(vbmeta_image_buf); |
| 2117 | --info.num_loaded_images; |
| 2118 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2119 | #else |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 2120 | /* Change the condition a little bit to include the test framework support. |
| 2121 | * We would never reach this point if device is in fastboot mode, even if we did |
| 2122 | * that means we are in test mode, so execute kernel authentication part for the |
| 2123 | * tests */ |
Parth Dixit | cb0c608 | 2015-12-30 15:01:13 +0530 | [diff] [blame] | 2124 | if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled()) |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2125 | { |
| 2126 | offset = imagesize_actual; |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 2127 | 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] | 2128 | { |
| 2129 | dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n"); |
| 2130 | return -1; |
| 2131 | } |
| 2132 | |
| 2133 | /* Read signature */ |
| 2134 | if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size)) |
| 2135 | { |
| 2136 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 2137 | return -1; |
| 2138 | } |
| 2139 | |
| 2140 | verify_signed_bootimg((uint32_t)image_addr, imagesize_actual); |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 2141 | /* The purpose of our test is done here */ |
Parth Dixit | cb0c608 | 2015-12-30 15:01:13 +0530 | [diff] [blame] | 2142 | if(is_test_mode_enabled() && auth_kernel_img) |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 2143 | return 0; |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2144 | } else { |
| 2145 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 2146 | #ifdef TZ_SAVE_KERNEL_HASH |
| 2147 | aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual); |
| 2148 | #endif /* TZ_SAVE_KERNEL_HASH */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 2149 | |
| 2150 | #ifdef MDTP_SUPPORT |
| 2151 | { |
| 2152 | /* Verify MDTP lock. |
| 2153 | * For boot & recovery partitions, MDTP will use boot_verifier APIs, |
| 2154 | * since verification was skipped in aboot. The signature is not part of the loaded image. |
| 2155 | */ |
| 2156 | mdtp_ext_partition_verification_t ext_partition; |
| 2157 | ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT; |
| 2158 | ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET; |
| 2159 | ext_partition.page_size = page_size; |
| 2160 | ext_partition.image_addr = (uint32)image_addr; |
| 2161 | ext_partition.image_size = imagesize_actual; |
| 2162 | ext_partition.sig_avail = FALSE; |
| 2163 | mdtp_fwlock_verify_lock(&ext_partition); |
| 2164 | } |
| 2165 | #endif /* MDTP_SUPPORT */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2166 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2167 | #endif |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2168 | |
Sridhar Parasuram | 00bfedb | 2015-05-26 14:21:27 -0700 | [diff] [blame] | 2169 | #if VERIFIED_BOOT |
taozhang | 93088bd | 2016-11-16 15:50:46 +0800 | [diff] [blame] | 2170 | if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm)) |
Reut Zysman | ba8a9d5 | 2016-02-18 11:44:04 +0200 | [diff] [blame] | 2171 | { |
| 2172 | #if FBCON_DISPLAY_MSG |
| 2173 | display_bootverify_menu(DISPLAY_MENU_ORANGE); |
| 2174 | wait_for_users_action(); |
| 2175 | #else |
| 2176 | dprintf(CRITICAL, |
| 2177 | "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n"); |
| 2178 | mdelay(5000); |
| 2179 | #endif |
| 2180 | } |
| 2181 | #endif |
| 2182 | |
| 2183 | #if VERIFIED_BOOT |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2184 | if (VB_M == target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 2185 | { |
| 2186 | /* set boot and system versions. */ |
| 2187 | set_os_version((unsigned char *)image_addr); |
| 2188 | // send root of trust |
| 2189 | if(!send_rot_command((uint32_t)device.is_unlocked)) |
| 2190 | ASSERT(0); |
| 2191 | } |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 2192 | #endif |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2193 | /* |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2194 | * Check if the kernel image is a gzip package. If yes, need to decompress it. |
| 2195 | * If not, continue booting. |
| 2196 | */ |
| 2197 | if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size)) |
| 2198 | { |
| 2199 | out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size); |
| 2200 | out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size; |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2201 | #if VERIFIED_BOOT_2 |
| 2202 | if (dtbo_image_sz) |
| 2203 | out_avai_len -= DTBO_IMG_BUF; |
| 2204 | #endif |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2205 | dprintf(INFO, "decompressing kernel image: start\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2206 | rc = decompress((unsigned char *)(image_addr + page_size), |
| 2207 | hdr->kernel_size, out_addr, out_avai_len, |
| 2208 | &dtb_offset, &out_len); |
| 2209 | if (rc) |
| 2210 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2211 | dprintf(CRITICAL, "decompressing kernel image failed!!!\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2212 | ASSERT(0); |
| 2213 | } |
| 2214 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2215 | dprintf(INFO, "decompressing kernel image: done\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2216 | kptr = (struct kernel64_hdr *)out_addr; |
| 2217 | kernel_start_addr = out_addr; |
| 2218 | kernel_size = out_len; |
| 2219 | } else { |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 2220 | dprintf(INFO, "Uncpmpressed kernel in use\n"); |
| 2221 | if (!strncmp((char*)(image_addr + page_size), |
| 2222 | PATCHED_KERNEL_MAGIC, |
| 2223 | sizeof(PATCHED_KERNEL_MAGIC) - 1)) { |
| 2224 | dprintf(INFO, "Patched kernel detected\n"); |
| 2225 | kptr = (struct kernel64_hdr *)(image_addr + page_size + |
| 2226 | PATCHED_KERNEL_HEADER_SIZE); |
| 2227 | //The size of the kernel is stored at start of kernel image + 16 |
| 2228 | //The dtb would start just after the kernel |
| 2229 | dtb_offset = *((uint32_t*)((unsigned char*) |
| 2230 | (image_addr + page_size + |
| 2231 | sizeof(PATCHED_KERNEL_MAGIC) - |
| 2232 | 1))); |
| 2233 | //The actual kernel starts after the 20 byte header. |
| 2234 | kernel_start_addr = (unsigned char*)(image_addr + |
| 2235 | page_size + PATCHED_KERNEL_HEADER_SIZE); |
| 2236 | kernel_size = hdr->kernel_size; |
| 2237 | patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE; |
| 2238 | } else { |
| 2239 | dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n"); |
| 2240 | kptr = (struct kernel64_hdr *)(image_addr + page_size); |
| 2241 | kernel_start_addr = (unsigned char *)(image_addr + page_size); |
| 2242 | kernel_size = hdr->kernel_size; |
| 2243 | } |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | /* |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2247 | * Update the kernel/ramdisk/tags address if the boot image header |
| 2248 | * has default values, these default values come from mkbootimg when |
| 2249 | * the boot image is flashed using fastboot flash:raw |
| 2250 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2251 | update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr)); |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2252 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2253 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 2254 | hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr)); |
| 2255 | hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr)); |
| 2256 | hdr->tags_addr = VA((addr_t)(hdr->tags_addr)); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2257 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2258 | kernel_size = ROUND_TO_PAGE(kernel_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2259 | /* Check if the addresses in the header are valid. */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2260 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) || |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2261 | check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) || |
| 2262 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) || |
| 2263 | check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2264 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2265 | dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2266 | return -1; |
| 2267 | } |
| 2268 | |
| 2269 | #ifndef DEVICE_TREE |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2270 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) || |
| 2271 | check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2272 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2273 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2274 | return -1; |
| 2275 | } |
| 2276 | #endif |
| 2277 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2278 | /* Move kernel, ramdisk and device tree to correct address */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2279 | memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2280 | 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] | 2281 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2282 | #if DEVICE_TREE |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 2283 | if(dt_size) { |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2284 | dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual); |
| 2285 | table = (struct dt_table*) dt_table_offset; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2286 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2287 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
| 2288 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 2289 | return -1; |
| 2290 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2291 | |
P.V. Phani Kumar | 8291676 | 2016-03-09 09:20:19 +0530 | [diff] [blame] | 2292 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 2293 | goes beyound hdr->dt_size*/ |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 2294 | 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] | 2295 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 2296 | return -1; |
| 2297 | } |
| 2298 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2299 | /* Find index of device tree within device tree table */ |
| 2300 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
| 2301 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 2302 | return -1; |
| 2303 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2304 | |
P.V. Phani Kumar | 3c33320 | 2016-03-09 11:54:37 +0530 | [diff] [blame] | 2305 | if(dt_entry.offset > (UINT_MAX - dt_entry.size)) { |
| 2306 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2307 | return -1; |
| 2308 | } |
| 2309 | |
| 2310 | /* Ensure we are not overshooting dt_size with the dt_entry selected */ |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 2311 | if ((dt_entry.offset + dt_entry.size) > dt_size) { |
P.V. Phani Kumar | 3c33320 | 2016-03-09 11:54:37 +0530 | [diff] [blame] | 2312 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2313 | return -1; |
| 2314 | } |
| 2315 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2316 | if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size)) |
| 2317 | { |
| 2318 | unsigned int compressed_size = 0; |
| 2319 | out_addr += out_len; |
| 2320 | out_avai_len -= out_len; |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2321 | dprintf(INFO, "decompressing dtb: start\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2322 | rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset, |
| 2323 | dt_entry.size, out_addr, out_avai_len, |
| 2324 | &compressed_size, &dtb_size); |
| 2325 | if (rc) |
| 2326 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2327 | dprintf(CRITICAL, "decompressing dtb failed!!!\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2328 | ASSERT(0); |
| 2329 | } |
| 2330 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2331 | dprintf(INFO, "decompressing dtb: done\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2332 | best_match_dt_addr = out_addr; |
| 2333 | } else { |
| 2334 | best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset; |
| 2335 | dtb_size = dt_entry.size; |
| 2336 | } |
| 2337 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2338 | /* Validate and Read device device tree in the tags_addr */ |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2339 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) || |
| 2340 | check_ddr_addr_range_bound(hdr->tags_addr, dtb_size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2341 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2342 | dprintf(CRITICAL, "Device tree addresses are not valid\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2343 | return -1; |
| 2344 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2345 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2346 | memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2347 | } else { |
| 2348 | /* Validate the tags_addr */ |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2349 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) || |
| 2350 | check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual)) |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 2351 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2352 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 2353 | return -1; |
| 2354 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2355 | /* |
| 2356 | * If appended dev tree is found, update the atags with |
| 2357 | * memory address to the DTB appended location on RAM. |
| 2358 | * Else update with the atags address in the kernel header |
Mayank Grover | 1ed3e8b | 2018-09-19 15:16:50 +0530 | [diff] [blame] | 2359 | * |
| 2360 | * Make sure everything from scratch address is read before next step! |
| 2361 | * 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] | 2362 | */ |
| 2363 | void *dtb; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 2364 | image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size); |
| 2365 | |
Nagireddy Annem | 0d60bb6 | 2019-08-16 19:16:00 +0530 | [diff] [blame] | 2366 | #ifdef OSVERSION_IN_BOOTIMAGE |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 2367 | if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) { |
| 2368 | |
| 2369 | image_buf = (void*)(image_addr); |
| 2370 | dtb_offset = dtb_image_offset; |
| 2371 | dtb_image_size = imagesize_actual; |
| 2372 | } |
Nagireddy Annem | 0d60bb6 | 2019-08-16 19:16:00 +0530 | [diff] [blame] | 2373 | #endif |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 2374 | |
| 2375 | dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset, |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 2376 | (void *)hdr->tags_addr); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2377 | if (!dtb) { |
| 2378 | dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n"); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 2379 | return -1; |
| 2380 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2381 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2382 | #endif |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2383 | |
Stanimir Varbanov | 69ec546 | 2013-07-18 18:17:42 +0300 | [diff] [blame] | 2384 | if (boot_into_recovery && !device.is_unlocked && !device.is_tampered) |
| 2385 | target_load_ssd_keystore(); |
| 2386 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2387 | unified_boot: |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2388 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 2389 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Dima Zavin | bd3daa0 | 2013-03-26 11:06:44 -0700 | [diff] [blame] | 2390 | (const char *)hdr->cmdline, board_machtype(), |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2391 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 2392 | |
| 2393 | return 0; |
| 2394 | } |
| 2395 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2396 | int boot_linux_from_flash(void) |
| 2397 | { |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2398 | boot_img_hdr *hdr = (void*) buf; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2399 | struct ptentry *ptn; |
| 2400 | struct ptable *ptable; |
| 2401 | unsigned offset = 0; |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 2402 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2403 | unsigned char *image_addr = 0; |
| 2404 | unsigned kernel_actual; |
| 2405 | unsigned ramdisk_actual; |
| 2406 | unsigned imagesize_actual; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2407 | unsigned second_actual = 0; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2408 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2409 | #if DEVICE_TREE |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2410 | struct dt_table *table = NULL; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 2411 | struct dt_entry dt_entry; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2412 | unsigned dt_table_offset; |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 2413 | uint32_t dt_actual; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2414 | uint32_t dt_hdr_size = 0; |
Vivek Kumar | 07bd086 | 2018-04-10 14:19:23 +0530 | [diff] [blame] | 2415 | uint32_t dtb_offset = 0; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2416 | unsigned int dtb_size = 0; |
| 2417 | unsigned char *best_match_dt_addr = NULL; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2418 | #endif |
| 2419 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 2420 | if (target_is_emmc_boot()) { |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2421 | hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 2422 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 2423 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
| 2424 | return -1; |
| 2425 | } |
| 2426 | goto continue_boot; |
| 2427 | } |
| 2428 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2429 | ptable = flash_get_ptable(); |
| 2430 | if (ptable == NULL) { |
| 2431 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 2432 | return -1; |
| 2433 | } |
| 2434 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 2435 | if(!boot_into_recovery) |
| 2436 | { |
| 2437 | ptn = ptable_find(ptable, "boot"); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2438 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 2439 | if (ptn == NULL) { |
| 2440 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 2441 | return -1; |
| 2442 | } |
| 2443 | } |
| 2444 | else |
| 2445 | { |
| 2446 | ptn = ptable_find(ptable, "recovery"); |
| 2447 | if (ptn == NULL) { |
| 2448 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 2449 | return -1; |
| 2450 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2451 | } |
| 2452 | |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2453 | /* Read boot.img header from flash */ |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 2454 | if (flash_read(ptn, offset, buf, page_size)) { |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2455 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 2456 | return -1; |
| 2457 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2458 | |
| 2459 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 2460 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2461 | return -1; |
| 2462 | } |
| 2463 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 2464 | if (hdr->page_size != page_size) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 2465 | 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] | 2466 | return -1; |
| 2467 | } |
| 2468 | |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 2469 | image_addr = (unsigned char *)target_get_scratch_address(); |
| 2470 | memcpy(image_addr, (void *)buf, page_size); |
vijay kumar | 287bb54 | 2015-09-29 13:01:52 +0530 | [diff] [blame] | 2471 | |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2472 | /* |
| 2473 | * Update the kernel/ramdisk/tags address if the boot image header |
| 2474 | * has default values, these default values come from mkbootimg when |
| 2475 | * the boot image is flashed using fastboot flash:raw |
| 2476 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2477 | update_ker_tags_rdisk_addr(hdr, false); |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2478 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2479 | /* Get virtual addresses since the hdr saves physical addresses. */ |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2480 | hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr)); |
| 2481 | hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr)); |
| 2482 | hdr->tags_addr = VA((addr_t)(hdr->tags_addr)); |
| 2483 | |
| 2484 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 2485 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2486 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2487 | |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 2488 | /* ensure commandline is terminated */ |
| 2489 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 2490 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2491 | /* Check if the addresses in the header are valid. */ |
| 2492 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) || |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2493 | check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) || |
| 2494 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) || |
| 2495 | check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2496 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2497 | dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2498 | return -1; |
| 2499 | } |
| 2500 | |
| 2501 | #ifndef DEVICE_TREE |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2502 | 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] | 2503 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n"); |
| 2504 | return -1; |
| 2505 | } |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2506 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2507 | |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2508 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) || |
| 2509 | check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE)) |
Vijay Kumar Pendoti | d3ed20e | 2016-09-20 00:34:46 +0530 | [diff] [blame] | 2510 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2511 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
Vijay Kumar Pendoti | d3ed20e | 2016-09-20 00:34:46 +0530 | [diff] [blame] | 2512 | return -1; |
| 2513 | } |
| 2514 | #else |
| 2515 | |
| 2516 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 2517 | dt_size = hdr->dt_size; |
| 2518 | #endif |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2519 | dt_actual = ROUND_TO_PAGE(dt_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2520 | 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] | 2521 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n"); |
| 2522 | return -1; |
| 2523 | } |
| 2524 | |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2525 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2526 | |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2527 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) || |
| 2528 | check_ddr_addr_range_bound(hdr->tags_addr, dt_size)) |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2529 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2530 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2531 | return -1; |
| 2532 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2533 | #endif |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2534 | |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2535 | /* Read full boot.img from flash */ |
| 2536 | dprintf(INFO, "Loading (%s) image (%d): start\n", |
| 2537 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
| 2538 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
| 2539 | |
| 2540 | if (UINT_MAX - page_size < imagesize_actual) |
| 2541 | { |
| 2542 | dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__); |
| 2543 | return -1; |
| 2544 | } |
| 2545 | |
| 2546 | /*Check the availability of RAM before reading boot image + max signature length from flash*/ |
| 2547 | if (target_get_max_flash_size() < (imagesize_actual + page_size)) |
| 2548 | { |
| 2549 | dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n"); |
| 2550 | return -1; |
| 2551 | } |
| 2552 | |
| 2553 | offset = page_size; |
| 2554 | /* Read image without signature and header */ |
| 2555 | if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size)) |
| 2556 | { |
| 2557 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 2558 | return -1; |
| 2559 | } |
| 2560 | |
| 2561 | dprintf(INFO, "Loading (%s) image (%d): done\n", |
| 2562 | (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual); |
| 2563 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
| 2564 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2565 | /* Authenticate Kernel */ |
Deepa Dinamani | 23b60d4 | 2013-06-24 18:10:52 -0700 | [diff] [blame] | 2566 | if(target_use_signed_kernel() && (!device.is_unlocked)) |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2567 | { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2568 | offset = imagesize_actual; |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2569 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2570 | /* Read signature */ |
| 2571 | if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size)) |
| 2572 | { |
| 2573 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2574 | return -1; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2575 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2576 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 2577 | verify_signed_bootimg((uint32_t)image_addr, imagesize_actual); |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2578 | } |
| 2579 | offset = page_size; |
| 2580 | if(hdr->second_size != 0) { |
| 2581 | if (UINT_MAX - offset < second_actual) |
| 2582 | { |
| 2583 | dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__); |
| 2584 | return -1; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2585 | } |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2586 | offset += second_actual; |
| 2587 | /* Second image loading not implemented. */ |
| 2588 | ASSERT(0); |
| 2589 | } |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2590 | /* Move kernel and ramdisk to correct address */ |
| 2591 | memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size); |
| 2592 | memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size); |
| 2593 | |
| 2594 | #if DEVICE_TREE |
| 2595 | if(dt_size != 0) { |
| 2596 | |
| 2597 | dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual); |
| 2598 | |
| 2599 | table = (struct dt_table*) dt_table_offset; |
| 2600 | |
| 2601 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
| 2602 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 2603 | return -1; |
| 2604 | } |
| 2605 | |
| 2606 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 2607 | goes beyound hdr->dt_size*/ |
| 2608 | if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) { |
| 2609 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 2610 | return -1; |
| 2611 | } |
| 2612 | |
| 2613 | /* Find index of device tree within device tree table */ |
| 2614 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
| 2615 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 2616 | return -1; |
| 2617 | } |
| 2618 | |
| 2619 | /* Validate and Read device device tree in the "tags_add */ |
| 2620 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) || |
| 2621 | check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size)) |
| 2622 | { |
| 2623 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
| 2624 | return -1; |
| 2625 | } |
| 2626 | |
| 2627 | if(dt_entry.offset > (UINT_MAX - dt_entry.size)) { |
| 2628 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2629 | return -1; |
| 2630 | } |
| 2631 | |
| 2632 | /* Ensure we are not overshooting dt_size with the dt_entry selected */ |
| 2633 | if ((dt_entry.offset + dt_entry.size) > dt_size) { |
| 2634 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2635 | return -1; |
| 2636 | } |
| 2637 | |
| 2638 | best_match_dt_addr = (unsigned char *)table + dt_entry.offset; |
| 2639 | dtb_size = dt_entry.size; |
| 2640 | memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size); |
Vivek Kumar | 07bd086 | 2018-04-10 14:19:23 +0530 | [diff] [blame] | 2641 | |
| 2642 | } else { |
| 2643 | /* Validate the tags_addr */ |
| 2644 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) || |
| 2645 | check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual)) |
| 2646 | { |
| 2647 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
| 2648 | return -1; |
| 2649 | } |
| 2650 | /* |
| 2651 | * If appended dev tree is found, update the atags with |
| 2652 | * memory address to the DTB appended location on RAM. |
| 2653 | * Else update with the atags address in the kernel header |
Mayank Grover | 1ed3e8b | 2018-09-19 15:16:50 +0530 | [diff] [blame] | 2654 | * |
| 2655 | * Make sure everything from scratch address is read before next step! |
| 2656 | * 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] | 2657 | */ |
| 2658 | void *dtb = NULL; |
| 2659 | dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr); |
| 2660 | if (!dtb) { |
| 2661 | dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n"); |
| 2662 | return -1; |
| 2663 | } |
| 2664 | } |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2665 | #endif |
| 2666 | if(target_use_signed_kernel() && (!device.is_unlocked)) |
| 2667 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2668 | /* Make sure everything from scratch address is read before next step!*/ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2669 | if(device.is_tampered) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2670 | { |
| 2671 | write_device_info_flash(&device); |
| 2672 | } |
Channagoud Kadabi | 5c86fe3 | 2012-02-16 10:58:48 +0530 | [diff] [blame] | 2673 | #if USE_PCOM_SECBOOT |
| 2674 | set_tamper_flag(device.is_tampered); |
| 2675 | #endif |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2676 | } |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 2677 | continue_boot: |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2678 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2679 | /* TODO: create/pass atags to kernel */ |
| 2680 | |
Ajay Dudani | e28a607 | 2011-07-01 13:59:46 -0700 | [diff] [blame] | 2681 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Dima Zavin | bd3daa0 | 2013-03-26 11:06:44 -0700 | [diff] [blame] | 2682 | (const char *)hdr->cmdline, board_machtype(), |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2683 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 2684 | |
| 2685 | return 0; |
| 2686 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2687 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2688 | void write_device_info_mmc(device_info *dev) |
| 2689 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2690 | unsigned long long ptn = 0; |
| 2691 | unsigned long long size; |
| 2692 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2693 | uint8_t lun = 0; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2694 | uint32_t ret = 0; |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2695 | uint32_t device_info_sz = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2696 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2697 | if (devinfo_present) |
| 2698 | index = partition_get_index("devinfo"); |
| 2699 | else |
| 2700 | index = partition_get_index("aboot"); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 2701 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2702 | ptn = partition_get_offset(index); |
| 2703 | if(ptn == 0) |
| 2704 | { |
| 2705 | return; |
| 2706 | } |
| 2707 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2708 | lun = partition_get_lun(index); |
| 2709 | mmc_set_lun(lun); |
| 2710 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2711 | size = partition_get_size(index); |
| 2712 | |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2713 | device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info), |
| 2714 | mmc_blocksize_mask); |
| 2715 | if (device_info_sz == UINT_MAX) |
| 2716 | { |
| 2717 | dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n"); |
| 2718 | return; |
| 2719 | } |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 2720 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2721 | if (devinfo_present) |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2722 | ret = mmc_write(ptn, device_info_sz, (void *)info_buf); |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2723 | else |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2724 | 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] | 2725 | if (ret) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2726 | { |
| 2727 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 2728 | ASSERT(0); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2729 | } |
| 2730 | } |
| 2731 | |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2732 | void read_device_info_mmc(struct device_info *info) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2733 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2734 | unsigned long long ptn = 0; |
| 2735 | unsigned long long size; |
| 2736 | int index = INVALID_PTN; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2737 | uint32_t ret = 0; |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2738 | uint32_t device_info_sz = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2739 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2740 | if ((index = partition_get_index("devinfo")) < 0) |
| 2741 | { |
| 2742 | devinfo_present = false; |
| 2743 | index = partition_get_index("aboot"); |
| 2744 | } |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 2745 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2746 | ptn = partition_get_offset(index); |
| 2747 | if(ptn == 0) |
| 2748 | { |
| 2749 | return; |
| 2750 | } |
| 2751 | |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 2752 | mmc_set_lun(partition_get_lun(index)); |
| 2753 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2754 | size = partition_get_size(index); |
| 2755 | |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2756 | device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info), |
| 2757 | mmc_blocksize_mask); |
| 2758 | if (device_info_sz == UINT_MAX) |
| 2759 | { |
| 2760 | dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n"); |
| 2761 | return; |
| 2762 | } |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 2763 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2764 | if (devinfo_present) |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2765 | ret = mmc_read(ptn, (void *)info_buf, device_info_sz); |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2766 | else |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2767 | 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] | 2768 | if (ret) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2769 | { |
| 2770 | dprintf(CRITICAL, "ERROR: Cannot read device info\n"); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 2771 | ASSERT(0); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2772 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2773 | } |
| 2774 | |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 2775 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 2776 | void write_prod_info_mmc(prod_info *dev) |
| 2777 | { |
| 2778 | unsigned long long ptn = 0; |
| 2779 | unsigned long long size; |
| 2780 | int index = INVALID_PTN; |
| 2781 | uint32_t blocksize; |
| 2782 | uint8_t lun = 0; |
| 2783 | uint32_t ret = 0; |
| 2784 | |
| 2785 | if (prodinfo_present) |
| 2786 | index = partition_get_index("prodinfo"); |
| 2787 | else |
| 2788 | index = partition_get_index("aboot"); |
| 2789 | |
| 2790 | ptn = partition_get_offset(index); |
| 2791 | if(ptn == 0) |
| 2792 | { |
| 2793 | return; |
| 2794 | } |
| 2795 | |
| 2796 | lun = partition_get_lun(index); |
| 2797 | mmc_set_lun(lun); |
| 2798 | |
| 2799 | size = partition_get_size(index); |
| 2800 | |
| 2801 | blocksize = mmc_get_device_blocksize(); |
| 2802 | |
| 2803 | if (prodinfo_present) |
| 2804 | ret = mmc_write(ptn, blocksize, (void *)prodinfo_buf); |
| 2805 | else |
| 2806 | ret = mmc_write((ptn + size - blocksize), blocksize, (void *)prodinfo_buf); |
| 2807 | if (ret) |
| 2808 | { |
| 2809 | dprintf(CRITICAL, "ERROR: Cannot write prod info\n"); |
| 2810 | ASSERT(0); |
| 2811 | } |
| 2812 | } |
| 2813 | |
| 2814 | void read_prod_info_mmc(struct prod_info *info) |
| 2815 | { |
| 2816 | unsigned long long ptn = 0; |
| 2817 | unsigned long long size; |
| 2818 | int index = INVALID_PTN; |
| 2819 | uint32_t blocksize; |
| 2820 | uint32_t ret = 0; |
| 2821 | |
| 2822 | if ((index = partition_get_index("prodinfo")) < 0) |
| 2823 | { |
| 2824 | prodinfo_present = false; |
| 2825 | index = partition_get_index("aboot"); |
| 2826 | } |
| 2827 | |
| 2828 | ptn = partition_get_offset(index); |
| 2829 | if(ptn == 0) |
| 2830 | { |
| 2831 | return; |
| 2832 | } |
| 2833 | |
| 2834 | mmc_set_lun(partition_get_lun(index)); |
| 2835 | |
| 2836 | size = partition_get_size(index); |
| 2837 | |
| 2838 | blocksize = mmc_get_device_blocksize(); |
| 2839 | |
| 2840 | if (prodinfo_present) |
| 2841 | ret = mmc_read(ptn, (void *)prodinfo_buf, blocksize); |
| 2842 | else |
| 2843 | ret = mmc_read((ptn + size - blocksize), (void *)prodinfo_buf, blocksize); |
| 2844 | if (ret) |
| 2845 | { |
| 2846 | dprintf(CRITICAL, "ERROR: Cannot read prod info\n"); |
| 2847 | ASSERT(0); |
| 2848 | } |
| 2849 | } |
| 2850 | #endif |
| 2851 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2852 | void write_device_info_flash(device_info *dev) |
| 2853 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2854 | 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] | 2855 | struct ptentry *ptn; |
| 2856 | struct ptable *ptable; |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2857 | if(info == NULL) |
| 2858 | { |
| 2859 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 2860 | ASSERT(0); |
| 2861 | } |
| 2862 | info_buf = info; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2863 | ptable = flash_get_ptable(); |
| 2864 | if (ptable == NULL) |
| 2865 | { |
| 2866 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 2867 | return; |
| 2868 | } |
| 2869 | |
| 2870 | ptn = ptable_find(ptable, "devinfo"); |
| 2871 | if (ptn == NULL) |
| 2872 | { |
Smita Ghosh | 670c8b8 | 2015-05-07 09:30:03 -0700 | [diff] [blame] | 2873 | dprintf(CRITICAL, "ERROR: No devinfo partition found\n"); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2874 | return; |
| 2875 | } |
| 2876 | |
Mayank Grover | dedbc89 | 2017-10-24 13:41:34 +0530 | [diff] [blame] | 2877 | memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2878 | memcpy(info, dev, sizeof(device_info)); |
| 2879 | |
| 2880 | if (flash_write(ptn, 0, (void *)info_buf, page_size)) |
| 2881 | { |
| 2882 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 2883 | return; |
| 2884 | } |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2885 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2886 | } |
| 2887 | |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2888 | static int read_allow_oem_unlock(device_info *dev) |
| 2889 | { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2890 | unsigned offset; |
| 2891 | int index; |
| 2892 | unsigned long long ptn; |
| 2893 | unsigned long long ptn_size; |
| 2894 | unsigned blocksize = mmc_get_device_blocksize(); |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2895 | STACKBUF_DMA_ALIGN(buf, blocksize); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2896 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2897 | index = partition_get_index(frp_ptns[0]); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2898 | if (index == INVALID_PTN) |
| 2899 | { |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2900 | index = partition_get_index(frp_ptns[1]); |
| 2901 | if (index == INVALID_PTN) |
| 2902 | { |
| 2903 | dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]); |
| 2904 | return -1; |
| 2905 | } |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2906 | } |
| 2907 | |
| 2908 | ptn = partition_get_offset(index); |
| 2909 | ptn_size = partition_get_size(index); |
| 2910 | offset = ptn_size - blocksize; |
| 2911 | |
Mayank Grover | 4886040 | 2016-11-29 12:34:53 +0530 | [diff] [blame] | 2912 | /* Set Lun for partition */ |
| 2913 | mmc_set_lun(partition_get_lun(index)); |
| 2914 | |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2915 | if (mmc_read(ptn + offset, (void *)buf, blocksize)) |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2916 | { |
| 2917 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 2918 | return -1; |
| 2919 | } |
| 2920 | |
| 2921 | /*is_allow_unlock is a bool value stored at the LSB of last byte*/ |
| 2922 | is_allow_unlock = buf[blocksize-1] & 0x01; |
| 2923 | return 0; |
| 2924 | } |
| 2925 | |
| 2926 | static int write_allow_oem_unlock(bool allow_unlock) |
| 2927 | { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2928 | unsigned offset; |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2929 | int index; |
| 2930 | unsigned long long ptn; |
| 2931 | unsigned long long ptn_size; |
| 2932 | unsigned blocksize = mmc_get_device_blocksize(); |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2933 | STACKBUF_DMA_ALIGN(buf, blocksize); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2934 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2935 | index = partition_get_index(frp_ptns[0]); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2936 | if (index == INVALID_PTN) |
| 2937 | { |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2938 | index = partition_get_index(frp_ptns[1]); |
| 2939 | if (index == INVALID_PTN) |
| 2940 | { |
| 2941 | dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]); |
| 2942 | return -1; |
| 2943 | } |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | ptn = partition_get_offset(index); |
| 2947 | ptn_size = partition_get_size(index); |
| 2948 | offset = ptn_size - blocksize; |
Mayank Grover | 4886040 | 2016-11-29 12:34:53 +0530 | [diff] [blame] | 2949 | mmc_set_lun(partition_get_lun(index)); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2950 | |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2951 | if (mmc_read(ptn + offset, (void *)buf, blocksize)) |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2952 | { |
| 2953 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 2954 | return -1; |
| 2955 | } |
| 2956 | |
| 2957 | /*is_allow_unlock is a bool value stored at the LSB of last byte*/ |
| 2958 | buf[blocksize-1] = allow_unlock; |
| 2959 | if (mmc_write(ptn + offset, blocksize, buf)) |
| 2960 | { |
| 2961 | dprintf(CRITICAL, "Writing MMC failed\n"); |
| 2962 | return -1; |
| 2963 | } |
| 2964 | |
| 2965 | return 0; |
| 2966 | } |
| 2967 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2968 | void read_device_info_flash(device_info *dev) |
| 2969 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2970 | 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] | 2971 | struct ptentry *ptn; |
| 2972 | struct ptable *ptable; |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2973 | if(info == NULL) |
| 2974 | { |
| 2975 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 2976 | ASSERT(0); |
| 2977 | } |
| 2978 | info_buf = info; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2979 | ptable = flash_get_ptable(); |
| 2980 | if (ptable == NULL) |
| 2981 | { |
| 2982 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 2983 | return; |
| 2984 | } |
| 2985 | |
| 2986 | ptn = ptable_find(ptable, "devinfo"); |
| 2987 | if (ptn == NULL) |
| 2988 | { |
Smita Ghosh | 670c8b8 | 2015-05-07 09:30:03 -0700 | [diff] [blame] | 2989 | dprintf(CRITICAL, "ERROR: No devinfo partition found\n"); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2990 | return; |
| 2991 | } |
| 2992 | |
| 2993 | if (flash_read(ptn, 0, (void *)info_buf, page_size)) |
| 2994 | { |
| 2995 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 2996 | return; |
| 2997 | } |
| 2998 | |
| 2999 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 3000 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3001 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
| 3002 | info->is_unlocked = 0; |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3003 | info->is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3004 | write_device_info_flash(info); |
| 3005 | } |
| 3006 | memcpy(dev, info, sizeof(device_info)); |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 3007 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | void write_device_info(device_info *dev) |
| 3011 | { |
| 3012 | if(target_is_emmc_boot()) |
| 3013 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 3014 | struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 3015 | if(info == NULL) |
| 3016 | { |
| 3017 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 3018 | ASSERT(0); |
| 3019 | } |
| 3020 | info_buf = info; |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3021 | memcpy(info, dev, sizeof(struct device_info)); |
| 3022 | |
| 3023 | #if USE_RPMB_FOR_DEVINFO |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3024 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3025 | is_secure_boot_enable()) { |
| 3026 | if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0) |
| 3027 | ASSERT(0); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 3028 | } |
Channagoud Kadabi | c80cb49 | 2015-04-28 16:08:28 -0700 | [diff] [blame] | 3029 | else |
| 3030 | write_device_info_mmc(info); |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3031 | #else |
| 3032 | write_device_info_mmc(info); |
| 3033 | #endif |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 3034 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3035 | } |
| 3036 | else |
| 3037 | { |
| 3038 | write_device_info_flash(dev); |
| 3039 | } |
| 3040 | } |
| 3041 | |
Monika Singh | 9431646 | 2018-03-15 18:39:01 +0530 | [diff] [blame] | 3042 | int read_rollback_index(uint32_t loc, uint64_t *roll_back_index) |
| 3043 | { |
| 3044 | if (!devinfo_present) { |
| 3045 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3046 | return -EINVAL; |
| 3047 | } |
| 3048 | if (loc >= ARRAY_SIZE(device.rollback_index)) { |
| 3049 | dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n", |
| 3050 | __func__, loc, ARRAY_SIZE(device.rollback_index)); |
| 3051 | ASSERT(0); |
| 3052 | } |
| 3053 | |
| 3054 | *roll_back_index = device.rollback_index[loc]; |
| 3055 | return 0; |
| 3056 | } |
| 3057 | |
| 3058 | int write_rollback_index(uint32_t loc, uint64_t roll_back_index) |
| 3059 | { |
| 3060 | if (!devinfo_present) { |
| 3061 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3062 | return -EINVAL; |
| 3063 | } |
| 3064 | if (loc >= ARRAY_SIZE(device.rollback_index)) { |
| 3065 | dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n", |
| 3066 | __func__, loc, ARRAY_SIZE(device.rollback_index)); |
| 3067 | ASSERT(0); |
| 3068 | } |
| 3069 | |
| 3070 | device.rollback_index[loc] = roll_back_index; |
| 3071 | write_device_info(&device); |
| 3072 | return 0; |
| 3073 | } |
| 3074 | |
Monika Singh | b0fad82 | 2018-03-15 18:50:55 +0530 | [diff] [blame] | 3075 | int store_userkey(uint8_t *user_key, uint32_t user_key_size) |
| 3076 | { |
| 3077 | if (!devinfo_present) { |
| 3078 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3079 | return -EINVAL; |
| 3080 | } |
| 3081 | |
| 3082 | if (user_key_size > ARRAY_SIZE(device.user_public_key)) { |
| 3083 | dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n"); |
| 3084 | return -ENODEV; |
| 3085 | } |
| 3086 | |
| 3087 | memcpy(device.user_public_key, user_key, user_key_size); |
| 3088 | device.user_public_key_length = user_key_size; |
| 3089 | write_device_info(&device); |
| 3090 | return 0; |
| 3091 | } |
| 3092 | |
| 3093 | int erase_userkey() |
| 3094 | { |
| 3095 | if (!devinfo_present) { |
| 3096 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3097 | return -EINVAL; |
| 3098 | } |
| 3099 | memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key)); |
| 3100 | device.user_public_key_length = 0; |
| 3101 | write_device_info(&device); |
| 3102 | return 0; |
| 3103 | } |
| 3104 | |
| 3105 | int get_userkey(uint8_t **user_key, uint32_t *user_key_size) |
| 3106 | { |
| 3107 | if (!devinfo_present) { |
| 3108 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3109 | return -EINVAL; |
| 3110 | } |
| 3111 | *user_key = device.user_public_key; |
| 3112 | *user_key_size = device.user_public_key_length; |
| 3113 | return 0; |
| 3114 | } |
| 3115 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3116 | void read_device_info(device_info *dev) |
| 3117 | { |
| 3118 | if(target_is_emmc_boot()) |
| 3119 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 3120 | struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 3121 | if(info == NULL) |
| 3122 | { |
| 3123 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 3124 | ASSERT(0); |
| 3125 | } |
| 3126 | info_buf = info; |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3127 | |
| 3128 | #if USE_RPMB_FOR_DEVINFO |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3129 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3130 | is_secure_boot_enable()) { |
| 3131 | if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0) |
| 3132 | ASSERT(0); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 3133 | } |
Channagoud Kadabi | c80cb49 | 2015-04-28 16:08:28 -0700 | [diff] [blame] | 3134 | else |
| 3135 | read_device_info_mmc(info); |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3136 | #else |
| 3137 | read_device_info_mmc(info); |
| 3138 | #endif |
| 3139 | |
| 3140 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 3141 | { |
| 3142 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3143 | if (is_secure_boot_enable()) { |
Channagoud Kadabi | 05f78ba | 2015-07-06 11:58:14 -0700 | [diff] [blame] | 3144 | info->is_unlocked = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3145 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3146 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3147 | info->is_unlock_critical = 0; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3148 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3149 | } else { |
Channagoud Kadabi | 2fda409 | 2015-07-07 13:34:11 -0700 | [diff] [blame] | 3150 | info->is_unlocked = 1; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3151 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3152 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3153 | info->is_unlock_critical = 1; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3154 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3155 | } |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3156 | info->is_tampered = 0; |
| 3157 | info->charger_screen_enabled = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3158 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3159 | if (VB_M <= target_get_vb_version()) |
Monika Singh | 1c71a1c | 2019-12-03 12:12:48 +0530 | [diff] [blame] | 3160 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3161 | info->verity_mode = 1; //enforcing by default |
Monika Singh | 1c71a1c | 2019-12-03 12:12:48 +0530 | [diff] [blame] | 3162 | info->user_public_key_length = 0; |
| 3163 | memset(info->rollback_index, 0, sizeof(info->rollback_index)); |
| 3164 | memset(info->user_public_key, 0, sizeof(info->user_public_key)); |
| 3165 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3166 | #endif |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3167 | write_device_info(info); |
| 3168 | } |
jessicatseng | 8e7644c | 2020-04-22 11:13:20 +0800 | [diff] [blame] | 3169 | |
| 3170 | info->charger_screen_enabled = 1; |
| 3171 | write_device_info(info); |
| 3172 | |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3173 | memcpy(dev, info, sizeof(device_info)); |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 3174 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3175 | } |
| 3176 | else |
| 3177 | { |
| 3178 | read_device_info_flash(dev); |
| 3179 | } |
| 3180 | } |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 3181 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 3182 | void write_prod_info(prod_info *dev) |
| 3183 | { |
| 3184 | if(target_is_emmc_boot()) |
| 3185 | { |
| 3186 | struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 3187 | if(info == NULL) |
| 3188 | { |
| 3189 | dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n"); |
| 3190 | ASSERT(0); |
| 3191 | } |
| 3192 | prodinfo_buf = info; |
| 3193 | memcpy(info, dev, sizeof(struct prod_info)); |
| 3194 | |
| 3195 | write_prod_info_mmc(info); |
| 3196 | free(info); |
| 3197 | } |
| 3198 | } |
| 3199 | |
| 3200 | void read_prod_info(prod_info *dev) |
| 3201 | { |
| 3202 | if(target_is_emmc_boot()) |
| 3203 | { |
| 3204 | struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 3205 | if(info == NULL) |
| 3206 | { |
| 3207 | dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n"); |
| 3208 | ASSERT(0); |
| 3209 | } |
| 3210 | prodinfo_buf = info; |
| 3211 | |
| 3212 | read_prod_info_mmc(info); |
| 3213 | |
| 3214 | if (memcmp(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE)) |
| 3215 | { |
| 3216 | memcpy(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE); |
| 3217 | memcpy(info->isn, "No_Serial_Number", PRODINFO_MAX_ISN_LEN); |
| 3218 | memcpy(info->ssn, "No_Custer_Serial_Number", PRODINFO_MAX_SSN_LEN); |
| 3219 | info->is_adb_enabled = 0; |
| 3220 | write_prod_info(info); |
| 3221 | } |
| 3222 | memcpy(dev, info, sizeof(prod_info)); |
| 3223 | free(info); |
| 3224 | } |
| 3225 | } |
| 3226 | #endif |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3227 | |
| 3228 | void reset_device_info() |
| 3229 | { |
| 3230 | dprintf(ALWAYS, "reset_device_info called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3231 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3232 | write_device_info(&device); |
| 3233 | } |
| 3234 | |
| 3235 | void set_device_root() |
| 3236 | { |
| 3237 | dprintf(ALWAYS, "set_device_root called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3238 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3239 | write_device_info(&device); |
| 3240 | } |
| 3241 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3242 | /* set device unlock value |
| 3243 | * Must check FRP before call this function |
| 3244 | * Need to wipe data when unlock status changed |
| 3245 | * type 0: oem unlock |
| 3246 | * type 1: unlock critical |
| 3247 | * status 0: unlock as false |
| 3248 | * status 1: lock as true |
| 3249 | */ |
| 3250 | void set_device_unlock_value(int type, bool status) |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 3251 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3252 | if (type == UNLOCK) |
| 3253 | device.is_unlocked = status; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3254 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3255 | else if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3256 | type == UNLOCK_CRITICAL) |
| 3257 | device.is_unlock_critical = status; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3258 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3259 | write_device_info(&device); |
| 3260 | } |
| 3261 | |
| 3262 | static void set_device_unlock(int type, bool status) |
| 3263 | { |
| 3264 | int is_unlocked = -1; |
| 3265 | char response[MAX_RSP_SIZE]; |
| 3266 | |
| 3267 | /* check device unlock status if it is as expected */ |
| 3268 | if (type == UNLOCK) |
| 3269 | is_unlocked = device.is_unlocked; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3270 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3271 | if(VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3272 | type == UNLOCK_CRITICAL) |
| 3273 | { |
| 3274 | is_unlocked = device.is_unlock_critical; |
| 3275 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3276 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3277 | if (is_unlocked == status) { |
| 3278 | snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!")); |
| 3279 | fastboot_info(response); |
| 3280 | fastboot_okay(""); |
| 3281 | return; |
| 3282 | } |
| 3283 | |
| 3284 | /* status is true, it means to unlock device */ |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 3285 | if (status && !is_allow_unlock) { |
| 3286 | fastboot_fail("oem unlock is not allowed"); |
| 3287 | return; |
| 3288 | } |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 3289 | |
ericlin | 4e397c4 | 2020-04-29 14:14:24 +0800 | [diff] [blame] | 3290 | if(target_build_variant_user()){ |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3291 | #if FBCON_DISPLAY_MSG |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 3292 | display_unlock_menu(type, status); |
| 3293 | fastboot_okay(""); |
| 3294 | return; |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3295 | #else |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 3296 | if (status && type == UNLOCK) { |
| 3297 | fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'"); |
| 3298 | return; |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 3299 | } |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 3300 | #endif |
ericlin | 4e397c4 | 2020-04-29 14:14:24 +0800 | [diff] [blame] | 3301 | } |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3302 | |
| 3303 | set_device_unlock_value(type, status); |
| 3304 | |
| 3305 | /* wipe data */ |
| 3306 | struct recovery_message msg; |
Kishor PK | 60a6821 | 2017-05-08 16:55:57 +0530 | [diff] [blame] | 3307 | memset(&msg, 0, sizeof(msg)); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3308 | snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data"); |
| 3309 | write_misc(0, &msg, sizeof(msg)); |
| 3310 | |
| 3311 | fastboot_okay(""); |
ericlin | 4e397c4 | 2020-04-29 14:14:24 +0800 | [diff] [blame] | 3312 | //reboot_device(RECOVERY_MODE); |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 3313 | } |
| 3314 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3315 | static bool critical_flash_allowed(const char * entry) |
| 3316 | { |
| 3317 | uint32_t i = 0; |
| 3318 | if (entry == NULL) |
| 3319 | return false; |
| 3320 | |
| 3321 | for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) { |
| 3322 | if(!strcmp(entry, critical_flash_allowed_ptn[i])) |
| 3323 | return true; |
| 3324 | } |
| 3325 | return false; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | #if DEVICE_TREE |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3329 | int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset) |
| 3330 | { |
| 3331 | uint32 dt_image_offset = 0; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3332 | uint32_t n; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3333 | struct dt_table *table = NULL; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3334 | struct dt_entry dt_entry; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3335 | uint32_t dt_hdr_size = 0; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3336 | unsigned int compressed_size = 0; |
| 3337 | unsigned int dtb_size = 0; |
| 3338 | unsigned int out_avai_len = 0; |
| 3339 | unsigned char *out_addr = NULL; |
| 3340 | unsigned char *best_match_dt_addr = NULL; |
| 3341 | int rc; |
| 3342 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 3343 | boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start); |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3344 | |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 3345 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 3346 | dt_size = hdr->dt_size; |
| 3347 | #endif |
| 3348 | |
| 3349 | if(dt_size != 0) { |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3350 | /* add kernel offset */ |
| 3351 | dt_image_offset += page_size; |
| 3352 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 3353 | dt_image_offset += n; |
| 3354 | |
| 3355 | /* add ramdisk offset */ |
| 3356 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 3357 | dt_image_offset += n; |
| 3358 | |
| 3359 | /* add second offset */ |
| 3360 | if(hdr->second_size != 0) { |
| 3361 | n = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 3362 | dt_image_offset += n; |
| 3363 | } |
| 3364 | |
| 3365 | /* offset now point to start of dt.img */ |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 3366 | table = (struct dt_table*)(boot_image_start + dt_image_offset); |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3367 | |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 3368 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3369 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 3370 | return -1; |
| 3371 | } |
P.V. Phani Kumar | 8291676 | 2016-03-09 09:20:19 +0530 | [diff] [blame] | 3372 | |
| 3373 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 3374 | goes beyound hdr->dt_size*/ |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 3375 | 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] | 3376 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 3377 | return -1; |
| 3378 | } |
| 3379 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 3380 | /* Find index of device tree within device tree table */ |
| 3381 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3382 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 3383 | return -1; |
| 3384 | } |
| 3385 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3386 | best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset; |
| 3387 | if (is_gzip_package(best_match_dt_addr, dt_entry.size)) |
| 3388 | { |
| 3389 | out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset; |
| 3390 | out_avai_len = target_get_max_flash_size() - scratch_offset; |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3391 | dprintf(INFO, "decompressing dtb: start\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3392 | rc = decompress(best_match_dt_addr, |
| 3393 | dt_entry.size, out_addr, out_avai_len, |
| 3394 | &compressed_size, &dtb_size); |
| 3395 | if (rc) |
| 3396 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3397 | dprintf(CRITICAL, "decompressing dtb failed!!!\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3398 | ASSERT(0); |
| 3399 | } |
| 3400 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3401 | dprintf(INFO, "decompressing dtb: done\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3402 | best_match_dt_addr = out_addr; |
| 3403 | } else { |
| 3404 | dtb_size = dt_entry.size; |
| 3405 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3406 | /* Validate and Read device device tree in the "tags_add */ |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3407 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) || |
| 3408 | check_ddr_addr_range_bound(hdr->tags_addr, dtb_size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3409 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3410 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3411 | return -1; |
| 3412 | } |
| 3413 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3414 | /* Read device device tree in the "tags_add */ |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3415 | memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3416 | } else |
| 3417 | return -1; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3418 | |
| 3419 | /* Everything looks fine. Return success. */ |
| 3420 | return 0; |
| 3421 | } |
| 3422 | #endif |
| 3423 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3424 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 3425 | { |
| 3426 | unsigned kernel_actual; |
| 3427 | unsigned ramdisk_actual; |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 3428 | unsigned second_actual; |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3429 | uint32_t image_actual; |
| 3430 | uint32_t dt_actual = 0; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 3431 | boot_img_hdr *hdr = NULL; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3432 | struct kernel64_hdr *kptr = NULL; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3433 | char *ptr = ((char*) data); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3434 | int ret = 0; |
| 3435 | uint8_t dtb_copied = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3436 | unsigned int out_len = 0; |
| 3437 | unsigned int out_avai_len = 0; |
| 3438 | unsigned char *out_addr = NULL; |
| 3439 | uint32_t dtb_offset = 0; |
| 3440 | unsigned char *kernel_start_addr = NULL; |
| 3441 | unsigned int kernel_size = 0; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3442 | unsigned int scratch_offset = 0; |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3443 | #if VERIFIED_BOOT_2 |
| 3444 | void *dtbo_image_buf = NULL; |
| 3445 | uint32_t dtbo_image_sz = 0; |
| 3446 | void *vbmeta_image_buf = NULL; |
| 3447 | uint32_t vbmeta_image_sz = 0; |
| 3448 | #endif |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3449 | #if !VERIFIED_BOOT_2 |
| 3450 | uint32_t sig_actual = 0; |
| 3451 | uint32_t sig_size = 0; |
| 3452 | #ifdef MDTP_SUPPORT |
| 3453 | static bool is_mdtp_activated = 0; |
| 3454 | #endif /* MDTP_SUPPORT */ |
| 3455 | #endif |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3456 | |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3457 | #if FBCON_DISPLAY_MSG |
| 3458 | /* Exit keys' detection thread firstly */ |
| 3459 | exit_menu_keys_detection(); |
| 3460 | #endif |
| 3461 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3462 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Channagoud Kadabi | 6d5375e | 2015-06-23 17:15:42 -0700 | [diff] [blame] | 3463 | if(target_build_variant_user() && !device.is_unlocked) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3464 | { |
| 3465 | fastboot_fail("unlock device to use this command"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3466 | goto boot_failed; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3467 | } |
| 3468 | #endif |
| 3469 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3470 | if (sz < sizeof(hdr)) { |
| 3471 | fastboot_fail("invalid bootimage header"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3472 | goto boot_failed; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3473 | } |
| 3474 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 3475 | hdr = (boot_img_hdr *)data; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3476 | |
| 3477 | /* ensure commandline is terminated */ |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3478 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3479 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3480 | if(target_is_emmc_boot() && hdr->page_size) { |
| 3481 | page_size = hdr->page_size; |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 3482 | page_mask = page_size - 1; |
| 3483 | } |
| 3484 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3485 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 3486 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 3487 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3488 | #if DEVICE_TREE |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 3489 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 3490 | dt_size = hdr->dt_size; |
| 3491 | #endif |
| 3492 | dt_actual = ROUND_TO_PAGE(dt_size, page_mask); |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3493 | #endif |
| 3494 | |
| 3495 | image_actual = ADD_OF(page_size, kernel_actual); |
| 3496 | image_actual = ADD_OF(image_actual, ramdisk_actual); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 3497 | image_actual = ADD_OF(image_actual, second_actual); |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3498 | image_actual = ADD_OF(image_actual, dt_actual); |
| 3499 | |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3500 | /* Checking to prevent oob access in read_der_message_length */ |
| 3501 | if (image_actual > sz) { |
| 3502 | fastboot_fail("bootimage header fields are invalid"); |
| 3503 | goto boot_failed; |
| 3504 | } |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3505 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3506 | #if VERIFIED_BOOT_2 |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 3507 | /* Pass size of boot partition, as imgsize, to avoid |
| 3508 | read fewer bytes error */ |
| 3509 | image_actual = partition_get_size(partition_get_index("boot")); |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3510 | |
| 3511 | /* load and validate dtbo partition */ |
| 3512 | load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz); |
| 3513 | |
| 3514 | /* load vbmeta partition */ |
| 3515 | load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz); |
| 3516 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3517 | memset(&info, 0, sizeof(bootinfo)); |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3518 | |
| 3519 | info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data); |
| 3520 | info.images[IMG_BOOT].imgsize = image_actual; |
| 3521 | info.images[IMG_BOOT].name = "boot"; |
| 3522 | ++info.num_loaded_images; |
| 3523 | |
| 3524 | /* Pass loaded dtbo image */ |
| 3525 | if (dtbo_image_buf != NULL) { |
| 3526 | info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf); |
| 3527 | info.images[IMG_DTBO].imgsize = dtbo_image_sz; |
| 3528 | info.images[IMG_DTBO].name = "dtbo"; |
| 3529 | ++info.num_loaded_images; |
| 3530 | } |
| 3531 | |
| 3532 | /* Pass loaded vbmeta image */ |
| 3533 | if (vbmeta_image_buf != NULL) { |
| 3534 | info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf; |
| 3535 | info.images[IMG_VBMETA].imgsize = vbmeta_image_sz; |
| 3536 | info.images[IMG_VBMETA].name = "vbmeta"; |
| 3537 | ++info.num_loaded_images; |
| 3538 | } |
| 3539 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3540 | info.multi_slot_boot = partition_multislot_is_supported(); |
| 3541 | if (load_image_and_auth(&info)) |
| 3542 | goto boot_failed; |
| 3543 | vbcmdline = info.vbcmdline; |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3544 | |
| 3545 | /* Free the buffer allocated to vbmeta post verification */ |
| 3546 | if (vbmeta_image_buf != NULL) { |
| 3547 | free(vbmeta_image_buf); |
| 3548 | --info.num_loaded_images; |
| 3549 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3550 | #else |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3551 | sig_size = sz - image_actual; |
| 3552 | |
P.V. Phani Kumar | eecfd82 | 2016-03-09 20:09:03 +0530 | [diff] [blame] | 3553 | if (target_use_signed_kernel() && (!device.is_unlocked)) { |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3554 | unsigned chk; |
P.V. Phani Kumar | eecfd82 | 2016-03-09 20:09:03 +0530 | [diff] [blame] | 3555 | /* Calculate the signature length from boot image */ |
| 3556 | sig_actual = read_der_message_length( |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3557 | (unsigned char*)(data + image_actual), sig_size); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3558 | chk = ADD_OF(image_actual, sig_actual); |
Channagoud Kadabi | 3d83901 | 2014-06-26 14:26:39 -0700 | [diff] [blame] | 3559 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3560 | if (chk > sz) { |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3561 | fastboot_fail("bootimage header fields are invalid"); |
| 3562 | goto boot_failed; |
| 3563 | } |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3564 | } |
| 3565 | |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 3566 | // Initialize boot state before trying to verify boot.img |
| 3567 | #if VERIFIED_BOOT |
Channagoud Kadabi | 699466e | 2015-11-03 12:37:42 -0800 | [diff] [blame] | 3568 | boot_verifier_init(); |
Mayank Grover | b337e93 | 2017-01-18 20:00:40 +0530 | [diff] [blame] | 3569 | #endif |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 3570 | /* Handle overflow if the input image size is greater than |
| 3571 | * boot image buffer can hold |
| 3572 | */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3573 | if ((target_get_max_flash_size() - page_size) < image_actual) |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 3574 | { |
| 3575 | fastboot_fail("booimage: size is greater than boot image buffer can hold"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3576 | goto boot_failed; |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 3577 | } |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 3578 | |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3579 | /* Verify the boot image |
| 3580 | * device & page_size are initialized in aboot_init |
| 3581 | */ |
Sridhar Parasuram | 7ae9fbc | 2015-09-17 09:56:30 -0700 | [diff] [blame] | 3582 | if (target_use_signed_kernel() && (!device.is_unlocked)) { |
Channagoud Kadabi | 3d83901 | 2014-06-26 14:26:39 -0700 | [diff] [blame] | 3583 | /* Pass size excluding signature size, otherwise we would try to |
| 3584 | * access signature beyond its length |
| 3585 | */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3586 | verify_signed_bootimg((uint32_t)data, image_actual); |
Sridhar Parasuram | 7ae9fbc | 2015-09-17 09:56:30 -0700 | [diff] [blame] | 3587 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3588 | #ifdef MDTP_SUPPORT |
| 3589 | else |
| 3590 | { |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3591 | /* fastboot boot is not allowed when MDTP is activated */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3592 | mdtp_ext_partition_verification_t ext_partition; |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 3593 | |
| 3594 | if (!is_mdtp_activated) { |
| 3595 | ext_partition.partition = MDTP_PARTITION_NONE; |
| 3596 | mdtp_fwlock_verify_lock(&ext_partition); |
| 3597 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3598 | } |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3599 | |
Amir Kotzer | 7c768c0 | 2016-04-13 09:08:05 +0300 | [diff] [blame] | 3600 | /* If mdtp state cannot be validate, block fastboot boot*/ |
| 3601 | if(mdtp_activated(&is_mdtp_activated)){ |
| 3602 | dprintf(CRITICAL, "mdtp_activated cannot validate state.\n"); |
| 3603 | dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n"); |
| 3604 | goto boot_failed; |
| 3605 | } |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3606 | if(is_mdtp_activated){ |
| 3607 | dprintf(CRITICAL, "fastboot boot command is not available.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3608 | goto boot_failed; |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3609 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3610 | #endif /* MDTP_SUPPORT */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3611 | #endif /* VERIFIED_BOOT_2 else */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3612 | |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 3613 | #if VERIFIED_BOOT |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3614 | if (VB_M == target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3615 | { |
| 3616 | /* set boot and system versions. */ |
| 3617 | set_os_version((unsigned char *)data); |
| 3618 | // send root of trust |
| 3619 | if(!send_rot_command((uint32_t)device.is_unlocked)) |
| 3620 | ASSERT(0); |
| 3621 | } |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 3622 | #endif |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 3623 | /* |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3624 | * Check if the kernel image is a gzip package. If yes, need to decompress it. |
| 3625 | * If not, continue booting. |
| 3626 | */ |
| 3627 | if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size)) |
| 3628 | { |
| 3629 | out_addr = (unsigned char *)target_get_scratch_address(); |
| 3630 | out_addr = (unsigned char *)(out_addr + image_actual + page_size); |
| 3631 | out_avai_len = target_get_max_flash_size() - image_actual - page_size; |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3632 | #if VERIFIED_BOOT_2 |
| 3633 | if (dtbo_image_sz) |
| 3634 | out_avai_len -= DTBO_IMG_BUF; |
| 3635 | #endif |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3636 | dprintf(INFO, "decompressing kernel image: start\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3637 | ret = decompress((unsigned char *)(ptr + page_size), |
| 3638 | hdr->kernel_size, out_addr, out_avai_len, |
| 3639 | &dtb_offset, &out_len); |
| 3640 | if (ret) |
| 3641 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3642 | dprintf(CRITICAL, "decompressing image failed!!!\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3643 | ASSERT(0); |
| 3644 | } |
| 3645 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3646 | dprintf(INFO, "decompressing kernel image: done\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3647 | kptr = (struct kernel64_hdr *)out_addr; |
| 3648 | kernel_start_addr = out_addr; |
| 3649 | kernel_size = out_len; |
| 3650 | } else { |
| 3651 | kptr = (struct kernel64_hdr*)((char *)data + page_size); |
| 3652 | kernel_start_addr = (unsigned char *)((char *)data + page_size); |
| 3653 | kernel_size = hdr->kernel_size; |
| 3654 | } |
| 3655 | |
| 3656 | /* |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 3657 | * Update the kernel/ramdisk/tags address if the boot image header |
| 3658 | * has default values, these default values come from mkbootimg when |
| 3659 | * the boot image is flashed using fastboot flash:raw |
| 3660 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 3661 | update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr)); |
Dima Zavin | 3cadfff | 2013-03-21 14:30:48 -0700 | [diff] [blame] | 3662 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3663 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 3664 | hdr->kernel_addr = VA(hdr->kernel_addr); |
| 3665 | hdr->ramdisk_addr = VA(hdr->ramdisk_addr); |
| 3666 | hdr->tags_addr = VA(hdr->tags_addr); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3667 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3668 | kernel_size = ROUND_TO_PAGE(kernel_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3669 | /* Check if the addresses in the header are valid. */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3670 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) || |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3671 | check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) || |
| 3672 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) || |
| 3673 | check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3674 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3675 | dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3676 | goto boot_failed; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3677 | } |
| 3678 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3679 | #if DEVICE_TREE |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3680 | scratch_offset = image_actual + page_size + out_len; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3681 | /* find correct dtb and copy it to right location */ |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3682 | ret = copy_dtb(data, scratch_offset); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3683 | |
| 3684 | dtb_copied = !ret ? 1 : 0; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3685 | #else |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3686 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) || |
| 3687 | check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3688 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3689 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3690 | goto boot_failed; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3691 | } |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3692 | #endif |
| 3693 | |
| 3694 | /* Load ramdisk & kernel */ |
| 3695 | 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] | 3696 | memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3697 | |
| 3698 | #if DEVICE_TREE |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3699 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) || |
| 3700 | check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual)) |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3701 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3702 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3703 | goto boot_failed; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3704 | } |
| 3705 | |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3706 | /* |
| 3707 | * If dtb is not found look for appended DTB in the kernel. |
| 3708 | * If appended dev tree is found, update the atags with |
| 3709 | * memory address to the DTB appended location on RAM. |
| 3710 | * Else update with the atags address in the kernel header |
| 3711 | */ |
| 3712 | if (!dtb_copied) { |
| 3713 | void *dtb; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3714 | dtb = dev_tree_appended((void*)(ptr + page_size), |
| 3715 | hdr->kernel_size, dtb_offset, |
Dima Zavin | e63e557 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 3716 | (void *)hdr->tags_addr); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3717 | if (!dtb) { |
| 3718 | fastboot_fail("dtb not found"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3719 | goto boot_failed; |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3720 | } |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3721 | } |
| 3722 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3723 | |
| 3724 | fastboot_okay(""); |
Amol Jadi | 7c4316c | 2013-10-07 14:19:26 -0700 | [diff] [blame] | 3725 | fastboot_stop(); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3726 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 3727 | boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr, |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3728 | (const char*) hdr->cmdline, board_machtype(), |
| 3729 | (void*) hdr->ramdisk_addr, hdr->ramdisk_size); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3730 | |
| 3731 | /* fastboot already stop, it's no need to show fastboot menu */ |
| 3732 | return; |
| 3733 | boot_failed: |
| 3734 | #if FBCON_DISPLAY_MSG |
| 3735 | /* revert to fastboot menu if boot failed */ |
| 3736 | display_fastboot_menu(); |
| 3737 | #endif |
| 3738 | return; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3739 | } |
| 3740 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3741 | void cmd_erase_nand(const char *arg, void *data, unsigned sz) |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3742 | { |
| 3743 | struct ptentry *ptn; |
| 3744 | struct ptable *ptable; |
| 3745 | |
| 3746 | ptable = flash_get_ptable(); |
| 3747 | if (ptable == NULL) { |
| 3748 | fastboot_fail("partition table doesn't exist"); |
| 3749 | return; |
| 3750 | } |
| 3751 | |
| 3752 | ptn = ptable_find(ptable, arg); |
| 3753 | if (ptn == NULL) { |
| 3754 | fastboot_fail("unknown partition name"); |
| 3755 | return; |
| 3756 | } |
| 3757 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3758 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 3759 | dprintf(INFO, "erasing avb_custom_key\n"); |
| 3760 | if (erase_userkey()) { |
| 3761 | fastboot_fail("Erasing avb_custom_key failed"); |
| 3762 | } else { |
| 3763 | fastboot_okay(""); |
| 3764 | } |
| 3765 | return; |
| 3766 | } |
| 3767 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3768 | if (flash_erase(ptn)) { |
| 3769 | fastboot_fail("failed to erase partition"); |
| 3770 | return; |
| 3771 | } |
| 3772 | fastboot_okay(""); |
| 3773 | } |
| 3774 | |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3775 | |
| 3776 | void cmd_erase_mmc(const char *arg, void *data, unsigned sz) |
| 3777 | { |
| 3778 | unsigned long long ptn = 0; |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 3779 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3780 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3781 | uint8_t lun = 0; |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 3782 | char *footer = NULL; |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 3783 | char EraseResultStr[MAX_RSP_SIZE] = ""; |
| 3784 | VirtualAbMergeStatus SnapshotMergeStatus; |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3785 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3786 | #if VERIFIED_BOOT |
| 3787 | if(!strcmp(arg, KEYSTORE_PTN_NAME)) |
| 3788 | { |
| 3789 | if(!device.is_unlocked) |
| 3790 | { |
| 3791 | fastboot_fail("unlock device to erase keystore"); |
| 3792 | return; |
| 3793 | } |
| 3794 | } |
| 3795 | #endif |
| 3796 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3797 | index = partition_get_index(arg); |
| 3798 | ptn = partition_get_offset(index); |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 3799 | size = partition_get_size(index); |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 3800 | |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 3801 | if (target_virtual_ab_supported()) { |
| 3802 | if (CheckVirtualAbCriticalPartition (arg)) { |
| 3803 | snprintf(EraseResultStr, MAX_RSP_SIZE,"Erase of %s is not allowed in %s state", |
| 3804 | arg, SnapshotMergeState); |
| 3805 | fastboot_fail(EraseResultStr); |
| 3806 | return; |
| 3807 | } |
| 3808 | SnapshotMergeStatus = GetSnapshotMergeStatus (); |
| 3809 | if (((SnapshotMergeStatus == MERGING) || (SnapshotMergeStatus == SNAPSHOTTED)) && |
| 3810 | !strncmp (arg, "super", strlen ("super"))) { |
| 3811 | |
| 3812 | if(SetSnapshotMergeStatus (CANCELLED)) |
| 3813 | { |
| 3814 | fastboot_fail("Failed to update snapshot state to cancel"); |
| 3815 | return; |
| 3816 | } |
| 3817 | |
| 3818 | //updating fbvar snapshot-merge-state |
| 3819 | snprintf(SnapshotMergeState,strlen(VabSnapshotMergeStatus[NONE_MERGE_STATUS]) + 1, |
| 3820 | "%s", VabSnapshotMergeStatus[NONE_MERGE_STATUS]); |
| 3821 | } |
| 3822 | } |
| 3823 | |
Monika Singh | c4778b7 | 2018-05-16 11:16:42 +0530 | [diff] [blame] | 3824 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 3825 | dprintf(INFO, "erasing avb_custom_key\n"); |
| 3826 | if (erase_userkey()) { |
| 3827 | fastboot_fail("Erasing avb_custom_key failed"); |
| 3828 | } else { |
| 3829 | fastboot_okay(""); |
| 3830 | } |
| 3831 | return; |
| 3832 | } |
| 3833 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3834 | if(ptn == 0) { |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 3835 | fastboot_fail("Partition table doesn't exist\n"); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3836 | return; |
| 3837 | } |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 3838 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3839 | lun = partition_get_lun(index); |
| 3840 | mmc_set_lun(lun); |
| 3841 | |
Sridhar Parasuram | d795712 | 2015-02-27 11:33:40 -0800 | [diff] [blame] | 3842 | if (platform_boot_dev_isemmc()) |
| 3843 | { |
| 3844 | if (mmc_erase_card(ptn, size)) { |
| 3845 | fastboot_fail("failed to erase partition\n"); |
| 3846 | return; |
| 3847 | } |
| 3848 | } else { |
| 3849 | BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE); |
| 3850 | size = partition_get_size(index); |
| 3851 | if (size > DEFAULT_ERASE_SIZE) |
| 3852 | size = DEFAULT_ERASE_SIZE; |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 3853 | |
Sridhar Parasuram | d795712 | 2015-02-27 11:33:40 -0800 | [diff] [blame] | 3854 | /* Simple inefficient version of erase. Just writing |
| 3855 | 0 in first several blocks */ |
| 3856 | if (mmc_write(ptn , size, (unsigned int *)out)) { |
| 3857 | fastboot_fail("failed to erase partition"); |
| 3858 | return; |
| 3859 | } |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 3860 | /*Erase FDE metadata at the userdata footer*/ |
| 3861 | if(!(strncmp(arg, "userdata", 8))) |
| 3862 | { |
| 3863 | footer = memalign(CACHE_LINE, FOOTER_SIZE); |
| 3864 | memset((void *)footer, 0, FOOTER_SIZE); |
| 3865 | |
| 3866 | size = partition_get_size(index); |
| 3867 | |
| 3868 | if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) { |
| 3869 | fastboot_fail("failed to erase userdata footer"); |
| 3870 | free(footer); |
| 3871 | return; |
| 3872 | } |
| 3873 | free(footer); |
| 3874 | } |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3875 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3876 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3877 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3878 | !(strncmp(arg, "userdata", 8)) && |
| 3879 | send_delete_keys_to_tz()) |
Sridhar Parasuram | 32b3066 | 2015-07-10 13:33:22 -0700 | [diff] [blame] | 3880 | ASSERT(0); |
| 3881 | #endif |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3882 | fastboot_okay(""); |
| 3883 | } |
| 3884 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3885 | void cmd_erase(const char *arg, void *data, unsigned sz) |
| 3886 | { |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3887 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 3888 | if (target_build_variant_user()) |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 3889 | { |
Sridhar Parasuram | c32d07d | 2015-07-12 10:57:48 -0700 | [diff] [blame] | 3890 | if(!device.is_unlocked) |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 3891 | { |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 3892 | fastboot_fail("device is locked. Cannot erase"); |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 3893 | return; |
| 3894 | } |
| 3895 | } |
| 3896 | #endif |
| 3897 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3898 | if(target_is_emmc_boot()) |
| 3899 | cmd_erase_mmc(arg, data, sz); |
| 3900 | else |
| 3901 | cmd_erase_nand(arg, data, sz); |
| 3902 | } |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3903 | |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 3904 | /* Get the size from partiton name */ |
| 3905 | static void get_partition_size(const char *arg, char *response) |
| 3906 | { |
| 3907 | uint64_t ptn = 0; |
| 3908 | uint64_t size; |
| 3909 | int index = INVALID_PTN; |
| 3910 | |
| 3911 | index = partition_get_index(arg); |
| 3912 | |
| 3913 | if (index == INVALID_PTN) |
| 3914 | { |
| 3915 | dprintf(CRITICAL, "Invalid partition index\n"); |
| 3916 | return; |
| 3917 | } |
| 3918 | |
| 3919 | ptn = partition_get_offset(index); |
| 3920 | |
| 3921 | if(!ptn) |
| 3922 | { |
| 3923 | dprintf(CRITICAL, "Invalid partition name %s\n", arg); |
| 3924 | return; |
| 3925 | } |
| 3926 | |
| 3927 | size = partition_get_size(index); |
| 3928 | |
| 3929 | snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size); |
| 3930 | return; |
| 3931 | } |
| 3932 | |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3933 | /* Function to check partition type of a partition*/ |
| 3934 | static fs_signature_type |
| 3935 | check_partition_fs_signature(const char *arg) |
| 3936 | { |
| 3937 | fs_signature_type ret = NO_FS; |
| 3938 | int index; |
| 3939 | unsigned long long ptn; |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3940 | char *buffer = memalign(CACHE_LINE, mmc_blocksize); |
| 3941 | uint32_t sb_blk_offset = 0; |
| 3942 | char *sb_buffer = buffer; |
| 3943 | |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3944 | if (!sb_buffer) |
| 3945 | { |
| 3946 | dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n"); |
| 3947 | goto out; |
| 3948 | } |
| 3949 | |
| 3950 | /* Read super block */ |
| 3951 | if ((index = partition_get_index(arg)) < 0) |
| 3952 | { |
| 3953 | dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg); |
| 3954 | goto out; |
| 3955 | } |
| 3956 | ptn = partition_get_offset(index); |
| 3957 | mmc_set_lun(partition_get_lun(index)); |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3958 | sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize); |
| 3959 | |
| 3960 | if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize), |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3961 | (void *)sb_buffer, mmc_blocksize)) |
| 3962 | { |
| 3963 | dprintf(CRITICAL, "ERROR: Failed to read Superblock\n"); |
| 3964 | goto out; |
| 3965 | } |
| 3966 | |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3967 | if (sb_blk_offset == 0) |
| 3968 | sb_buffer += FS_SUPERBLOCK_OFFSET; |
| 3969 | |
| 3970 | if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC) |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3971 | { |
| 3972 | dprintf(SPEW, "%s() Found EXT FS\n", arg); |
| 3973 | ret = EXT_FS_SIGNATURE; |
| 3974 | } |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3975 | else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC) |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3976 | { |
| 3977 | dprintf(SPEW, "%s() Found F2FS FS\n", arg); |
| 3978 | ret = EXT_F2FS_SIGNATURE; |
| 3979 | } |
| 3980 | else |
| 3981 | { |
| 3982 | dprintf(SPEW, "%s() Reverting to default 0x%x\n", |
| 3983 | arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB]))); |
| 3984 | ret = NO_FS; |
| 3985 | } |
| 3986 | |
| 3987 | out: |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3988 | if(buffer) |
| 3989 | free(buffer); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3990 | return ret; |
| 3991 | } |
| 3992 | |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 3993 | /* Function to get partition type */ |
| 3994 | static void get_partition_type(const char *arg, char *response) |
| 3995 | { |
| 3996 | uint n = 0; |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3997 | fs_signature_type fs_signature; |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 3998 | |
| 3999 | if (arg == NULL || |
| 4000 | response == NULL) |
| 4001 | { |
| 4002 | dprintf(CRITICAL, "Invalid input parameter\n"); |
| 4003 | return; |
| 4004 | } |
| 4005 | |
| 4006 | /* By default copy raw to response */ |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 4007 | strlcpy(response, RAW_STR, MAX_RSP_SIZE); |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 4008 | |
| 4009 | /* Mark partiton type for known paritions only */ |
| 4010 | for (n=0; n < ARRAY_SIZE(part_type_known); n++) |
| 4011 | { |
| 4012 | if (!strncmp(part_type_known[n].part_name, arg, strlen(arg))) |
| 4013 | { |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4014 | /* Check partition for FS signature */ |
| 4015 | fs_signature = check_partition_fs_signature(arg); |
| 4016 | switch (fs_signature) |
| 4017 | { |
| 4018 | case EXT_FS_SIGNATURE: |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 4019 | strlcpy(response, EXT_STR, MAX_RSP_SIZE); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4020 | break; |
| 4021 | case EXT_F2FS_SIGNATURE: |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 4022 | strlcpy(response, F2FS_STR, MAX_RSP_SIZE); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4023 | break; |
| 4024 | case NO_FS: |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 4025 | strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4026 | } |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 4027 | } |
| 4028 | } |
| 4029 | return; |
| 4030 | } |
| 4031 | |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4032 | /* |
| 4033 | * Publish the partition type & size info |
| 4034 | * fastboot getvar will publish the required information. |
| 4035 | * fastboot getvar partition_size:<partition_name>: partition size in hex |
| 4036 | * fastboot getvar partition_type:<partition_name>: partition type (ext/fat) |
| 4037 | */ |
| 4038 | static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts) |
| 4039 | { |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 4040 | uint8_t i; |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4041 | static bool published = false; |
| 4042 | struct partition_entry *ptn_entry = |
| 4043 | partition_get_partition_entries(); |
| 4044 | memset(info, 0, sizeof(struct getvar_partition_info)* num_parts); |
| 4045 | |
| 4046 | for (i = 0; i < num_parts; i++) { |
| 4047 | strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE); |
| 4048 | strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE); |
| 4049 | strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE); |
| 4050 | |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 4051 | get_partition_type(info[i].part_name, info[i].type_response); |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4052 | get_partition_size(info[i].part_name, info[i].size_response); |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4053 | if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE) |
| 4054 | { |
| 4055 | dprintf(CRITICAL, "partition size name truncated\n"); |
| 4056 | return; |
| 4057 | } |
| 4058 | if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE) |
| 4059 | { |
| 4060 | dprintf(CRITICAL, "partition type name truncated\n"); |
| 4061 | return; |
| 4062 | } |
| 4063 | |
| 4064 | if (!published) |
| 4065 | { |
| 4066 | /* publish partition size & type info */ |
| 4067 | fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response); |
| 4068 | fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response); |
| 4069 | } |
| 4070 | } |
| 4071 | if (!published) |
| 4072 | published = true; |
| 4073 | } |
| 4074 | |
| 4075 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4076 | void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz) |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 4077 | { |
| 4078 | unsigned long long ptn = 0; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 4079 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 4080 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4081 | char *token = NULL; |
| 4082 | char *pname = NULL; |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 4083 | char *sp; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4084 | uint8_t lun = 0; |
| 4085 | bool lun_set = false; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4086 | int current_active_slot = INVALID; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 4087 | |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 4088 | token = strtok_r((char *)arg, ":", &sp); |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4089 | pname = token; |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 4090 | token = strtok_r(NULL, ":", &sp); |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4091 | if(token) |
| 4092 | { |
| 4093 | lun = atoi(token); |
| 4094 | mmc_set_lun(lun); |
| 4095 | lun_set = true; |
| 4096 | } |
| 4097 | |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4098 | if (pname) |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 4099 | { |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 4100 | if (!strncmp(pname, "frp-unlock", strlen("frp-unlock"))) |
| 4101 | { |
| 4102 | if (!aboot_frp_unlock(pname, data, sz)) |
| 4103 | { |
| 4104 | fastboot_info("FRP unlock successful"); |
| 4105 | fastboot_okay(""); |
| 4106 | } |
| 4107 | else |
| 4108 | fastboot_fail("Secret key is invalid, please update the bootloader with secret key"); |
| 4109 | |
| 4110 | return; |
| 4111 | } |
| 4112 | |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4113 | if (!strcmp(pname, "partition")) |
| 4114 | { |
| 4115 | dprintf(INFO, "Attempt to write partition image.\n"); |
| 4116 | if (write_partition(sz, (unsigned char *) data)) { |
| 4117 | fastboot_fail("failed to write partition"); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 4118 | return; |
| 4119 | } |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4120 | /* Re-publish partition table */ |
| 4121 | publish_getvar_partition_info(part_info, partition_get_partition_count()); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4122 | |
| 4123 | /* Rescan partition table to ensure we have multislot support*/ |
| 4124 | if (partition_scan_for_multislot()) |
| 4125 | { |
| 4126 | current_active_slot = partition_find_active_slot(); |
| 4127 | dprintf(INFO, "Multislot supported: Slot %s active", |
| 4128 | (SUFFIX_SLOT(current_active_slot))); |
| 4129 | } |
| 4130 | partition_mark_active_slot(current_active_slot); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 4131 | } |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4132 | else |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4133 | { |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4134 | #if VERIFIED_BOOT |
| 4135 | if(!strcmp(pname, KEYSTORE_PTN_NAME)) |
| 4136 | { |
| 4137 | if(!device.is_unlocked) |
| 4138 | { |
| 4139 | fastboot_fail("unlock device to flash keystore"); |
| 4140 | return; |
| 4141 | } |
Gaurav Nebhwani | c7313cc | 2015-12-15 22:25:04 +0530 | [diff] [blame] | 4142 | if(!boot_verify_validate_keystore((unsigned char *)data,sz)) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4143 | { |
| 4144 | fastboot_fail("image is not a keystore file"); |
| 4145 | return; |
| 4146 | } |
| 4147 | } |
| 4148 | #endif |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4149 | index = partition_get_index(pname); |
| 4150 | ptn = partition_get_offset(index); |
| 4151 | if(ptn == 0) { |
| 4152 | fastboot_fail("partition table doesn't exist"); |
| 4153 | return; |
| 4154 | } |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4155 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4156 | if (!strncmp(pname, "boot", strlen("boot")) |
| 4157 | || !strcmp(pname, "recovery")) |
| 4158 | { |
Ashish Bhimanpalliwar | 84f7db9 | 2020-09-23 11:59:16 +0530 | [diff] [blame] | 4159 | 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] | 4160 | fastboot_fail("image is not a boot image"); |
| 4161 | return; |
| 4162 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4163 | |
| 4164 | /* Reset multislot_partition attributes in case of flashing boot */ |
| 4165 | if (partition_multislot_is_supported()) |
| 4166 | { |
| 4167 | partition_reset_attributes(index); |
| 4168 | } |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4169 | } |
| 4170 | |
| 4171 | if(!lun_set) |
| 4172 | { |
| 4173 | lun = partition_get_lun(index); |
| 4174 | mmc_set_lun(lun); |
| 4175 | } |
| 4176 | |
| 4177 | size = partition_get_size(index); |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 4178 | if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) { |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4179 | fastboot_fail("size too large"); |
| 4180 | return; |
| 4181 | } |
| 4182 | else if (mmc_write(ptn , sz, (unsigned int *)data)) { |
| 4183 | fastboot_fail("flash write failure"); |
| 4184 | return; |
| 4185 | } |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 4186 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 4187 | } |
| 4188 | fastboot_okay(""); |
| 4189 | return; |
| 4190 | } |
| 4191 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4192 | void cmd_flash_meta_img(const char *arg, void *data, unsigned sz) |
| 4193 | { |
| 4194 | int i, images; |
| 4195 | meta_header_t *meta_header; |
| 4196 | img_header_entry_t *img_header_entry; |
Parth Dixit | 3e2d303 | 2016-03-04 17:11:52 +0530 | [diff] [blame] | 4197 | /*End of the image address*/ |
| 4198 | uintptr_t data_end; |
| 4199 | |
| 4200 | if( (UINT_MAX - sz) > (uintptr_t)data ) |
| 4201 | data_end = (uintptr_t)data + sz; |
| 4202 | else |
| 4203 | { |
| 4204 | fastboot_fail("Cannot flash: image header corrupt"); |
| 4205 | return; |
| 4206 | } |
| 4207 | |
Ashish Bhimanpalliwar | 21494e1 | 2020-10-07 13:24:03 +0530 | [diff] [blame] | 4208 | 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] | 4209 | { |
| 4210 | fastboot_fail("Cannot flash: image header corrupt"); |
| 4211 | return; |
| 4212 | } |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4213 | |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 4214 | /* If device is locked: |
| 4215 | * Forbid to flash image to avoid the device to bypass the image |
| 4216 | * which with "any" name other than bootloader. Because it maybe |
| 4217 | * a meta package of all partitions. |
| 4218 | */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4219 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 4220 | if (target_build_variant_user()) { |
| 4221 | if (!device.is_unlocked) { |
| 4222 | fastboot_fail("Device is locked, meta image flashing is not allowed"); |
| 4223 | return; |
| 4224 | } |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4225 | |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 4226 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4227 | !device.is_unlock_critical) |
| 4228 | { |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 4229 | fastboot_fail("Device is critical locked, Meta image flashing is not allowed"); |
| 4230 | return; |
| 4231 | } |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 4232 | } |
| 4233 | #endif |
| 4234 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4235 | meta_header = (meta_header_t*) data; |
Ashish Bhimanpalliwar | 21494e1 | 2020-10-07 13:24:03 +0530 | [diff] [blame] | 4236 | 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] | 4237 | { |
| 4238 | fastboot_fail("Cannot flash: image header corrupt"); |
| 4239 | return; |
| 4240 | } |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4241 | img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t)); |
| 4242 | |
| 4243 | images = meta_header->img_hdr_sz / sizeof(img_header_entry_t); |
| 4244 | |
| 4245 | for (i=0; i<images; i++) { |
| 4246 | |
| 4247 | if((img_header_entry[i].ptn_name == NULL) || |
| 4248 | (img_header_entry[i].start_offset == 0) || |
| 4249 | (img_header_entry[i].size == 0)) |
| 4250 | break; |
Kishor PK | 4983150 | 2017-04-21 17:55:43 +0530 | [diff] [blame] | 4251 | if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) { |
| 4252 | fastboot_fail("Integer overflow detected in start_offset of img"); |
| 4253 | break; |
| 4254 | } |
| 4255 | else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) { |
| 4256 | fastboot_fail("Integer overflow detected in size of img"); |
| 4257 | break; |
| 4258 | } |
Parth Dixit | 3e2d303 | 2016-03-04 17:11:52 +0530 | [diff] [blame] | 4259 | if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset |
| 4260 | + img_header_entry[i].size) ) |
| 4261 | { |
| 4262 | fastboot_fail("Cannot flash: image size mismatch"); |
| 4263 | break; |
| 4264 | } |
| 4265 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4266 | cmd_flash_mmc_img(img_header_entry[i].ptn_name, |
| 4267 | (void *) data + img_header_entry[i].start_offset, |
| 4268 | img_header_entry[i].size); |
| 4269 | } |
| 4270 | |
Channagoud Kadabi | aafbbfc | 2015-03-04 17:52:56 -0800 | [diff] [blame] | 4271 | if (!strncmp(arg, "bootloader", strlen("bootloader"))) |
| 4272 | { |
| 4273 | strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN); |
| 4274 | strlcat(device.bootloader_version, "-", MAX_VERSION_LEN); |
| 4275 | strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN); |
| 4276 | } |
| 4277 | else |
| 4278 | { |
| 4279 | strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN); |
| 4280 | strlcat(device.radio_version, "-", MAX_VERSION_LEN); |
| 4281 | strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN); |
| 4282 | } |
| 4283 | |
| 4284 | write_device_info(&device); |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4285 | fastboot_okay(""); |
| 4286 | return; |
| 4287 | } |
| 4288 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4289 | void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz) |
| 4290 | { |
| 4291 | unsigned int chunk; |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4292 | uint64_t chunk_data_sz; |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4293 | uint32_t *fill_buf = NULL; |
| 4294 | uint32_t fill_val; |
Mayank Grover | 4f50bba | 2017-07-19 18:17:08 +0530 | [diff] [blame] | 4295 | uint32_t blk_sz_actual = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4296 | sparse_header_t *sparse_header; |
| 4297 | chunk_header_t *chunk_header; |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 4298 | uint32_t total_blocks = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4299 | unsigned long long ptn = 0; |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 4300 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 4301 | int index = INVALID_PTN; |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 4302 | uint32_t i; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4303 | uint8_t lun = 0; |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4304 | /*End of the sparse image address*/ |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4305 | uintptr_t data_end = (uintptr_t)data + sz; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4306 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 4307 | index = partition_get_index(arg); |
| 4308 | ptn = partition_get_offset(index); |
| 4309 | if(ptn == 0) { |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4310 | fastboot_fail("partition table doesn't exist"); |
| 4311 | return; |
| 4312 | } |
| 4313 | |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 4314 | size = partition_get_size(index); |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 4315 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4316 | lun = partition_get_lun(index); |
| 4317 | mmc_set_lun(lun); |
| 4318 | |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4319 | if (sz < sizeof(sparse_header_t)) { |
| 4320 | fastboot_fail("size too low"); |
| 4321 | return; |
| 4322 | } |
| 4323 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4324 | /* Read and skip over sparse image header */ |
| 4325 | sparse_header = (sparse_header_t *) data; |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4326 | |
Mayank Grover | 48bd9bb | 2017-07-19 12:04:16 +0530 | [diff] [blame] | 4327 | if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){ |
| 4328 | fastboot_fail("Invalid block size\n"); |
| 4329 | return; |
| 4330 | } |
| 4331 | |
vijay kumar | 1321f34 | 2015-03-27 12:13:42 +0530 | [diff] [blame] | 4332 | 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] | 4333 | fastboot_fail("size too large"); |
| 4334 | return; |
| 4335 | } |
| 4336 | |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 4337 | data += sizeof(sparse_header_t); |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4338 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4339 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4340 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 4341 | return; |
| 4342 | } |
| 4343 | |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 4344 | if(sparse_header->file_hdr_sz != sizeof(sparse_header_t)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4345 | { |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 4346 | fastboot_fail("sparse header size mismatch"); |
| 4347 | return; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4348 | } |
| 4349 | |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 4350 | dprintf (SPEW, "=== Sparse Image Header ===\n"); |
| 4351 | dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic); |
| 4352 | dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version); |
| 4353 | dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version); |
| 4354 | dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz); |
| 4355 | dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz); |
| 4356 | dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz); |
| 4357 | dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks); |
| 4358 | dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4359 | |
| 4360 | /* Start processing chunks */ |
| 4361 | for (chunk=0; chunk<sparse_header->total_chunks; chunk++) |
| 4362 | { |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4363 | /* Make sure the total image size does not exceed the partition size */ |
| 4364 | if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) { |
| 4365 | fastboot_fail("size too large"); |
| 4366 | return; |
| 4367 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4368 | /* Read and skip over chunk header */ |
| 4369 | chunk_header = (chunk_header_t *) data; |
| 4370 | data += sizeof(chunk_header_t); |
| 4371 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4372 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4373 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 4374 | return; |
| 4375 | } |
| 4376 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4377 | dprintf (SPEW, "=== Chunk Header ===\n"); |
| 4378 | dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type); |
| 4379 | dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz); |
| 4380 | dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz); |
| 4381 | |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4382 | if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4383 | { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4384 | fastboot_fail("chunk header size mismatch"); |
| 4385 | return; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4386 | } |
| 4387 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4388 | chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz; |
| 4389 | |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4390 | /* Make sure that the chunk size calculated from sparse image does not |
| 4391 | * exceed partition size |
| 4392 | */ |
| 4393 | if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size) |
| 4394 | { |
| 4395 | fastboot_fail("Chunk data size exceeds partition size"); |
| 4396 | return; |
| 4397 | } |
| 4398 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4399 | switch (chunk_header->chunk_type) |
| 4400 | { |
| 4401 | case CHUNK_TYPE_RAW: |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4402 | 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] | 4403 | chunk_data_sz)) |
| 4404 | { |
| 4405 | fastboot_fail("Bogus chunk size for chunk type Raw"); |
| 4406 | return; |
| 4407 | } |
| 4408 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4409 | if (data_end < (uintptr_t)data + chunk_data_sz) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4410 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 4411 | return; |
| 4412 | } |
| 4413 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4414 | /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32 |
| 4415 | otherwise it will return in the line above |
| 4416 | */ |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 4417 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4418 | (uint32_t)chunk_data_sz, |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 4419 | (unsigned int*)data)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4420 | { |
| 4421 | fastboot_fail("flash write failure"); |
| 4422 | return; |
| 4423 | } |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4424 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 4425 | fastboot_fail("Bogus size for RAW chunk type"); |
| 4426 | return; |
| 4427 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4428 | total_blocks += chunk_header->chunk_sz; |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4429 | data += (uint32_t)chunk_data_sz; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4430 | break; |
| 4431 | |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4432 | case CHUNK_TYPE_FILL: |
| 4433 | if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz + |
| 4434 | sizeof(uint32_t))) |
| 4435 | { |
| 4436 | fastboot_fail("Bogus chunk size for chunk type FILL"); |
| 4437 | return; |
| 4438 | } |
| 4439 | |
Mayank Grover | 4f50bba | 2017-07-19 18:17:08 +0530 | [diff] [blame] | 4440 | blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE); |
| 4441 | /* Integer overflow detected */ |
| 4442 | if (blk_sz_actual < sparse_header->blk_sz) |
| 4443 | { |
| 4444 | fastboot_fail("Invalid block size"); |
| 4445 | return; |
| 4446 | } |
| 4447 | |
| 4448 | fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual); |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4449 | if (!fill_buf) |
| 4450 | { |
| 4451 | fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL"); |
| 4452 | return; |
| 4453 | } |
| 4454 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4455 | if (data_end < (uintptr_t)data + sizeof(uint32_t)) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4456 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
Gaurav Nebhwani | 163cbf8 | 2016-03-17 21:21:27 +0530 | [diff] [blame] | 4457 | free(fill_buf); |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4458 | return; |
| 4459 | } |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4460 | fill_val = *(uint32_t *)data; |
| 4461 | data = (char *) data + sizeof(uint32_t); |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4462 | |
| 4463 | for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++) |
| 4464 | { |
| 4465 | fill_buf[i] = fill_val; |
| 4466 | } |
| 4467 | |
Gaurav Nebhwani | e94cbe1 | 2016-03-17 21:16:34 +0530 | [diff] [blame] | 4468 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) |
| 4469 | { |
| 4470 | fastboot_fail("bogus size for chunk FILL type"); |
| 4471 | free(fill_buf); |
| 4472 | return; |
| 4473 | } |
| 4474 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4475 | for (i = 0; i < chunk_header->chunk_sz; i++) |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4476 | { |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4477 | /* Make sure that the data written to partition does not exceed partition size */ |
| 4478 | if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size) |
| 4479 | { |
| 4480 | fastboot_fail("Chunk data size for fill type exceeds partition size"); |
Gaurav Nebhwani | 163cbf8 | 2016-03-17 21:21:27 +0530 | [diff] [blame] | 4481 | free(fill_buf); |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4482 | return; |
| 4483 | } |
| 4484 | |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4485 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
| 4486 | sparse_header->blk_sz, |
| 4487 | fill_buf)) |
| 4488 | { |
| 4489 | fastboot_fail("flash write failure"); |
| 4490 | free(fill_buf); |
| 4491 | return; |
| 4492 | } |
| 4493 | |
| 4494 | total_blocks++; |
| 4495 | } |
| 4496 | |
| 4497 | free(fill_buf); |
| 4498 | break; |
| 4499 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4500 | case CHUNK_TYPE_DONT_CARE: |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4501 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 4502 | fastboot_fail("bogus size for chunk DONT CARE type"); |
| 4503 | return; |
| 4504 | } |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 4505 | total_blocks += chunk_header->chunk_sz; |
| 4506 | break; |
| 4507 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4508 | case CHUNK_TYPE_CRC: |
| 4509 | if(chunk_header->total_sz != sparse_header->chunk_hdr_sz) |
| 4510 | { |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4511 | fastboot_fail("Bogus chunk size for chunk type CRC"); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4512 | return; |
| 4513 | } |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4514 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 4515 | fastboot_fail("bogus size for chunk CRC type"); |
| 4516 | return; |
| 4517 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4518 | total_blocks += chunk_header->chunk_sz; |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4519 | if ((uintptr_t)data > UINT_MAX - chunk_data_sz) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4520 | fastboot_fail("integer overflow occured"); |
| 4521 | return; |
| 4522 | } |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4523 | data += (uint32_t)chunk_data_sz; |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4524 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4525 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 4526 | return; |
| 4527 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4528 | break; |
| 4529 | |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 4530 | default: |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4531 | dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4532 | fastboot_fail("Unknown chunk type"); |
| 4533 | return; |
| 4534 | } |
| 4535 | } |
| 4536 | |
Ajay Dudani | 0c6927b | 2011-05-18 11:12:16 -0700 | [diff] [blame] | 4537 | dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n", |
| 4538 | total_blocks, sparse_header->total_blks); |
| 4539 | |
| 4540 | if(total_blocks != sparse_header->total_blks) |
| 4541 | { |
| 4542 | fastboot_fail("sparse image write failure"); |
| 4543 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4544 | |
| 4545 | fastboot_okay(""); |
| 4546 | return; |
| 4547 | } |
| 4548 | |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 4549 | static bool CheckVirtualAbCriticalPartition (const char *PartitionName) |
| 4550 | { |
| 4551 | VirtualAbMergeStatus SnapshotMergeStatus; |
| 4552 | uint32 Iter = 0; |
| 4553 | |
| 4554 | SnapshotMergeStatus = GetSnapshotMergeStatus (); |
| 4555 | if ((SnapshotMergeStatus == MERGING || SnapshotMergeStatus == SNAPSHOTTED)) { |
| 4556 | for (Iter = 0; Iter < ARRAY_SIZE (VirtualAbCriticalPartitions); Iter++) { |
| 4557 | if (!strncmp (PartitionName, VirtualAbCriticalPartitions[Iter], |
| 4558 | strlen (VirtualAbCriticalPartitions[Iter]))) |
| 4559 | return true; |
| 4560 | } |
| 4561 | } |
| 4562 | return false; |
| 4563 | } |
| 4564 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4565 | void cmd_flash_mmc(const char *arg, void *data, unsigned sz) |
| 4566 | { |
| 4567 | sparse_header_t *sparse_header; |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4568 | meta_header_t *meta_header; |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 4569 | VirtualAbMergeStatus SnapshotMergeStatus; |
| 4570 | char FlashResultStr[MAX_RSP_SIZE] = ""; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4571 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 4572 | #ifdef SSD_ENABLE |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 4573 | /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */ |
| 4574 | unsigned int *magic_number = (unsigned int *) data; |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4575 | int ret=0; |
| 4576 | uint32 major_version=0; |
| 4577 | uint32 minor_version=0; |
| 4578 | |
| 4579 | ret = scm_svc_version(&major_version,&minor_version); |
| 4580 | if(!ret) |
| 4581 | { |
| 4582 | if(major_version >= 2) |
| 4583 | { |
Amir Samuelov | bb65ce0 | 2013-05-05 12:20:18 +0300 | [diff] [blame] | 4584 | if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") ) |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4585 | { |
| 4586 | ret = encrypt_scm((uint32 **) &data, &sz); |
| 4587 | if (ret != 0) { |
| 4588 | dprintf(CRITICAL, "ERROR: Encryption Failure\n"); |
| 4589 | return; |
| 4590 | } |
| 4591 | |
Amir Samuelov | bb65ce0 | 2013-05-05 12:20:18 +0300 | [diff] [blame] | 4592 | /* Protect only for SSD */ |
| 4593 | if (!strcmp(arg, "ssd")) { |
| 4594 | ret = scm_protect_keystore((uint32 *) data, sz); |
| 4595 | if (ret != 0) { |
| 4596 | dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n"); |
| 4597 | return; |
| 4598 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4599 | } |
| 4600 | } |
| 4601 | else |
| 4602 | { |
| 4603 | ret = decrypt_scm_v2((uint32 **) &data, &sz); |
| 4604 | if(ret != 0) |
| 4605 | { |
| 4606 | dprintf(CRITICAL,"ERROR: Decryption Failure\n"); |
| 4607 | return; |
| 4608 | } |
| 4609 | } |
| 4610 | } |
| 4611 | else |
| 4612 | { |
| 4613 | if (magic_number[0] == DECRYPT_MAGIC_0 && |
| 4614 | magic_number[1] == DECRYPT_MAGIC_1) |
| 4615 | { |
| 4616 | ret = decrypt_scm((uint32 **) &data, &sz); |
| 4617 | if (ret != 0) { |
| 4618 | dprintf(CRITICAL, "ERROR: Invalid secure image\n"); |
| 4619 | return; |
| 4620 | } |
| 4621 | } |
| 4622 | else if (magic_number[0] == ENCRYPT_MAGIC_0 && |
| 4623 | magic_number[1] == ENCRYPT_MAGIC_1) |
| 4624 | { |
| 4625 | ret = encrypt_scm((uint32 **) &data, &sz); |
| 4626 | if (ret != 0) { |
| 4627 | dprintf(CRITICAL, "ERROR: Encryption Failure\n"); |
| 4628 | return; |
| 4629 | } |
| 4630 | } |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 4631 | } |
| 4632 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4633 | else |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 4634 | { |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4635 | dprintf(CRITICAL,"INVALID SVC Version\n"); |
| 4636 | return; |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 4637 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4638 | #endif /* SSD_ENABLE */ |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 4639 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4640 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 4641 | if (target_build_variant_user()) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4642 | { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4643 | /* if device is locked: |
| 4644 | * common partition will not allow to be flashed |
| 4645 | * critical partition will allow to flash image. |
| 4646 | */ |
| 4647 | if(!device.is_unlocked && !critical_flash_allowed(arg)) { |
| 4648 | fastboot_fail("Partition flashing is not allowed"); |
| 4649 | return; |
| 4650 | } |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4651 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4652 | /* if device critical is locked: |
| 4653 | * common partition will allow to be flashed |
| 4654 | * critical partition will not allow to flash image. |
| 4655 | */ |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 4656 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4657 | !device.is_unlock_critical && |
| 4658 | critical_flash_allowed(arg)) { |
| 4659 | fastboot_fail("Critical partition flashing is not allowed"); |
| 4660 | return; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4661 | } |
| 4662 | } |
| 4663 | #endif |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 4664 | |
| 4665 | if (target_virtual_ab_supported()) |
| 4666 | { |
| 4667 | if (CheckVirtualAbCriticalPartition (arg)) { |
| 4668 | snprintf(FlashResultStr, MAX_RSP_SIZE,"Flashing of %s is not allowed in %s state", |
| 4669 | arg, SnapshotMergeState); |
| 4670 | fastboot_fail(FlashResultStr); |
| 4671 | return; |
| 4672 | } |
| 4673 | |
| 4674 | SnapshotMergeStatus = GetSnapshotMergeStatus (); |
| 4675 | if (((SnapshotMergeStatus == MERGING) || (SnapshotMergeStatus == SNAPSHOTTED)) && |
| 4676 | !strncmp (arg, "super", strlen ("super"))) { |
| 4677 | if(SetSnapshotMergeStatus (CANCELLED)) |
| 4678 | { |
| 4679 | fastboot_fail("Failed to update snapshot state to cancel"); |
| 4680 | return; |
| 4681 | } |
| 4682 | |
| 4683 | //updating fbvar snapshot-merge-state |
| 4684 | snprintf(SnapshotMergeState,strlen(VabSnapshotMergeStatus[NONE_MERGE_STATUS]) + 1, |
| 4685 | "%s", VabSnapshotMergeStatus[NONE_MERGE_STATUS]); |
| 4686 | } |
| 4687 | } |
| 4688 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4689 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 4690 | dprintf(INFO, "flashing avb_custom_key\n"); |
| 4691 | if (store_userkey(data, sz)) { |
| 4692 | fastboot_fail("Flashing avb_custom_key failed"); |
| 4693 | } else { |
| 4694 | fastboot_okay(""); |
| 4695 | } |
| 4696 | return; |
| 4697 | } |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4698 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 4699 | sparse_header = (sparse_header_t *) data; |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4700 | meta_header = (meta_header_t *) data; |
| 4701 | if (sparse_header->magic == SPARSE_HEADER_MAGIC) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4702 | cmd_flash_mmc_sparse_img(arg, data, sz); |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4703 | else if (meta_header->magic == META_HEADER_MAGIC) |
| 4704 | cmd_flash_meta_img(arg, data, sz); |
| 4705 | else |
| 4706 | cmd_flash_mmc_img(arg, data, sz); |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 4707 | |
| 4708 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 4709 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4710 | (!strncmp(arg, "system", 6)) && |
| 4711 | !device.verity_mode) |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 4712 | // reset dm_verity mode to enforcing |
| 4713 | device.verity_mode = 1; |
| 4714 | write_device_info(&device); |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 4715 | #endif |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 4716 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4717 | return; |
| 4718 | } |
| 4719 | |
Tanya Brokhman | 84eaaf6 | 2015-02-24 21:57:06 +0200 | [diff] [blame] | 4720 | void cmd_updatevol(const char *vol_name, void *data, unsigned sz) |
| 4721 | { |
| 4722 | struct ptentry *sys_ptn; |
| 4723 | struct ptable *ptable; |
| 4724 | |
| 4725 | ptable = flash_get_ptable(); |
| 4726 | if (ptable == NULL) { |
| 4727 | fastboot_fail("partition table doesn't exist"); |
| 4728 | return; |
| 4729 | } |
| 4730 | |
| 4731 | sys_ptn = ptable_find(ptable, "system"); |
| 4732 | if (sys_ptn == NULL) { |
| 4733 | fastboot_fail("system partition not found"); |
| 4734 | return; |
| 4735 | } |
| 4736 | |
| 4737 | sz = ROUND_TO_PAGE(sz, page_mask); |
| 4738 | if (update_ubi_vol(sys_ptn, vol_name, data, sz)) |
| 4739 | fastboot_fail("update_ubi_vol failed"); |
| 4740 | else |
| 4741 | fastboot_okay(""); |
| 4742 | } |
| 4743 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 4744 | void cmd_flash_nand(const char *arg, void *data, unsigned sz) |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4745 | { |
| 4746 | struct ptentry *ptn; |
| 4747 | struct ptable *ptable; |
| 4748 | unsigned extra = 0; |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 4749 | uint64_t partition_size = 0; |
Vijay Kumar Pendoti | c6f22a7 | 2017-05-19 22:44:34 +0530 | [diff] [blame] | 4750 | unsigned bytes_to_round_page = 0; |
| 4751 | unsigned rounded_size = 0; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4752 | |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4753 | if((uintptr_t)data > (UINT_MAX - sz)) { |
| 4754 | fastboot_fail("Cannot flash: image header corrupt"); |
| 4755 | return; |
| 4756 | } |
| 4757 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4758 | ptable = flash_get_ptable(); |
| 4759 | if (ptable == NULL) { |
| 4760 | fastboot_fail("partition table doesn't exist"); |
| 4761 | return; |
| 4762 | } |
| 4763 | |
| 4764 | ptn = ptable_find(ptable, arg); |
| 4765 | if (ptn == NULL) { |
Tanya Brokhman | 84eaaf6 | 2015-02-24 21:57:06 +0200 | [diff] [blame] | 4766 | dprintf(INFO, "unknown partition name (%s). Trying updatevol\n", |
| 4767 | arg); |
| 4768 | cmd_updatevol(arg, data, sz); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4769 | return; |
| 4770 | } |
| 4771 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4772 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 4773 | dprintf(INFO, "flashing avb_custom_key\n"); |
| 4774 | if (store_userkey(data, sz)) { |
| 4775 | fastboot_fail("Flashing avb_custom_key failed"); |
| 4776 | } else { |
| 4777 | fastboot_okay(""); |
| 4778 | } |
| 4779 | return; |
| 4780 | } |
| 4781 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4782 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4783 | if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) { |
| 4784 | dprintf(INFO, "Verified the BOOT_MAGIC in image header \n"); |
| 4785 | } else { |
| 4786 | fastboot_fail("Image is not a boot image"); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4787 | return; |
| 4788 | } |
| 4789 | } |
| 4790 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 4791 | if (!strcmp(ptn->name, "system") |
Deepa Dinamani | 13e32c4 | 2012-03-12 14:34:17 -0700 | [diff] [blame] | 4792 | || !strcmp(ptn->name, "userdata") |
| 4793 | || !strcmp(ptn->name, "persist") |
Sundarajan Srinivasan | b063a85 | 2013-11-19 14:02:27 -0800 | [diff] [blame] | 4794 | || !strcmp(ptn->name, "recoveryfs") |
Sundarajan Srinivasan | 595b71e | 2013-11-05 12:44:34 -0800 | [diff] [blame] | 4795 | || !strcmp(ptn->name, "modem")) |
Tanya Brokhman | 1c94f1a | 2015-02-15 09:05:03 +0200 | [diff] [blame] | 4796 | extra = 1; |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4797 | else { |
Vijay Kumar Pendoti | c6f22a7 | 2017-05-19 22:44:34 +0530 | [diff] [blame] | 4798 | rounded_size = ROUNDUP(sz, page_size); |
| 4799 | bytes_to_round_page = rounded_size - sz; |
| 4800 | if (bytes_to_round_page) { |
| 4801 | if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) { |
| 4802 | fastboot_fail("Integer overflow detected"); |
| 4803 | return; |
| 4804 | } |
| 4805 | if (((uintptr_t)data + sz + bytes_to_round_page) > |
| 4806 | ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) { |
| 4807 | fastboot_fail("Buffer size is not aligned to page_size"); |
| 4808 | return; |
| 4809 | } |
| 4810 | else { |
| 4811 | memset(data + sz, 0, bytes_to_round_page); |
| 4812 | sz = rounded_size; |
| 4813 | } |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4814 | } |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 4815 | } |
| 4816 | |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4817 | /*Checking partition_size for the possible integer overflow */ |
| 4818 | partition_size = validate_partition_size(ptn); |
| 4819 | |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 4820 | if (sz > partition_size) { |
| 4821 | fastboot_fail("Image size too large"); |
| 4822 | return; |
| 4823 | } |
| 4824 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4825 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
Mayank Grover | 6cde935 | 2017-06-06 18:45:23 +0530 | [diff] [blame] | 4826 | if ((sz > UBI_EC_HDR_SIZE) && |
| 4827 | (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) { |
Tanya Brokhman | 1c94f1a | 2015-02-15 09:05:03 +0200 | [diff] [blame] | 4828 | if (flash_ubi_img(ptn, data, sz)) { |
| 4829 | fastboot_fail("flash write failure"); |
| 4830 | return; |
| 4831 | } |
| 4832 | } else { |
| 4833 | if (flash_write(ptn, extra, data, sz)) { |
| 4834 | fastboot_fail("flash write failure"); |
| 4835 | return; |
| 4836 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4837 | } |
| 4838 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 4839 | fastboot_okay(""); |
| 4840 | } |
| 4841 | |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4842 | |
| 4843 | static inline uint64_t validate_partition_size(struct ptentry *ptn) |
| 4844 | { |
| 4845 | if (ptn->length && flash_num_pages_per_blk() && page_size) { |
| 4846 | if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) { |
| 4847 | return ptn->length * flash_num_pages_per_blk() * page_size; |
| 4848 | } |
| 4849 | } |
| 4850 | return 0; |
| 4851 | } |
| 4852 | |
| 4853 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 4854 | void cmd_flash(const char *arg, void *data, unsigned sz) |
| 4855 | { |
| 4856 | if(target_is_emmc_boot()) |
| 4857 | cmd_flash_mmc(arg, data, sz); |
| 4858 | else |
| 4859 | cmd_flash_nand(arg, data, sz); |
| 4860 | } |
| 4861 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4862 | void cmd_continue(const char *arg, void *data, unsigned sz) |
| 4863 | { |
| 4864 | fastboot_okay(""); |
Amol Jadi | 7c4316c | 2013-10-07 14:19:26 -0700 | [diff] [blame] | 4865 | fastboot_stop(); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4866 | |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4867 | if (target_is_emmc_boot()) |
| 4868 | { |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 4869 | #if FBCON_DISPLAY_MSG |
lijuang | de34d50 | 2016-02-26 16:04:50 +0800 | [diff] [blame] | 4870 | /* Exit keys' detection thread firstly */ |
| 4871 | exit_menu_keys_detection(); |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 4872 | #endif |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4873 | boot_linux_from_mmc(); |
| 4874 | } |
| 4875 | else |
| 4876 | { |
| 4877 | boot_linux_from_flash(); |
| 4878 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4879 | } |
| 4880 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 4881 | void cmd_reboot(const char *arg, void *data, unsigned sz) |
| 4882 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4883 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 4884 | fastboot_okay(""); |
| 4885 | reboot_device(0); |
| 4886 | } |
| 4887 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4888 | void cmd_set_active(const char *arg, void *data, unsigned sz) |
| 4889 | { |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4890 | char *p, *sp = NULL; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4891 | unsigned i,current_active_slot; |
| 4892 | const char *current_slot_suffix; |
| 4893 | |
| 4894 | if (!partition_multislot_is_supported()) |
| 4895 | { |
| 4896 | fastboot_fail("Command not supported"); |
| 4897 | return; |
| 4898 | } |
| 4899 | |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 4900 | if (target_virtual_ab_supported()) { |
| 4901 | if (GetSnapshotMergeStatus () == MERGING) { |
| 4902 | fastboot_fail ("Slot Change is not allowed in merging state"); |
| 4903 | return; |
| 4904 | } |
| 4905 | } |
| 4906 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4907 | if (arg) |
| 4908 | { |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4909 | p = strtok_r((char *)arg, ":", &sp); |
Mayank Grover | 5eb5f69 | 2017-07-19 20:16:04 +0530 | [diff] [blame] | 4910 | if (p) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4911 | { |
| 4912 | current_active_slot = partition_find_active_slot(); |
| 4913 | |
| 4914 | /* Check if trying to make curent slot active */ |
| 4915 | current_slot_suffix = SUFFIX_SLOT(current_active_slot); |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4916 | current_slot_suffix = strtok_r((char *)current_slot_suffix, |
| 4917 | (char *)suffix_delimiter, &sp); |
| 4918 | |
Mayank Grover | 5eb5f69 | 2017-07-19 20:16:04 +0530 | [diff] [blame] | 4919 | if (current_slot_suffix && |
| 4920 | !strncmp(p, current_slot_suffix, strlen(current_slot_suffix))) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4921 | { |
| 4922 | fastboot_okay("Slot already set active"); |
| 4923 | return; |
| 4924 | } |
| 4925 | else |
| 4926 | { |
| 4927 | for (i = 0; i < AB_SUPPORTED_SLOTS; i++) |
| 4928 | { |
| 4929 | current_slot_suffix = SUFFIX_SLOT(i); |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4930 | current_slot_suffix = strtok_r((char *)current_slot_suffix, |
| 4931 | (char *)suffix_delimiter, &sp); |
Mayank Grover | 5eb5f69 | 2017-07-19 20:16:04 +0530 | [diff] [blame] | 4932 | if (current_slot_suffix && |
| 4933 | !strncmp(p, current_slot_suffix, strlen(current_slot_suffix))) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4934 | { |
lijuang | 8b03e5f | 2019-07-12 18:16:19 +0800 | [diff] [blame] | 4935 | partition_switch_slots(current_active_slot, i, true); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4936 | publish_getvar_multislot_vars(); |
| 4937 | fastboot_okay(""); |
| 4938 | return; |
| 4939 | } |
| 4940 | } |
| 4941 | } |
| 4942 | } |
| 4943 | } |
| 4944 | fastboot_fail("Invalid slot suffix."); |
| 4945 | return; |
| 4946 | } |
| 4947 | |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 4948 | #if DYNAMIC_PARTITION_SUPPORT |
| 4949 | void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz) |
| 4950 | { |
| 4951 | dprintf(INFO, "rebooting the device - userspace fastboot\n"); |
| 4952 | if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) { |
| 4953 | dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n"); |
| 4954 | fastboot_fail("Failed to update recovery command"); |
| 4955 | return; |
| 4956 | } |
| 4957 | fastboot_okay(""); |
| 4958 | reboot_device(REBOOT_MODE_UNKNOWN); |
| 4959 | |
| 4960 | //shouldn't come here. |
| 4961 | dprintf(CRITICAL, "ERROR: Failed to reboot device\n"); |
| 4962 | return; |
| 4963 | } |
| 4964 | |
| 4965 | void cmd_reboot_recovery(const char *arg, void *data, unsigned sz) |
| 4966 | { |
| 4967 | dprintf(INFO, "rebooting the device - recovery\n"); |
| 4968 | if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) { |
| 4969 | dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n"); |
| 4970 | fastboot_fail("Failed to update recovery command"); |
| 4971 | return; |
| 4972 | } |
| 4973 | fastboot_okay(""); |
| 4974 | reboot_device(REBOOT_MODE_UNKNOWN); |
| 4975 | |
| 4976 | //shouldn't come here. |
| 4977 | dprintf(CRITICAL, "ERROR: Failed to reboot device\n"); |
| 4978 | return; |
| 4979 | } |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 4980 | |
| 4981 | #ifdef VIRTUAL_AB_OTA |
| 4982 | void CmdUpdateSnapshot(const char *arg, void *data, unsigned sz) |
| 4983 | { |
| 4984 | char *command = NULL; |
| 4985 | const char *delim = ":"; |
| 4986 | char *sp; |
| 4987 | |
| 4988 | if (arg) { |
| 4989 | command = strtok_r((char *)arg, delim, &sp); |
| 4990 | if (command) { |
| 4991 | command++; |
| 4992 | |
| 4993 | if(!strncmp (command, "merge", AsciiStrLen ("merge"))) { |
| 4994 | if (GetSnapshotMergeStatus () == MERGING) { |
| 4995 | cmd_reboot_fastboot(Arg, Data, Size); |
| 4996 | } |
| 4997 | FastbootOkay (""); |
| 4998 | return; |
| 4999 | } |
| 5000 | else if (!strncmp (Command, "cancel", AsciiStrLen ("cancel"))) { |
| 5001 | if(!device.is_unlocked) { |
| 5002 | fastboot_fail ("Snapshot Cancel is not allowed in Lock State"); |
| 5003 | return; |
| 5004 | } |
| 5005 | |
| 5006 | if (SetSnapshotMergeStatus (CANCELLED)) |
| 5007 | { |
| 5008 | fastboot_fail("Failed to update snapshot state to cancel"); |
| 5009 | return; |
| 5010 | } |
| 5011 | |
| 5012 | //updating fbvar snapshot-merge-state |
| 5013 | snprintf(SnapshotMergeState, strlen(VabSnapshotMergeStatus[NONE_MERGE_STATUS]) + 1, |
| 5014 | "%s", VabSnapshotMergeStatus[NONE_MERGE_STATUS]); |
| 5015 | fastboot_okay(""); |
| 5016 | return; |
| 5017 | } |
| 5018 | } |
| 5019 | } |
| 5020 | fastboot_fail("Invalid snapshot-update command"); |
| 5021 | return; |
| 5022 | } |
| 5023 | #endif |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 5024 | #endif |
| 5025 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 5026 | void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz) |
| 5027 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5028 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 5029 | fastboot_okay(""); |
| 5030 | reboot_device(FASTBOOT_MODE); |
| 5031 | } |
| 5032 | |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5033 | void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size) |
| 5034 | { |
| 5035 | dprintf(INFO, "Enabling charger screen check\n"); |
| 5036 | device.charger_screen_enabled = 1; |
| 5037 | write_device_info(&device); |
| 5038 | fastboot_okay(""); |
| 5039 | } |
| 5040 | |
| 5041 | void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size) |
| 5042 | { |
| 5043 | dprintf(INFO, "Disabling charger screen check\n"); |
| 5044 | device.charger_screen_enabled = 0; |
| 5045 | write_device_info(&device); |
| 5046 | fastboot_okay(""); |
| 5047 | } |
| 5048 | |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5049 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5050 | void CmdOemEnableAdb(const char *arg, void *data, unsigned size) |
| 5051 | { |
| 5052 | dprintf(INFO, "Enabling Adb\n"); |
| 5053 | prod.is_adb_enabled = 1; |
| 5054 | write_prod_info(&prod); |
| 5055 | fastboot_okay(""); |
| 5056 | } |
| 5057 | #endif |
| 5058 | |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5059 | void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size) |
| 5060 | { |
| 5061 | char *p = NULL; |
| 5062 | const char *delim = " \t\n\r"; |
Parth Dixit | 407f15b | 2016-01-07 14:47:37 +0530 | [diff] [blame] | 5063 | char *sp; |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5064 | |
| 5065 | if (arg) { |
Parth Dixit | 407f15b | 2016-01-07 14:47:37 +0530 | [diff] [blame] | 5066 | p = strtok_r((char *)arg, delim, &sp); |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5067 | if (p) { |
| 5068 | if (!strncmp(p, "0", 1)) { |
| 5069 | device.charger_screen_enabled = 0; |
| 5070 | } else if (!strncmp(p, "1", 1)) { |
| 5071 | device.charger_screen_enabled = 1; |
| 5072 | } |
| 5073 | } |
| 5074 | } |
| 5075 | |
| 5076 | /* update charger_screen_enabled value for getvar |
| 5077 | * command |
| 5078 | */ |
| 5079 | snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d", |
| 5080 | device.charger_screen_enabled); |
| 5081 | |
| 5082 | write_device_info(&device); |
| 5083 | fastboot_okay(""); |
| 5084 | } |
| 5085 | |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 5086 | void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size) |
| 5087 | { |
| 5088 | dprintf(INFO, "Selecting display panel %s\n", arg); |
| 5089 | if (arg) |
| 5090 | strlcpy(device.display_panel, arg, |
| 5091 | sizeof(device.display_panel)); |
| 5092 | write_device_info(&device); |
| 5093 | fastboot_okay(""); |
| 5094 | } |
| 5095 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 5096 | void cmd_oem_unlock(const char *arg, void *data, unsigned sz) |
| 5097 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5098 | set_device_unlock(UNLOCK, TRUE); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5099 | } |
| 5100 | |
| 5101 | void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz) |
| 5102 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5103 | if(!device.is_unlocked) { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5104 | if(!is_allow_unlock) { |
| 5105 | fastboot_fail("oem unlock is not allowed"); |
| 5106 | return; |
| 5107 | } |
| 5108 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5109 | set_device_unlock_value(UNLOCK, TRUE); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5110 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5111 | /* wipe data */ |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5112 | struct recovery_message msg; |
Kishor PK | 60a6821 | 2017-05-08 16:55:57 +0530 | [diff] [blame] | 5113 | memset(&msg, 0, sizeof(msg)); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5114 | snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data"); |
| 5115 | write_misc(0, &msg, sizeof(msg)); |
| 5116 | |
| 5117 | fastboot_okay(""); |
| 5118 | reboot_device(RECOVERY_MODE); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 5119 | } |
| 5120 | fastboot_okay(""); |
| 5121 | } |
| 5122 | |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 5123 | static int aboot_frp_unlock(char *pname, void *data, unsigned sz) |
| 5124 | { |
Mayank Grover | 70f8b0e | 2017-05-17 11:49:00 +0530 | [diff] [blame] | 5125 | int ret=1; |
| 5126 | bool authentication_success=false; |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 5127 | |
Mayank Grover | 70f8b0e | 2017-05-17 11:49:00 +0530 | [diff] [blame] | 5128 | /* |
| 5129 | Authentication method not implemented. |
| 5130 | |
| 5131 | OEM to implement, authentication system which on successful validataion, |
| 5132 | calls write_allow_oem_unlock() with is_allow_unlock. |
| 5133 | */ |
| 5134 | #if 0 |
| 5135 | authentication_success = oem_specific_auth_mthd(); |
| 5136 | #endif |
| 5137 | |
| 5138 | if (authentication_success) |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 5139 | { |
Mayank Grover | 70f8b0e | 2017-05-17 11:49:00 +0530 | [diff] [blame] | 5140 | is_allow_unlock = true; |
| 5141 | write_allow_oem_unlock(is_allow_unlock); |
| 5142 | ret = 0; |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 5143 | } |
| 5144 | return ret; |
| 5145 | } |
| 5146 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 5147 | void cmd_oem_lock(const char *arg, void *data, unsigned sz) |
| 5148 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5149 | set_device_unlock(UNLOCK, FALSE); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 5150 | } |
| 5151 | |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5152 | void cmd_oem_devinfo(const char *arg, void *data, unsigned sz) |
| 5153 | { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5154 | char response[MAX_RSP_SIZE]; |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5155 | snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false")); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5156 | fastboot_info(response); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5157 | snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false")); |
| 5158 | fastboot_info(response); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 5159 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 5160 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5161 | { |
| 5162 | snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", |
| 5163 | (device.is_unlock_critical ? "true" : "false")); |
| 5164 | fastboot_info(response); |
| 5165 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 5166 | #endif |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5167 | 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] | 5168 | fastboot_info(response); |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 5169 | snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel)); |
| 5170 | fastboot_info(response); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5171 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5172 | snprintf(response, sizeof(response), "\tAdb enabled: %s", prod.is_adb_enabled ? "true" : "false"); |
| 5173 | fastboot_info(response); |
| 5174 | #endif |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5175 | fastboot_okay(""); |
| 5176 | } |
| 5177 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5178 | void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz) |
| 5179 | { |
| 5180 | char response[MAX_RSP_SIZE]; |
| 5181 | snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock); |
| 5182 | fastboot_info(response); |
| 5183 | fastboot_okay(""); |
| 5184 | } |
| 5185 | |
| 5186 | void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz) |
| 5187 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5188 | set_device_unlock(UNLOCK_CRITICAL, FALSE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5189 | } |
| 5190 | |
| 5191 | void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz) |
| 5192 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5193 | set_device_unlock(UNLOCK_CRITICAL, TRUE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5194 | } |
| 5195 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 5196 | void cmd_preflash(const char *arg, void *data, unsigned sz) |
| 5197 | { |
| 5198 | fastboot_okay(""); |
| 5199 | } |
| 5200 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5201 | static uint8_t logo_header[LOGO_IMG_HEADER_SIZE]; |
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 | int splash_screen_check_header(logo_img_header *header) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5204 | { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5205 | if (memcmp(header->magic, LOGO_IMG_MAGIC, 8)) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5206 | return -1; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5207 | if (header->width == 0 || header->height == 0) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5208 | return -1; |
Ashish Bhimanpalliwar | 36f9f00 | 2021-04-14 13:50:52 +0530 | [diff] [blame] | 5209 | if (((header->blocks == 0) || (header->blocks > UINT_MAX/512))) { |
Ashish Bhimanpalliwar | eab60c6 | 2020-10-09 16:31:11 +0530 | [diff] [blame] | 5210 | return -1; |
| 5211 | } |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5212 | return 0; |
| 5213 | } |
| 5214 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5215 | int splash_screen_flash() |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5216 | { |
| 5217 | struct ptentry *ptn; |
| 5218 | struct ptable *ptable; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5219 | struct logo_img_header *header; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5220 | struct fbcon_config *fb_display = NULL; |
Channagoud Kadabi | b3ccf5c | 2014-12-03 12:39:29 -0800 | [diff] [blame] | 5221 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5222 | ptable = flash_get_ptable(); |
| 5223 | if (ptable == NULL) { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5224 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 5225 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5226 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5227 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5228 | ptn = ptable_find(ptable, "splash"); |
| 5229 | if (ptn == NULL) { |
| 5230 | dprintf(CRITICAL, "ERROR: splash Partition not found\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5231 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5232 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5233 | if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5234 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5235 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5236 | } |
| 5237 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5238 | header = (struct logo_img_header *)logo_header; |
| 5239 | if (splash_screen_check_header(header)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5240 | dprintf(CRITICAL, "ERROR: Boot image header invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5241 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5242 | } |
| 5243 | |
| 5244 | fb_display = fbcon_display(); |
| 5245 | if (fb_display) { |
Ashish Bhimanpalliwar | eab60c6 | 2020-10-09 16:31:11 +0530 | [diff] [blame] | 5246 | if (header->type && |
| 5247 | ((header->blocks * 512) <= (fb_display->width * |
| 5248 | fb_display->height * (fb_display->bpp / 8)))) { |
raghavendra ambadas | 554f4fd | 2018-10-09 15:50:40 +0530 | [diff] [blame] | 5249 | /* RLE24 compressed data */ |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5250 | uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET; |
| 5251 | |
| 5252 | /* if the logo is full-screen size, remove "fbcon_clear()" */ |
| 5253 | if ((header->width != fb_display->width) |
| 5254 | || (header->height != fb_display->height)) |
| 5255 | fbcon_clear(); |
| 5256 | |
| 5257 | if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0, |
| 5258 | (uint32_t *)base, |
| 5259 | (header->blocks * 512))) { |
| 5260 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 5261 | return -1; |
| 5262 | } |
| 5263 | fbcon_extract_to_screen(header, base); |
| 5264 | return 0; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5265 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5266 | |
Veera Sundaram Sankaran | c33416f | 2015-09-28 10:25:55 -0700 | [diff] [blame] | 5267 | if ((header->width > fb_display->width) || (header->height > fb_display->height)) { |
| 5268 | 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] | 5269 | return -1; |
| 5270 | } |
| 5271 | |
Vineet Bajaj | 99291ed | 2014-09-09 12:29:46 +0530 | [diff] [blame] | 5272 | uint8_t *base = (uint8_t *) fb_display->base; |
Sachin Bhayare | 619e9e4 | 2017-05-15 13:10:31 +0530 | [diff] [blame] | 5273 | uint32_t fb_size = ROUNDUP(fb_display->width * |
| 5274 | fb_display->height * |
| 5275 | (fb_display->bpp / 8), 4096); |
| 5276 | uint32_t splash_size = ((((header->width * header->height * |
| 5277 | fb_display->bpp/8) + 511) >> 9) << 9); |
| 5278 | |
Ashish Bhimanpalliwar | 36f9f00 | 2021-04-14 13:50:52 +0530 | [diff] [blame] | 5279 | if ((header->height * header->width * (fb_display->bpp/8)) > (header->blocks * 512)) { |
| 5280 | dprintf(CRITICAL, "ERROR: Splash image size invalid\n"); |
| 5281 | return -1; |
| 5282 | } |
| 5283 | |
Sachin Bhayare | 619e9e4 | 2017-05-15 13:10:31 +0530 | [diff] [blame] | 5284 | if (splash_size > fb_size) { |
| 5285 | dprintf(CRITICAL, "ERROR: Splash image size invalid\n"); |
| 5286 | return -1; |
| 5287 | } |
| 5288 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5289 | if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0, |
| 5290 | (uint32_t *)base, |
| 5291 | ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5292 | fbcon_clear(); |
Vineet Bajaj | 99291ed | 2014-09-09 12:29:46 +0530 | [diff] [blame] | 5293 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5294 | return -1; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5295 | } |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5296 | } |
| 5297 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5298 | return 0; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5299 | } |
| 5300 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5301 | int splash_screen_mmc() |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5302 | { |
| 5303 | int index = INVALID_PTN; |
| 5304 | unsigned long long ptn = 0; |
| 5305 | struct fbcon_config *fb_display = NULL; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5306 | struct logo_img_header *header; |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5307 | uint32_t blocksize, realsize, readsize; |
| 5308 | uint8_t *base; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5309 | |
| 5310 | index = partition_get_index("splash"); |
| 5311 | if (index == 0) { |
| 5312 | dprintf(CRITICAL, "ERROR: splash Partition table not found\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5313 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5314 | } |
| 5315 | |
| 5316 | ptn = partition_get_offset(index); |
| 5317 | if (ptn == 0) { |
| 5318 | dprintf(CRITICAL, "ERROR: splash Partition invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5319 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5320 | } |
| 5321 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5322 | mmc_set_lun(partition_get_lun(index)); |
| 5323 | |
| 5324 | blocksize = mmc_get_device_blocksize(); |
| 5325 | if (blocksize == 0) { |
| 5326 | dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n"); |
| 5327 | return -1; |
| 5328 | } |
| 5329 | |
| 5330 | fb_display = fbcon_display(); |
Channagoud Kadabi | daf10a6 | 2015-07-22 11:51:55 -0700 | [diff] [blame] | 5331 | if (!fb_display) |
| 5332 | { |
| 5333 | dprintf(CRITICAL, "ERROR: fb config is not allocated\n"); |
| 5334 | return -1; |
| 5335 | } |
| 5336 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5337 | base = (uint8_t *) fb_display->base; |
| 5338 | |
jialongjhan | 2769d20 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5339 | if (mmc_read(ptn , (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5340 | dprintf(CRITICAL, "ERROR: Cannot read splash image header\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5341 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5342 | } |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5343 | header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5344 | if (splash_screen_check_header(header)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5345 | dprintf(CRITICAL, "ERROR: Splash image header invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5346 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5347 | } |
| 5348 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5349 | if (fb_display) { |
Ashish Bhimanpalliwar | eab60c6 | 2020-10-09 16:31:11 +0530 | [diff] [blame] | 5350 | if (header->type && |
Ashish Bhimanpalliwar | ce39f48 | 2020-10-06 15:27:37 +0530 | [diff] [blame] | 5351 | (((UINT_MAX - LOGO_IMG_HEADER_SIZE) / 512) >= header->blocks) && |
raghavendra ambadas | 554f4fd | 2018-10-09 15:50:40 +0530 | [diff] [blame] | 5352 | ((header->blocks * 512) <= (fb_display->width * |
| 5353 | fb_display->height * (fb_display->bpp / 8)))) { |
| 5354 | /* 1 RLE24 compressed data */ |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5355 | base += LOGO_IMG_OFFSET; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5356 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5357 | realsize = header->blocks * 512; |
| 5358 | readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize; |
| 5359 | |
| 5360 | /* if the logo is not full-screen size, clean screen */ |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5361 | if ((header->width != fb_display->width) |
| 5362 | || (header->height != fb_display->height)) |
| 5363 | fbcon_clear(); |
| 5364 | |
Sachin Bhayare | 619e9e4 | 2017-05-15 13:10:31 +0530 | [diff] [blame] | 5365 | uint32_t fb_size = ROUNDUP(fb_display->width * |
| 5366 | fb_display->height * |
| 5367 | (fb_display->bpp / 8), 4096); |
| 5368 | |
| 5369 | if (readsize > fb_size) { |
| 5370 | dprintf(CRITICAL, "ERROR: Splash image size invalid\n"); |
| 5371 | return -1; |
| 5372 | } |
| 5373 | |
jialongjhan | 2769d20 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5374 | if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5375 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 5376 | return -1; |
| 5377 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5378 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5379 | fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE)); |
| 5380 | } else { /* 2 Raw BGR data */ |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5381 | |
Veera Sundaram Sankaran | c33416f | 2015-09-28 10:25:55 -0700 | [diff] [blame] | 5382 | if ((header->width > fb_display->width) || (header->height > fb_display->height)) { |
| 5383 | 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] | 5384 | return -1; |
| 5385 | } |
| 5386 | |
| 5387 | realsize = header->width * header->height * fb_display->bpp / 8; |
| 5388 | readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize; |
Ashish Bhimanpalliwar | 36f9f00 | 2021-04-14 13:50:52 +0530 | [diff] [blame] | 5389 | if (realsize > (header->blocks * 512)) { |
| 5390 | dprintf(CRITICAL, "Logo Size error\n"); |
| 5391 | return -1; |
| 5392 | } |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5393 | |
| 5394 | if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */ |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 5395 | 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] | 5396 | fbcon_clear(); |
| 5397 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 5398 | return -1; |
| 5399 | } |
| 5400 | } else { |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 5401 | if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize , |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5402 | (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) { |
| 5403 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 5404 | return -1; |
| 5405 | } |
| 5406 | memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize); |
| 5407 | } |
| 5408 | } |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5409 | } |
| 5410 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5411 | return 0; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5412 | } |
| 5413 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5414 | int fetch_image_from_partition() |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5415 | { |
| 5416 | if (target_is_emmc_boot()) { |
| 5417 | return splash_screen_mmc(); |
| 5418 | } else { |
| 5419 | return splash_screen_flash(); |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5420 | } |
| 5421 | } |
| 5422 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5423 | void publish_getvar_multislot_vars() |
| 5424 | { |
| 5425 | int i,count; |
| 5426 | static bool published = false; |
| 5427 | static char slot_count[MAX_RSP_SIZE]; |
| 5428 | static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS]; |
| 5429 | static char active_slot_suffix[MAX_RSP_SIZE]; |
| 5430 | static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE]; |
| 5431 | static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE]; |
| 5432 | const char *tmp; |
| 5433 | char tmpbuff[MAX_GET_VAR_NAME_SIZE]; |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5434 | signed active_slt; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5435 | |
| 5436 | if (!published) |
| 5437 | { |
| 5438 | /* Update slot meta info */ |
| 5439 | count = partition_fill_partition_meta(has_slot_pname, has_slot_reply, |
| 5440 | partition_get_partition_count()); |
| 5441 | for(i=0; i<count; i++) |
| 5442 | { |
| 5443 | memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE); |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5444 | snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s", |
| 5445 | has_slot_pname[i]); |
| 5446 | strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5447 | fastboot_publish(has_slot_pname[i], has_slot_reply[i]); |
| 5448 | } |
| 5449 | |
| 5450 | for (i=0; i<AB_SUPPORTED_SLOTS; i++) |
| 5451 | { |
| 5452 | tmp = SUFFIX_SLOT(i); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5453 | tmp++; // to remove "_" from slot_suffix. |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5454 | snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable), |
| 5455 | "slot-unbootable:%s", tmp); |
| 5456 | snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active), |
| 5457 | "slot-active:%s", tmp); |
| 5458 | snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful), |
| 5459 | "slot-success:%s", tmp); |
| 5460 | snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count), |
| 5461 | "slot-retry-count:%s", tmp); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5462 | fastboot_publish(slot_info[i].slot_is_unbootable, |
| 5463 | slot_info[i].slot_is_unbootable_rsp); |
| 5464 | fastboot_publish(slot_info[i].slot_is_active, |
| 5465 | slot_info[i].slot_is_active_rsp); |
| 5466 | fastboot_publish(slot_info[i].slot_is_succesful, |
| 5467 | slot_info[i].slot_is_succesful_rsp); |
| 5468 | fastboot_publish(slot_info[i].slot_retry_count, |
| 5469 | slot_info[i].slot_retry_count_rsp); |
| 5470 | } |
| 5471 | fastboot_publish("current-slot", active_slot_suffix); |
| 5472 | snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS); |
| 5473 | fastboot_publish("slot-count", slot_count); |
| 5474 | published = true; |
| 5475 | } |
| 5476 | |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5477 | active_slt = partition_find_active_slot(); |
| 5478 | if (active_slt != INVALID) |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5479 | { |
| 5480 | tmp = SUFFIX_SLOT(active_slt); |
| 5481 | tmp++; // to remove "_" from slot_suffix. |
| 5482 | snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp); |
| 5483 | } |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5484 | else |
| 5485 | strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix)); |
| 5486 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5487 | /* Update partition meta information */ |
| 5488 | partition_fill_slot_meta(slot_info); |
| 5489 | return; |
| 5490 | } |
| 5491 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5492 | void get_product_name(unsigned char *buf) |
| 5493 | { |
| 5494 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD)); |
| 5495 | return; |
| 5496 | } |
| 5497 | |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5498 | #if PRODUCT_IOT |
| 5499 | void get_bootloader_version_iot(unsigned char *buf) |
| 5500 | { |
| 5501 | if (buf != NULL) |
| 5502 | { |
| 5503 | strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN); |
| 5504 | strlcat(buf, "-", MAX_VERSION_LEN); |
| 5505 | strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN); |
| 5506 | } |
| 5507 | return; |
| 5508 | } |
| 5509 | #endif |
| 5510 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5511 | void get_bootloader_version(unsigned char *buf) |
| 5512 | { |
jhchen | a8a15dd | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 5513 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5514 | return; |
| 5515 | } |
| 5516 | |
| 5517 | void get_baseband_version(unsigned char *buf) |
| 5518 | { |
jhchen | a8a15dd | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 5519 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5520 | return; |
| 5521 | } |
| 5522 | |
Monika Singh | 32a09f7 | 2018-03-14 13:08:29 +0530 | [diff] [blame] | 5523 | bool is_device_locked_critical() |
| 5524 | { |
| 5525 | return device.is_unlock_critical ? false:true; |
| 5526 | } |
| 5527 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5528 | bool is_device_locked() |
| 5529 | { |
| 5530 | return device.is_unlocked ? false:true; |
| 5531 | } |
| 5532 | |
Monika Singh | 32a09f7 | 2018-03-14 13:08:29 +0530 | [diff] [blame] | 5533 | bool is_verity_enforcing() |
| 5534 | { |
| 5535 | return device.verity_mode ? true:false; |
| 5536 | } |
| 5537 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5538 | /* register commands and variables for fastboot */ |
| 5539 | void aboot_fastboot_register_commands(void) |
| 5540 | { |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5541 | int i; |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 5542 | char hw_platform_buf[MAX_RSP_SIZE]; |
Rahul Shahare | e7baede | 2021-02-02 13:23:57 +0530 | [diff] [blame] | 5543 | VirtualAbMergeStatus SnapshotMergeStatus; |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5544 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5545 | struct fastboot_cmd_desc cmd_list[] = { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5546 | /* By default the enabled list is empty. */ |
| 5547 | {"", NULL}, |
| 5548 | /* move commands enclosed within the below ifndef to here |
| 5549 | * if they need to be enabled in user build. |
| 5550 | */ |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5551 | #ifndef DISABLE_FASTBOOT_CMDS |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5552 | /* Register the following commands only for non-user builds */ |
| 5553 | {"flash:", cmd_flash}, |
| 5554 | {"erase:", cmd_erase}, |
| 5555 | {"boot", cmd_boot}, |
| 5556 | {"continue", cmd_continue}, |
| 5557 | {"reboot", cmd_reboot}, |
| 5558 | {"reboot-bootloader", cmd_reboot_bootloader}, |
| 5559 | {"oem unlock", cmd_oem_unlock}, |
| 5560 | {"oem unlock-go", cmd_oem_unlock_go}, |
| 5561 | {"oem lock", cmd_oem_lock}, |
| 5562 | {"flashing unlock", cmd_oem_unlock}, |
| 5563 | {"flashing lock", cmd_oem_lock}, |
| 5564 | {"flashing lock_critical", cmd_flashing_lock_critical}, |
| 5565 | {"flashing unlock_critical", cmd_flashing_unlock_critical}, |
| 5566 | {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability}, |
| 5567 | {"oem device-info", cmd_oem_devinfo}, |
| 5568 | {"preflash", cmd_preflash}, |
| 5569 | {"oem enable-charger-screen", cmd_oem_enable_charger_screen}, |
| 5570 | {"oem disable-charger-screen", cmd_oem_disable_charger_screen}, |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5571 | {"oem off-mode-charge", cmd_oem_off_mode_charger}, |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5572 | {"oem select-display-panel", cmd_oem_select_display_panel}, |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 5573 | {"set_active",cmd_set_active}, |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 5574 | #if DYNAMIC_PARTITION_SUPPORT |
| 5575 | {"reboot-fastboot",cmd_reboot_fastboot}, |
| 5576 | {"reboot-recovery",cmd_reboot_recovery}, |
| 5577 | #endif |
Rahul Shahare | f7a5f8b | 2021-02-04 16:51:36 +0530 | [diff] [blame] | 5578 | #ifdef VIRTUAL_AB_OTA |
| 5579 | {"snapshot-update", CmdUpdateSnapshot}, |
| 5580 | #endif |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 5581 | #if UNITTEST_FW_SUPPORT |
Channagoud Kadabi | d5ddf48 | 2015-09-28 10:31:35 -0700 | [diff] [blame] | 5582 | {"oem run-tests", cmd_oem_runtests}, |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 5583 | #endif |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5584 | #endif |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5585 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5586 | {"oem adb_enable", CmdOemEnableAdb}, |
| 5587 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5588 | }; |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5589 | |
| 5590 | int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]); |
| 5591 | for (i = 1; i < fastboot_cmds_count; i++) |
| 5592 | fastboot_register(cmd_list[i].name,cmd_list[i].cb); |
| 5593 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5594 | /* publish variables and their values */ |
| 5595 | fastboot_publish("product", TARGET(BOARD)); |
| 5596 | fastboot_publish("kernel", "lk"); |
| 5597 | fastboot_publish("serialno", sn_buf); |
| 5598 | |
Mayank Grover | a64dfbe | 2018-05-17 14:06:34 +0530 | [diff] [blame] | 5599 | /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/ |
| 5600 | snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version()); |
| 5601 | fastboot_publish("hw-revision", soc_version_str); |
| 5602 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5603 | /* |
| 5604 | * partition info is supported only for emmc partitions |
| 5605 | * Calling this for NAND prints some error messages which |
| 5606 | * is harmless but misleading. Avoid calling this for NAND |
| 5607 | * devices. |
| 5608 | */ |
| 5609 | if (target_is_emmc_boot()) |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5610 | publish_getvar_partition_info(part_info, partition_get_partition_count()); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5611 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5612 | if (partition_multislot_is_supported()) |
| 5613 | publish_getvar_multislot_vars(); |
| 5614 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5615 | /* Max download size supported */ |
lijuang | 2fbed91 | 2019-07-12 14:15:05 +0800 | [diff] [blame] | 5616 | #if !VERIFIED_BOOT_2 |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5617 | snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", |
| 5618 | target_get_max_flash_size()); |
lijuang | 2fbed91 | 2019-07-12 14:15:05 +0800 | [diff] [blame] | 5619 | #else |
| 5620 | snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", |
| 5621 | SUB_SALT_BUFF_OFFSET(target_get_max_flash_size())); |
| 5622 | #endif |
| 5623 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5624 | fastboot_publish("max-download-size", (const char *) max_download_size); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5625 | /* Is the charger screen check enabled */ |
| 5626 | snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d", |
| 5627 | device.charger_screen_enabled); |
| 5628 | fastboot_publish("charger-screen-enabled", |
| 5629 | (const char *) charger_screen_enabled); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5630 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5631 | read_prod_info(&prod); |
| 5632 | snprintf(AdbEnable, MAX_RSP_SIZE, "%d", |
| 5633 | prod.is_adb_enabled); |
| 5634 | fastboot_publish("adb-enabled", |
| 5635 | (const char *) AdbEnable); |
| 5636 | #endif |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5637 | fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled); |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 5638 | snprintf(panel_display_mode, MAX_RSP_SIZE, "%s", |
| 5639 | device.display_panel); |
| 5640 | fastboot_publish("display-panel", |
| 5641 | (const char *) panel_display_mode); |
Mukesh Ojha | bb6e7f3 | 2018-03-02 15:04:06 +0530 | [diff] [blame] | 5642 | |
| 5643 | if (target_is_emmc_boot()) |
| 5644 | { |
| 5645 | mmc_blocksize = mmc_get_device_blocksize(); |
| 5646 | } |
| 5647 | else |
| 5648 | { |
| 5649 | mmc_blocksize = flash_block_size(); |
| 5650 | } |
| 5651 | snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize); |
| 5652 | fastboot_publish("erase-block-size", (const char *) block_size_string); |
| 5653 | fastboot_publish("logical-block-size", (const char *) block_size_string); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5654 | #if PRODUCT_IOT |
| 5655 | get_bootloader_version_iot(&bootloader_version_string); |
| 5656 | fastboot_publish("version-bootloader", (const char *) bootloader_version_string); |
| 5657 | |
| 5658 | /* Version baseband is n/a for apq iot devices */ |
| 5659 | fastboot_publish("version-baseband", "N/A"); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5660 | #else |
Channagoud Kadabi | aafbbfc | 2015-03-04 17:52:56 -0800 | [diff] [blame] | 5661 | fastboot_publish("version-bootloader", (const char *) device.bootloader_version); |
| 5662 | fastboot_publish("version-baseband", (const char *) device.radio_version); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5663 | #endif |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 5664 | fastboot_publish("secure", is_secure_boot_enable()? "yes":"no"); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5665 | fastboot_publish("unlocked", device.is_unlocked ? "yes":"no"); |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 5666 | smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf)); |
| 5667 | snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf, |
| 5668 | target_is_emmc_boot()? "eMMC":"UFS"); |
| 5669 | fastboot_publish("variant", (const char *) get_variant); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 5670 | #if CHECK_BAT_VOLTAGE |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 5671 | update_battery_status(); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 5672 | fastboot_publish("battery-voltage", (const char *) battery_voltage); |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 5673 | fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 5674 | #endif |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 5675 | if (target_dynamic_partition_supported()) |
| 5676 | fastboot_publish("is-userspace", "no"); |
Rahul Shahare | e7baede | 2021-02-02 13:23:57 +0530 | [diff] [blame] | 5677 | |
| 5678 | if (target_virtual_ab_supported()) { |
| 5679 | SnapshotMergeStatus = GetSnapshotMergeStatus (); |
| 5680 | |
| 5681 | switch (SnapshotMergeStatus) { |
| 5682 | case SNAPSHOTTED: |
| 5683 | SnapshotMergeStatus = SNAPSHOTTED; |
| 5684 | break; |
| 5685 | case MERGING: |
| 5686 | SnapshotMergeStatus = MERGING; |
| 5687 | break; |
| 5688 | default: |
| 5689 | SnapshotMergeStatus = NONE_MERGE_STATUS; |
| 5690 | break; |
| 5691 | } |
| 5692 | |
| 5693 | snprintf(SnapshotMergeState, |
| 5694 | strlen(VabSnapshotMergeStatus[SnapshotMergeStatus]) + 1, |
| 5695 | "%s", VabSnapshotMergeStatus[SnapshotMergeStatus]); |
| 5696 | fastboot_publish("snapshot-update-state", SnapshotMergeState); |
| 5697 | } |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5698 | } |
| 5699 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 5700 | void aboot_init(const struct app_descriptor *app) |
| 5701 | { |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 5702 | unsigned reboot_mode = 0; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5703 | int boot_err_type = 0; |
| 5704 | int boot_slot = INVALID; |
jessicatseng | 43b1e35 | 2020-05-26 13:17:56 +0800 | [diff] [blame] | 5705 | int vbat = 0; |
| 5706 | char boot_vbat[MAX_RSP_SIZE]; |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 5707 | |
lijuang | 3983173 | 2016-01-08 17:49:02 +0800 | [diff] [blame] | 5708 | /* Initialise wdog to catch early lk crashes */ |
| 5709 | #if WDOG_SUPPORT |
| 5710 | msm_wdog_init(); |
| 5711 | #endif |
| 5712 | |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 5713 | /* Setup page size information for nv storage */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5714 | if (target_is_emmc_boot()) |
| 5715 | { |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 5716 | page_size = mmc_page_size(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5717 | page_mask = page_size - 1; |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 5718 | mmc_blocksize = mmc_get_device_blocksize(); |
| 5719 | mmc_blocksize_mask = mmc_blocksize - 1; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5720 | } |
| 5721 | else |
| 5722 | { |
| 5723 | page_size = flash_page_size(); |
| 5724 | page_mask = page_size - 1; |
| 5725 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 5726 | ASSERT((MEMBASE + MEMSIZE) > MEMBASE); |
| 5727 | |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5728 | read_device_info(&device); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5729 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5730 | read_prod_info(&prod); |
| 5731 | #endif |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5732 | read_allow_oem_unlock(&device); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 5733 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5734 | /* Detect multi-slot support */ |
| 5735 | if (partition_multislot_is_supported()) |
| 5736 | { |
| 5737 | boot_slot = partition_find_active_slot(); |
| 5738 | if (boot_slot == INVALID) |
| 5739 | { |
| 5740 | boot_into_fastboot = true; |
| 5741 | dprintf(INFO, "Active Slot: (INVALID)\n"); |
| 5742 | } |
| 5743 | else |
| 5744 | { |
| 5745 | /* Setting the state of system to boot active slot */ |
| 5746 | partition_mark_active_slot(boot_slot); |
| 5747 | dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot)); |
| 5748 | } |
| 5749 | } |
| 5750 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 5751 | target_serialno((unsigned char *) sn_buf); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5752 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5753 | dprintf(CRITICAL,"serial number: %s\n",sn_buf); |
| 5754 | #else |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 5755 | dprintf(SPEW,"serial number: %s\n",sn_buf); |
tracychui | 9d60fc5 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5756 | #endif |
| 5757 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5758 | read_prod_info(&prod); |
| 5759 | snprintf((char *)cust_sn_buf, PRODINFO_MAX_SSN_LEN + 1, "%s", prod.ssn); |
| 5760 | dprintf(CRITICAL,"customer serial number: %s\n", cust_sn_buf); |
| 5761 | snprintf((char *)factory_sn_buf, PRODINFO_MAX_ISN_LEN + 1, "%s", prod.isn); |
| 5762 | dprintf(CRITICAL,"factory serial number: %s\n", factory_sn_buf); |
| 5763 | #endif |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 5764 | memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE); |
| 5765 | |
Matthew Qin | defd556 | 2014-07-11 18:02:40 +0800 | [diff] [blame] | 5766 | /* |
| 5767 | * Check power off reason if user force reset, |
| 5768 | * if yes phone will do normal boot. |
| 5769 | */ |
| 5770 | if (is_user_force_reset()) |
| 5771 | goto normal_boot; |
| 5772 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5773 | /* Check if we should do something other than booting up */ |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 5774 | if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN)) |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 5775 | { |
Pavel Nedev | a4c9d3a | 2013-05-15 14:42:34 +0300 | [diff] [blame] | 5776 | dprintf(ALWAYS,"dload mode key sequence detected\n"); |
lijuang | 395b5e6 | 2015-11-19 17:39:44 +0800 | [diff] [blame] | 5777 | reboot_device(EMERGENCY_DLOAD); |
| 5778 | dprintf(CRITICAL,"Failed to reboot into dload mode\n"); |
| 5779 | |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 5780 | boot_into_fastboot = true; |
| 5781 | } |
| 5782 | if (!boot_into_fastboot) |
| 5783 | { |
| 5784 | if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP)) |
| 5785 | boot_into_recovery = 1; |
| 5786 | if (!boot_into_recovery && |
| 5787 | (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN))) |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5788 | boot_into_fastboot = true; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 5789 | } |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5790 | #if NO_KEYPAD_DRIVER |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 5791 | if (fastboot_trigger()) |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5792 | boot_into_fastboot = true; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5793 | #endif |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 5794 | |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5795 | #if USE_PON_REBOOT_REG |
| 5796 | reboot_mode = check_hard_reboot_mode(); |
| 5797 | #else |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 5798 | reboot_mode = check_reboot_mode(); |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5799 | #endif |
| 5800 | if (reboot_mode == RECOVERY_MODE) |
| 5801 | { |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 5802 | boot_into_recovery = 1; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5803 | } |
| 5804 | else if(reboot_mode == FASTBOOT_MODE) |
| 5805 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5806 | boot_into_fastboot = true; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5807 | } |
| 5808 | else if(reboot_mode == ALARM_BOOT) |
| 5809 | { |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 5810 | boot_reason_alarm = true; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5811 | } |
Monika Singh | b0db4b8 | 2018-09-26 12:18:02 +0530 | [diff] [blame] | 5812 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 5813 | else if (VB_M <= target_get_vb_version()) |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5814 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5815 | if (reboot_mode == DM_VERITY_ENFORCING) |
| 5816 | { |
| 5817 | device.verity_mode = 1; |
| 5818 | write_device_info(&device); |
| 5819 | } |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 5820 | #if ENABLE_VB_ATTEST |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5821 | else if (reboot_mode == DM_VERITY_EIO) |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 5822 | #else |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5823 | else if (reboot_mode == DM_VERITY_LOGGING) |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 5824 | #endif |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5825 | { |
| 5826 | device.verity_mode = 0; |
| 5827 | write_device_info(&device); |
| 5828 | } |
| 5829 | else if (reboot_mode == DM_VERITY_KEYSCLEAR) |
| 5830 | { |
| 5831 | if(send_delete_keys_to_tz()) |
| 5832 | ASSERT(0); |
| 5833 | } |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5834 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 5835 | #endif |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 5836 | |
jialongjhan | 2769d20 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5837 | /* Display splash screen if enabled */ |
| 5838 | #if DISPLAY_SPLASH_SCREEN |
| 5839 | #if NO_ALARM_DISPLAY |
| 5840 | if (!check_alarm_boot()) { |
| 5841 | #endif |
| 5842 | dprintf(SPEW, "Display Init: Start\n"); |
| 5843 | #if DISPLAY_HDMI_PRIMARY |
| 5844 | if (!strlen(device.display_panel)) |
| 5845 | strlcpy(device.display_panel, DISPLAY_PANEL_HDMI, |
| 5846 | sizeof(device.display_panel)); |
| 5847 | #endif |
| 5848 | #if ENABLE_WBC |
| 5849 | /* Wait if the display shutdown is in progress */ |
| 5850 | while(pm_app_display_shutdown_in_prgs()); |
| 5851 | if (!pm_appsbl_display_init_done()) |
| 5852 | target_display_init(device.display_panel); |
| 5853 | else |
| 5854 | display_image_on_screen(); |
| 5855 | #else |
| 5856 | target_display_init(device.display_panel); |
| 5857 | #endif |
| 5858 | dprintf(SPEW, "Display Init: Done\n"); |
| 5859 | #if NO_ALARM_DISPLAY |
| 5860 | } |
| 5861 | #endif |
| 5862 | #endif |
| 5863 | |
| 5864 | |
Matthew Qin | defd556 | 2014-07-11 18:02:40 +0800 | [diff] [blame] | 5865 | normal_boot: |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5866 | if (!boot_into_fastboot) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5867 | { |
jessicatseng | 43b1e35 | 2020-05-26 13:17:56 +0800 | [diff] [blame] | 5868 | if(!target_pause_for_battery_charge()) |
| 5869 | { |
| 5870 | vbat = target_get_battery_voltage(); |
| 5871 | snprintf(boot_vbat, MAX_RSP_SIZE, "%d", vbat); |
| 5872 | dprintf(CRITICAL,"battery_voltage: %s\n", boot_vbat); |
| 5873 | if(vbat < 3500000) |
| 5874 | { |
| 5875 | display_lowbattery_image_on_screen(); |
| 5876 | //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 Start |
| 5877 | msm_display_flush(); |
| 5878 | //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 End |
| 5879 | dprintf(CRITICAL,"Low battery, cannot boot up...\n"); |
| 5880 | mdelay(3000); |
| 5881 | shutdown_device(); |
| 5882 | } |
| 5883 | } |
| 5884 | |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5885 | if (target_is_emmc_boot()) |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5886 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5887 | if(emmc_recovery_init()) |
| 5888 | dprintf(ALWAYS,"error in emmc_recovery_init\n"); |
| 5889 | if(target_use_signed_kernel()) |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5890 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5891 | if((device.is_unlocked) || (device.is_tampered)) |
| 5892 | { |
| 5893 | #ifdef TZ_TAMPER_FUSE |
Monika Singh | 9825746 | 2018-06-06 11:28:49 +0530 | [diff] [blame] | 5894 | set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE); |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5895 | #endif |
| 5896 | #if USE_PCOM_SECBOOT |
| 5897 | set_tamper_flag(device.is_tampered); |
| 5898 | #endif |
| 5899 | } |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5900 | } |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 5901 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5902 | retry_boot: |
| 5903 | /* Trying to boot active partition */ |
| 5904 | if (partition_multislot_is_supported()) |
| 5905 | { |
| 5906 | boot_slot = partition_find_boot_slot(); |
| 5907 | partition_mark_active_slot(boot_slot); |
| 5908 | if (boot_slot == INVALID) |
| 5909 | goto fastboot; |
| 5910 | } |
| 5911 | |
| 5912 | boot_err_type = boot_linux_from_mmc(); |
| 5913 | switch (boot_err_type) |
| 5914 | { |
| 5915 | case ERR_INVALID_PAGE_SIZE: |
| 5916 | case ERR_DT_PARSE: |
| 5917 | case ERR_ABOOT_ADDR_OVERLAP: |
Mayank Grover | 10cabbe | 2017-10-30 19:11:05 +0530 | [diff] [blame] | 5918 | case ERR_INVALID_BOOT_MAGIC: |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5919 | if(partition_multislot_is_supported()) |
Mayank Grover | 10cabbe | 2017-10-30 19:11:05 +0530 | [diff] [blame] | 5920 | { |
| 5921 | /* |
| 5922 | * Deactivate current slot, as it failed to |
| 5923 | * boot, and retry next slot. |
| 5924 | */ |
| 5925 | partition_deactivate_slot(boot_slot); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5926 | goto retry_boot; |
Mayank Grover | 10cabbe | 2017-10-30 19:11:05 +0530 | [diff] [blame] | 5927 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5928 | else |
| 5929 | break; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5930 | default: |
| 5931 | break; |
| 5932 | /* going to fastboot menu */ |
| 5933 | } |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5934 | } |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5935 | else |
| 5936 | { |
| 5937 | recovery_init(); |
| 5938 | #if USE_PCOM_SECBOOT |
| 5939 | if((device.is_unlocked) || (device.is_tampered)) |
| 5940 | set_tamper_flag(device.is_tampered); |
| 5941 | #endif |
| 5942 | boot_linux_from_flash(); |
| 5943 | } |
| 5944 | dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting " |
| 5945 | "to fastboot mode.\n"); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5946 | } |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5947 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5948 | fastboot: |
jialongjhan | 2769d20 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5949 | |
| 5950 | mdelay(1000); |
| 5951 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5952 | /* We are here means regular boot did not happen. Start fastboot. */ |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 5953 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5954 | /* register aboot specific fastboot commands */ |
| 5955 | aboot_fastboot_register_commands(); |
Amol Jadi | 57abe4c | 2011-05-24 15:47:27 -0700 | [diff] [blame] | 5956 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5957 | /* dump partition table for debug info */ |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 5958 | partition_dump(); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5959 | |
| 5960 | /* initialize and start fastboot */ |
Saranya Chidura | 41b3453 | 2018-10-16 12:29:52 +0530 | [diff] [blame] | 5961 | #if !VERIFIED_BOOT_2 |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5962 | fastboot_init(target_get_scratch_address(), target_get_max_flash_size()); |
Saranya Chidura | 41b3453 | 2018-10-16 12:29:52 +0530 | [diff] [blame] | 5963 | #else |
| 5964 | /* Add salt buffer offset at start of image address to copy VB salt */ |
| 5965 | fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()), |
| 5966 | SUB_SALT_BUFF_OFFSET(target_get_max_flash_size())); |
| 5967 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5968 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 5969 | display_fastboot_menu(); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5970 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 5971 | } |
| 5972 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 5973 | uint32_t get_page_size() |
| 5974 | { |
| 5975 | return page_size; |
| 5976 | } |
| 5977 | |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 5978 | /* |
| 5979 | * Calculated and save hash (SHA256) for non-signed boot image. |
| 5980 | * |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5981 | * @param image_addr - Boot image address |
| 5982 | * @param image_size - Size of the boot image |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 5983 | * |
| 5984 | * @return int - 0 on success, negative value on failure. |
| 5985 | */ |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5986 | 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] | 5987 | { |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5988 | unsigned int digest[8]; |
| 5989 | #if IMAGE_VERIF_ALGO_SHA1 |
| 5990 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1; |
| 5991 | #else |
| 5992 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256; |
| 5993 | #endif |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 5994 | |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5995 | target_crypto_init_params(); |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 5996 | 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] | 5997 | |
| 5998 | save_kernel_hash_cmd(digest); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5999 | 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] | 6000 | |
| 6001 | return 0; |
| 6002 | } |
| 6003 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 6004 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 6005 | APP_START(aboot) |
| 6006 | .init = aboot_init, |
| 6007 | APP_END |