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