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" |
tracychui | 3b4abb7 | 2020-06-15 10:50:49 +0800 | [diff] [blame^] | 98 | /*[TracyChui]Add memory_config property 20200615 start */ |
| 99 | #include <mmu.h> |
| 100 | /*[TracyChui]Add memory_config property 20200615 end */ |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 101 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 102 | extern bool target_use_signed_kernel(void); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 103 | extern void platform_uninit(void); |
Channagoud Kadabi | 33defe2 | 2013-06-18 18:35:40 -0700 | [diff] [blame] | 104 | extern void target_uninit(void); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 105 | extern int get_target_boot_params(const char *cmdline, const char *part, |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 106 | char **buf); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 107 | |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 108 | void *info_buf; |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 109 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 110 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 111 | void *prodinfo_buf; |
| 112 | #endif |
| 113 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 114 | void write_device_info_mmc(device_info *dev); |
| 115 | void write_device_info_flash(device_info *dev); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 116 | 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] | 117 | static int aboot_frp_unlock(char *pname, void *data, unsigned sz); |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 118 | static inline uint64_t validate_partition_size(); |
Parth Dixit | 54ac3bb | 2017-03-07 15:52:48 +0530 | [diff] [blame] | 119 | bool pwr_key_is_pressed = false; |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 120 | static bool is_systemd_present=false; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 121 | static void publish_getvar_multislot_vars(); |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 122 | /* fastboot command function pointer */ |
| 123 | typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 124 | bool get_perm_attr_status(); |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 125 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 126 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 127 | void write_prod_info(prod_info *dev); |
| 128 | #endif |
| 129 | /*[20200605][TracyChui] Implement get Serial Number end */ |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 130 | |
| 131 | struct fastboot_cmd_desc { |
| 132 | char * name; |
| 133 | fastboot_cmd_fn cb; |
| 134 | }; |
| 135 | |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 136 | #define EXPAND(NAME) #NAME |
| 137 | #define TARGET(NAME) EXPAND(NAME) |
Brian Swetland | 2defe16 | 2009-08-18 14:35:59 -0700 | [diff] [blame] | 138 | |
Ajay Singh Parmar | a7865a8 | 2015-04-16 21:27:52 -0700 | [diff] [blame] | 139 | #define DISPLAY_PANEL_HDMI "hdmi" |
| 140 | |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 141 | #ifdef MEMBASE |
| 142 | #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE)) |
| 143 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 144 | #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000 |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 145 | #endif |
| 146 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 147 | #ifndef MEMSIZE |
| 148 | #define MEMSIZE 1024*1024 |
| 149 | #endif |
| 150 | |
| 151 | #define MAX_TAGS_SIZE 1024 |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 152 | #define PLL_CODES_OFFSET 4096 |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 153 | /* make 4096 as default size to ensure EFS,EXT4's erasing */ |
| 154 | #define DEFAULT_ERASE_SIZE 4096 |
Ujwal Patel | c0b0a25 | 2015-08-16 14:05:35 -0700 | [diff] [blame] | 155 | #define MAX_PANEL_BUF_SIZE 196 |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 156 | #define FOOTER_SIZE 16384 |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 157 | |
Dhaval Patel | f83d73b | 2014-06-23 16:24:37 -0700 | [diff] [blame] | 158 | #define DISPLAY_DEFAULT_PREFIX "mdss_mdp" |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 159 | #define BOOT_DEV_MAX_LEN 64 |
Sundarajan Srinivasan | 595b71e | 2013-11-05 12:44:34 -0800 | [diff] [blame] | 160 | |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 161 | #define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false |
| 162 | |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 163 | #define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX |
| 164 | |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 165 | //Size of the header that is used in case the boot image has |
| 166 | //a uncompressed kernel + appended dtb |
| 167 | #define PATCHED_KERNEL_HEADER_SIZE 20 |
| 168 | |
| 169 | //String used to determine if the boot image has |
| 170 | //a uncompressed kernel + appended dtb |
| 171 | #define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG" |
| 172 | |
Sridhar Parasuram | 7bd4aaf | 2015-02-12 11:14:38 -0800 | [diff] [blame] | 173 | #if USE_BOOTDEV_CMDLINE |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 174 | static const char *emmc_cmdline = " androidboot.bootdevice="; |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 175 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 176 | static const char *emmc_cmdline = " androidboot.emmc=true"; |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 177 | #endif |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 178 | static const char *dynamic_bootdev_cmdline = |
| 179 | " androidboot.boot_devices=soc/"; |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 180 | static const char *usb_sn_cmdline = " androidboot.serialno="; |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 181 | static const char *androidboot_mode = " androidboot.mode="; |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 182 | |
| 183 | static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target"; |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 184 | static const char *alarmboot_cmdline = " androidboot.alarmboot=true"; |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 185 | static const char *loglevel = " quiet"; |
Ajay Dudani | ca3a33c | 2011-11-18 08:31:40 -0800 | [diff] [blame] | 186 | static const char *battchg_pause = " androidboot.mode=charger"; |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 187 | /*[20200605][TracyChui] Implement get Serial Number start */ |
| 188 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 189 | static const char *cust_sn_cmdline = " androidboot.customer_serialno="; |
| 190 | static const char *factory_sn_cmdline = " androidboot.factory_serialno="; |
| 191 | static const char *UsbAdbEnable = " androidboot.adb_enable=1"; |
| 192 | #endif |
| 193 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 194 | static const char *auth_kernel = " androidboot.authorized_kernel=true"; |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 195 | static const char *secondary_gpt_enable = " gpt"; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 196 | #ifdef MDTP_SUPPORT |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 197 | static const char *mdtp_activated_flag = " mdtp"; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 198 | #endif |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 199 | static const char *baseband_apq = " androidboot.baseband=apq"; |
| 200 | static const char *baseband_msm = " androidboot.baseband=msm"; |
| 201 | static const char *baseband_csfb = " androidboot.baseband=csfb"; |
| 202 | static const char *baseband_svlte2a = " androidboot.baseband=svlte2a"; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 203 | static const char *baseband_mdm = " androidboot.baseband=mdm"; |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 204 | static const char *baseband_mdm2 = " androidboot.baseband=mdm2"; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 205 | static const char *baseband_sglte = " androidboot.baseband=sglte"; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 206 | static const char *baseband_dsda = " androidboot.baseband=dsda"; |
| 207 | static const char *baseband_dsda2 = " androidboot.baseband=dsda2"; |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 208 | static const char *baseband_sglte2 = " androidboot.baseband=sglte2"; |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 209 | static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1"; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 210 | static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr"; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 211 | static const char *androidboot_slot_suffix = " androidboot.slot_suffix="; |
| 212 | static const char *skip_ramfs = " skip_initramfs"; |
tracychui | 3b4abb7 | 2020-06-15 10:50:49 +0800 | [diff] [blame^] | 213 | /* [TracyChui]Add memory_config property 20200615 start */ |
| 214 | static const char *memory_config_3G = " androidboot.memory_config=32GB,3GB"; |
| 215 | static const char *memory_config_4G = " androidboot.memory_config=64GB,4GB"; |
| 216 | /* [TracyChui]Add memory_config property 20200615 end */ |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 217 | |
| 218 | #if HIBERNATION_SUPPORT |
| 219 | static const char *resume = " resume=/dev/mmcblk0p"; |
| 220 | #endif |
| 221 | |
Sourabh Banerjee | 6c7153c | 2018-03-20 01:21:57 +0530 | [diff] [blame] | 222 | #ifdef INIT_BIN_LE |
| 223 | static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE; |
| 224 | #else |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 225 | static const char *sys_path_cmdline = " rootwait ro init=/init"; |
Sourabh Banerjee | 6c7153c | 2018-03-20 01:21:57 +0530 | [diff] [blame] | 226 | #endif |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 227 | |
| 228 | #if VERITY_LE |
| 229 | static const char *verity_dev = " root=/dev/dm-0"; |
| 230 | static const char *verity_system_part = " dm=\"system"; |
| 231 | static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p"; |
| 232 | #else |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 233 | static const char *sys_path = " root=/dev/mmcblk0p"; |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 234 | |
| 235 | #define MAX_DTBO_IDX_STR 64 |
| 236 | static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx="; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 237 | |
| 238 | #define MAX_DTB_IDX_STR MAX_DTBO_IDX_STR |
| 239 | static const char *android_boot_dtb_idx = " androidboot.dtb_idx="; |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 240 | #endif |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 241 | |
michaellin | cb058f1 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 242 | //<20200424-michaellin, Add PCBA stage to system properties |
| 243 | #include <platform/gpio.h> |
| 244 | static const char *PCBA_STAGE_0 = " androidboot.pcbastage=EP0"; |
| 245 | static const char *PCBA_STAGE_1 = " androidboot.pcbastage=EP1"; |
| 246 | static const char *PCBA_STAGE_2 = " androidboot.pcbastage=EP2"; |
| 247 | static const char *PCBA_STAGE_3 = " androidboot.pcbastage=FP"; |
| 248 | static const char *PCBA_STAGE_4 = " androidboot.pcbastage=MP"; |
michaellin | 52b647a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 249 | static const char *PCBA_STAGE_5 = " androidboot.pcbastage=MP-8903MB_001"; |
michaellin | cb058f1 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 250 | static const char *PCBA_STAGE_F = " androidboot.pcbastage=Reserved"; |
| 251 | //>20200424-michaellin |
| 252 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 253 | #if VERIFIED_BOOT |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 254 | static const char *verity_mode = " androidboot.veritymode="; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 255 | static const char *verified_state= " androidboot.verifiedbootstate="; |
Parth Dixit | a5715a0 | 2015-10-29 12:25:10 +0530 | [diff] [blame] | 256 | static const char *keymaster_v1= " androidboot.keymaster=1"; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 257 | //indexed based on enum values, green is 0 by default |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 258 | |
| 259 | struct verified_boot_verity_mode vbvm[] = |
| 260 | { |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 261 | #if ENABLE_VB_ATTEST |
| 262 | {false, "eio"}, |
| 263 | #else |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 264 | {false, "logging"}, |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 265 | #endif |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 266 | {true, "enforcing"}, |
| 267 | }; |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 268 | struct verified_boot_state_name vbsn[] = |
| 269 | { |
| 270 | {GREEN, "green"}, |
| 271 | {ORANGE, "orange"}, |
| 272 | {YELLOW,"yellow"}, |
| 273 | {RED,"red" }, |
| 274 | }; |
| 275 | #endif |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 276 | /*As per spec delay wait time before shutdown in Red state*/ |
| 277 | #define DELAY_WAIT 30000 |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 278 | static unsigned page_size = 0; |
| 279 | static unsigned page_mask = 0; |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 280 | static unsigned mmc_blocksize = 0; |
| 281 | static unsigned mmc_blocksize_mask = 0; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 282 | static char ffbm_mode_string[FFBM_MODE_BUF_SIZE]; |
| 283 | static bool boot_into_ffbm; |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 284 | static char *target_boot_params = NULL; |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 285 | static bool boot_reason_alarm; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 286 | static bool devinfo_present = true; |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 287 | /*[20200605][TracyChui] Implement get Serial Number start */ |
| 288 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 289 | static bool prodinfo_present = true; |
| 290 | #endif |
| 291 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 292 | bool boot_into_fastboot = false; |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 293 | static uint32_t dt_size = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 294 | static char *vbcmdline; |
| 295 | static bootinfo info = {0}; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 296 | static void *recovery_dtbo_buf = NULL; |
| 297 | static uint32_t recovery_dtbo_size = 0; |
| 298 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 299 | /* Assuming unauthorized kernel image by default */ |
| 300 | static int auth_kernel_img = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 301 | 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] | 302 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 303 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 304 | static prod_info prod = {PRODINFO_MAGIC, {0}, {0}, 0}; |
| 305 | #endif |
| 306 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 307 | |
jhchen | 7a504d1 | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 308 | /*[Arima_8910][jhchen] add fuse check property 20181031 begin*/ |
| 309 | #ifdef ENABLE_FUSE_CHECK |
| 310 | static const char *fuse_blown = " androidboot.oem.color=red"; |
| 311 | static const char *fuse_not_blown = " androidboot.oem.color=brown"; |
| 312 | #endif |
| 313 | /*[Arima_8910][jhchen] 20181031 end*/ |
| 314 | |
| 315 | /*[Arima_8910][jhchen] add fuse check property 20181031 begin*/ |
| 316 | #ifdef ENABLE_FUSE_CHECK |
| 317 | int is_fused = 0; |
| 318 | #endif |
| 319 | /*[Arima_8910][jhchen] 20181031 end*/ |
| 320 | |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 321 | static bool is_allow_unlock = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 322 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 323 | static char frp_ptns[2][8] = {"config","frp"}; |
| 324 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 325 | static const char *critical_flash_allowed_ptn[] = { |
| 326 | "aboot", |
| 327 | "rpm", |
| 328 | "tz", |
| 329 | "sbl", |
| 330 | "sdi", |
| 331 | "sbl1", |
| 332 | "xbl", |
| 333 | "hyp", |
| 334 | "pmic", |
| 335 | "bootloader", |
| 336 | "devinfo", |
| 337 | "partition"}; |
| 338 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 339 | struct atag_ptbl_entry |
| 340 | { |
| 341 | char name[16]; |
| 342 | unsigned offset; |
| 343 | unsigned size; |
| 344 | unsigned flags; |
| 345 | }; |
| 346 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 347 | /* |
| 348 | * Partition info, required to be published |
| 349 | * for fastboot |
| 350 | */ |
| 351 | struct getvar_partition_info { |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 352 | char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */ |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 353 | char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */ |
| 354 | char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */ |
| 355 | char size_response[MAX_RSP_SIZE]; /* fastboot response for size */ |
| 356 | char type_response[MAX_RSP_SIZE]; /* fastboot response for type */ |
| 357 | }; |
| 358 | |
| 359 | /* |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 360 | * Update the part_type_known for known paritions types. |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 361 | */ |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 362 | #define RAW_STR "raw" |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 363 | #define EXT_STR "ext4" |
| 364 | #define F2FS_STR "f2fs" |
| 365 | |
| 366 | #define FS_SUPERBLOCK_OFFSET 0x400 |
| 367 | #define EXT_MAGIC 0xEF53 |
| 368 | #define EXT_MAGIC_OFFSET_SB 0x38 |
| 369 | #define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number |
| 370 | #define F2FS_MAGIC_OFFSET_SB 0x0 |
| 371 | |
| 372 | typedef enum fs_signature_type { |
| 373 | EXT_FS_SIGNATURE = 1, |
| 374 | EXT_F2FS_SIGNATURE = 2, |
| 375 | NO_FS = -1 |
| 376 | } fs_signature_type; |
| 377 | |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 378 | struct getvar_partition_info part_info[NUM_PARTITIONS]; |
| 379 | struct getvar_partition_info part_type_known[] = |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 380 | { |
Mayank Grover | 4992021 | 2018-02-09 18:15:55 +0530 | [diff] [blame] | 381 | { "system" , "partition-size:", "partition-type:", "", "ext4" }, |
| 382 | { "userdata" , "partition-size:", "partition-type:", "", "ext4" }, |
| 383 | { "cache" , "partition-size:", "partition-type:", "", "ext4" }, |
| 384 | { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" }, |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | char max_download_size[MAX_RSP_SIZE]; |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 388 | char charger_screen_enabled[MAX_RSP_SIZE]; |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 389 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 390 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 391 | char cust_sn_buf[PRODINFO_MAX_SSN_LEN + 1]; |
| 392 | char factory_sn_buf[PRODINFO_MAX_SSN_LEN + 1]; |
| 393 | char AdbEnable[MAX_RSP_SIZE]; |
| 394 | #endif |
| 395 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 396 | char sn_buf[PRODINFO_MAX_ISN_LEN + 1]; |
| 397 | #else |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 398 | char sn_buf[13]; |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 399 | #endif |
| 400 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 401 | char display_panel_buf[MAX_PANEL_BUF_SIZE]; |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 402 | char panel_display_mode[MAX_RSP_SIZE]; |
Mayank Grover | a64dfbe | 2018-05-17 14:06:34 +0530 | [diff] [blame] | 403 | char soc_version_str[MAX_RSP_SIZE]; |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 404 | char block_size_string[MAX_RSP_SIZE]; |
Mukesh Ojha | bb6e7f3 | 2018-03-02 15:04:06 +0530 | [diff] [blame] | 405 | #if PRODUCT_IOT |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 406 | |
| 407 | /* For IOT we are using custom version */ |
| 408 | #define PRODUCT_IOT_VERSION "IOT001" |
| 409 | char bootloader_version_string[MAX_RSP_SIZE]; |
| 410 | #endif |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 411 | |
| 412 | #if CHECK_BAT_VOLTAGE |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 413 | char battery_voltage[MAX_RSP_SIZE]; |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 414 | char battery_soc_ok [MAX_RSP_SIZE]; |
| 415 | #endif |
| 416 | |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 417 | char get_variant[MAX_RSP_SIZE]; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 418 | |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 419 | extern int emmc_recovery_init(void); |
| 420 | |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 421 | #if NO_KEYPAD_DRIVER |
| 422 | extern int fastboot_trigger(void); |
| 423 | #endif |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 424 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 425 | 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] | 426 | { |
| 427 | /* overwrite the destination of specified for the project */ |
Channagoud Kadabi | 7042fa3 | 2013-04-26 16:44:14 -0700 | [diff] [blame] | 428 | #ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 429 | if (is_arm64) |
| 430 | hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR; |
| 431 | else |
| 432 | hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR; |
Channagoud Kadabi | 7042fa3 | 2013-04-26 16:44:14 -0700 | [diff] [blame] | 433 | hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR; |
| 434 | hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR; |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 435 | #endif |
| 436 | } |
| 437 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 438 | static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn) |
| 439 | { |
| 440 | struct atag_ptbl_entry atag_ptn; |
| 441 | |
| 442 | memcpy(atag_ptn.name, ptn->name, 16); |
| 443 | atag_ptn.name[15] = '\0'; |
| 444 | atag_ptn.offset = ptn->start; |
| 445 | atag_ptn.size = ptn->length; |
| 446 | atag_ptn.flags = ptn->flags; |
| 447 | memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry)); |
| 448 | *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned); |
| 449 | } |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 450 | #ifdef VERIFIED_BOOT_2 |
| 451 | void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz) |
| 452 | { |
| 453 | int index = 0; |
| 454 | char *vbm_img_buf = NULL; |
| 455 | unsigned long long ptn = 0; |
| 456 | unsigned long long ptn_size = 0; |
| 457 | |
| 458 | /* Immediately return if dtbo is not supported */ |
| 459 | index = partition_get_index("vbmeta"); |
| 460 | ptn = partition_get_offset(index); |
| 461 | if(!ptn) |
| 462 | { |
| 463 | dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n"); |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | ptn_size = partition_get_size(index); |
| 468 | if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF) |
| 469 | { |
| 470 | dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n"); |
| 471 | return; |
| 472 | } |
| 473 | |
| 474 | vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE)); |
| 475 | if (!vbm_img_buf) |
| 476 | { |
| 477 | dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n"); |
| 478 | return; |
| 479 | } |
| 480 | |
| 481 | mmc_set_lun(partition_get_lun(index)); |
| 482 | if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size)) |
| 483 | { |
| 484 | dprintf(CRITICAL, "ERROR: vbmeta read failure\n"); |
| 485 | free(vbm_img_buf); |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | *vbmeta_image_buf = vbm_img_buf; |
| 490 | *vbmeta_image_sz = (uint32_t)ptn_size; |
| 491 | return; |
| 492 | } |
| 493 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 494 | |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 495 | #if CHECK_BAT_VOLTAGE |
| 496 | void update_battery_status(void) |
| 497 | { |
| 498 | snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage()); |
| 499 | snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no"); |
| 500 | } |
| 501 | #endif |
| 502 | |
michaellin | cb058f1 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 503 | //<20200424-michaellin, Add PCBA stage to system properties |
| 504 | uint32_t GetPcbaVariant(void) |
| 505 | { |
| 506 | uint8_t GPIO_97=10; |
| 507 | uint8_t GPIO_98=10; |
| 508 | uint8_t GPIO_99=10; |
| 509 | |
| 510 | uint8_t pcba_stage = 0; |
| 511 | |
| 512 | // Config GPIO |
| 513 | gpio_tlmm_config(97, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE); |
| 514 | gpio_tlmm_config(98, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE); |
| 515 | gpio_tlmm_config(99, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE); |
| 516 | |
| 517 | /* Wait for the gpio config to take effect - debounce time */ |
| 518 | thread_sleep(10); |
| 519 | |
| 520 | /* Get status of GPIO */ |
| 521 | GPIO_97 = gpio_status(97); |
| 522 | GPIO_98 = gpio_status(98); |
| 523 | GPIO_99 = gpio_status(99); |
| 524 | |
| 525 | pcba_stage = (GPIO_99 << 2) + (GPIO_98 << 1) + GPIO_97; |
| 526 | dprintf(ALWAYS, "pcba_stage status: %u\n", pcba_stage); |
michaellin | 52b647a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 527 | // GPIO_99 GPIO_98 GPIO_97 |
michaellin | cb058f1 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 528 | // 0 0 0 EP0 |
| 529 | // 0 0 1 EP1 |
| 530 | // 0 1 0 EP2 |
michaellin | 52b647a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 531 | // 0 1 1 FP(8901MB-007)/PP |
| 532 | // 1 0 0 MP(8901MB-008) |
| 533 | // 1 0 1 MP-8903MB_001 |
michaellin | cb058f1 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 534 | return pcba_stage; |
| 535 | } |
| 536 | //>20200424-michaellin |
| 537 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 538 | unsigned char *update_cmdline(const char * cmdline) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 539 | { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 540 | int cmdline_len = 0; |
| 541 | int have_cmdline = 0; |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 542 | unsigned char *cmdline_final = NULL; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 543 | int pause_at_bootup = 0; |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 544 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 545 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 546 | int AdbEnable = 0; |
| 547 | #endif |
| 548 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 549 | bool warm_boot = false; |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 550 | bool gpt_exists = partition_gpt_exists(); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 551 | int have_target_boot_params = 0; |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 552 | char *boot_dev_buf = NULL; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 553 | #ifdef MDTP_SUPPORT |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 554 | bool is_mdtp_activated = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 555 | #endif |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 556 | int current_active_slot = INVALID; |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 557 | int system_ptn_index = -1; |
| 558 | unsigned int lun = 0; |
| 559 | char lun_char_base = 'a'; |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 560 | #if VERITY_LE |
| 561 | int syspath_buflen = strlen(verity_dev) |
| 562 | + strlen(verity_system_part) + (sizeof(char) * 2) + 2 |
| 563 | + strlen(verity_params) + sizeof(int) + 2; |
| 564 | #else |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 565 | int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/ |
| 566 | char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0"; |
| 567 | int dtbo_idx = INVALID_PTN; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 568 | char dtb_idx_str[MAX_DTB_IDX_STR] = "\0"; |
| 569 | int dtb_idx = INVALID_PTN; |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 570 | #endif |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 571 | char syspath_buf[syspath_buflen]; |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 572 | #if HIBERNATION_SUPPORT |
| 573 | int resume_buflen = strlen(resume) + sizeof(int) + 2; |
| 574 | char resume_buf[resume_buflen]; |
| 575 | int swap_ptn_index = INVALID_PTN; |
| 576 | #endif |
| 577 | |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 578 | #if VERIFIED_BOOT |
| 579 | uint32_t boot_state = RED; |
| 580 | #endif |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 581 | |
| 582 | #if USE_LE_SYSTEMD |
| 583 | is_systemd_present=true; |
| 584 | #endif |
| 585 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 586 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 587 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 588 | { |
| 589 | boot_state = boot_verify_get_state(); |
| 590 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 591 | #endif |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 592 | |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 593 | #ifdef MDTP_SUPPORT |
| 594 | mdtp_activated(&is_mdtp_activated); |
| 595 | #endif /* MDTP_SUPPORT */ |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 596 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 597 | if (cmdline && cmdline[0]) { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 598 | cmdline_len = strlen(cmdline); |
| 599 | have_cmdline = 1; |
| 600 | } |
| 601 | if (target_is_emmc_boot()) { |
| 602 | cmdline_len += strlen(emmc_cmdline); |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 603 | boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN); |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 604 | if (!boot_dev_buf) { |
| 605 | dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n"); |
| 606 | } else { |
| 607 | platform_boot_dev_cmdline(boot_dev_buf); |
| 608 | #if USE_BOOTDEV_CMDLINE |
| 609 | cmdline_len += strlen(boot_dev_buf); |
Sundarajan Srinivasan | 3827a10 | 2013-09-10 13:57:40 -0700 | [diff] [blame] | 610 | #endif |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 611 | if (target_dynamic_partition_supported()) { |
| 612 | cmdline_len += strlen(dynamic_bootdev_cmdline); |
| 613 | cmdline_len += strlen(boot_dev_buf); |
| 614 | } |
| 615 | } |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 616 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 617 | |
| 618 | cmdline_len += strlen(usb_sn_cmdline); |
| 619 | cmdline_len += strlen(sn_buf); |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 620 | /*[20200605][TracyChui] Implement get Serial Number start */ |
| 621 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 622 | cmdline_len += strlen(cust_sn_cmdline); |
| 623 | cmdline_len += strlen(cust_sn_buf); |
| 624 | cmdline_len += strlen(factory_sn_cmdline); |
| 625 | cmdline_len += strlen(factory_sn_buf); |
| 626 | #endif |
| 627 | /*[20200605][TracyChui] Implement get Serial Number end */ |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 628 | |
tracychui | 3b4abb7 | 2020-06-15 10:50:49 +0800 | [diff] [blame^] | 629 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 630 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 631 | if (VB_M <= target_get_vb_version()) |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 632 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 633 | cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name); |
| 634 | if ((device.verity_mode != 0 ) && (device.verity_mode != 1)) |
| 635 | { |
| 636 | dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n"); |
| 637 | ASSERT(0); |
| 638 | } |
| 639 | cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name); |
| 640 | cmdline_len += strlen(keymaster_v1); |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 641 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 642 | #endif |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 643 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 644 | |
| 645 | if (vbcmdline != NULL) { |
| 646 | dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n", |
| 647 | strlen(vbcmdline)); |
| 648 | cmdline_len += strlen(vbcmdline); |
| 649 | } |
| 650 | |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 651 | if (boot_into_recovery && gpt_exists) |
| 652 | cmdline_len += strlen(secondary_gpt_enable); |
| 653 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 654 | #ifdef MDTP_SUPPORT |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 655 | if(is_mdtp_activated) |
| 656 | cmdline_len += strlen(mdtp_activated_flag); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 657 | #endif |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 658 | if (boot_into_ffbm) { |
| 659 | cmdline_len += strlen(androidboot_mode); |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 660 | |
| 661 | if(is_systemd_present) |
| 662 | cmdline_len += strlen(systemd_ffbm_mode); |
| 663 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 664 | cmdline_len += strlen(ffbm_mode_string); |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 665 | /* reduce kernel console messages to speed-up boot */ |
| 666 | cmdline_len += strlen(loglevel); |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 667 | } else if (boot_reason_alarm) { |
| 668 | cmdline_len += strlen(alarmboot_cmdline); |
Zhenhua Huang | 431dafa | 2015-06-30 16:13:37 +0800 | [diff] [blame] | 669 | } else if ((target_build_variant_user() || device.charger_screen_enabled) |
lijuang | 266b17d | 2018-08-17 18:53:42 +0800 | [diff] [blame] | 670 | && target_pause_for_battery_charge() && !boot_into_recovery) { |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 671 | pause_at_bootup = 1; |
| 672 | cmdline_len += strlen(battchg_pause); |
| 673 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 674 | |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 675 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 676 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 677 | if (prod.is_adb_enabled) { |
| 678 | dprintf(CRITICAL, "Device will enable adb\n"); |
| 679 | |
| 680 | prod.is_adb_enabled = 0; |
| 681 | write_prod_info(&prod); |
| 682 | |
| 683 | AdbEnable = 1; |
| 684 | cmdline_len += strlen(UsbAdbEnable); |
| 685 | } |
| 686 | #endif |
| 687 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
| 688 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 689 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 690 | cmdline_len += strlen(auth_kernel); |
| 691 | } |
| 692 | |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 693 | if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" : |
| 694 | "system", |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 695 | &target_boot_params) == 0) { |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 696 | have_target_boot_params = 1; |
| 697 | cmdline_len += strlen(target_boot_params); |
| 698 | } |
| 699 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 700 | /* Determine correct androidboot.baseband to use */ |
| 701 | switch(target_baseband()) |
| 702 | { |
| 703 | case BASEBAND_APQ: |
| 704 | cmdline_len += strlen(baseband_apq); |
| 705 | break; |
| 706 | |
| 707 | case BASEBAND_MSM: |
| 708 | cmdline_len += strlen(baseband_msm); |
| 709 | break; |
| 710 | |
| 711 | case BASEBAND_CSFB: |
| 712 | cmdline_len += strlen(baseband_csfb); |
| 713 | break; |
| 714 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 715 | case BASEBAND_SVLTE2A: |
| 716 | cmdline_len += strlen(baseband_svlte2a); |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 717 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 718 | |
| 719 | case BASEBAND_MDM: |
| 720 | cmdline_len += strlen(baseband_mdm); |
| 721 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 722 | |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 723 | case BASEBAND_MDM2: |
| 724 | cmdline_len += strlen(baseband_mdm2); |
| 725 | break; |
| 726 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 727 | case BASEBAND_SGLTE: |
| 728 | cmdline_len += strlen(baseband_sglte); |
| 729 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 730 | |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 731 | case BASEBAND_SGLTE2: |
| 732 | cmdline_len += strlen(baseband_sglte2); |
| 733 | break; |
| 734 | |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 735 | case BASEBAND_DSDA: |
| 736 | cmdline_len += strlen(baseband_dsda); |
| 737 | break; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 738 | |
| 739 | case BASEBAND_DSDA2: |
| 740 | cmdline_len += strlen(baseband_dsda2); |
| 741 | break; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 742 | case BASEBAND_APQ_NOWGR: |
| 743 | cmdline_len += strlen(baseband_apq_nowgr); |
| 744 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 745 | } |
| 746 | |
michaellin | cb058f1 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 747 | //<20200424-michaellin, Add PCBA stage to system properties |
| 748 | switch(GetPcbaVariant()) |
| 749 | { |
| 750 | case 0: // EP0 |
| 751 | cmdline_len += strlen(PCBA_STAGE_0); |
| 752 | break; |
| 753 | |
| 754 | case 1: // EP1 |
| 755 | cmdline_len += strlen(PCBA_STAGE_1); |
| 756 | break; |
| 757 | |
| 758 | case 2: // EP2 |
| 759 | cmdline_len += strlen(PCBA_STAGE_2); |
| 760 | break; |
| 761 | |
michaellin | 52b647a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 762 | case 3: // FP(8901MB-007)/PP |
michaellin | cb058f1 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 763 | cmdline_len += strlen(PCBA_STAGE_3); |
| 764 | break; |
| 765 | |
michaellin | 52b647a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 766 | case 4: // MP(8901MB-008) |
michaellin | cb058f1 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 767 | cmdline_len += strlen(PCBA_STAGE_4); |
tracychui | 3b4abb7 | 2020-06-15 10:50:49 +0800 | [diff] [blame^] | 768 | break; |
michaellin | cb058f1 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 769 | |
michaellin | 52b647a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 770 | case 5: // MP-8903MB_001 |
| 771 | cmdline_len += strlen(PCBA_STAGE_5); |
tracychui | 3b4abb7 | 2020-06-15 10:50:49 +0800 | [diff] [blame^] | 772 | break; |
michaellin | 52b647a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 773 | |
michaellin | cb058f1 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 774 | default:// Reserved |
| 775 | cmdline_len += strlen(PCBA_STAGE_F); |
| 776 | break; |
| 777 | } |
| 778 | //>20200424-michaellin |
| 779 | |
tracychui | 3b4abb7 | 2020-06-15 10:50:49 +0800 | [diff] [blame^] | 780 | /* [TracyChui]Add memory_config property 20200615 start */ |
| 781 | if(smem_get_ddr_size() == MEM_3GB ) |
| 782 | { |
| 783 | cmdline_len += strlen(memory_config_3G); |
| 784 | } |
| 785 | else |
| 786 | { |
| 787 | cmdline_len += strlen(memory_config_4G); |
| 788 | } |
| 789 | /* [TracyChui]Add memory_config property 20200615 end */ |
| 790 | |
Vladislav Levenetz | 3cd4524 | 2017-04-05 08:58:07 +0300 | [diff] [blame] | 791 | #if ENABLE_DISPLAY |
Lijuan Gao | 4a5b832 | 2014-07-24 10:38:42 +0800 | [diff] [blame] | 792 | if (cmdline) { |
| 793 | if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) && |
Padmanabhan Komanduru | bccbcdc | 2015-06-30 16:19:24 +0530 | [diff] [blame] | 794 | target_display_panel_node(display_panel_buf, |
| 795 | MAX_PANEL_BUF_SIZE) && |
Lijuan Gao | 4a5b832 | 2014-07-24 10:38:42 +0800 | [diff] [blame] | 796 | strlen(display_panel_buf)) { |
| 797 | cmdline_len += strlen(display_panel_buf); |
| 798 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 799 | } |
Vladislav Levenetz | 3cd4524 | 2017-04-05 08:58:07 +0300 | [diff] [blame] | 800 | #endif |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 801 | |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 802 | if (target_warm_boot()) { |
| 803 | warm_boot = true; |
| 804 | cmdline_len += strlen(warmboot_cmdline); |
| 805 | } |
| 806 | |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 807 | if (target_uses_system_as_root() || |
| 808 | partition_multislot_is_supported()) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 809 | { |
| 810 | current_active_slot = partition_find_active_slot(); |
| 811 | cmdline_len += (strlen(androidboot_slot_suffix)+ |
| 812 | strlen(SUFFIX_SLOT(current_active_slot))); |
| 813 | |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 814 | system_ptn_index = partition_get_index("system"); |
| 815 | if (platform_boot_dev_isemmc()) |
| 816 | { |
Sourabh Banerjee | 386b132 | 2018-02-27 09:37:28 +0530 | [diff] [blame] | 817 | #if VERITY_LE |
| 818 | /* |
| 819 | Condition 4: Verity and A/B both enabled |
| 820 | Eventual command line looks like: |
| 821 | ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ... |
| 822 | ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>" |
| 823 | */ |
| 824 | snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"", |
| 825 | verity_dev, |
| 826 | verity_system_part, suffix_slot[current_active_slot], |
| 827 | verity_params, system_ptn_index + 1); |
| 828 | #else |
| 829 | /* |
| 830 | Condition 5: A/B enabled, but verity disabled |
| 831 | Eventual command line looks like: |
| 832 | ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ... |
| 833 | ... root=/dev/mmcblk0p<NN> ... |
| 834 | */ |
| 835 | snprintf(syspath_buf, syspath_buflen, " %s%d", |
| 836 | sys_path, system_ptn_index + 1); |
| 837 | #endif |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 838 | } |
| 839 | else |
| 840 | { |
| 841 | lun = partition_get_lun(system_ptn_index); |
| 842 | snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d", |
| 843 | lun_char_base + lun, |
| 844 | partition_get_index_in_lun("system", lun)); |
| 845 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 846 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 847 | #ifndef VERIFIED_BOOT_2 |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 848 | cmdline_len += strlen(syspath_buf); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 849 | #endif |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | if (target_uses_system_as_root() || |
| 853 | partition_multislot_is_supported()) |
| 854 | { |
| 855 | cmdline_len += strlen(sys_path_cmdline); |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 856 | |
| 857 | /* For dynamic partition, support skip skip_initramfs */ |
| 858 | if (!target_dynamic_partition_supported() && |
| 859 | !boot_into_recovery) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 860 | cmdline_len += strlen(skip_ramfs); |
| 861 | } |
| 862 | |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 863 | #if HIBERNATION_SUPPORT |
| 864 | if (platform_boot_dev_isemmc()) |
| 865 | { |
| 866 | swap_ptn_index = partition_get_index("swap"); |
| 867 | if (swap_ptn_index != INVALID_PTN) |
| 868 | { |
| 869 | snprintf(resume_buf, resume_buflen, |
| 870 | " %s%d", resume, |
| 871 | (swap_ptn_index + 1)); |
| 872 | cmdline_len += strlen(resume_buf); |
| 873 | } |
| 874 | else |
| 875 | { |
| 876 | dprintf(INFO, "WARNING: swap partition not found\n"); |
| 877 | } |
| 878 | } |
| 879 | #endif |
| 880 | |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 881 | #if TARGET_CMDLINE_SUPPORT |
| 882 | char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF); |
| 883 | int target_cmd_line_len; |
| 884 | ASSERT(target_cmdline_buf); |
| 885 | target_cmd_line_len = target_update_cmdline(target_cmdline_buf); |
| 886 | cmdline_len += target_cmd_line_len; |
| 887 | #endif |
| 888 | |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 889 | #if !VERITY_LE |
| 890 | dtbo_idx = get_dtbo_idx (); |
| 891 | if (dtbo_idx != INVALID_PTN) { |
| 892 | snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d", |
| 893 | android_boot_dtbo_idx, dtbo_idx); |
| 894 | cmdline_len += strlen (dtbo_idx_str); |
| 895 | } |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 896 | |
| 897 | dtb_idx = get_dtb_idx (); |
| 898 | if (dtb_idx != INVALID_PTN) { |
| 899 | snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d", |
| 900 | android_boot_dtb_idx, dtb_idx); |
| 901 | cmdline_len += strlen (dtb_idx_str); |
| 902 | } |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 903 | #endif |
| 904 | |
jhchen | 7a504d1 | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 905 | /*[Arima_8910][jhchen] add fuse check property 20181031 begin*/ |
| 906 | #ifdef ENABLE_FUSE_CHECK |
| 907 | { |
| 908 | int hw_key_status = readl(0xA01D0); |
| 909 | is_fused = (hw_key_status & 0x2) >> 1; |
| 910 | dprintf(CRITICAL, "hw_key_status = 0x%x, is_fused=%d\n", hw_key_status, is_fused); |
| 911 | if (is_fused) { |
| 912 | cmdline_len += strlen(fuse_blown); |
| 913 | } else { |
| 914 | cmdline_len += strlen(fuse_not_blown); |
| 915 | } |
| 916 | } |
| 917 | #endif |
| 918 | /*[Arima_8910][jhchen] 20181031 end*/ |
| 919 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 920 | if (cmdline_len > 0) { |
| 921 | const char *src; |
Maria Yu | 52254c0 | 2014-07-04 16:14:54 +0800 | [diff] [blame] | 922 | unsigned char *dst; |
| 923 | |
| 924 | cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3)); |
| 925 | ASSERT(cmdline_final != NULL); |
vijay kumar | 287bb54 | 2015-09-29 13:01:52 +0530 | [diff] [blame] | 926 | memset((void *)cmdline_final, 0, sizeof(*cmdline_final)); |
Maria Yu | 52254c0 | 2014-07-04 16:14:54 +0800 | [diff] [blame] | 927 | dst = cmdline_final; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 928 | |
Amol Jadi | 168b771 | 2012-03-06 16:15:00 -0800 | [diff] [blame] | 929 | /* Save start ptr for debug print */ |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 930 | if (have_cmdline) { |
| 931 | src = cmdline; |
| 932 | while ((*dst++ = *src++)); |
| 933 | } |
| 934 | if (target_is_emmc_boot()) { |
| 935 | src = emmc_cmdline; |
| 936 | if (have_cmdline) --dst; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 937 | have_cmdline = 1; |
| 938 | while ((*dst++ = *src++)); |
Sridhar Parasuram | 7bd4aaf | 2015-02-12 11:14:38 -0800 | [diff] [blame] | 939 | #if USE_BOOTDEV_CMDLINE |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 940 | src = boot_dev_buf; |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 941 | if (have_cmdline && |
| 942 | boot_dev_buf) { |
| 943 | --dst; |
| 944 | while ((*dst++ = *src++)); |
| 945 | } |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 946 | #endif |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 947 | /* Dynamic partition append boot_devices */ |
| 948 | if (target_dynamic_partition_supported() && |
| 949 | boot_dev_buf) { |
| 950 | src = dynamic_bootdev_cmdline; |
| 951 | if (have_cmdline) --dst; |
| 952 | while ((*dst++ = *src++)); |
| 953 | src = boot_dev_buf; |
| 954 | if (have_cmdline) --dst; |
| 955 | while ((*dst++ = *src++)); |
| 956 | } |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 957 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 958 | |
michaellin | cb058f1 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 959 | //<20200424-michaellin, Add PCBA stage to system properties |
| 960 | switch(GetPcbaVariant()) |
| 961 | { |
| 962 | case 0: |
| 963 | src = PCBA_STAGE_0; |
| 964 | if (have_cmdline) --dst; |
| 965 | while ((*dst++ = *src++)); |
| 966 | break; |
| 967 | case 1: |
| 968 | src = PCBA_STAGE_1; |
| 969 | if (have_cmdline) --dst; |
| 970 | while ((*dst++ = *src++)); |
| 971 | break; |
| 972 | case 2: |
| 973 | src = PCBA_STAGE_2; |
| 974 | if (have_cmdline) --dst; |
| 975 | while ((*dst++ = *src++)); |
| 976 | break; |
| 977 | case 3: |
| 978 | src = PCBA_STAGE_3; |
| 979 | if (have_cmdline) --dst; |
| 980 | while ((*dst++ = *src++)); |
| 981 | break; |
| 982 | case 4: |
| 983 | src = PCBA_STAGE_4; |
| 984 | if (have_cmdline) --dst; |
| 985 | while ((*dst++ = *src++)); |
| 986 | break; |
michaellin | 52b647a | 2020-05-05 14:58:47 +0800 | [diff] [blame] | 987 | case 5: |
| 988 | src = PCBA_STAGE_5; |
| 989 | if (have_cmdline) --dst; |
| 990 | while ((*dst++ = *src++)); |
| 991 | break; |
michaellin | cb058f1 | 2020-04-24 15:05:10 +0800 | [diff] [blame] | 992 | default: |
| 993 | src = PCBA_STAGE_F; |
| 994 | if (have_cmdline) --dst; |
| 995 | while ((*dst++ = *src++)); |
| 996 | break; |
| 997 | } |
| 998 | //>20200424-michaellin |
| 999 | |
tracychui | 3b4abb7 | 2020-06-15 10:50:49 +0800 | [diff] [blame^] | 1000 | /* [TracyChui]Add memory_config property 20200615 start */ |
| 1001 | if(smem_get_ddr_size() == MEM_3GB ) |
| 1002 | { |
| 1003 | src = memory_config_3G; |
| 1004 | if (have_cmdline) --dst; |
| 1005 | while ((*dst++ = *src++)); |
| 1006 | } |
| 1007 | else |
| 1008 | { |
| 1009 | src = memory_config_4G; |
| 1010 | if (have_cmdline) --dst; |
| 1011 | while ((*dst++ = *src++)); |
| 1012 | } |
| 1013 | /* [TracyChui]Add memory_config property 20200615 end */ |
| 1014 | |
Sridhar Parasuram | 4311b8e | 2015-05-28 17:01:59 -0700 | [diff] [blame] | 1015 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 1016 | if (VB_M <= target_get_vb_version()) |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 1017 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1018 | src = verified_state; |
| 1019 | if(have_cmdline) --dst; |
| 1020 | have_cmdline = 1; |
| 1021 | while ((*dst++ = *src++)); |
| 1022 | src = vbsn[boot_state].name; |
| 1023 | if(have_cmdline) --dst; |
| 1024 | while ((*dst++ = *src++)); |
| 1025 | |
| 1026 | if ((device.verity_mode != 0 ) && (device.verity_mode != 1)) |
| 1027 | { |
| 1028 | dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n"); |
| 1029 | ASSERT(0); |
| 1030 | } |
| 1031 | src = verity_mode; |
| 1032 | if(have_cmdline) --dst; |
| 1033 | while ((*dst++ = *src++)); |
| 1034 | src = vbvm[device.verity_mode].name; |
| 1035 | if(have_cmdline) -- dst; |
| 1036 | while ((*dst++ = *src++)); |
| 1037 | src = keymaster_v1; |
| 1038 | if(have_cmdline) --dst; |
| 1039 | while ((*dst++ = *src++)); |
Sridhar Parasuram | 7d8bb9a | 2015-09-23 18:21:19 -0700 | [diff] [blame] | 1040 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 1041 | #endif |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1042 | |
| 1043 | if (vbcmdline != NULL) { |
| 1044 | src = vbcmdline; |
| 1045 | if (have_cmdline) --dst; |
| 1046 | while ((*dst++ = *src++)); |
| 1047 | } |
| 1048 | |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 1049 | src = usb_sn_cmdline; |
| 1050 | if (have_cmdline) --dst; |
| 1051 | have_cmdline = 1; |
| 1052 | while ((*dst++ = *src++)); |
| 1053 | src = sn_buf; |
| 1054 | if (have_cmdline) --dst; |
| 1055 | have_cmdline = 1; |
| 1056 | while ((*dst++ = *src++)); |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 1057 | |
| 1058 | /* [20200605][TracyChui] Implement get Serial Number */ |
| 1059 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 1060 | src = cust_sn_cmdline; |
| 1061 | if (have_cmdline) --dst; |
| 1062 | have_cmdline = 1; |
| 1063 | while ((*dst++ = *src++)); |
| 1064 | src = cust_sn_buf; |
| 1065 | if (have_cmdline) --dst; |
| 1066 | have_cmdline = 1; |
| 1067 | while ((*dst++ = *src++)); |
| 1068 | src = factory_sn_cmdline; |
| 1069 | if (have_cmdline) --dst; |
| 1070 | have_cmdline = 1; |
| 1071 | while ((*dst++ = *src++)); |
| 1072 | src = factory_sn_buf; |
| 1073 | if (have_cmdline) --dst; |
| 1074 | have_cmdline = 1; |
| 1075 | while ((*dst++ = *src++)); |
| 1076 | #endif |
| 1077 | /*[20200605][TracyChui] Implement get Serial Number end */ |
Hanumant Singh | 8e1ac23 | 2014-01-29 13:41:51 -0800 | [diff] [blame] | 1078 | if (warm_boot) { |
| 1079 | if (have_cmdline) --dst; |
| 1080 | src = warmboot_cmdline; |
| 1081 | while ((*dst++ = *src++)); |
| 1082 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 1083 | |
Pavel Nedev | 5614d22 | 2013-06-17 18:01:02 +0300 | [diff] [blame] | 1084 | if (boot_into_recovery && gpt_exists) { |
| 1085 | src = secondary_gpt_enable; |
| 1086 | if (have_cmdline) --dst; |
| 1087 | while ((*dst++ = *src++)); |
| 1088 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1089 | #ifdef MDTP_SUPPORT |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 1090 | if (is_mdtp_activated) { |
| 1091 | src = mdtp_activated_flag; |
| 1092 | if (have_cmdline) --dst; |
| 1093 | while ((*dst++ = *src++)); |
| 1094 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1095 | #endif |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 1096 | if (boot_into_ffbm) { |
| 1097 | src = androidboot_mode; |
| 1098 | if (have_cmdline) --dst; |
| 1099 | while ((*dst++ = *src++)); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 1100 | src = ffbm_mode_string; |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 1101 | if (have_cmdline) --dst; |
| 1102 | while ((*dst++ = *src++)); |
Hareesh Gautham | abf1e1e | 2017-04-12 18:09:24 +0530 | [diff] [blame] | 1103 | |
| 1104 | if(is_systemd_present) { |
| 1105 | src = systemd_ffbm_mode; |
| 1106 | if (have_cmdline) --dst; |
| 1107 | while ((*dst++ = *src++)); |
| 1108 | } |
| 1109 | |
Pavel Nedev | 898298c | 2013-02-27 12:36:09 -0800 | [diff] [blame] | 1110 | src = loglevel; |
| 1111 | if (have_cmdline) --dst; |
| 1112 | while ((*dst++ = *src++)); |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 1113 | } else if (boot_reason_alarm) { |
| 1114 | src = alarmboot_cmdline; |
| 1115 | if (have_cmdline) --dst; |
| 1116 | while ((*dst++ = *src++)); |
Pavel Nedev | 328ac82 | 2013-04-05 15:25:11 +0300 | [diff] [blame] | 1117 | } else if (pause_at_bootup) { |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 1118 | src = battchg_pause; |
| 1119 | if (have_cmdline) --dst; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 1120 | while ((*dst++ = *src++)); |
| 1121 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 1122 | |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 1123 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 1124 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 1125 | if (AdbEnable) { |
| 1126 | src = UsbAdbEnable; |
| 1127 | if (have_cmdline) --dst; |
| 1128 | while ((*dst++ = *src++)); |
| 1129 | } |
| 1130 | #endif |
| 1131 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
| 1132 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1133 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 1134 | src = auth_kernel; |
| 1135 | if (have_cmdline) --dst; |
| 1136 | while ((*dst++ = *src++)); |
| 1137 | } |
| 1138 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 1139 | switch(target_baseband()) |
| 1140 | { |
| 1141 | case BASEBAND_APQ: |
| 1142 | src = baseband_apq; |
| 1143 | if (have_cmdline) --dst; |
| 1144 | while ((*dst++ = *src++)); |
| 1145 | break; |
| 1146 | |
| 1147 | case BASEBAND_MSM: |
| 1148 | src = baseband_msm; |
| 1149 | if (have_cmdline) --dst; |
| 1150 | while ((*dst++ = *src++)); |
| 1151 | break; |
| 1152 | |
| 1153 | case BASEBAND_CSFB: |
| 1154 | src = baseband_csfb; |
| 1155 | if (have_cmdline) --dst; |
| 1156 | while ((*dst++ = *src++)); |
| 1157 | break; |
| 1158 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 1159 | case BASEBAND_SVLTE2A: |
| 1160 | src = baseband_svlte2a; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 1161 | if (have_cmdline) --dst; |
| 1162 | while ((*dst++ = *src++)); |
| 1163 | break; |
Ajay Dudani | 403bc49 | 2011-09-30 16:17:21 -0700 | [diff] [blame] | 1164 | |
| 1165 | case BASEBAND_MDM: |
| 1166 | src = baseband_mdm; |
| 1167 | if (have_cmdline) --dst; |
| 1168 | while ((*dst++ = *src++)); |
| 1169 | break; |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 1170 | |
Sundarajan Srinivasan | aaa8aff | 2013-11-12 17:19:14 -0800 | [diff] [blame] | 1171 | case BASEBAND_MDM2: |
| 1172 | src = baseband_mdm2; |
| 1173 | if (have_cmdline) --dst; |
| 1174 | while ((*dst++ = *src++)); |
| 1175 | break; |
| 1176 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 1177 | case BASEBAND_SGLTE: |
| 1178 | src = baseband_sglte; |
| 1179 | if (have_cmdline) --dst; |
| 1180 | while ((*dst++ = *src++)); |
| 1181 | break; |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 1182 | |
sundarajan srinivasan | f8e9f3f | 2013-03-04 15:56:58 -0800 | [diff] [blame] | 1183 | case BASEBAND_SGLTE2: |
| 1184 | src = baseband_sglte2; |
| 1185 | if (have_cmdline) --dst; |
| 1186 | while ((*dst++ = *src++)); |
| 1187 | break; |
| 1188 | |
Channagoud Kadabi | 141f298 | 2012-10-31 11:23:02 +0530 | [diff] [blame] | 1189 | case BASEBAND_DSDA: |
| 1190 | src = baseband_dsda; |
| 1191 | if (have_cmdline) --dst; |
| 1192 | while ((*dst++ = *src++)); |
| 1193 | break; |
Amol Jadi | 2a15a27 | 2013-01-22 12:03:36 -0800 | [diff] [blame] | 1194 | |
| 1195 | case BASEBAND_DSDA2: |
| 1196 | src = baseband_dsda2; |
| 1197 | if (have_cmdline) --dst; |
| 1198 | while ((*dst++ = *src++)); |
| 1199 | break; |
Vijay Kumar Pendoti | b228cfc | 2016-06-13 20:15:23 +0530 | [diff] [blame] | 1200 | case BASEBAND_APQ_NOWGR: |
| 1201 | src = baseband_apq_nowgr; |
| 1202 | if (have_cmdline) --dst; |
| 1203 | while ((*dst++ = *src++)); |
| 1204 | break; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 1205 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 1206 | |
| 1207 | if (strlen(display_panel_buf)) { |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 1208 | src = display_panel_buf; |
| 1209 | if (have_cmdline) --dst; |
| 1210 | while ((*dst++ = *src++)); |
| 1211 | } |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 1212 | |
| 1213 | if (have_target_boot_params) { |
| 1214 | if (have_cmdline) --dst; |
| 1215 | src = target_boot_params; |
| 1216 | while ((*dst++ = *src++)); |
vijay kumar | 870515d | 2015-08-31 16:37:24 +0530 | [diff] [blame] | 1217 | free(target_boot_params); |
Joonwoo Park | 6111278 | 2013-10-02 19:50:39 -0700 | [diff] [blame] | 1218 | } |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 1219 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1220 | if (partition_multislot_is_supported() && have_cmdline) |
| 1221 | { |
| 1222 | src = androidboot_slot_suffix; |
| 1223 | --dst; |
| 1224 | while ((*dst++ = *src++)); |
| 1225 | --dst; |
| 1226 | src = SUFFIX_SLOT(current_active_slot); |
| 1227 | while ((*dst++ = *src++)); |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1228 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1229 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1230 | |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1231 | /* |
| 1232 | * System-As-Root behaviour, system.img should contain both |
| 1233 | * system content and ramdisk content, and should be mounted at |
| 1234 | * root(a/b). |
| 1235 | * Apending skip_ramfs for non a/b builds which use, system as root. |
| 1236 | */ |
| 1237 | if ((target_uses_system_as_root() || |
| 1238 | partition_multislot_is_supported()) && |
| 1239 | have_cmdline) |
| 1240 | { |
Mayank Grover | b716edf | 2019-05-08 16:06:55 +0530 | [diff] [blame] | 1241 | if (!target_dynamic_partition_supported() && |
| 1242 | !boot_into_recovery) |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1243 | { |
| 1244 | src = skip_ramfs; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1245 | --dst; |
| 1246 | while ((*dst++ = *src++)); |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | src = sys_path_cmdline; |
| 1250 | --dst; |
| 1251 | while ((*dst++ = *src++)); |
Mayank Grover | 3804be7 | 2017-06-22 11:59:23 +0530 | [diff] [blame] | 1252 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1253 | #ifndef VERIFIED_BOOT_2 |
Mayank Grover | 5384ed8 | 2018-05-09 12:09:24 +0530 | [diff] [blame] | 1254 | src = syspath_buf; |
| 1255 | --dst; |
| 1256 | while ((*dst++ = *src++)); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1257 | #endif |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1258 | } |
| 1259 | |
Mayank Grover | 466d656 | 2018-05-10 14:52:20 +0530 | [diff] [blame] | 1260 | #if HIBERNATION_SUPPORT |
| 1261 | if (swap_ptn_index != INVALID_PTN) |
| 1262 | { |
| 1263 | src = resume_buf; |
| 1264 | --dst; |
| 1265 | while ((*dst++ = *src++)); |
| 1266 | } |
| 1267 | #endif |
| 1268 | |
Channagoud Kadabi | 85c7ec3 | 2016-01-28 23:09:21 -0800 | [diff] [blame] | 1269 | #if TARGET_CMDLINE_SUPPORT |
| 1270 | if (target_cmdline_buf && target_cmd_line_len) |
| 1271 | { |
| 1272 | if (have_cmdline) --dst; |
| 1273 | src = target_cmdline_buf; |
| 1274 | while((*dst++ = *src++)); |
| 1275 | free(target_cmdline_buf); |
| 1276 | } |
| 1277 | #endif |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 1278 | |
| 1279 | #if !VERITY_LE |
| 1280 | if (dtbo_idx != INVALID_PTN) { |
| 1281 | src = dtbo_idx_str; |
| 1282 | --dst; |
| 1283 | while ((*dst++ = *src++)); |
| 1284 | } |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1285 | |
| 1286 | if (dtb_idx != INVALID_PTN) { |
| 1287 | src = dtb_idx_str; |
| 1288 | --dst; |
| 1289 | while ((*dst++ = *src++)); |
| 1290 | } |
lijuang | 83ef4b2 | 2018-08-23 11:01:55 +0800 | [diff] [blame] | 1291 | #endif |
jhchen | 7a504d1 | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 1292 | |
| 1293 | /*[Arima_8910][jhchen] add fuse check property 20181031 begin*/ |
| 1294 | #ifdef ENABLE_FUSE_CHECK |
| 1295 | if (is_fused) { |
| 1296 | src = fuse_blown; |
| 1297 | if (have_cmdline) --dst; |
| 1298 | while ((*dst++ = *src++)); |
| 1299 | } else { |
| 1300 | src = fuse_not_blown; |
| 1301 | if (have_cmdline) --dst; |
| 1302 | while ((*dst++ = *src++)); |
| 1303 | } |
| 1304 | #endif |
| 1305 | /*[Arima_8910][jhchen] 20181031 end*/ |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1306 | } |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 1307 | |
| 1308 | |
Sundarajan Srinivasan | e52065a | 2014-03-20 16:25:59 -0700 | [diff] [blame] | 1309 | if (boot_dev_buf) |
| 1310 | free(boot_dev_buf); |
| 1311 | |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 1312 | if (cmdline_final) |
| 1313 | dprintf(INFO, "cmdline: %s\n", cmdline_final); |
| 1314 | else |
| 1315 | dprintf(INFO, "cmdline is NULL\n"); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1316 | return cmdline_final; |
| 1317 | } |
| 1318 | |
| 1319 | unsigned *atag_core(unsigned *ptr) |
| 1320 | { |
| 1321 | /* CORE */ |
| 1322 | *ptr++ = 2; |
| 1323 | *ptr++ = 0x54410001; |
| 1324 | |
| 1325 | return ptr; |
| 1326 | |
| 1327 | } |
| 1328 | |
| 1329 | unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk, |
| 1330 | unsigned ramdisk_size) |
| 1331 | { |
| 1332 | if (ramdisk_size) { |
| 1333 | *ptr++ = 4; |
| 1334 | *ptr++ = 0x54420005; |
| 1335 | *ptr++ = (unsigned)ramdisk; |
| 1336 | *ptr++ = ramdisk_size; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1337 | } |
| 1338 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1339 | return ptr; |
| 1340 | } |
| 1341 | |
| 1342 | unsigned *atag_ptable(unsigned **ptr_addr) |
| 1343 | { |
| 1344 | int i; |
| 1345 | struct ptable *ptable; |
| 1346 | |
| 1347 | if ((ptable = flash_get_ptable()) && (ptable->count != 0)) { |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1348 | *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) / |
| 1349 | sizeof(unsigned))); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1350 | *(*ptr_addr)++ = 0x4d534d70; |
| 1351 | for (i = 0; i < ptable->count; ++i) |
| 1352 | ptentry_to_tag(ptr_addr, ptable_get(ptable, i)); |
| 1353 | } |
| 1354 | |
| 1355 | return (*ptr_addr); |
| 1356 | } |
| 1357 | |
| 1358 | unsigned *atag_cmdline(unsigned *ptr, const char *cmdline) |
| 1359 | { |
| 1360 | int cmdline_length = 0; |
| 1361 | int n; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1362 | char *dest; |
| 1363 | |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1364 | cmdline_length = strlen((const char*)cmdline); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1365 | n = (cmdline_length + 4) & (~3); |
| 1366 | |
| 1367 | *ptr++ = (n / 4) + 2; |
| 1368 | *ptr++ = 0x54410009; |
| 1369 | dest = (char *) ptr; |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1370 | while ((*dest++ = *cmdline++)); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1371 | ptr += (n / 4); |
| 1372 | |
| 1373 | return ptr; |
| 1374 | } |
| 1375 | |
| 1376 | unsigned *atag_end(unsigned *ptr) |
| 1377 | { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1378 | /* END */ |
| 1379 | *ptr++ = 0; |
| 1380 | *ptr++ = 0; |
| 1381 | |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1382 | return ptr; |
| 1383 | } |
| 1384 | |
| 1385 | void generate_atags(unsigned *ptr, const char *cmdline, |
| 1386 | void *ramdisk, unsigned ramdisk_size) |
| 1387 | { |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 1388 | unsigned *orig_ptr = ptr; |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1389 | ptr = atag_core(ptr); |
| 1390 | ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size); |
| 1391 | ptr = target_atag_mem(ptr); |
| 1392 | |
| 1393 | /* Skip NAND partition ATAGS for eMMC boot */ |
| 1394 | if (!target_is_emmc_boot()){ |
| 1395 | ptr = atag_ptable(&ptr); |
| 1396 | } |
| 1397 | |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 1398 | /* |
| 1399 | * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned |
| 1400 | * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes |
| 1401 | */ |
Mayank Grover | bc8a2ef | 2018-02-23 18:03:36 +0530 | [diff] [blame] | 1402 | if (!cmdline) |
| 1403 | return; |
| 1404 | |
vijay kumar | 21a3745 | 2015-12-29 16:23:21 +0530 | [diff] [blame] | 1405 | if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) { |
| 1406 | ptr = atag_cmdline(ptr, cmdline); |
| 1407 | ptr = atag_end(ptr); |
| 1408 | } |
| 1409 | else { |
| 1410 | dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n"); |
| 1411 | ASSERT(0); |
| 1412 | } |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1415 | typedef void entry_func_ptr(unsigned, unsigned, unsigned*); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1416 | void boot_linux(void *kernel, unsigned *tags, |
| 1417 | const char *cmdline, unsigned machtype, |
| 1418 | void *ramdisk, unsigned ramdisk_size) |
| 1419 | { |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1420 | unsigned char *final_cmdline; |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1421 | #if DEVICE_TREE |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1422 | int ret = 0; |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1423 | #endif |
| 1424 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 1425 | void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel)); |
Deepa Dinamani | 25a9f76 | 2012-11-30 15:57:15 -0800 | [diff] [blame] | 1426 | uint32_t tags_phys = PA((addr_t)tags); |
vijay kumar | 1a50a64 | 2015-11-16 12:41:15 +0530 | [diff] [blame] | 1427 | struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel))); |
Deepa Dinamani | 25a9f76 | 2012-11-30 15:57:15 -0800 | [diff] [blame] | 1428 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1429 | ramdisk = (void *)PA((addr_t)ramdisk); |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1430 | |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1431 | final_cmdline = update_cmdline((const char*)cmdline); |
| 1432 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1433 | #if DEVICE_TREE |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1434 | dprintf(INFO, "Updating device tree: start\n"); |
| 1435 | |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1436 | /* Update the Device Tree */ |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1437 | 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] | 1438 | if(ret) |
| 1439 | { |
| 1440 | dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n"); |
| 1441 | ASSERT(0); |
| 1442 | } |
Amol Jadi | b6be5c1 | 2012-11-14 13:39:51 -0800 | [diff] [blame] | 1443 | dprintf(INFO, "Updating device tree: done\n"); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1444 | #else |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1445 | /* Generating the Atags */ |
Amol Jadi | 10c7d1c | 2013-01-25 13:24:29 -0800 | [diff] [blame] | 1446 | generate_atags(tags, final_cmdline, ramdisk, ramdisk_size); |
Neeti Desai | 17379b8 | 2012-06-04 18:42:53 -0700 | [diff] [blame] | 1447 | #endif |
Neeti Desai | e245d49 | 2012-06-01 12:52:13 -0700 | [diff] [blame] | 1448 | |
Monika Singh | 0ec6db8 | 2019-07-18 16:57:58 +0530 | [diff] [blame] | 1449 | #if VERIFIED_BOOT |
Monika Singh | b0db4b8 | 2018-09-26 12:18:02 +0530 | [diff] [blame] | 1450 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1451 | { |
| 1452 | if (device.verity_mode == 0) { |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1453 | #if FBCON_DISPLAY_MSG |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1454 | #if ENABLE_VB_ATTEST |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1455 | display_bootverify_menu(DISPLAY_MENU_EIO); |
| 1456 | wait_for_users_action(); |
| 1457 | if(!pwr_key_is_pressed) |
| 1458 | shutdown_device(); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1459 | #else |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1460 | display_bootverify_menu(DISPLAY_MENU_LOGGING); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1461 | #endif |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1462 | wait_for_users_action(); |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1463 | #else |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1464 | dprintf(CRITICAL, |
| 1465 | "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n"); |
| 1466 | mdelay(5000); |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1467 | #endif |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 1468 | } |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1469 | } |
lijuang | bdd9bb4 | 2016-03-01 18:22:17 +0800 | [diff] [blame] | 1470 | #endif |
| 1471 | |
| 1472 | #if VERIFIED_BOOT |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 1473 | /* Write protect the device info */ |
Channagoud Kadabi | 3bd9d1e | 2015-05-05 16:18:20 -0700 | [diff] [blame] | 1474 | 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] | 1475 | { |
| 1476 | dprintf(INFO, "Failed to write protect dev info\n"); |
| 1477 | ASSERT(0); |
| 1478 | } |
| 1479 | #endif |
| 1480 | |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 1481 | /* Turn off splash screen if enabled */ |
| 1482 | #if DISPLAY_SPLASH_SCREEN |
| 1483 | target_display_shutdown(); |
| 1484 | #endif |
| 1485 | |
Veera Sundaram Sankaran | 67ea093 | 2015-09-25 10:09:30 -0700 | [diff] [blame] | 1486 | /* Perform target specific cleanup */ |
| 1487 | target_uninit(); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1488 | free_verified_boot_resource(&info); |
| 1489 | if (final_cmdline) |
| 1490 | free(final_cmdline); |
Aravind Venkateswaran | 8f07624 | 2014-02-25 16:25:30 -0800 | [diff] [blame] | 1491 | |
Deepa Dinamani | 33734bc | 2013-03-06 12:16:06 -0800 | [diff] [blame] | 1492 | 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] | 1493 | entry, ramdisk, ramdisk_size, (void *)tags_phys); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1494 | |
| 1495 | enter_critical_section(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1496 | |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 1497 | /* do any platform specific cleanup before kernel entry */ |
| 1498 | platform_uninit(); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1499 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1500 | arch_disable_cache(UCACHE); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 1501 | |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 1502 | #if ARM_WITH_MMU |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1503 | arch_disable_mmu(); |
Amol Jadi | 504f9fe | 2012-08-16 13:56:48 -0700 | [diff] [blame] | 1504 | #endif |
Amol Jadi | 492d5a5 | 2013-03-15 16:12:34 -0700 | [diff] [blame] | 1505 | bs_set_timestamp(BS_KERNEL_ENTRY); |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1506 | |
| 1507 | if (IS_ARM64(kptr)) |
| 1508 | /* Jump to a 64bit kernel */ |
| 1509 | scm_elexec_call((paddr_t)kernel, tags_phys); |
| 1510 | else |
| 1511 | /* Jump to a 32bit kernel */ |
| 1512 | entry(0, machtype, (unsigned*)tags_phys); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1513 | } |
| 1514 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1515 | /* Function to check if the memory address range falls within the aboot |
| 1516 | * boundaries. |
| 1517 | * start: Start of the memory region |
| 1518 | * size: Size of the memory region |
| 1519 | */ |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 1520 | int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1521 | { |
| 1522 | /* Check for boundary conditions. */ |
Sundarajan Srinivasan | ce2a0ea | 2013-12-16 17:02:56 -0800 | [diff] [blame] | 1523 | if ((UINT_MAX - start) < size) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1524 | return -1; |
| 1525 | |
| 1526 | /* Check for memory overlap. */ |
| 1527 | if ((start < MEMBASE) && ((start + size) <= MEMBASE)) |
| 1528 | return 0; |
Channagoud Kadabi | 9414391 | 2013-10-15 12:53:52 -0700 | [diff] [blame] | 1529 | else if (start >= (MEMBASE + MEMSIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1530 | return 0; |
| 1531 | else |
| 1532 | return -1; |
| 1533 | } |
| 1534 | |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 1535 | /* Function to check if the memory address range falls beyond ddr region. |
| 1536 | * start: Start of the memory region |
| 1537 | * size: Size of the memory region |
| 1538 | */ |
| 1539 | int check_ddr_addr_range_bound(uintptr_t start, uint32_t size) |
| 1540 | { |
| 1541 | uintptr_t ddr_pa_start_addr = PA(get_ddr_start()); |
| 1542 | uint64_t ddr_size = smem_get_ddr_size(); |
| 1543 | uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size; |
| 1544 | uintptr_t pa_start_addr = PA(start); |
| 1545 | |
| 1546 | /* Check for boundary conditions. */ |
| 1547 | if ((UINT_MAX - start) < size) |
| 1548 | return -1; |
| 1549 | |
| 1550 | /* Check if memory range is beyond the ddr range. */ |
| 1551 | if (pa_start_addr < ddr_pa_start_addr || |
| 1552 | pa_start_addr >= (ddr_pa_end_addr) || |
| 1553 | (pa_start_addr + size) > ddr_pa_end_addr) |
| 1554 | return -1; |
| 1555 | else |
| 1556 | return 0; |
| 1557 | } |
| 1558 | |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1559 | 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] | 1560 | |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1561 | int getimage(void **image_buffer, uint32_t *imgsize, |
| 1562 | const char *imgname) |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1563 | { |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1564 | uint32_t loadedindex; |
| 1565 | if (image_buffer == NULL || imgsize == NULL || |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1566 | imgname == NULL) { |
| 1567 | dprintf(CRITICAL, "getimage: invalid parameters\n"); |
| 1568 | return -1; |
| 1569 | } |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1570 | for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) { |
| 1571 | if (!strncmp(info.images[loadedindex].name, imgname, |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1572 | strlen(imgname))) { |
Mayank Grover | dd080e8 | 2018-09-04 20:12:13 +0530 | [diff] [blame] | 1573 | *image_buffer = info.images[loadedindex].image_buffer; |
| 1574 | *imgsize = info.images[loadedindex].imgsize; |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 1575 | dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n", |
| 1576 | info.images[loadedindex].name, |
| 1577 | info.images[loadedindex].imgsize); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1578 | return 0; |
| 1579 | } |
| 1580 | } |
| 1581 | return -1; |
| 1582 | } |
| 1583 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1584 | static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size) |
| 1585 | { |
| 1586 | int ret; |
Channagoud Kadabi | a8c623f | 2015-01-13 14:48:48 -0800 | [diff] [blame] | 1587 | |
| 1588 | #if !VERIFIED_BOOT |
Sundarajan Srinivasan | ce54d6e | 2013-11-11 12:45:00 -0800 | [diff] [blame] | 1589 | #if IMAGE_VERIF_ALGO_SHA1 |
| 1590 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1; |
| 1591 | #else |
| 1592 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256; |
| 1593 | #endif |
Channagoud Kadabi | a8c623f | 2015-01-13 14:48:48 -0800 | [diff] [blame] | 1594 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1595 | |
| 1596 | /* Assume device is rooted at this time. */ |
| 1597 | device.is_tampered = 1; |
| 1598 | |
| 1599 | dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size); |
| 1600 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1601 | #if VERIFIED_BOOT |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 1602 | uint32_t bootstate; |
| 1603 | if(boot_into_recovery && |
Mayank Grover | 68fbf0d | 2017-10-24 14:13:39 +0530 | [diff] [blame] | 1604 | (!partition_multislot_is_supported())) |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 1605 | { |
| 1606 | ret = boot_verify_image((unsigned char *)bootimg_addr, |
| 1607 | bootimg_size, "/recovery", &bootstate); |
| 1608 | } |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1609 | else |
Monika Singh | 7d2fc27 | 2018-03-16 17:16:01 +0530 | [diff] [blame] | 1610 | { |
| 1611 | ret = boot_verify_image((unsigned char *)bootimg_addr, |
| 1612 | bootimg_size, "/boot", &bootstate); |
| 1613 | } |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1614 | boot_verify_print_state(); |
| 1615 | #else |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1616 | ret = image_verify((unsigned char *)bootimg_addr, |
| 1617 | (unsigned char *)(bootimg_addr + bootimg_size), |
| 1618 | bootimg_size, |
Sundarajan Srinivasan | ce54d6e | 2013-11-11 12:45:00 -0800 | [diff] [blame] | 1619 | auth_algo); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1620 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1621 | dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret); |
| 1622 | |
| 1623 | if (ret) |
| 1624 | { |
| 1625 | /* Authorized kernel */ |
| 1626 | device.is_tampered = 0; |
Sundarajan Srinivasan | 3fb21f1 | 2013-09-16 18:36:15 -0700 | [diff] [blame] | 1627 | auth_kernel_img = 1; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 1630 | #ifdef MDTP_SUPPORT |
| 1631 | { |
| 1632 | /* Verify MDTP lock. |
| 1633 | * For boot & recovery partitions, use aboot's verification result. |
| 1634 | */ |
| 1635 | mdtp_ext_partition_verification_t ext_partition; |
| 1636 | ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT; |
| 1637 | ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID; |
| 1638 | ext_partition.page_size = 0; /* Not needed since already validated */ |
| 1639 | ext_partition.image_addr = 0; /* Not needed since already validated */ |
| 1640 | ext_partition.image_size = 0; /* Not needed since already validated */ |
| 1641 | ext_partition.sig_avail = FALSE; /* Not needed since already validated */ |
| 1642 | mdtp_fwlock_verify_lock(&ext_partition); |
| 1643 | } |
| 1644 | #endif /* MDTP_SUPPORT */ |
| 1645 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1646 | #if USE_PCOM_SECBOOT |
| 1647 | set_tamper_flag(device.is_tampered); |
| 1648 | #endif |
| 1649 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1650 | #if VERIFIED_BOOT |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1651 | switch(boot_verify_get_state()) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1652 | { |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1653 | case RED: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1654 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 1655 | display_bootverify_menu(DISPLAY_MENU_RED); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1656 | #if ENABLE_VB_ATTEST |
| 1657 | mdelay(DELAY_WAIT); |
| 1658 | shutdown_device(); |
| 1659 | #else |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1660 | wait_for_users_action(); |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 1661 | #endif |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1662 | #else |
| 1663 | dprintf(CRITICAL, |
| 1664 | "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n"); |
| 1665 | mdelay(5000); |
| 1666 | #endif |
| 1667 | |
| 1668 | break; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1669 | case YELLOW: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1670 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 1671 | display_bootverify_menu(DISPLAY_MENU_YELLOW); |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1672 | wait_for_users_action(); |
| 1673 | #else |
| 1674 | dprintf(CRITICAL, |
| 1675 | "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n"); |
| 1676 | mdelay(5000); |
| 1677 | #endif |
| 1678 | break; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1679 | default: |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 1680 | break; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1681 | } |
| 1682 | #endif |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1683 | #if !VERIFIED_BOOT |
Unnati Gandhi | 1be0475 | 2015-03-27 19:41:53 +0530 | [diff] [blame] | 1684 | if(device.is_tampered) |
| 1685 | { |
| 1686 | write_device_info_mmc(&device); |
| 1687 | #ifdef TZ_TAMPER_FUSE |
Monika Singh | 9825746 | 2018-06-06 11:28:49 +0530 | [diff] [blame] | 1688 | set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE); |
Unnati Gandhi | 1be0475 | 2015-03-27 19:41:53 +0530 | [diff] [blame] | 1689 | #endif |
| 1690 | #ifdef ASSERT_ON_TAMPER |
| 1691 | dprintf(CRITICAL, "Device is tampered. Asserting..\n"); |
| 1692 | ASSERT(0); |
| 1693 | #endif |
| 1694 | } |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame] | 1695 | #endif |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1698 | static bool check_format_bit() |
| 1699 | { |
| 1700 | bool ret = false; |
| 1701 | int index; |
| 1702 | uint64_t offset; |
| 1703 | struct boot_selection_info *in = NULL; |
| 1704 | char *buf = NULL; |
| 1705 | |
| 1706 | index = partition_get_index("bootselect"); |
| 1707 | if (index == INVALID_PTN) |
| 1708 | { |
| 1709 | dprintf(INFO, "Unable to locate /bootselect partition\n"); |
| 1710 | return ret; |
| 1711 | } |
| 1712 | offset = partition_get_offset(index); |
| 1713 | if(!offset) |
| 1714 | { |
| 1715 | dprintf(INFO, "partition /bootselect doesn't exist\n"); |
| 1716 | return ret; |
| 1717 | } |
| 1718 | buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE)); |
Mayank Grover | 4886040 | 2016-11-29 12:34:53 +0530 | [diff] [blame] | 1719 | mmc_set_lun(partition_get_lun(index)); |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1720 | ASSERT(buf); |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1721 | if (mmc_read(offset, (uint32_t *)buf, page_size)) |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1722 | { |
| 1723 | dprintf(INFO, "mmc read failure /bootselect %d\n", page_size); |
| 1724 | free(buf); |
| 1725 | return ret; |
| 1726 | } |
| 1727 | in = (struct boot_selection_info *) buf; |
| 1728 | if ((in->signature == BOOTSELECT_SIGNATURE) && |
| 1729 | (in->version == BOOTSELECT_VERSION)) { |
| 1730 | if ((in->state_info & BOOTSELECT_FORMAT) && |
| 1731 | !(in->state_info & BOOTSELECT_FACTORY)) |
| 1732 | ret = true; |
| 1733 | } else { |
| 1734 | dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n", |
| 1735 | in->signature, in->version); |
| 1736 | ASSERT(0); |
| 1737 | } |
| 1738 | free(buf); |
| 1739 | return ret; |
| 1740 | } |
| 1741 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1742 | void boot_verifier_init() |
| 1743 | { |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 1744 | uint32_t boot_state; |
| 1745 | /* Check if device unlock */ |
| 1746 | if(device.is_unlocked) |
| 1747 | { |
| 1748 | boot_verify_send_event(DEV_UNLOCK); |
| 1749 | boot_verify_print_state(); |
| 1750 | dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n"); |
| 1751 | return; |
| 1752 | } |
| 1753 | else |
| 1754 | { |
| 1755 | boot_verify_send_event(BOOT_INIT); |
| 1756 | } |
| 1757 | |
| 1758 | /* Initialize keystore */ |
| 1759 | boot_state = boot_verify_keystore_init(); |
| 1760 | if(boot_state == YELLOW) |
| 1761 | { |
| 1762 | boot_verify_print_state(); |
| 1763 | dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n"); |
| 1764 | } |
| 1765 | } |
| 1766 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1767 | /* Function to return recovery appended dtbo buffer info */ |
| 1768 | void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf) |
| 1769 | { |
| 1770 | *dtbo_size = recovery_dtbo_size; |
| 1771 | *dtbo_buf = recovery_dtbo_buf; |
| 1772 | return; |
| 1773 | } |
| 1774 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1775 | int boot_linux_from_mmc(void) |
| 1776 | { |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1777 | boot_img_hdr *hdr = (void*) buf; |
| 1778 | boot_img_hdr *uhdr; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1779 | unsigned offset = 0; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 1780 | int rcode; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1781 | unsigned long long ptn = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1782 | int index = INVALID_PTN; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1783 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1784 | unsigned char *image_addr = 0; |
| 1785 | unsigned kernel_actual; |
| 1786 | unsigned ramdisk_actual; |
| 1787 | unsigned imagesize_actual; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1788 | unsigned second_actual = 0; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1789 | void * image_buf = NULL; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1790 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1791 | unsigned int dtb_size = 0; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1792 | unsigned dtb_image_size = 0; |
| 1793 | uint32_t dtb_image_offset = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1794 | unsigned int out_len = 0; |
| 1795 | unsigned int out_avai_len = 0; |
| 1796 | unsigned char *out_addr = NULL; |
| 1797 | uint32_t dtb_offset = 0; |
| 1798 | unsigned char *kernel_start_addr = NULL; |
| 1799 | unsigned int kernel_size = 0; |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 1800 | unsigned int patched_kernel_hdr_size = 0; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1801 | uint64_t image_size = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 1802 | int rc; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1803 | #if VERIFIED_BOOT_2 |
| 1804 | int status; |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 1805 | void *dtbo_image_buf = NULL; |
| 1806 | uint32_t dtbo_image_sz = 0; |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 1807 | void *vbmeta_image_buf = NULL; |
| 1808 | uint32_t vbmeta_image_sz = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 1809 | #endif |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1810 | char *ptn_name = NULL; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1811 | #if DEVICE_TREE |
| 1812 | struct dt_table *table; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 1813 | struct dt_entry dt_entry; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1814 | unsigned dt_table_offset; |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 1815 | uint32_t dt_actual; |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 1816 | uint32_t dt_hdr_size; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 1817 | unsigned char *best_match_dt_addr = NULL; |
Neeti Desai | 465491e | 2012-07-31 12:53:35 -0700 | [diff] [blame] | 1818 | #endif |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1819 | struct kernel64_hdr *kptr = NULL; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1820 | int current_active_slot = INVALID; |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1821 | |
Maunik Shah | 0f3c8ac | 2014-03-02 17:47:58 +0530 | [diff] [blame] | 1822 | if (check_format_bit()) |
| 1823 | boot_into_recovery = 1; |
| 1824 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 1825 | if (!boot_into_recovery) { |
| 1826 | memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string)); |
| 1827 | rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string)); |
| 1828 | if (rcode <= 0) { |
| 1829 | boot_into_ffbm = false; |
| 1830 | if (rcode < 0) |
| 1831 | dprintf(CRITICAL,"failed to get ffbm cookie"); |
| 1832 | } else |
| 1833 | boot_into_ffbm = true; |
| 1834 | } else |
| 1835 | boot_into_ffbm = false; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1836 | uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1837 | if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 1838 | dprintf(INFO, "Unified boot method!\n"); |
| 1839 | hdr = uhdr; |
| 1840 | goto unified_boot; |
| 1841 | } |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1842 | |
| 1843 | /* For a/b recovery image code is on boot partition. |
| 1844 | If we support multislot, always use boot partition. */ |
| 1845 | if (boot_into_recovery && |
Mayank Grover | 8ab4a76 | 2019-04-25 17:23:34 +0530 | [diff] [blame] | 1846 | ((!partition_multislot_is_supported()) || |
| 1847 | (target_dynamic_partition_supported()))) |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1848 | ptn_name = "recovery"; |
| 1849 | else |
| 1850 | ptn_name = "boot"; |
| 1851 | |
| 1852 | index = partition_get_index(ptn_name); |
| 1853 | ptn = partition_get_offset(index); |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1854 | image_size = partition_get_size(index); |
| 1855 | if(ptn == 0 || image_size == 0) { |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1856 | dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name); |
| 1857 | return -1; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1858 | } |
Mayank Grover | c93cad8 | 2017-10-03 12:23:45 +0530 | [diff] [blame] | 1859 | |
Channagoud Kadabi | ef0547c | 2015-02-10 12:57:38 -0800 | [diff] [blame] | 1860 | /* Set Lun for boot & recovery partitions */ |
| 1861 | mmc_set_lun(partition_get_lun(index)); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1862 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 1863 | if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) { |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1864 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 1865 | return -1; |
| 1866 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1867 | |
| 1868 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 1869 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 1870 | return ERR_INVALID_BOOT_MAGIC; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1871 | } |
| 1872 | |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 1873 | if (hdr->page_size && (hdr->page_size != page_size)) { |
vijay kumar | 2e21b3a | 2014-06-26 17:40:15 +0530 | [diff] [blame] | 1874 | |
| 1875 | if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) { |
| 1876 | dprintf(CRITICAL, "ERROR: Invalid page size\n"); |
| 1877 | return -1; |
| 1878 | } |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 1879 | page_size = hdr->page_size; |
| 1880 | page_mask = page_size - 1; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1883 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 1884 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1885 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 1886 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1887 | image_addr = (unsigned char *)target_get_scratch_address(); |
Mayank Grover | 6fd3679 | 2018-09-10 13:26:58 +0530 | [diff] [blame] | 1888 | #if VERIFIED_BOOT_2 |
| 1889 | /* Create hole in start of image for VB salt to copy */ |
| 1890 | image_addr += SALT_BUFF_OFFSET; |
| 1891 | #endif |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 1892 | memcpy(image_addr, (void *)buf, page_size); |
| 1893 | |
| 1894 | /* ensure commandline is terminated */ |
| 1895 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1896 | |
| 1897 | #if DEVICE_TREE |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 1898 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 1899 | dt_size = hdr->dt_size; |
Mayank Grover | 5a50258 | 2018-09-12 11:24:49 +0530 | [diff] [blame] | 1900 | #else |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1901 | dprintf(INFO, "BootImage Header: %d\n", hdr->header_version); |
Mayank Grover | 5a50258 | 2018-09-12 11:24:49 +0530 | [diff] [blame] | 1902 | #endif |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1903 | |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 1904 | dt_actual = ROUND_TO_PAGE(dt_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1905 | 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] | 1906 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__); |
| 1907 | return -1; |
| 1908 | } |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1909 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1910 | #else |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1911 | 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] | 1912 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__); |
| 1913 | return -1; |
| 1914 | } |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 1915 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1916 | #endif |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1917 | dtb_image_size = hdr->kernel_size; |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 1918 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1919 | #ifdef OSVERSION_IN_BOOTIMAGE |
| 1920 | /* If header version is ONE and booting into recovery, |
| 1921 | dtbo is appended with recovery image. |
| 1922 | Doing following: |
| 1923 | * Validating the recovery offset and size. |
| 1924 | * Extracting recovery dtbo to be used as dtbo. |
| 1925 | */ |
| 1926 | if (boot_into_recovery && |
| 1927 | hdr->header_version == BOOT_HEADER_VERSION_ONE) |
| 1928 | { |
| 1929 | struct boot_img_hdr_v1 *hdr1 = |
| 1930 | (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr)); |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1931 | unsigned int recovery_dtbo_actual = 0; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1932 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1933 | recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask); |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1934 | if ((hdr1->header_size != |
| 1935 | sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr))) |
| 1936 | { |
| 1937 | dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size); |
| 1938 | return -1; |
| 1939 | } |
| 1940 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1941 | if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF) |
| 1942 | { |
| 1943 | 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] | 1944 | MAX_SUPPORTED_DTBO_IMG_BUF); |
| 1945 | return -1; |
| 1946 | } |
| 1947 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1948 | if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual)) |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1949 | { |
| 1950 | dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__); |
| 1951 | return -1; |
| 1952 | } |
| 1953 | |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1954 | if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) { |
| 1955 | dprintf(CRITICAL, |
| 1956 | "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__); |
| 1957 | return -1; |
| 1958 | } |
| 1959 | |
| 1960 | if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size) |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1961 | { |
| 1962 | dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx," |
| 1963 | " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n", |
| 1964 | hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size); |
| 1965 | return -1; |
| 1966 | } |
| 1967 | |
| 1968 | recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr); |
Mayank Grover | a1a83c7 | 2018-09-24 11:23:15 +0530 | [diff] [blame] | 1969 | recovery_dtbo_size = recovery_dtbo_actual; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 1970 | imagesize_actual += recovery_dtbo_size; |
| 1971 | |
| 1972 | dprintf(SPEW, "Header version: %d\n", hdr->header_version); |
| 1973 | dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size); |
| 1974 | dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset); |
| 1975 | |
| 1976 | } |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1977 | |
| 1978 | if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) { |
| 1979 | struct boot_img_hdr_v1 *hdr1 = |
| 1980 | (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr)); |
| 1981 | struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *) |
| 1982 | (image_addr + sizeof(boot_img_hdr) + |
| 1983 | BOOT_IMAGE_HEADER_V2_OFFSET); |
| 1984 | unsigned int recovery_dtbo_actual = 0; |
| 1985 | |
lijuang | 0b17ba2 | 2019-09-04 14:20:47 +0800 | [diff] [blame] | 1986 | recovery_dtbo_actual = |
| 1987 | ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask); |
| 1988 | imagesize_actual += recovery_dtbo_actual; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 1989 | |
| 1990 | imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask); |
| 1991 | |
| 1992 | |
| 1993 | dtb_image_offset = page_size + patched_kernel_hdr_size + |
| 1994 | kernel_actual + ramdisk_actual + second_actual + |
| 1995 | recovery_dtbo_actual; |
| 1996 | |
| 1997 | dprintf(SPEW, "Header version: %d\n", hdr->header_version); |
| 1998 | dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset); |
| 1999 | } |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2000 | #endif |
| 2001 | |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 2002 | |
Saranya Chidura | b1aaf23 | 2018-11-19 15:47:50 +0530 | [diff] [blame] | 2003 | /* Validate the boot/recovery image size is within the bounds of partition size */ |
| 2004 | if (imagesize_actual > image_size) { |
| 2005 | dprintf(CRITICAL, "Image size is greater than partition size.\n"); |
| 2006 | return -1; |
| 2007 | } |
| 2008 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2009 | #if VERIFIED_BOOT |
| 2010 | boot_verifier_init(); |
| 2011 | #endif |
| 2012 | |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 2013 | #if VERIFIED_BOOT_2 |
| 2014 | /* read full partition if device is unlocked */ |
| 2015 | if (device.is_unlocked) |
| 2016 | imagesize_actual = image_size; |
| 2017 | #endif |
| 2018 | |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 2019 | if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual)) |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2020 | { |
| 2021 | dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n"); |
| 2022 | return -1; |
| 2023 | } |
| 2024 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2025 | /* |
| 2026 | * Update loading flow of bootimage to support compressed/uncompressed |
| 2027 | * bootimage on both 64bit and 32bit platform. |
| 2028 | * 1. Load bootimage from emmc partition onto DDR. |
| 2029 | * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel |
| 2030 | * 3. Check kernel header and update kernel load addr for 64bit and 32bit |
| 2031 | * platform accordingly. |
| 2032 | * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data. |
| 2033 | */ |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 2034 | if (partition_multislot_is_supported()) |
| 2035 | { |
| 2036 | current_active_slot = partition_find_active_slot(); |
| 2037 | dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n", |
| 2038 | imagesize_actual, SUFFIX_SLOT(current_active_slot)); |
| 2039 | } |
| 2040 | else |
| 2041 | { |
| 2042 | dprintf(INFO, "Loading (%s) image (%d): start\n", |
| 2043 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
| 2044 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2045 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
| 2046 | |
Gaurav Nebhwani | 43e2a46 | 2016-03-17 21:32:56 +0530 | [diff] [blame] | 2047 | if ((target_get_max_flash_size() - page_size) < imagesize_actual) |
| 2048 | { |
| 2049 | dprintf(CRITICAL, "booimage size is greater than DDR can hold\n"); |
| 2050 | return -1; |
| 2051 | } |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 2052 | offset = page_size; |
| 2053 | /* Read image without signature and header*/ |
| 2054 | 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] | 2055 | { |
| 2056 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 2057 | return -1; |
| 2058 | } |
| 2059 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 2060 | if (partition_multislot_is_supported()) |
| 2061 | { |
| 2062 | dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n", |
| 2063 | imagesize_actual, SUFFIX_SLOT(current_active_slot)); |
| 2064 | } |
| 2065 | else |
| 2066 | { |
| 2067 | dprintf(INFO, "Loading (%s) image (%d): done\n", |
Channagoud Kadabi | f0705b5 | 2015-08-20 14:16:08 -0700 | [diff] [blame] | 2068 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
| 2069 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 2070 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2071 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
| 2072 | |
| 2073 | /* Authenticate Kernel */ |
| 2074 | dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n", |
| 2075 | (int) target_use_signed_kernel(), |
| 2076 | device.is_unlocked, |
| 2077 | device.is_tampered); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2078 | #if VERIFIED_BOOT_2 |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 2079 | /* if device is unlocked skip reading signature, as full partition is read */ |
| 2080 | if (!device.is_unlocked) |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2081 | { |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 2082 | offset = imagesize_actual; |
| 2083 | if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size)) |
| 2084 | { |
| 2085 | dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n"); |
| 2086 | return -1; |
| 2087 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2088 | |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 2089 | /* Read signature */ |
| 2090 | if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size)) |
| 2091 | { |
| 2092 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 2093 | return -1; |
| 2094 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2095 | } |
| 2096 | |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2097 | /* load and validate dtbo partition */ |
| 2098 | load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz); |
| 2099 | |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 2100 | /* load vbmeta partition */ |
| 2101 | load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz); |
| 2102 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2103 | memset(&info, 0, sizeof(bootinfo)); |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2104 | |
| 2105 | /* Pass loaded boot image passed */ |
Mayank Grover | 6fd3679 | 2018-09-10 13:26:58 +0530 | [diff] [blame] | 2106 | info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr); |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2107 | info.images[IMG_BOOT].imgsize = imagesize_actual; |
| 2108 | info.images[IMG_BOOT].name = ptn_name; |
| 2109 | ++info.num_loaded_images; |
| 2110 | |
| 2111 | /* Pass loaded dtbo image */ |
| 2112 | if (dtbo_image_buf != NULL) { |
Mayank Grover | 6fd3679 | 2018-09-10 13:26:58 +0530 | [diff] [blame] | 2113 | info.images[IMG_DTBO].image_buffer = |
| 2114 | SUB_SALT_BUFF_OFFSET(dtbo_image_buf); |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2115 | info.images[IMG_DTBO].imgsize = dtbo_image_sz; |
| 2116 | info.images[IMG_DTBO].name = "dtbo"; |
| 2117 | ++info.num_loaded_images; |
| 2118 | } |
| 2119 | |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 2120 | /* Pass loaded vbmeta image */ |
| 2121 | if (vbmeta_image_buf != NULL) { |
| 2122 | info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf; |
| 2123 | info.images[IMG_VBMETA].imgsize = vbmeta_image_sz; |
| 2124 | info.images[IMG_VBMETA].name = "vbmeta"; |
| 2125 | ++info.num_loaded_images; |
| 2126 | } |
| 2127 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2128 | info.header_version = hdr->header_version; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2129 | info.multi_slot_boot = partition_multislot_is_supported(); |
| 2130 | info.bootreason_alarm = boot_reason_alarm; |
| 2131 | info.bootinto_recovery = boot_into_recovery; |
| 2132 | status = load_image_and_auth(&info); |
| 2133 | if(status) |
| 2134 | return -1; |
| 2135 | |
| 2136 | vbcmdline = info.vbcmdline; |
Mayank Grover | 9df84c0 | 2018-08-30 15:46:35 +0530 | [diff] [blame] | 2137 | |
| 2138 | /* Free the buffer allocated to vbmeta post verification */ |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 2139 | if (vbmeta_image_buf != NULL) { |
| 2140 | free(vbmeta_image_buf); |
| 2141 | --info.num_loaded_images; |
| 2142 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2143 | #else |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 2144 | /* Change the condition a little bit to include the test framework support. |
| 2145 | * We would never reach this point if device is in fastboot mode, even if we did |
| 2146 | * that means we are in test mode, so execute kernel authentication part for the |
| 2147 | * tests */ |
Parth Dixit | cb0c608 | 2015-12-30 15:01:13 +0530 | [diff] [blame] | 2148 | if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled()) |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2149 | { |
| 2150 | offset = imagesize_actual; |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 2151 | 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] | 2152 | { |
| 2153 | dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n"); |
| 2154 | return -1; |
| 2155 | } |
| 2156 | |
| 2157 | /* Read signature */ |
| 2158 | if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size)) |
| 2159 | { |
| 2160 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 2161 | return -1; |
| 2162 | } |
| 2163 | |
| 2164 | verify_signed_bootimg((uint32_t)image_addr, imagesize_actual); |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 2165 | /* The purpose of our test is done here */ |
Parth Dixit | cb0c608 | 2015-12-30 15:01:13 +0530 | [diff] [blame] | 2166 | if(is_test_mode_enabled() && auth_kernel_img) |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 2167 | return 0; |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2168 | } else { |
| 2169 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 2170 | #ifdef TZ_SAVE_KERNEL_HASH |
| 2171 | aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual); |
| 2172 | #endif /* TZ_SAVE_KERNEL_HASH */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 2173 | |
| 2174 | #ifdef MDTP_SUPPORT |
| 2175 | { |
| 2176 | /* Verify MDTP lock. |
| 2177 | * For boot & recovery partitions, MDTP will use boot_verifier APIs, |
| 2178 | * since verification was skipped in aboot. The signature is not part of the loaded image. |
| 2179 | */ |
| 2180 | mdtp_ext_partition_verification_t ext_partition; |
| 2181 | ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT; |
| 2182 | ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET; |
| 2183 | ext_partition.page_size = page_size; |
| 2184 | ext_partition.image_addr = (uint32)image_addr; |
| 2185 | ext_partition.image_size = imagesize_actual; |
| 2186 | ext_partition.sig_avail = FALSE; |
| 2187 | mdtp_fwlock_verify_lock(&ext_partition); |
| 2188 | } |
| 2189 | #endif /* MDTP_SUPPORT */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2190 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2191 | #endif |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2192 | |
Sridhar Parasuram | 00bfedb | 2015-05-26 14:21:27 -0700 | [diff] [blame] | 2193 | #if VERIFIED_BOOT |
taozhang | 93088bd | 2016-11-16 15:50:46 +0800 | [diff] [blame] | 2194 | if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm)) |
Reut Zysman | ba8a9d5 | 2016-02-18 11:44:04 +0200 | [diff] [blame] | 2195 | { |
| 2196 | #if FBCON_DISPLAY_MSG |
| 2197 | display_bootverify_menu(DISPLAY_MENU_ORANGE); |
| 2198 | wait_for_users_action(); |
| 2199 | #else |
| 2200 | dprintf(CRITICAL, |
| 2201 | "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n"); |
| 2202 | mdelay(5000); |
| 2203 | #endif |
| 2204 | } |
| 2205 | #endif |
| 2206 | |
| 2207 | #if VERIFIED_BOOT |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2208 | if (VB_M == target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 2209 | { |
| 2210 | /* set boot and system versions. */ |
| 2211 | set_os_version((unsigned char *)image_addr); |
| 2212 | // send root of trust |
| 2213 | if(!send_rot_command((uint32_t)device.is_unlocked)) |
| 2214 | ASSERT(0); |
| 2215 | } |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 2216 | #endif |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2217 | /* |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2218 | * Check if the kernel image is a gzip package. If yes, need to decompress it. |
| 2219 | * If not, continue booting. |
| 2220 | */ |
| 2221 | if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size)) |
| 2222 | { |
| 2223 | out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size); |
| 2224 | out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size; |
Mayank Grover | 027a941 | 2018-09-04 15:12:05 +0530 | [diff] [blame] | 2225 | #if VERIFIED_BOOT_2 |
| 2226 | if (dtbo_image_sz) |
| 2227 | out_avai_len -= DTBO_IMG_BUF; |
| 2228 | #endif |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2229 | dprintf(INFO, "decompressing kernel image: start\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2230 | rc = decompress((unsigned char *)(image_addr + page_size), |
| 2231 | hdr->kernel_size, out_addr, out_avai_len, |
| 2232 | &dtb_offset, &out_len); |
| 2233 | if (rc) |
| 2234 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2235 | dprintf(CRITICAL, "decompressing kernel image failed!!!\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2236 | ASSERT(0); |
| 2237 | } |
| 2238 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2239 | dprintf(INFO, "decompressing kernel image: done\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2240 | kptr = (struct kernel64_hdr *)out_addr; |
| 2241 | kernel_start_addr = out_addr; |
| 2242 | kernel_size = out_len; |
| 2243 | } else { |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 2244 | dprintf(INFO, "Uncpmpressed kernel in use\n"); |
| 2245 | if (!strncmp((char*)(image_addr + page_size), |
| 2246 | PATCHED_KERNEL_MAGIC, |
| 2247 | sizeof(PATCHED_KERNEL_MAGIC) - 1)) { |
| 2248 | dprintf(INFO, "Patched kernel detected\n"); |
| 2249 | kptr = (struct kernel64_hdr *)(image_addr + page_size + |
| 2250 | PATCHED_KERNEL_HEADER_SIZE); |
| 2251 | //The size of the kernel is stored at start of kernel image + 16 |
| 2252 | //The dtb would start just after the kernel |
| 2253 | dtb_offset = *((uint32_t*)((unsigned char*) |
| 2254 | (image_addr + page_size + |
| 2255 | sizeof(PATCHED_KERNEL_MAGIC) - |
| 2256 | 1))); |
| 2257 | //The actual kernel starts after the 20 byte header. |
| 2258 | kernel_start_addr = (unsigned char*)(image_addr + |
| 2259 | page_size + PATCHED_KERNEL_HEADER_SIZE); |
| 2260 | kernel_size = hdr->kernel_size; |
| 2261 | patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE; |
| 2262 | } else { |
| 2263 | dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n"); |
| 2264 | kptr = (struct kernel64_hdr *)(image_addr + page_size); |
| 2265 | kernel_start_addr = (unsigned char *)(image_addr + page_size); |
| 2266 | kernel_size = hdr->kernel_size; |
| 2267 | } |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | /* |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2271 | * Update the kernel/ramdisk/tags address if the boot image header |
| 2272 | * has default values, these default values come from mkbootimg when |
| 2273 | * the boot image is flashed using fastboot flash:raw |
| 2274 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2275 | update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr)); |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2276 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2277 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 2278 | hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr)); |
| 2279 | hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr)); |
| 2280 | hdr->tags_addr = VA((addr_t)(hdr->tags_addr)); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2281 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2282 | kernel_size = ROUND_TO_PAGE(kernel_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2283 | /* Check if the addresses in the header are valid. */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2284 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) || |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2285 | check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) || |
| 2286 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) || |
| 2287 | check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2288 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2289 | dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2290 | return -1; |
| 2291 | } |
| 2292 | |
| 2293 | #ifndef DEVICE_TREE |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2294 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) || |
| 2295 | check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2296 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2297 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2298 | return -1; |
| 2299 | } |
| 2300 | #endif |
| 2301 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2302 | /* Move kernel, ramdisk and device tree to correct address */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 2303 | memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2304 | 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] | 2305 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2306 | #if DEVICE_TREE |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 2307 | if(dt_size) { |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2308 | dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual); |
| 2309 | table = (struct dt_table*) dt_table_offset; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2310 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2311 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
| 2312 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 2313 | return -1; |
| 2314 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2315 | |
P.V. Phani Kumar | 8291676 | 2016-03-09 09:20:19 +0530 | [diff] [blame] | 2316 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 2317 | goes beyound hdr->dt_size*/ |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 2318 | 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] | 2319 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 2320 | return -1; |
| 2321 | } |
| 2322 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2323 | /* Find index of device tree within device tree table */ |
| 2324 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
| 2325 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 2326 | return -1; |
| 2327 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2328 | |
P.V. Phani Kumar | 3c33320 | 2016-03-09 11:54:37 +0530 | [diff] [blame] | 2329 | if(dt_entry.offset > (UINT_MAX - dt_entry.size)) { |
| 2330 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2331 | return -1; |
| 2332 | } |
| 2333 | |
| 2334 | /* Ensure we are not overshooting dt_size with the dt_entry selected */ |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 2335 | if ((dt_entry.offset + dt_entry.size) > dt_size) { |
P.V. Phani Kumar | 3c33320 | 2016-03-09 11:54:37 +0530 | [diff] [blame] | 2336 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2337 | return -1; |
| 2338 | } |
| 2339 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2340 | if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size)) |
| 2341 | { |
| 2342 | unsigned int compressed_size = 0; |
| 2343 | out_addr += out_len; |
| 2344 | out_avai_len -= out_len; |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2345 | dprintf(INFO, "decompressing dtb: start\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2346 | rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset, |
| 2347 | dt_entry.size, out_addr, out_avai_len, |
| 2348 | &compressed_size, &dtb_size); |
| 2349 | if (rc) |
| 2350 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2351 | dprintf(CRITICAL, "decompressing dtb failed!!!\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2352 | ASSERT(0); |
| 2353 | } |
| 2354 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 2355 | dprintf(INFO, "decompressing dtb: done\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2356 | best_match_dt_addr = out_addr; |
| 2357 | } else { |
| 2358 | best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset; |
| 2359 | dtb_size = dt_entry.size; |
| 2360 | } |
| 2361 | |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2362 | /* Validate and Read device device tree in the tags_addr */ |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2363 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) || |
| 2364 | check_ddr_addr_range_bound(hdr->tags_addr, dtb_size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2365 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2366 | dprintf(CRITICAL, "Device tree addresses are not valid\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2367 | return -1; |
| 2368 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2369 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 2370 | memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2371 | } else { |
| 2372 | /* Validate the tags_addr */ |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2373 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) || |
| 2374 | check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual)) |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 2375 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2376 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 2377 | return -1; |
| 2378 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2379 | /* |
| 2380 | * If appended dev tree is found, update the atags with |
| 2381 | * memory address to the DTB appended location on RAM. |
| 2382 | * Else update with the atags address in the kernel header |
Mayank Grover | 1ed3e8b | 2018-09-19 15:16:50 +0530 | [diff] [blame] | 2383 | * |
| 2384 | * Make sure everything from scratch address is read before next step! |
| 2385 | * 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] | 2386 | */ |
| 2387 | void *dtb; |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 2388 | image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size); |
| 2389 | |
Nagireddy Annem | 0d60bb6 | 2019-08-16 19:16:00 +0530 | [diff] [blame] | 2390 | #ifdef OSVERSION_IN_BOOTIMAGE |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 2391 | if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) { |
| 2392 | |
| 2393 | image_buf = (void*)(image_addr); |
| 2394 | dtb_offset = dtb_image_offset; |
| 2395 | dtb_image_size = imagesize_actual; |
| 2396 | } |
Nagireddy Annem | 0d60bb6 | 2019-08-16 19:16:00 +0530 | [diff] [blame] | 2397 | #endif |
Parth Dixit | 1375d9e | 2019-07-08 20:56:13 +0530 | [diff] [blame] | 2398 | |
| 2399 | dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset, |
Ameya Thakur | 10a3345 | 2016-06-13 14:24:26 -0700 | [diff] [blame] | 2400 | (void *)hdr->tags_addr); |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2401 | if (!dtb) { |
| 2402 | dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n"); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 2403 | return -1; |
| 2404 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2405 | } |
Matthew Qin | 49e51fa | 2015-02-09 10:40:45 +0800 | [diff] [blame] | 2406 | #endif |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2407 | |
Stanimir Varbanov | 69ec546 | 2013-07-18 18:17:42 +0300 | [diff] [blame] | 2408 | if (boot_into_recovery && !device.is_unlocked && !device.is_tampered) |
| 2409 | target_load_ssd_keystore(); |
| 2410 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2411 | unified_boot: |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2412 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 2413 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Dima Zavin | bd3daa0 | 2013-03-26 11:06:44 -0700 | [diff] [blame] | 2414 | (const char *)hdr->cmdline, board_machtype(), |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 2415 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 2416 | |
| 2417 | return 0; |
| 2418 | } |
| 2419 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2420 | int boot_linux_from_flash(void) |
| 2421 | { |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2422 | boot_img_hdr *hdr = (void*) buf; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2423 | struct ptentry *ptn; |
| 2424 | struct ptable *ptable; |
| 2425 | unsigned offset = 0; |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 2426 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2427 | unsigned char *image_addr = 0; |
| 2428 | unsigned kernel_actual; |
| 2429 | unsigned ramdisk_actual; |
| 2430 | unsigned imagesize_actual; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2431 | unsigned second_actual = 0; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2432 | |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2433 | #if DEVICE_TREE |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2434 | struct dt_table *table = NULL; |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 2435 | struct dt_entry dt_entry; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2436 | unsigned dt_table_offset; |
Deepa Dinamani | 7aaf83d | 2012-12-21 11:27:01 -0800 | [diff] [blame] | 2437 | uint32_t dt_actual; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 2438 | uint32_t dt_hdr_size = 0; |
Vivek Kumar | 07bd086 | 2018-04-10 14:19:23 +0530 | [diff] [blame] | 2439 | uint32_t dtb_offset = 0; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2440 | unsigned int dtb_size = 0; |
| 2441 | unsigned char *best_match_dt_addr = NULL; |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2442 | #endif |
| 2443 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 2444 | if (target_is_emmc_boot()) { |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 2445 | hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 2446 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 2447 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
| 2448 | return -1; |
| 2449 | } |
| 2450 | goto continue_boot; |
| 2451 | } |
| 2452 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2453 | ptable = flash_get_ptable(); |
| 2454 | if (ptable == NULL) { |
| 2455 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 2456 | return -1; |
| 2457 | } |
| 2458 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 2459 | if(!boot_into_recovery) |
| 2460 | { |
| 2461 | ptn = ptable_find(ptable, "boot"); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 2462 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 2463 | if (ptn == NULL) { |
| 2464 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 2465 | return -1; |
| 2466 | } |
| 2467 | } |
| 2468 | else |
| 2469 | { |
| 2470 | ptn = ptable_find(ptable, "recovery"); |
| 2471 | if (ptn == NULL) { |
| 2472 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 2473 | return -1; |
| 2474 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2475 | } |
| 2476 | |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2477 | /* Read boot.img header from flash */ |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 2478 | if (flash_read(ptn, offset, buf, page_size)) { |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2479 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 2480 | return -1; |
| 2481 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2482 | |
| 2483 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 2484 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2485 | return -1; |
| 2486 | } |
| 2487 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 2488 | if (hdr->page_size != page_size) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 2489 | 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] | 2490 | return -1; |
| 2491 | } |
| 2492 | |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 2493 | image_addr = (unsigned char *)target_get_scratch_address(); |
| 2494 | memcpy(image_addr, (void *)buf, page_size); |
vijay kumar | 287bb54 | 2015-09-29 13:01:52 +0530 | [diff] [blame] | 2495 | |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2496 | /* |
| 2497 | * Update the kernel/ramdisk/tags address if the boot image header |
| 2498 | * has default values, these default values come from mkbootimg when |
| 2499 | * the boot image is flashed using fastboot flash:raw |
| 2500 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 2501 | update_ker_tags_rdisk_addr(hdr, false); |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 2502 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2503 | /* Get virtual addresses since the hdr saves physical addresses. */ |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2504 | hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr)); |
| 2505 | hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr)); |
| 2506 | hdr->tags_addr = VA((addr_t)(hdr->tags_addr)); |
| 2507 | |
| 2508 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 2509 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2510 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2511 | |
Kishor PK | 9e91b59 | 2017-05-24 12:14:55 +0530 | [diff] [blame] | 2512 | /* ensure commandline is terminated */ |
| 2513 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 2514 | |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2515 | /* Check if the addresses in the header are valid. */ |
| 2516 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) || |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2517 | check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) || |
| 2518 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) || |
| 2519 | check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2520 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2521 | dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2522 | return -1; |
| 2523 | } |
| 2524 | |
| 2525 | #ifndef DEVICE_TREE |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2526 | 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] | 2527 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n"); |
| 2528 | return -1; |
| 2529 | } |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2530 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2531 | |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2532 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) || |
| 2533 | check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE)) |
Vijay Kumar Pendoti | d3ed20e | 2016-09-20 00:34:46 +0530 | [diff] [blame] | 2534 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2535 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
Vijay Kumar Pendoti | d3ed20e | 2016-09-20 00:34:46 +0530 | [diff] [blame] | 2536 | return -1; |
| 2537 | } |
| 2538 | #else |
| 2539 | |
| 2540 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 2541 | dt_size = hdr->dt_size; |
| 2542 | #endif |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2543 | dt_actual = ROUND_TO_PAGE(dt_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2544 | 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] | 2545 | dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n"); |
| 2546 | return -1; |
| 2547 | } |
| 2548 | |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 2549 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2550 | |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2551 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) || |
| 2552 | check_ddr_addr_range_bound(hdr->tags_addr, dt_size)) |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2553 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 2554 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
Mayank Grover | 032736f | 2017-07-14 20:34:51 +0530 | [diff] [blame] | 2555 | return -1; |
| 2556 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2557 | #endif |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 2558 | |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2559 | /* Read full boot.img from flash */ |
| 2560 | dprintf(INFO, "Loading (%s) image (%d): start\n", |
| 2561 | (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual); |
| 2562 | bs_set_timestamp(BS_KERNEL_LOAD_START); |
| 2563 | |
| 2564 | if (UINT_MAX - page_size < imagesize_actual) |
| 2565 | { |
| 2566 | dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__); |
| 2567 | return -1; |
| 2568 | } |
| 2569 | |
| 2570 | /*Check the availability of RAM before reading boot image + max signature length from flash*/ |
| 2571 | if (target_get_max_flash_size() < (imagesize_actual + page_size)) |
| 2572 | { |
| 2573 | dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n"); |
| 2574 | return -1; |
| 2575 | } |
| 2576 | |
| 2577 | offset = page_size; |
| 2578 | /* Read image without signature and header */ |
| 2579 | if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size)) |
| 2580 | { |
| 2581 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 2582 | return -1; |
| 2583 | } |
| 2584 | |
| 2585 | dprintf(INFO, "Loading (%s) image (%d): done\n", |
| 2586 | (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual); |
| 2587 | bs_set_timestamp(BS_KERNEL_LOAD_DONE); |
| 2588 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2589 | /* Authenticate Kernel */ |
Deepa Dinamani | 23b60d4 | 2013-06-24 18:10:52 -0700 | [diff] [blame] | 2590 | if(target_use_signed_kernel() && (!device.is_unlocked)) |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2591 | { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2592 | offset = imagesize_actual; |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2593 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2594 | /* Read signature */ |
| 2595 | if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size)) |
| 2596 | { |
| 2597 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 2598 | return -1; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2599 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2600 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 2601 | verify_signed_bootimg((uint32_t)image_addr, imagesize_actual); |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2602 | } |
| 2603 | offset = page_size; |
| 2604 | if(hdr->second_size != 0) { |
| 2605 | if (UINT_MAX - offset < second_actual) |
| 2606 | { |
| 2607 | dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__); |
| 2608 | return -1; |
vijay kumar | 8f53e36 | 2015-11-24 13:38:11 +0530 | [diff] [blame] | 2609 | } |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2610 | offset += second_actual; |
| 2611 | /* Second image loading not implemented. */ |
| 2612 | ASSERT(0); |
| 2613 | } |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2614 | /* Move kernel and ramdisk to correct address */ |
| 2615 | memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size); |
| 2616 | memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size); |
| 2617 | |
| 2618 | #if DEVICE_TREE |
| 2619 | if(dt_size != 0) { |
| 2620 | |
| 2621 | dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual); |
| 2622 | |
| 2623 | table = (struct dt_table*) dt_table_offset; |
| 2624 | |
| 2625 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
| 2626 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 2627 | return -1; |
| 2628 | } |
| 2629 | |
| 2630 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 2631 | goes beyound hdr->dt_size*/ |
| 2632 | if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) { |
| 2633 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 2634 | return -1; |
| 2635 | } |
| 2636 | |
| 2637 | /* Find index of device tree within device tree table */ |
| 2638 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
| 2639 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 2640 | return -1; |
| 2641 | } |
| 2642 | |
| 2643 | /* Validate and Read device device tree in the "tags_add */ |
| 2644 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) || |
| 2645 | check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size)) |
| 2646 | { |
| 2647 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
| 2648 | return -1; |
| 2649 | } |
| 2650 | |
| 2651 | if(dt_entry.offset > (UINT_MAX - dt_entry.size)) { |
| 2652 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2653 | return -1; |
| 2654 | } |
| 2655 | |
| 2656 | /* Ensure we are not overshooting dt_size with the dt_entry selected */ |
| 2657 | if ((dt_entry.offset + dt_entry.size) > dt_size) { |
| 2658 | dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n"); |
| 2659 | return -1; |
| 2660 | } |
| 2661 | |
| 2662 | best_match_dt_addr = (unsigned char *)table + dt_entry.offset; |
| 2663 | dtb_size = dt_entry.size; |
| 2664 | memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size); |
Vivek Kumar | 07bd086 | 2018-04-10 14:19:23 +0530 | [diff] [blame] | 2665 | |
| 2666 | } else { |
| 2667 | /* Validate the tags_addr */ |
| 2668 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) || |
| 2669 | check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual)) |
| 2670 | { |
| 2671 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
| 2672 | return -1; |
| 2673 | } |
| 2674 | /* |
| 2675 | * If appended dev tree is found, update the atags with |
| 2676 | * memory address to the DTB appended location on RAM. |
| 2677 | * Else update with the atags address in the kernel header |
Mayank Grover | 1ed3e8b | 2018-09-19 15:16:50 +0530 | [diff] [blame] | 2678 | * |
| 2679 | * Make sure everything from scratch address is read before next step! |
| 2680 | * 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] | 2681 | */ |
| 2682 | void *dtb = NULL; |
| 2683 | dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr); |
| 2684 | if (!dtb) { |
| 2685 | dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n"); |
| 2686 | return -1; |
| 2687 | } |
| 2688 | } |
Mayank Grover | fc7a2ce | 2017-12-20 12:58:17 +0530 | [diff] [blame] | 2689 | #endif |
| 2690 | if(target_use_signed_kernel() && (!device.is_unlocked)) |
| 2691 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2692 | /* Make sure everything from scratch address is read before next step!*/ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 2693 | if(device.is_tampered) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2694 | { |
| 2695 | write_device_info_flash(&device); |
| 2696 | } |
Channagoud Kadabi | 5c86fe3 | 2012-02-16 10:58:48 +0530 | [diff] [blame] | 2697 | #if USE_PCOM_SECBOOT |
| 2698 | set_tamper_flag(device.is_tampered); |
| 2699 | #endif |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 2700 | } |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 2701 | continue_boot: |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2702 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2703 | /* TODO: create/pass atags to kernel */ |
| 2704 | |
Ajay Dudani | e28a607 | 2011-07-01 13:59:46 -0700 | [diff] [blame] | 2705 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Dima Zavin | bd3daa0 | 2013-03-26 11:06:44 -0700 | [diff] [blame] | 2706 | (const char *)hdr->cmdline, board_machtype(), |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 2707 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 2708 | |
| 2709 | return 0; |
| 2710 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 2711 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2712 | void write_device_info_mmc(device_info *dev) |
| 2713 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2714 | unsigned long long ptn = 0; |
| 2715 | unsigned long long size; |
| 2716 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2717 | uint8_t lun = 0; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2718 | uint32_t ret = 0; |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2719 | uint32_t device_info_sz = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2720 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2721 | if (devinfo_present) |
| 2722 | index = partition_get_index("devinfo"); |
| 2723 | else |
| 2724 | index = partition_get_index("aboot"); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 2725 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2726 | ptn = partition_get_offset(index); |
| 2727 | if(ptn == 0) |
| 2728 | { |
| 2729 | return; |
| 2730 | } |
| 2731 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 2732 | lun = partition_get_lun(index); |
| 2733 | mmc_set_lun(lun); |
| 2734 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2735 | size = partition_get_size(index); |
| 2736 | |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2737 | device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info), |
| 2738 | mmc_blocksize_mask); |
| 2739 | if (device_info_sz == UINT_MAX) |
| 2740 | { |
| 2741 | dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n"); |
| 2742 | return; |
| 2743 | } |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 2744 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2745 | if (devinfo_present) |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2746 | ret = mmc_write(ptn, device_info_sz, (void *)info_buf); |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2747 | else |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2748 | 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] | 2749 | if (ret) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2750 | { |
| 2751 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 2752 | ASSERT(0); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2753 | } |
| 2754 | } |
| 2755 | |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 2756 | void read_device_info_mmc(struct device_info *info) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2757 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2758 | unsigned long long ptn = 0; |
| 2759 | unsigned long long size; |
| 2760 | int index = INVALID_PTN; |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2761 | uint32_t ret = 0; |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2762 | uint32_t device_info_sz = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2763 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2764 | if ((index = partition_get_index("devinfo")) < 0) |
| 2765 | { |
| 2766 | devinfo_present = false; |
| 2767 | index = partition_get_index("aboot"); |
| 2768 | } |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 2769 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2770 | ptn = partition_get_offset(index); |
| 2771 | if(ptn == 0) |
| 2772 | { |
| 2773 | return; |
| 2774 | } |
| 2775 | |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 2776 | mmc_set_lun(partition_get_lun(index)); |
| 2777 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2778 | size = partition_get_size(index); |
| 2779 | |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2780 | device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info), |
| 2781 | mmc_blocksize_mask); |
| 2782 | if (device_info_sz == UINT_MAX) |
| 2783 | { |
| 2784 | dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n"); |
| 2785 | return; |
| 2786 | } |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 2787 | |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2788 | if (devinfo_present) |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2789 | ret = mmc_read(ptn, (void *)info_buf, device_info_sz); |
Channagoud Kadabi | 80a182b | 2015-03-11 17:04:23 -0700 | [diff] [blame] | 2790 | else |
Mayank Grover | ddd348d | 2018-01-23 14:07:09 +0530 | [diff] [blame] | 2791 | 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] | 2792 | if (ret) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2793 | { |
| 2794 | dprintf(CRITICAL, "ERROR: Cannot read device info\n"); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 2795 | ASSERT(0); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2796 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2797 | } |
| 2798 | |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 2799 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 2800 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 2801 | void write_prod_info_mmc(prod_info *dev) |
| 2802 | { |
| 2803 | unsigned long long ptn = 0; |
| 2804 | unsigned long long size; |
| 2805 | int index = INVALID_PTN; |
| 2806 | uint32_t blocksize; |
| 2807 | uint8_t lun = 0; |
| 2808 | uint32_t ret = 0; |
| 2809 | |
| 2810 | if (prodinfo_present) |
| 2811 | index = partition_get_index("prodinfo"); |
| 2812 | else |
| 2813 | index = partition_get_index("aboot"); |
| 2814 | |
| 2815 | ptn = partition_get_offset(index); |
| 2816 | if(ptn == 0) |
| 2817 | { |
| 2818 | return; |
| 2819 | } |
| 2820 | |
| 2821 | lun = partition_get_lun(index); |
| 2822 | mmc_set_lun(lun); |
| 2823 | |
| 2824 | size = partition_get_size(index); |
| 2825 | |
| 2826 | blocksize = mmc_get_device_blocksize(); |
| 2827 | |
| 2828 | if (prodinfo_present) |
| 2829 | ret = mmc_write(ptn, blocksize, (void *)prodinfo_buf); |
| 2830 | else |
| 2831 | ret = mmc_write((ptn + size - blocksize), blocksize, (void *)prodinfo_buf); |
| 2832 | if (ret) |
| 2833 | { |
| 2834 | dprintf(CRITICAL, "ERROR: Cannot write prod info\n"); |
| 2835 | ASSERT(0); |
| 2836 | } |
| 2837 | } |
| 2838 | |
| 2839 | void read_prod_info_mmc(struct prod_info *info) |
| 2840 | { |
| 2841 | unsigned long long ptn = 0; |
| 2842 | unsigned long long size; |
| 2843 | int index = INVALID_PTN; |
| 2844 | uint32_t blocksize; |
| 2845 | uint32_t ret = 0; |
| 2846 | |
| 2847 | if ((index = partition_get_index("prodinfo")) < 0) |
| 2848 | { |
| 2849 | prodinfo_present = false; |
| 2850 | index = partition_get_index("aboot"); |
| 2851 | } |
| 2852 | |
| 2853 | ptn = partition_get_offset(index); |
| 2854 | if(ptn == 0) |
| 2855 | { |
| 2856 | return; |
| 2857 | } |
| 2858 | |
| 2859 | mmc_set_lun(partition_get_lun(index)); |
| 2860 | |
| 2861 | size = partition_get_size(index); |
| 2862 | |
| 2863 | blocksize = mmc_get_device_blocksize(); |
| 2864 | |
| 2865 | if (prodinfo_present) |
| 2866 | ret = mmc_read(ptn, (void *)prodinfo_buf, blocksize); |
| 2867 | else |
| 2868 | ret = mmc_read((ptn + size - blocksize), (void *)prodinfo_buf, blocksize); |
| 2869 | if (ret) |
| 2870 | { |
| 2871 | dprintf(CRITICAL, "ERROR: Cannot read prod info\n"); |
| 2872 | ASSERT(0); |
| 2873 | } |
| 2874 | } |
| 2875 | #endif |
| 2876 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
| 2877 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2878 | void write_device_info_flash(device_info *dev) |
| 2879 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2880 | 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] | 2881 | struct ptentry *ptn; |
| 2882 | struct ptable *ptable; |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2883 | if(info == NULL) |
| 2884 | { |
| 2885 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 2886 | ASSERT(0); |
| 2887 | } |
| 2888 | info_buf = info; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2889 | ptable = flash_get_ptable(); |
| 2890 | if (ptable == NULL) |
| 2891 | { |
| 2892 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 2893 | return; |
| 2894 | } |
| 2895 | |
| 2896 | ptn = ptable_find(ptable, "devinfo"); |
| 2897 | if (ptn == NULL) |
| 2898 | { |
Smita Ghosh | 670c8b8 | 2015-05-07 09:30:03 -0700 | [diff] [blame] | 2899 | dprintf(CRITICAL, "ERROR: No devinfo partition found\n"); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2900 | return; |
| 2901 | } |
| 2902 | |
Mayank Grover | dedbc89 | 2017-10-24 13:41:34 +0530 | [diff] [blame] | 2903 | memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2904 | memcpy(info, dev, sizeof(device_info)); |
| 2905 | |
| 2906 | if (flash_write(ptn, 0, (void *)info_buf, page_size)) |
| 2907 | { |
| 2908 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 2909 | return; |
| 2910 | } |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2911 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2912 | } |
| 2913 | |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2914 | static int read_allow_oem_unlock(device_info *dev) |
| 2915 | { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2916 | unsigned offset; |
| 2917 | int index; |
| 2918 | unsigned long long ptn; |
| 2919 | unsigned long long ptn_size; |
| 2920 | unsigned blocksize = mmc_get_device_blocksize(); |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2921 | STACKBUF_DMA_ALIGN(buf, blocksize); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2922 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2923 | index = partition_get_index(frp_ptns[0]); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2924 | if (index == INVALID_PTN) |
| 2925 | { |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2926 | index = partition_get_index(frp_ptns[1]); |
| 2927 | if (index == INVALID_PTN) |
| 2928 | { |
| 2929 | dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]); |
| 2930 | return -1; |
| 2931 | } |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2932 | } |
| 2933 | |
| 2934 | ptn = partition_get_offset(index); |
| 2935 | ptn_size = partition_get_size(index); |
| 2936 | offset = ptn_size - blocksize; |
| 2937 | |
Mayank Grover | 4886040 | 2016-11-29 12:34:53 +0530 | [diff] [blame] | 2938 | /* Set Lun for partition */ |
| 2939 | mmc_set_lun(partition_get_lun(index)); |
| 2940 | |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2941 | if (mmc_read(ptn + offset, (void *)buf, blocksize)) |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2942 | { |
| 2943 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 2944 | return -1; |
| 2945 | } |
| 2946 | |
| 2947 | /*is_allow_unlock is a bool value stored at the LSB of last byte*/ |
| 2948 | is_allow_unlock = buf[blocksize-1] & 0x01; |
| 2949 | return 0; |
| 2950 | } |
| 2951 | |
| 2952 | static int write_allow_oem_unlock(bool allow_unlock) |
| 2953 | { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2954 | unsigned offset; |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2955 | int index; |
| 2956 | unsigned long long ptn; |
| 2957 | unsigned long long ptn_size; |
| 2958 | unsigned blocksize = mmc_get_device_blocksize(); |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2959 | STACKBUF_DMA_ALIGN(buf, blocksize); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2960 | |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2961 | index = partition_get_index(frp_ptns[0]); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2962 | if (index == INVALID_PTN) |
| 2963 | { |
vijay kumar | ca2e681 | 2015-07-08 20:28:25 +0530 | [diff] [blame] | 2964 | index = partition_get_index(frp_ptns[1]); |
| 2965 | if (index == INVALID_PTN) |
| 2966 | { |
| 2967 | dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]); |
| 2968 | return -1; |
| 2969 | } |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | ptn = partition_get_offset(index); |
| 2973 | ptn_size = partition_get_size(index); |
| 2974 | offset = ptn_size - blocksize; |
Mayank Grover | 4886040 | 2016-11-29 12:34:53 +0530 | [diff] [blame] | 2975 | mmc_set_lun(partition_get_lun(index)); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2976 | |
Channagoud Kadabi | b1fe406 | 2015-08-07 16:08:44 -0700 | [diff] [blame] | 2977 | if (mmc_read(ptn + offset, (void *)buf, blocksize)) |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 2978 | { |
| 2979 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 2980 | return -1; |
| 2981 | } |
| 2982 | |
| 2983 | /*is_allow_unlock is a bool value stored at the LSB of last byte*/ |
| 2984 | buf[blocksize-1] = allow_unlock; |
| 2985 | if (mmc_write(ptn + offset, blocksize, buf)) |
| 2986 | { |
| 2987 | dprintf(CRITICAL, "Writing MMC failed\n"); |
| 2988 | return -1; |
| 2989 | } |
| 2990 | |
| 2991 | return 0; |
| 2992 | } |
| 2993 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 2994 | void read_device_info_flash(device_info *dev) |
| 2995 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2996 | 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] | 2997 | struct ptentry *ptn; |
| 2998 | struct ptable *ptable; |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 2999 | if(info == NULL) |
| 3000 | { |
| 3001 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 3002 | ASSERT(0); |
| 3003 | } |
| 3004 | info_buf = info; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3005 | ptable = flash_get_ptable(); |
| 3006 | if (ptable == NULL) |
| 3007 | { |
| 3008 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 3009 | return; |
| 3010 | } |
| 3011 | |
| 3012 | ptn = ptable_find(ptable, "devinfo"); |
| 3013 | if (ptn == NULL) |
| 3014 | { |
Smita Ghosh | 670c8b8 | 2015-05-07 09:30:03 -0700 | [diff] [blame] | 3015 | dprintf(CRITICAL, "ERROR: No devinfo partition found\n"); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3016 | return; |
| 3017 | } |
| 3018 | |
| 3019 | if (flash_read(ptn, 0, (void *)info_buf, page_size)) |
| 3020 | { |
| 3021 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 3022 | return; |
| 3023 | } |
| 3024 | |
| 3025 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 3026 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3027 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
| 3028 | info->is_unlocked = 0; |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3029 | info->is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3030 | write_device_info_flash(info); |
| 3031 | } |
| 3032 | memcpy(dev, info, sizeof(device_info)); |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 3033 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | void write_device_info(device_info *dev) |
| 3037 | { |
| 3038 | if(target_is_emmc_boot()) |
| 3039 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 3040 | struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 3041 | if(info == NULL) |
| 3042 | { |
| 3043 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 3044 | ASSERT(0); |
| 3045 | } |
| 3046 | info_buf = info; |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3047 | memcpy(info, dev, sizeof(struct device_info)); |
| 3048 | |
| 3049 | #if USE_RPMB_FOR_DEVINFO |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3050 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3051 | is_secure_boot_enable()) { |
| 3052 | if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0) |
| 3053 | ASSERT(0); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 3054 | } |
Channagoud Kadabi | c80cb49 | 2015-04-28 16:08:28 -0700 | [diff] [blame] | 3055 | else |
| 3056 | write_device_info_mmc(info); |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3057 | #else |
| 3058 | write_device_info_mmc(info); |
| 3059 | #endif |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 3060 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3061 | } |
| 3062 | else |
| 3063 | { |
| 3064 | write_device_info_flash(dev); |
| 3065 | } |
| 3066 | } |
| 3067 | |
Monika Singh | 9431646 | 2018-03-15 18:39:01 +0530 | [diff] [blame] | 3068 | int read_rollback_index(uint32_t loc, uint64_t *roll_back_index) |
| 3069 | { |
| 3070 | if (!devinfo_present) { |
| 3071 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3072 | return -EINVAL; |
| 3073 | } |
| 3074 | if (loc >= ARRAY_SIZE(device.rollback_index)) { |
| 3075 | dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n", |
| 3076 | __func__, loc, ARRAY_SIZE(device.rollback_index)); |
| 3077 | ASSERT(0); |
| 3078 | } |
| 3079 | |
| 3080 | *roll_back_index = device.rollback_index[loc]; |
| 3081 | return 0; |
| 3082 | } |
| 3083 | |
| 3084 | int write_rollback_index(uint32_t loc, uint64_t roll_back_index) |
| 3085 | { |
| 3086 | if (!devinfo_present) { |
| 3087 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3088 | return -EINVAL; |
| 3089 | } |
| 3090 | if (loc >= ARRAY_SIZE(device.rollback_index)) { |
| 3091 | dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n", |
| 3092 | __func__, loc, ARRAY_SIZE(device.rollback_index)); |
| 3093 | ASSERT(0); |
| 3094 | } |
| 3095 | |
| 3096 | device.rollback_index[loc] = roll_back_index; |
| 3097 | write_device_info(&device); |
| 3098 | return 0; |
| 3099 | } |
| 3100 | |
Monika Singh | b0fad82 | 2018-03-15 18:50:55 +0530 | [diff] [blame] | 3101 | int store_userkey(uint8_t *user_key, uint32_t user_key_size) |
| 3102 | { |
| 3103 | if (!devinfo_present) { |
| 3104 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3105 | return -EINVAL; |
| 3106 | } |
| 3107 | |
| 3108 | if (user_key_size > ARRAY_SIZE(device.user_public_key)) { |
| 3109 | dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n"); |
| 3110 | return -ENODEV; |
| 3111 | } |
| 3112 | |
| 3113 | memcpy(device.user_public_key, user_key, user_key_size); |
| 3114 | device.user_public_key_length = user_key_size; |
| 3115 | write_device_info(&device); |
| 3116 | return 0; |
| 3117 | } |
| 3118 | |
| 3119 | int erase_userkey() |
| 3120 | { |
| 3121 | if (!devinfo_present) { |
| 3122 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3123 | return -EINVAL; |
| 3124 | } |
| 3125 | memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key)); |
| 3126 | device.user_public_key_length = 0; |
| 3127 | write_device_info(&device); |
| 3128 | return 0; |
| 3129 | } |
| 3130 | |
| 3131 | int get_userkey(uint8_t **user_key, uint32_t *user_key_size) |
| 3132 | { |
| 3133 | if (!devinfo_present) { |
| 3134 | dprintf(CRITICAL, "DeviceInfo not initalized \n"); |
| 3135 | return -EINVAL; |
| 3136 | } |
| 3137 | *user_key = device.user_public_key; |
| 3138 | *user_key_size = device.user_public_key_length; |
| 3139 | return 0; |
| 3140 | } |
| 3141 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3142 | void read_device_info(device_info *dev) |
| 3143 | { |
| 3144 | if(target_is_emmc_boot()) |
| 3145 | { |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 3146 | struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 3147 | if(info == NULL) |
| 3148 | { |
| 3149 | dprintf(CRITICAL, "Failed to allocate memory for device info struct\n"); |
| 3150 | ASSERT(0); |
| 3151 | } |
| 3152 | info_buf = info; |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3153 | |
| 3154 | #if USE_RPMB_FOR_DEVINFO |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3155 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3156 | is_secure_boot_enable()) { |
| 3157 | if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0) |
| 3158 | ASSERT(0); |
Sridhar Parasuram | e1a9792 | 2015-07-27 15:31:22 -0700 | [diff] [blame] | 3159 | } |
Channagoud Kadabi | c80cb49 | 2015-04-28 16:08:28 -0700 | [diff] [blame] | 3160 | else |
| 3161 | read_device_info_mmc(info); |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3162 | #else |
| 3163 | read_device_info_mmc(info); |
| 3164 | #endif |
| 3165 | |
| 3166 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 3167 | { |
| 3168 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3169 | if (is_secure_boot_enable()) { |
Channagoud Kadabi | 05f78ba | 2015-07-06 11:58:14 -0700 | [diff] [blame] | 3170 | info->is_unlocked = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3171 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3172 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3173 | info->is_unlock_critical = 0; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3174 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3175 | } else { |
Channagoud Kadabi | 2fda409 | 2015-07-07 13:34:11 -0700 | [diff] [blame] | 3176 | info->is_unlocked = 1; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3177 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3178 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3179 | info->is_unlock_critical = 1; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3180 | #endif |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3181 | } |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3182 | info->is_tampered = 0; |
| 3183 | info->charger_screen_enabled = 0; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3184 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3185 | if (VB_M <= target_get_vb_version()) |
Monika Singh | 1c71a1c | 2019-12-03 12:12:48 +0530 | [diff] [blame] | 3186 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3187 | info->verity_mode = 1; //enforcing by default |
Monika Singh | 1c71a1c | 2019-12-03 12:12:48 +0530 | [diff] [blame] | 3188 | info->user_public_key_length = 0; |
| 3189 | memset(info->rollback_index, 0, sizeof(info->rollback_index)); |
| 3190 | memset(info->user_public_key, 0, sizeof(info->user_public_key)); |
| 3191 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3192 | #endif |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3193 | write_device_info(info); |
| 3194 | } |
jessicatseng | 1b9da34 | 2020-04-22 11:13:20 +0800 | [diff] [blame] | 3195 | |
| 3196 | //<2020/04/22-JessicaTseng, Enable off-charging |
| 3197 | info->charger_screen_enabled = 1; |
| 3198 | write_device_info(info); |
| 3199 | //>2020/04/22-JessicaTseng |
| 3200 | |
Channagoud Kadabi | 036c605 | 2015-02-09 15:19:59 -0800 | [diff] [blame] | 3201 | memcpy(dev, info, sizeof(device_info)); |
Sridhar Parasuram | 7e16d17 | 2015-07-05 11:35:23 -0700 | [diff] [blame] | 3202 | free(info); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3203 | } |
| 3204 | else |
| 3205 | { |
| 3206 | read_device_info_flash(dev); |
| 3207 | } |
| 3208 | } |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 3209 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 3210 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 3211 | void write_prod_info(prod_info *dev) |
| 3212 | { |
| 3213 | if(target_is_emmc_boot()) |
| 3214 | { |
| 3215 | struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 3216 | if(info == NULL) |
| 3217 | { |
| 3218 | dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n"); |
| 3219 | ASSERT(0); |
| 3220 | } |
| 3221 | prodinfo_buf = info; |
| 3222 | memcpy(info, dev, sizeof(struct prod_info)); |
| 3223 | |
| 3224 | write_prod_info_mmc(info); |
| 3225 | free(info); |
| 3226 | } |
| 3227 | } |
| 3228 | |
| 3229 | void read_prod_info(prod_info *dev) |
| 3230 | { |
| 3231 | if(target_is_emmc_boot()) |
| 3232 | { |
| 3233 | struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE)); |
| 3234 | if(info == NULL) |
| 3235 | { |
| 3236 | dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n"); |
| 3237 | ASSERT(0); |
| 3238 | } |
| 3239 | prodinfo_buf = info; |
| 3240 | |
| 3241 | read_prod_info_mmc(info); |
| 3242 | |
| 3243 | if (memcmp(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE)) |
| 3244 | { |
| 3245 | memcpy(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE); |
| 3246 | memcpy(info->isn, "No_Serial_Number", PRODINFO_MAX_ISN_LEN); |
| 3247 | memcpy(info->ssn, "No_Custer_Serial_Number", PRODINFO_MAX_SSN_LEN); |
| 3248 | info->is_adb_enabled = 0; |
| 3249 | write_prod_info(info); |
| 3250 | } |
| 3251 | memcpy(dev, info, sizeof(prod_info)); |
| 3252 | free(info); |
| 3253 | } |
| 3254 | } |
| 3255 | #endif |
| 3256 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3257 | |
| 3258 | void reset_device_info() |
| 3259 | { |
| 3260 | dprintf(ALWAYS, "reset_device_info called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3261 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3262 | write_device_info(&device); |
| 3263 | } |
| 3264 | |
| 3265 | void set_device_root() |
| 3266 | { |
| 3267 | dprintf(ALWAYS, "set_device_root called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 3268 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 3269 | write_device_info(&device); |
| 3270 | } |
| 3271 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3272 | /* set device unlock value |
| 3273 | * Must check FRP before call this function |
| 3274 | * Need to wipe data when unlock status changed |
| 3275 | * type 0: oem unlock |
| 3276 | * type 1: unlock critical |
| 3277 | * status 0: unlock as false |
| 3278 | * status 1: lock as true |
| 3279 | */ |
| 3280 | void set_device_unlock_value(int type, bool status) |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 3281 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3282 | if (type == UNLOCK) |
| 3283 | device.is_unlocked = status; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3284 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3285 | else if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3286 | type == UNLOCK_CRITICAL) |
| 3287 | device.is_unlock_critical = status; |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3288 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3289 | write_device_info(&device); |
| 3290 | } |
| 3291 | |
| 3292 | static void set_device_unlock(int type, bool status) |
| 3293 | { |
| 3294 | int is_unlocked = -1; |
| 3295 | char response[MAX_RSP_SIZE]; |
| 3296 | |
| 3297 | /* check device unlock status if it is as expected */ |
| 3298 | if (type == UNLOCK) |
| 3299 | is_unlocked = device.is_unlocked; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3300 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3301 | if(VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3302 | type == UNLOCK_CRITICAL) |
| 3303 | { |
| 3304 | is_unlocked = device.is_unlock_critical; |
| 3305 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 3306 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3307 | if (is_unlocked == status) { |
| 3308 | snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!")); |
| 3309 | fastboot_info(response); |
| 3310 | fastboot_okay(""); |
| 3311 | return; |
| 3312 | } |
| 3313 | |
| 3314 | /* status is true, it means to unlock device */ |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 3315 | if (status && !is_allow_unlock) { |
| 3316 | fastboot_fail("oem unlock is not allowed"); |
| 3317 | return; |
| 3318 | } |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 3319 | |
ericlin | 8ef2adf | 2020-04-29 14:14:24 +0800 | [diff] [blame] | 3320 | //<2018/01/09-EricLin, To remove display_unlock_menu for quiet mode. |
| 3321 | if(target_build_variant_user()){ |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3322 | #if FBCON_DISPLAY_MSG |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 3323 | display_unlock_menu(type, status); |
| 3324 | fastboot_okay(""); |
| 3325 | return; |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3326 | #else |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 3327 | if (status && type == UNLOCK) { |
| 3328 | fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'"); |
| 3329 | return; |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 3330 | } |
lijuang | 07c5d6e | 2018-04-23 18:32:13 +0800 | [diff] [blame] | 3331 | #endif |
ericlin | 8ef2adf | 2020-04-29 14:14:24 +0800 | [diff] [blame] | 3332 | } |
| 3333 | //<2018/01/09-EricLin |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3334 | |
| 3335 | set_device_unlock_value(type, status); |
| 3336 | |
| 3337 | /* wipe data */ |
| 3338 | struct recovery_message msg; |
Kishor PK | 60a6821 | 2017-05-08 16:55:57 +0530 | [diff] [blame] | 3339 | memset(&msg, 0, sizeof(msg)); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 3340 | snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data"); |
| 3341 | write_misc(0, &msg, sizeof(msg)); |
| 3342 | |
| 3343 | fastboot_okay(""); |
ericlin | 8ef2adf | 2020-04-29 14:14:24 +0800 | [diff] [blame] | 3344 | //<2018/10/16-EricLin, To remove reset after oem lock/unlock. |
| 3345 | //reboot_device(RECOVERY_MODE); |
| 3346 | //<2018/10/16-EricLin |
lijuang | 21f12f5 | 2015-08-22 16:22:19 +0800 | [diff] [blame] | 3347 | } |
| 3348 | |
tracychui | 8fd71dc | 2020-06-11 10:17:13 +0800 | [diff] [blame] | 3349 | /*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start*/ |
| 3350 | #if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK) |
| 3351 | static void set_device_unlock_skip_ui_check(int type, bool status) |
| 3352 | { |
| 3353 | int is_unlocked = -1; |
| 3354 | char response[MAX_RSP_SIZE]; |
| 3355 | |
| 3356 | /* check device unlock status if it is as expected */ |
| 3357 | if (type == UNLOCK) |
| 3358 | is_unlocked = device.is_unlocked; |
| 3359 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
| 3360 | if(VB_M <= target_get_vb_version() && |
| 3361 | type == UNLOCK_CRITICAL) |
| 3362 | { |
| 3363 | is_unlocked = device.is_unlock_critical; |
| 3364 | } |
| 3365 | #endif |
| 3366 | if (is_unlocked == status) { |
| 3367 | snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!")); |
| 3368 | fastboot_info(response); |
| 3369 | fastboot_okay(""); |
| 3370 | return; |
| 3371 | } |
| 3372 | |
| 3373 | set_device_unlock_value(type, status); |
| 3374 | fastboot_okay(""); |
| 3375 | } |
| 3376 | #endif |
tracychui | 3b4abb7 | 2020-06-15 10:50:49 +0800 | [diff] [blame^] | 3377 | /*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */ |
tracychui | 8fd71dc | 2020-06-11 10:17:13 +0800 | [diff] [blame] | 3378 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 3379 | static bool critical_flash_allowed(const char * entry) |
| 3380 | { |
| 3381 | uint32_t i = 0; |
| 3382 | if (entry == NULL) |
| 3383 | return false; |
| 3384 | |
| 3385 | for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) { |
| 3386 | if(!strcmp(entry, critical_flash_allowed_ptn[i])) |
| 3387 | return true; |
| 3388 | } |
| 3389 | return false; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | #if DEVICE_TREE |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3393 | int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset) |
| 3394 | { |
| 3395 | uint32 dt_image_offset = 0; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3396 | uint32_t n; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3397 | struct dt_table *table = NULL; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3398 | struct dt_entry dt_entry; |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3399 | uint32_t dt_hdr_size = 0; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3400 | unsigned int compressed_size = 0; |
| 3401 | unsigned int dtb_size = 0; |
| 3402 | unsigned int out_avai_len = 0; |
| 3403 | unsigned char *out_addr = NULL; |
| 3404 | unsigned char *best_match_dt_addr = NULL; |
| 3405 | int rc; |
| 3406 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 3407 | boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start); |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3408 | |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 3409 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 3410 | dt_size = hdr->dt_size; |
| 3411 | #endif |
| 3412 | |
| 3413 | if(dt_size != 0) { |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3414 | /* add kernel offset */ |
| 3415 | dt_image_offset += page_size; |
| 3416 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 3417 | dt_image_offset += n; |
| 3418 | |
| 3419 | /* add ramdisk offset */ |
| 3420 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 3421 | dt_image_offset += n; |
| 3422 | |
| 3423 | /* add second offset */ |
| 3424 | if(hdr->second_size != 0) { |
| 3425 | n = ROUND_TO_PAGE(hdr->second_size, page_mask); |
| 3426 | dt_image_offset += n; |
| 3427 | } |
| 3428 | |
| 3429 | /* offset now point to start of dt.img */ |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 3430 | table = (struct dt_table*)(boot_image_start + dt_image_offset); |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3431 | |
Deepa Dinamani | 19648b4 | 2013-09-05 17:05:55 -0700 | [diff] [blame] | 3432 | if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) { |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3433 | dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n"); |
| 3434 | return -1; |
| 3435 | } |
P.V. Phani Kumar | 8291676 | 2016-03-09 09:20:19 +0530 | [diff] [blame] | 3436 | |
| 3437 | /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header) |
| 3438 | goes beyound hdr->dt_size*/ |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 3439 | 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] | 3440 | dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n"); |
| 3441 | return -1; |
| 3442 | } |
| 3443 | |
Joel King | aa335dc | 2013-06-03 16:11:08 -0700 | [diff] [blame] | 3444 | /* Find index of device tree within device tree table */ |
| 3445 | if(dev_tree_get_entry_info(table, &dt_entry) != 0){ |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3446 | dprintf(CRITICAL, "ERROR: Getting device tree address failed\n"); |
| 3447 | return -1; |
| 3448 | } |
| 3449 | |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3450 | best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset; |
| 3451 | if (is_gzip_package(best_match_dt_addr, dt_entry.size)) |
| 3452 | { |
| 3453 | out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset; |
| 3454 | out_avai_len = target_get_max_flash_size() - scratch_offset; |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3455 | dprintf(INFO, "decompressing dtb: start\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3456 | rc = decompress(best_match_dt_addr, |
| 3457 | dt_entry.size, out_addr, out_avai_len, |
| 3458 | &compressed_size, &dtb_size); |
| 3459 | if (rc) |
| 3460 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3461 | dprintf(CRITICAL, "decompressing dtb failed!!!\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3462 | ASSERT(0); |
| 3463 | } |
| 3464 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3465 | dprintf(INFO, "decompressing dtb: done\n"); |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3466 | best_match_dt_addr = out_addr; |
| 3467 | } else { |
| 3468 | dtb_size = dt_entry.size; |
| 3469 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3470 | /* Validate and Read device device tree in the "tags_add */ |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3471 | if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) || |
| 3472 | check_ddr_addr_range_bound(hdr->tags_addr, dtb_size)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3473 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3474 | dprintf(CRITICAL, "Device tree addresses are not valid.\n"); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3475 | return -1; |
| 3476 | } |
| 3477 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3478 | /* Read device device tree in the "tags_add */ |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3479 | memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3480 | } else |
| 3481 | return -1; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3482 | |
| 3483 | /* Everything looks fine. Return success. */ |
| 3484 | return 0; |
| 3485 | } |
| 3486 | #endif |
| 3487 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3488 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 3489 | { |
| 3490 | unsigned kernel_actual; |
| 3491 | unsigned ramdisk_actual; |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 3492 | unsigned second_actual; |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3493 | uint32_t image_actual; |
| 3494 | uint32_t dt_actual = 0; |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 3495 | boot_img_hdr *hdr = NULL; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3496 | struct kernel64_hdr *kptr = NULL; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3497 | char *ptr = ((char*) data); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3498 | int ret = 0; |
| 3499 | uint8_t dtb_copied = 0; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3500 | unsigned int out_len = 0; |
| 3501 | unsigned int out_avai_len = 0; |
| 3502 | unsigned char *out_addr = NULL; |
| 3503 | uint32_t dtb_offset = 0; |
| 3504 | unsigned char *kernel_start_addr = NULL; |
| 3505 | unsigned int kernel_size = 0; |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3506 | unsigned int scratch_offset = 0; |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3507 | #if VERIFIED_BOOT_2 |
| 3508 | void *dtbo_image_buf = NULL; |
| 3509 | uint32_t dtbo_image_sz = 0; |
| 3510 | void *vbmeta_image_buf = NULL; |
| 3511 | uint32_t vbmeta_image_sz = 0; |
| 3512 | #endif |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3513 | #if !VERIFIED_BOOT_2 |
| 3514 | uint32_t sig_actual = 0; |
| 3515 | uint32_t sig_size = 0; |
| 3516 | #ifdef MDTP_SUPPORT |
| 3517 | static bool is_mdtp_activated = 0; |
| 3518 | #endif /* MDTP_SUPPORT */ |
| 3519 | #endif |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3520 | |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3521 | #if FBCON_DISPLAY_MSG |
| 3522 | /* Exit keys' detection thread firstly */ |
| 3523 | exit_menu_keys_detection(); |
| 3524 | #endif |
| 3525 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3526 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Channagoud Kadabi | 6d5375e | 2015-06-23 17:15:42 -0700 | [diff] [blame] | 3527 | if(target_build_variant_user() && !device.is_unlocked) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3528 | { |
| 3529 | fastboot_fail("unlock device to use this command"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3530 | goto boot_failed; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3531 | } |
| 3532 | #endif |
| 3533 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3534 | if (sz < sizeof(hdr)) { |
| 3535 | fastboot_fail("invalid bootimage header"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3536 | goto boot_failed; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3537 | } |
| 3538 | |
Mayank Grover | e1ab96c | 2018-09-04 20:31:31 +0530 | [diff] [blame] | 3539 | hdr = (boot_img_hdr *)data; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3540 | |
| 3541 | /* ensure commandline is terminated */ |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3542 | hdr->cmdline[BOOT_ARGS_SIZE-1] = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3543 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3544 | if(target_is_emmc_boot() && hdr->page_size) { |
| 3545 | page_size = hdr->page_size; |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 3546 | page_mask = page_size - 1; |
| 3547 | } |
| 3548 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3549 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 3550 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 3551 | second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask); |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3552 | #if DEVICE_TREE |
Parth Dixit | 4097b62 | 2016-03-15 14:42:27 +0530 | [diff] [blame] | 3553 | #ifndef OSVERSION_IN_BOOTIMAGE |
| 3554 | dt_size = hdr->dt_size; |
| 3555 | #endif |
| 3556 | dt_actual = ROUND_TO_PAGE(dt_size, page_mask); |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3557 | #endif |
| 3558 | |
| 3559 | image_actual = ADD_OF(page_size, kernel_actual); |
| 3560 | image_actual = ADD_OF(image_actual, ramdisk_actual); |
Kishor PK | d8ddcad | 2017-07-27 13:53:57 +0530 | [diff] [blame] | 3561 | image_actual = ADD_OF(image_actual, second_actual); |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3562 | image_actual = ADD_OF(image_actual, dt_actual); |
| 3563 | |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3564 | /* Checking to prevent oob access in read_der_message_length */ |
| 3565 | if (image_actual > sz) { |
| 3566 | fastboot_fail("bootimage header fields are invalid"); |
| 3567 | goto boot_failed; |
| 3568 | } |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3569 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3570 | #if VERIFIED_BOOT_2 |
Mayank Grover | 1ceaee2 | 2019-04-01 17:54:32 +0530 | [diff] [blame] | 3571 | /* Pass size of boot partition, as imgsize, to avoid |
| 3572 | read fewer bytes error */ |
| 3573 | image_actual = partition_get_size(partition_get_index("boot")); |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3574 | |
| 3575 | /* load and validate dtbo partition */ |
| 3576 | load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz); |
| 3577 | |
| 3578 | /* load vbmeta partition */ |
| 3579 | load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz); |
| 3580 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3581 | memset(&info, 0, sizeof(bootinfo)); |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3582 | |
| 3583 | info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data); |
| 3584 | info.images[IMG_BOOT].imgsize = image_actual; |
| 3585 | info.images[IMG_BOOT].name = "boot"; |
| 3586 | ++info.num_loaded_images; |
| 3587 | |
| 3588 | /* Pass loaded dtbo image */ |
| 3589 | if (dtbo_image_buf != NULL) { |
| 3590 | info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf); |
| 3591 | info.images[IMG_DTBO].imgsize = dtbo_image_sz; |
| 3592 | info.images[IMG_DTBO].name = "dtbo"; |
| 3593 | ++info.num_loaded_images; |
| 3594 | } |
| 3595 | |
| 3596 | /* Pass loaded vbmeta image */ |
| 3597 | if (vbmeta_image_buf != NULL) { |
| 3598 | info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf; |
| 3599 | info.images[IMG_VBMETA].imgsize = vbmeta_image_sz; |
| 3600 | info.images[IMG_VBMETA].name = "vbmeta"; |
| 3601 | ++info.num_loaded_images; |
| 3602 | } |
| 3603 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3604 | info.multi_slot_boot = partition_multislot_is_supported(); |
| 3605 | if (load_image_and_auth(&info)) |
| 3606 | goto boot_failed; |
| 3607 | vbcmdline = info.vbcmdline; |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3608 | |
| 3609 | /* Free the buffer allocated to vbmeta post verification */ |
| 3610 | if (vbmeta_image_buf != NULL) { |
| 3611 | free(vbmeta_image_buf); |
| 3612 | --info.num_loaded_images; |
| 3613 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3614 | #else |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3615 | sig_size = sz - image_actual; |
| 3616 | |
P.V. Phani Kumar | eecfd82 | 2016-03-09 20:09:03 +0530 | [diff] [blame] | 3617 | if (target_use_signed_kernel() && (!device.is_unlocked)) { |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3618 | unsigned chk; |
P.V. Phani Kumar | eecfd82 | 2016-03-09 20:09:03 +0530 | [diff] [blame] | 3619 | /* Calculate the signature length from boot image */ |
| 3620 | sig_actual = read_der_message_length( |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3621 | (unsigned char*)(data + image_actual), sig_size); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3622 | chk = ADD_OF(image_actual, sig_actual); |
Channagoud Kadabi | 3d83901 | 2014-06-26 14:26:39 -0700 | [diff] [blame] | 3623 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3624 | if (chk > sz) { |
Kishor PK | 5134b33 | 2017-05-09 17:50:08 +0530 | [diff] [blame] | 3625 | fastboot_fail("bootimage header fields are invalid"); |
| 3626 | goto boot_failed; |
| 3627 | } |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3628 | } |
| 3629 | |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 3630 | // Initialize boot state before trying to verify boot.img |
| 3631 | #if VERIFIED_BOOT |
Channagoud Kadabi | 699466e | 2015-11-03 12:37:42 -0800 | [diff] [blame] | 3632 | boot_verifier_init(); |
Mayank Grover | b337e93 | 2017-01-18 20:00:40 +0530 | [diff] [blame] | 3633 | #endif |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 3634 | /* Handle overflow if the input image size is greater than |
| 3635 | * boot image buffer can hold |
| 3636 | */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3637 | if ((target_get_max_flash_size() - page_size) < image_actual) |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 3638 | { |
| 3639 | fastboot_fail("booimage: size is greater than boot image buffer can hold"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3640 | goto boot_failed; |
Channagoud Kadabi | bf6ce7d | 2015-09-17 13:25:35 -0700 | [diff] [blame] | 3641 | } |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 3642 | |
Channagoud Kadabi | fc3b31f | 2014-06-18 17:41:01 -0700 | [diff] [blame] | 3643 | /* Verify the boot image |
| 3644 | * device & page_size are initialized in aboot_init |
| 3645 | */ |
Sridhar Parasuram | 7ae9fbc | 2015-09-17 09:56:30 -0700 | [diff] [blame] | 3646 | if (target_use_signed_kernel() && (!device.is_unlocked)) { |
Channagoud Kadabi | 3d83901 | 2014-06-26 14:26:39 -0700 | [diff] [blame] | 3647 | /* Pass size excluding signature size, otherwise we would try to |
| 3648 | * access signature beyond its length |
| 3649 | */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3650 | verify_signed_bootimg((uint32_t)data, image_actual); |
Sridhar Parasuram | 7ae9fbc | 2015-09-17 09:56:30 -0700 | [diff] [blame] | 3651 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3652 | #ifdef MDTP_SUPPORT |
| 3653 | else |
| 3654 | { |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3655 | /* fastboot boot is not allowed when MDTP is activated */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3656 | mdtp_ext_partition_verification_t ext_partition; |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 3657 | |
| 3658 | if (!is_mdtp_activated) { |
| 3659 | ext_partition.partition = MDTP_PARTITION_NONE; |
| 3660 | mdtp_fwlock_verify_lock(&ext_partition); |
| 3661 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3662 | } |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3663 | |
Amir Kotzer | 7c768c0 | 2016-04-13 09:08:05 +0300 | [diff] [blame] | 3664 | /* If mdtp state cannot be validate, block fastboot boot*/ |
| 3665 | if(mdtp_activated(&is_mdtp_activated)){ |
| 3666 | dprintf(CRITICAL, "mdtp_activated cannot validate state.\n"); |
| 3667 | dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n"); |
| 3668 | goto boot_failed; |
| 3669 | } |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3670 | if(is_mdtp_activated){ |
| 3671 | dprintf(CRITICAL, "fastboot boot command is not available.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3672 | goto boot_failed; |
Shay Nachmani | 062aa3f | 2015-05-17 17:28:44 +0300 | [diff] [blame] | 3673 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3674 | #endif /* MDTP_SUPPORT */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3675 | #endif /* VERIFIED_BOOT_2 else */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 3676 | |
Sridhar Parasuram | 361e4cd | 2015-09-24 11:19:19 -0700 | [diff] [blame] | 3677 | #if VERIFIED_BOOT |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3678 | if (VB_M == target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3679 | { |
| 3680 | /* set boot and system versions. */ |
| 3681 | set_os_version((unsigned char *)data); |
| 3682 | // send root of trust |
| 3683 | if(!send_rot_command((uint32_t)device.is_unlocked)) |
| 3684 | ASSERT(0); |
| 3685 | } |
Parth Dixit | bc9b649 | 2015-10-18 00:41:38 +0530 | [diff] [blame] | 3686 | #endif |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 3687 | /* |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3688 | * Check if the kernel image is a gzip package. If yes, need to decompress it. |
| 3689 | * If not, continue booting. |
| 3690 | */ |
| 3691 | if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size)) |
| 3692 | { |
| 3693 | out_addr = (unsigned char *)target_get_scratch_address(); |
| 3694 | out_addr = (unsigned char *)(out_addr + image_actual + page_size); |
| 3695 | out_avai_len = target_get_max_flash_size() - image_actual - page_size; |
Saranya Chidura | b493333 | 2018-10-15 17:30:06 +0530 | [diff] [blame] | 3696 | #if VERIFIED_BOOT_2 |
| 3697 | if (dtbo_image_sz) |
| 3698 | out_avai_len -= DTBO_IMG_BUF; |
| 3699 | #endif |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3700 | dprintf(INFO, "decompressing kernel image: start\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3701 | ret = decompress((unsigned char *)(ptr + page_size), |
| 3702 | hdr->kernel_size, out_addr, out_avai_len, |
| 3703 | &dtb_offset, &out_len); |
| 3704 | if (ret) |
| 3705 | { |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3706 | dprintf(CRITICAL, "decompressing image failed!!!\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3707 | ASSERT(0); |
| 3708 | } |
| 3709 | |
Matthew Qin | 0b15b32 | 2015-05-19 05:20:54 -0400 | [diff] [blame] | 3710 | dprintf(INFO, "decompressing kernel image: done\n"); |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3711 | kptr = (struct kernel64_hdr *)out_addr; |
| 3712 | kernel_start_addr = out_addr; |
| 3713 | kernel_size = out_len; |
| 3714 | } else { |
| 3715 | kptr = (struct kernel64_hdr*)((char *)data + page_size); |
| 3716 | kernel_start_addr = (unsigned char *)((char *)data + page_size); |
| 3717 | kernel_size = hdr->kernel_size; |
| 3718 | } |
| 3719 | |
| 3720 | /* |
Channagoud Kadabi | a22144f | 2013-03-20 11:49:01 -0700 | [diff] [blame] | 3721 | * Update the kernel/ramdisk/tags address if the boot image header |
| 3722 | * has default values, these default values come from mkbootimg when |
| 3723 | * the boot image is flashed using fastboot flash:raw |
| 3724 | */ |
Abhimanyu Kapur | 0f79d57 | 2014-02-19 22:03:02 -0800 | [diff] [blame] | 3725 | update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr)); |
Dima Zavin | 3cadfff | 2013-03-21 14:30:48 -0700 | [diff] [blame] | 3726 | |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3727 | /* Get virtual addresses since the hdr saves physical addresses. */ |
| 3728 | hdr->kernel_addr = VA(hdr->kernel_addr); |
| 3729 | hdr->ramdisk_addr = VA(hdr->ramdisk_addr); |
| 3730 | hdr->tags_addr = VA(hdr->tags_addr); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3731 | |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3732 | kernel_size = ROUND_TO_PAGE(kernel_size, page_mask); |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3733 | /* Check if the addresses in the header are valid. */ |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3734 | if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) || |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3735 | check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) || |
| 3736 | check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) || |
| 3737 | check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3738 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3739 | dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3740 | goto boot_failed; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3741 | } |
| 3742 | |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3743 | #if DEVICE_TREE |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3744 | scratch_offset = image_actual + page_size + out_len; |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3745 | /* find correct dtb and copy it to right location */ |
Matthew Qin | 271927e | 2015-03-31 22:07:07 -0400 | [diff] [blame] | 3746 | ret = copy_dtb(data, scratch_offset); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3747 | |
| 3748 | dtb_copied = !ret ? 1 : 0; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3749 | #else |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3750 | if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) || |
| 3751 | check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE)) |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3752 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3753 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3754 | goto boot_failed; |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 3755 | } |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3756 | #endif |
| 3757 | |
| 3758 | /* Load ramdisk & kernel */ |
| 3759 | 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] | 3760 | memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3761 | |
| 3762 | #if DEVICE_TREE |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3763 | if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) || |
| 3764 | check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual)) |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3765 | { |
Mayank Grover | e559cec | 2017-10-17 15:12:03 +0530 | [diff] [blame] | 3766 | dprintf(CRITICAL, "Tags addresses are not valid.\n"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3767 | goto boot_failed; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3768 | } |
| 3769 | |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3770 | /* |
| 3771 | * If dtb is not found look for appended DTB in the kernel. |
| 3772 | * If appended dev tree is found, update the atags with |
| 3773 | * memory address to the DTB appended location on RAM. |
| 3774 | * Else update with the atags address in the kernel header |
| 3775 | */ |
| 3776 | if (!dtb_copied) { |
| 3777 | void *dtb; |
Matthew Qin | bb7923d | 2015-02-09 10:56:09 +0800 | [diff] [blame] | 3778 | dtb = dev_tree_appended((void*)(ptr + page_size), |
| 3779 | hdr->kernel_size, dtb_offset, |
Dima Zavin | e63e557 | 2013-05-03 12:23:06 -0700 | [diff] [blame] | 3780 | (void *)hdr->tags_addr); |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3781 | if (!dtb) { |
| 3782 | fastboot_fail("dtb not found"); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3783 | goto boot_failed; |
Channagoud Kadabi | 8e9020f | 2013-04-16 11:24:32 -0700 | [diff] [blame] | 3784 | } |
Amol Jadi | cb52407 | 2012-08-09 16:40:18 -0700 | [diff] [blame] | 3785 | } |
| 3786 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3787 | |
| 3788 | fastboot_okay(""); |
Amol Jadi | 7c4316c | 2013-10-07 14:19:26 -0700 | [diff] [blame] | 3789 | fastboot_stop(); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3790 | |
Dima Zavin | 77e41f3 | 2013-03-06 16:10:43 -0800 | [diff] [blame] | 3791 | boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr, |
Deepa Dinamani | 0bf2f44 | 2012-10-19 11:41:06 -0700 | [diff] [blame] | 3792 | (const char*) hdr->cmdline, board_machtype(), |
| 3793 | (void*) hdr->ramdisk_addr, hdr->ramdisk_size); |
lijuang | 2008ff2 | 2016-03-07 17:56:27 +0800 | [diff] [blame] | 3794 | |
| 3795 | /* fastboot already stop, it's no need to show fastboot menu */ |
| 3796 | return; |
| 3797 | boot_failed: |
| 3798 | #if FBCON_DISPLAY_MSG |
| 3799 | /* revert to fastboot menu if boot failed */ |
| 3800 | display_fastboot_menu(); |
| 3801 | #endif |
| 3802 | return; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 3803 | } |
| 3804 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3805 | void cmd_erase_nand(const char *arg, void *data, unsigned sz) |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3806 | { |
| 3807 | struct ptentry *ptn; |
| 3808 | struct ptable *ptable; |
| 3809 | |
| 3810 | ptable = flash_get_ptable(); |
| 3811 | if (ptable == NULL) { |
| 3812 | fastboot_fail("partition table doesn't exist"); |
| 3813 | return; |
| 3814 | } |
| 3815 | |
| 3816 | ptn = ptable_find(ptable, arg); |
| 3817 | if (ptn == NULL) { |
| 3818 | fastboot_fail("unknown partition name"); |
| 3819 | return; |
| 3820 | } |
| 3821 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3822 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 3823 | dprintf(INFO, "erasing avb_custom_key\n"); |
| 3824 | if (erase_userkey()) { |
| 3825 | fastboot_fail("Erasing avb_custom_key failed"); |
| 3826 | } else { |
| 3827 | fastboot_okay(""); |
| 3828 | } |
| 3829 | return; |
| 3830 | } |
| 3831 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 3832 | if (flash_erase(ptn)) { |
| 3833 | fastboot_fail("failed to erase partition"); |
| 3834 | return; |
| 3835 | } |
| 3836 | fastboot_okay(""); |
| 3837 | } |
| 3838 | |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3839 | |
| 3840 | void cmd_erase_mmc(const char *arg, void *data, unsigned sz) |
| 3841 | { |
| 3842 | unsigned long long ptn = 0; |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 3843 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3844 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3845 | uint8_t lun = 0; |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 3846 | char *footer = NULL; |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3847 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 3848 | #if VERIFIED_BOOT |
| 3849 | if(!strcmp(arg, KEYSTORE_PTN_NAME)) |
| 3850 | { |
| 3851 | if(!device.is_unlocked) |
| 3852 | { |
| 3853 | fastboot_fail("unlock device to erase keystore"); |
| 3854 | return; |
| 3855 | } |
| 3856 | } |
| 3857 | #endif |
| 3858 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3859 | index = partition_get_index(arg); |
| 3860 | ptn = partition_get_offset(index); |
Oliver Wang | cee448d | 2013-10-22 18:40:13 +0800 | [diff] [blame] | 3861 | size = partition_get_size(index); |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 3862 | |
Monika Singh | c4778b7 | 2018-05-16 11:16:42 +0530 | [diff] [blame] | 3863 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 3864 | dprintf(INFO, "erasing avb_custom_key\n"); |
| 3865 | if (erase_userkey()) { |
| 3866 | fastboot_fail("Erasing avb_custom_key failed"); |
| 3867 | } else { |
| 3868 | fastboot_okay(""); |
| 3869 | } |
| 3870 | return; |
| 3871 | } |
| 3872 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 3873 | if(ptn == 0) { |
Neeti Desai | ca8c960 | 2011-10-06 11:40:00 -0700 | [diff] [blame] | 3874 | fastboot_fail("Partition table doesn't exist\n"); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3875 | return; |
| 3876 | } |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 3877 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 3878 | lun = partition_get_lun(index); |
| 3879 | mmc_set_lun(lun); |
| 3880 | |
Sridhar Parasuram | d795712 | 2015-02-27 11:33:40 -0800 | [diff] [blame] | 3881 | if (platform_boot_dev_isemmc()) |
| 3882 | { |
| 3883 | if (mmc_erase_card(ptn, size)) { |
| 3884 | fastboot_fail("failed to erase partition\n"); |
| 3885 | return; |
| 3886 | } |
| 3887 | } else { |
| 3888 | BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE); |
| 3889 | size = partition_get_size(index); |
| 3890 | if (size > DEFAULT_ERASE_SIZE) |
| 3891 | size = DEFAULT_ERASE_SIZE; |
Kun Liang | 2f1601a | 2013-08-12 16:29:54 +0800 | [diff] [blame] | 3892 | |
Sridhar Parasuram | d795712 | 2015-02-27 11:33:40 -0800 | [diff] [blame] | 3893 | /* Simple inefficient version of erase. Just writing |
| 3894 | 0 in first several blocks */ |
| 3895 | if (mmc_write(ptn , size, (unsigned int *)out)) { |
| 3896 | fastboot_fail("failed to erase partition"); |
| 3897 | return; |
| 3898 | } |
Vijay Kumar Pendoti | 0b21f46 | 2016-05-02 17:09:18 +0530 | [diff] [blame] | 3899 | /*Erase FDE metadata at the userdata footer*/ |
| 3900 | if(!(strncmp(arg, "userdata", 8))) |
| 3901 | { |
| 3902 | footer = memalign(CACHE_LINE, FOOTER_SIZE); |
| 3903 | memset((void *)footer, 0, FOOTER_SIZE); |
| 3904 | |
| 3905 | size = partition_get_size(index); |
| 3906 | |
| 3907 | if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) { |
| 3908 | fastboot_fail("failed to erase userdata footer"); |
| 3909 | free(footer); |
| 3910 | return; |
| 3911 | } |
| 3912 | free(footer); |
| 3913 | } |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3914 | } |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3915 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 3916 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 3917 | !(strncmp(arg, "userdata", 8)) && |
| 3918 | send_delete_keys_to_tz()) |
Sridhar Parasuram | 32b3066 | 2015-07-10 13:33:22 -0700 | [diff] [blame] | 3919 | ASSERT(0); |
| 3920 | #endif |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3921 | fastboot_okay(""); |
| 3922 | } |
| 3923 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3924 | void cmd_erase(const char *arg, void *data, unsigned sz) |
| 3925 | { |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 3926 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 3927 | if (target_build_variant_user()) |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 3928 | { |
Sridhar Parasuram | c32d07d | 2015-07-12 10:57:48 -0700 | [diff] [blame] | 3929 | if(!device.is_unlocked) |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 3930 | { |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 3931 | fastboot_fail("device is locked. Cannot erase"); |
Channagoud Kadabi | ce3cf42 | 2015-04-17 16:02:30 -0700 | [diff] [blame] | 3932 | return; |
| 3933 | } |
| 3934 | } |
| 3935 | #endif |
| 3936 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 3937 | if(target_is_emmc_boot()) |
| 3938 | cmd_erase_mmc(arg, data, sz); |
| 3939 | else |
| 3940 | cmd_erase_nand(arg, data, sz); |
| 3941 | } |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 3942 | |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 3943 | /* Get the size from partiton name */ |
| 3944 | static void get_partition_size(const char *arg, char *response) |
| 3945 | { |
| 3946 | uint64_t ptn = 0; |
| 3947 | uint64_t size; |
| 3948 | int index = INVALID_PTN; |
| 3949 | |
| 3950 | index = partition_get_index(arg); |
| 3951 | |
| 3952 | if (index == INVALID_PTN) |
| 3953 | { |
| 3954 | dprintf(CRITICAL, "Invalid partition index\n"); |
| 3955 | return; |
| 3956 | } |
| 3957 | |
| 3958 | ptn = partition_get_offset(index); |
| 3959 | |
| 3960 | if(!ptn) |
| 3961 | { |
| 3962 | dprintf(CRITICAL, "Invalid partition name %s\n", arg); |
| 3963 | return; |
| 3964 | } |
| 3965 | |
| 3966 | size = partition_get_size(index); |
| 3967 | |
| 3968 | snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size); |
| 3969 | return; |
| 3970 | } |
| 3971 | |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3972 | /* Function to check partition type of a partition*/ |
| 3973 | static fs_signature_type |
| 3974 | check_partition_fs_signature(const char *arg) |
| 3975 | { |
| 3976 | fs_signature_type ret = NO_FS; |
| 3977 | int index; |
| 3978 | unsigned long long ptn; |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3979 | char *buffer = memalign(CACHE_LINE, mmc_blocksize); |
| 3980 | uint32_t sb_blk_offset = 0; |
| 3981 | char *sb_buffer = buffer; |
| 3982 | |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 3983 | if (!sb_buffer) |
| 3984 | { |
| 3985 | dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n"); |
| 3986 | goto out; |
| 3987 | } |
| 3988 | |
| 3989 | /* Read super block */ |
| 3990 | if ((index = partition_get_index(arg)) < 0) |
| 3991 | { |
| 3992 | dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg); |
| 3993 | goto out; |
| 3994 | } |
| 3995 | ptn = partition_get_offset(index); |
| 3996 | mmc_set_lun(partition_get_lun(index)); |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 3997 | sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize); |
| 3998 | |
| 3999 | if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize), |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4000 | (void *)sb_buffer, mmc_blocksize)) |
| 4001 | { |
| 4002 | dprintf(CRITICAL, "ERROR: Failed to read Superblock\n"); |
| 4003 | goto out; |
| 4004 | } |
| 4005 | |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 4006 | if (sb_blk_offset == 0) |
| 4007 | sb_buffer += FS_SUPERBLOCK_OFFSET; |
| 4008 | |
| 4009 | if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC) |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4010 | { |
| 4011 | dprintf(SPEW, "%s() Found EXT FS\n", arg); |
| 4012 | ret = EXT_FS_SIGNATURE; |
| 4013 | } |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 4014 | else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC) |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4015 | { |
| 4016 | dprintf(SPEW, "%s() Found F2FS FS\n", arg); |
| 4017 | ret = EXT_F2FS_SIGNATURE; |
| 4018 | } |
| 4019 | else |
| 4020 | { |
| 4021 | dprintf(SPEW, "%s() Reverting to default 0x%x\n", |
| 4022 | arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB]))); |
| 4023 | ret = NO_FS; |
| 4024 | } |
| 4025 | |
| 4026 | out: |
Mayank Grover | 399826a | 2018-08-27 12:15:15 +0530 | [diff] [blame] | 4027 | if(buffer) |
| 4028 | free(buffer); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4029 | return ret; |
| 4030 | } |
| 4031 | |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 4032 | /* Function to get partition type */ |
| 4033 | static void get_partition_type(const char *arg, char *response) |
| 4034 | { |
| 4035 | uint n = 0; |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4036 | fs_signature_type fs_signature; |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 4037 | |
| 4038 | if (arg == NULL || |
| 4039 | response == NULL) |
| 4040 | { |
| 4041 | dprintf(CRITICAL, "Invalid input parameter\n"); |
| 4042 | return; |
| 4043 | } |
| 4044 | |
| 4045 | /* By default copy raw to response */ |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 4046 | strlcpy(response, RAW_STR, MAX_RSP_SIZE); |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 4047 | |
| 4048 | /* Mark partiton type for known paritions only */ |
| 4049 | for (n=0; n < ARRAY_SIZE(part_type_known); n++) |
| 4050 | { |
| 4051 | if (!strncmp(part_type_known[n].part_name, arg, strlen(arg))) |
| 4052 | { |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4053 | /* Check partition for FS signature */ |
| 4054 | fs_signature = check_partition_fs_signature(arg); |
| 4055 | switch (fs_signature) |
| 4056 | { |
| 4057 | case EXT_FS_SIGNATURE: |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 4058 | strlcpy(response, EXT_STR, MAX_RSP_SIZE); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4059 | break; |
| 4060 | case EXT_F2FS_SIGNATURE: |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 4061 | strlcpy(response, F2FS_STR, MAX_RSP_SIZE); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4062 | break; |
| 4063 | case NO_FS: |
Mayank Grover | 0e55904 | 2018-04-11 17:49:30 +0530 | [diff] [blame] | 4064 | strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE); |
Mayank Grover | 52cd10a | 2018-03-15 12:57:54 +0530 | [diff] [blame] | 4065 | } |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 4066 | } |
| 4067 | } |
| 4068 | return; |
| 4069 | } |
| 4070 | |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4071 | /* |
| 4072 | * Publish the partition type & size info |
| 4073 | * fastboot getvar will publish the required information. |
| 4074 | * fastboot getvar partition_size:<partition_name>: partition size in hex |
| 4075 | * fastboot getvar partition_type:<partition_name>: partition type (ext/fat) |
| 4076 | */ |
| 4077 | static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts) |
| 4078 | { |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 4079 | uint8_t i; |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4080 | static bool published = false; |
| 4081 | struct partition_entry *ptn_entry = |
| 4082 | partition_get_partition_entries(); |
| 4083 | memset(info, 0, sizeof(struct getvar_partition_info)* num_parts); |
| 4084 | |
| 4085 | for (i = 0; i < num_parts; i++) { |
| 4086 | strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE); |
| 4087 | strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE); |
| 4088 | strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE); |
| 4089 | |
Mayank Grover | d38fe01 | 2018-03-13 15:33:16 +0530 | [diff] [blame] | 4090 | get_partition_type(info[i].part_name, info[i].type_response); |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4091 | get_partition_size(info[i].part_name, info[i].size_response); |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4092 | if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE) |
| 4093 | { |
| 4094 | dprintf(CRITICAL, "partition size name truncated\n"); |
| 4095 | return; |
| 4096 | } |
| 4097 | if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE) |
| 4098 | { |
| 4099 | dprintf(CRITICAL, "partition type name truncated\n"); |
| 4100 | return; |
| 4101 | } |
| 4102 | |
| 4103 | if (!published) |
| 4104 | { |
| 4105 | /* publish partition size & type info */ |
| 4106 | fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response); |
| 4107 | fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response); |
| 4108 | } |
| 4109 | } |
| 4110 | if (!published) |
| 4111 | published = true; |
| 4112 | } |
| 4113 | |
| 4114 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4115 | void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz) |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 4116 | { |
| 4117 | unsigned long long ptn = 0; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 4118 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 4119 | int index = INVALID_PTN; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4120 | char *token = NULL; |
| 4121 | char *pname = NULL; |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 4122 | char *sp; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4123 | uint8_t lun = 0; |
| 4124 | bool lun_set = false; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4125 | int current_active_slot = INVALID; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 4126 | |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 4127 | token = strtok_r((char *)arg, ":", &sp); |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4128 | pname = token; |
Sridhar Parasuram | efc133f | 2015-05-04 13:35:41 -0700 | [diff] [blame] | 4129 | token = strtok_r(NULL, ":", &sp); |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4130 | if(token) |
| 4131 | { |
| 4132 | lun = atoi(token); |
| 4133 | mmc_set_lun(lun); |
| 4134 | lun_set = true; |
| 4135 | } |
| 4136 | |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4137 | if (pname) |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 4138 | { |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 4139 | if (!strncmp(pname, "frp-unlock", strlen("frp-unlock"))) |
| 4140 | { |
| 4141 | if (!aboot_frp_unlock(pname, data, sz)) |
| 4142 | { |
| 4143 | fastboot_info("FRP unlock successful"); |
| 4144 | fastboot_okay(""); |
| 4145 | } |
| 4146 | else |
| 4147 | fastboot_fail("Secret key is invalid, please update the bootloader with secret key"); |
| 4148 | |
| 4149 | return; |
| 4150 | } |
| 4151 | |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4152 | if (!strcmp(pname, "partition")) |
| 4153 | { |
| 4154 | dprintf(INFO, "Attempt to write partition image.\n"); |
| 4155 | if (write_partition(sz, (unsigned char *) data)) { |
| 4156 | fastboot_fail("failed to write partition"); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 4157 | return; |
| 4158 | } |
Mayank Grover | 11ff969 | 2018-01-11 11:54:49 +0530 | [diff] [blame] | 4159 | /* Re-publish partition table */ |
| 4160 | publish_getvar_partition_info(part_info, partition_get_partition_count()); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4161 | |
| 4162 | /* Rescan partition table to ensure we have multislot support*/ |
| 4163 | if (partition_scan_for_multislot()) |
| 4164 | { |
| 4165 | current_active_slot = partition_find_active_slot(); |
| 4166 | dprintf(INFO, "Multislot supported: Slot %s active", |
| 4167 | (SUFFIX_SLOT(current_active_slot))); |
| 4168 | } |
| 4169 | partition_mark_active_slot(current_active_slot); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 4170 | } |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4171 | else |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4172 | { |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4173 | #if VERIFIED_BOOT |
| 4174 | if(!strcmp(pname, KEYSTORE_PTN_NAME)) |
| 4175 | { |
| 4176 | if(!device.is_unlocked) |
| 4177 | { |
| 4178 | fastboot_fail("unlock device to flash keystore"); |
| 4179 | return; |
| 4180 | } |
Gaurav Nebhwani | c7313cc | 2015-12-15 22:25:04 +0530 | [diff] [blame] | 4181 | if(!boot_verify_validate_keystore((unsigned char *)data,sz)) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4182 | { |
| 4183 | fastboot_fail("image is not a keystore file"); |
| 4184 | return; |
| 4185 | } |
| 4186 | } |
| 4187 | #endif |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4188 | index = partition_get_index(pname); |
| 4189 | ptn = partition_get_offset(index); |
| 4190 | if(ptn == 0) { |
| 4191 | fastboot_fail("partition table doesn't exist"); |
| 4192 | return; |
| 4193 | } |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4194 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4195 | if (!strncmp(pname, "boot", strlen("boot")) |
| 4196 | || !strcmp(pname, "recovery")) |
| 4197 | { |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4198 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 4199 | fastboot_fail("image is not a boot image"); |
| 4200 | return; |
| 4201 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4202 | |
| 4203 | /* Reset multislot_partition attributes in case of flashing boot */ |
| 4204 | if (partition_multislot_is_supported()) |
| 4205 | { |
| 4206 | partition_reset_attributes(index); |
| 4207 | } |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | if(!lun_set) |
| 4211 | { |
| 4212 | lun = partition_get_lun(index); |
| 4213 | mmc_set_lun(lun); |
| 4214 | } |
| 4215 | |
| 4216 | size = partition_get_size(index); |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 4217 | if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) { |
Mao Jinlong | 226f33a | 2014-07-04 17:24:10 +0800 | [diff] [blame] | 4218 | fastboot_fail("size too large"); |
| 4219 | return; |
| 4220 | } |
| 4221 | else if (mmc_write(ptn , sz, (unsigned int *)data)) { |
| 4222 | fastboot_fail("flash write failure"); |
| 4223 | return; |
| 4224 | } |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 4225 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 4226 | } |
| 4227 | fastboot_okay(""); |
| 4228 | return; |
| 4229 | } |
| 4230 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4231 | void cmd_flash_meta_img(const char *arg, void *data, unsigned sz) |
| 4232 | { |
| 4233 | int i, images; |
| 4234 | meta_header_t *meta_header; |
| 4235 | img_header_entry_t *img_header_entry; |
Parth Dixit | 3e2d303 | 2016-03-04 17:11:52 +0530 | [diff] [blame] | 4236 | /*End of the image address*/ |
| 4237 | uintptr_t data_end; |
| 4238 | |
| 4239 | if( (UINT_MAX - sz) > (uintptr_t)data ) |
| 4240 | data_end = (uintptr_t)data + sz; |
| 4241 | else |
| 4242 | { |
| 4243 | fastboot_fail("Cannot flash: image header corrupt"); |
| 4244 | return; |
| 4245 | } |
| 4246 | |
| 4247 | if( data_end < ((uintptr_t)data + sizeof(meta_header_t))) |
| 4248 | { |
| 4249 | fastboot_fail("Cannot flash: image header corrupt"); |
| 4250 | return; |
| 4251 | } |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4252 | |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 4253 | /* If device is locked: |
| 4254 | * Forbid to flash image to avoid the device to bypass the image |
| 4255 | * which with "any" name other than bootloader. Because it maybe |
| 4256 | * a meta package of all partitions. |
| 4257 | */ |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4258 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 4259 | if (target_build_variant_user()) { |
| 4260 | if (!device.is_unlocked) { |
| 4261 | fastboot_fail("Device is locked, meta image flashing is not allowed"); |
| 4262 | return; |
| 4263 | } |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4264 | |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 4265 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4266 | !device.is_unlock_critical) |
| 4267 | { |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 4268 | fastboot_fail("Device is critical locked, Meta image flashing is not allowed"); |
| 4269 | return; |
| 4270 | } |
lijuang | 8ee2188 | 2016-04-19 16:57:11 +0800 | [diff] [blame] | 4271 | } |
| 4272 | #endif |
| 4273 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4274 | meta_header = (meta_header_t*) data; |
Parth Dixit | 3e2d303 | 2016-03-04 17:11:52 +0530 | [diff] [blame] | 4275 | if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz)) |
| 4276 | { |
| 4277 | fastboot_fail("Cannot flash: image header corrupt"); |
| 4278 | return; |
| 4279 | } |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4280 | img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t)); |
| 4281 | |
| 4282 | images = meta_header->img_hdr_sz / sizeof(img_header_entry_t); |
| 4283 | |
| 4284 | for (i=0; i<images; i++) { |
| 4285 | |
| 4286 | if((img_header_entry[i].ptn_name == NULL) || |
| 4287 | (img_header_entry[i].start_offset == 0) || |
| 4288 | (img_header_entry[i].size == 0)) |
| 4289 | break; |
Kishor PK | 4983150 | 2017-04-21 17:55:43 +0530 | [diff] [blame] | 4290 | if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) { |
| 4291 | fastboot_fail("Integer overflow detected in start_offset of img"); |
| 4292 | break; |
| 4293 | } |
| 4294 | else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) { |
| 4295 | fastboot_fail("Integer overflow detected in size of img"); |
| 4296 | break; |
| 4297 | } |
Parth Dixit | 3e2d303 | 2016-03-04 17:11:52 +0530 | [diff] [blame] | 4298 | if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset |
| 4299 | + img_header_entry[i].size) ) |
| 4300 | { |
| 4301 | fastboot_fail("Cannot flash: image size mismatch"); |
| 4302 | break; |
| 4303 | } |
| 4304 | |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4305 | cmd_flash_mmc_img(img_header_entry[i].ptn_name, |
| 4306 | (void *) data + img_header_entry[i].start_offset, |
| 4307 | img_header_entry[i].size); |
| 4308 | } |
| 4309 | |
Channagoud Kadabi | aafbbfc | 2015-03-04 17:52:56 -0800 | [diff] [blame] | 4310 | if (!strncmp(arg, "bootloader", strlen("bootloader"))) |
| 4311 | { |
| 4312 | strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN); |
| 4313 | strlcat(device.bootloader_version, "-", MAX_VERSION_LEN); |
| 4314 | strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN); |
| 4315 | } |
| 4316 | else |
| 4317 | { |
| 4318 | strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN); |
| 4319 | strlcat(device.radio_version, "-", MAX_VERSION_LEN); |
| 4320 | strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN); |
| 4321 | } |
| 4322 | |
| 4323 | write_device_info(&device); |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4324 | fastboot_okay(""); |
| 4325 | return; |
| 4326 | } |
| 4327 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4328 | void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz) |
| 4329 | { |
| 4330 | unsigned int chunk; |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4331 | uint64_t chunk_data_sz; |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4332 | uint32_t *fill_buf = NULL; |
| 4333 | uint32_t fill_val; |
Mayank Grover | 4f50bba | 2017-07-19 18:17:08 +0530 | [diff] [blame] | 4334 | uint32_t blk_sz_actual = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4335 | sparse_header_t *sparse_header; |
| 4336 | chunk_header_t *chunk_header; |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 4337 | uint32_t total_blocks = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4338 | unsigned long long ptn = 0; |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 4339 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 4340 | int index = INVALID_PTN; |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 4341 | uint32_t i; |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4342 | uint8_t lun = 0; |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4343 | /*End of the sparse image address*/ |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4344 | uintptr_t data_end = (uintptr_t)data + sz; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4345 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 4346 | index = partition_get_index(arg); |
| 4347 | ptn = partition_get_offset(index); |
| 4348 | if(ptn == 0) { |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4349 | fastboot_fail("partition table doesn't exist"); |
| 4350 | return; |
| 4351 | } |
| 4352 | |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 4353 | size = partition_get_size(index); |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame] | 4354 | |
Sundarajan Srinivasan | d3514bb | 2013-12-04 17:02:00 -0800 | [diff] [blame] | 4355 | lun = partition_get_lun(index); |
| 4356 | mmc_set_lun(lun); |
| 4357 | |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4358 | if (sz < sizeof(sparse_header_t)) { |
| 4359 | fastboot_fail("size too low"); |
| 4360 | return; |
| 4361 | } |
| 4362 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4363 | /* Read and skip over sparse image header */ |
| 4364 | sparse_header = (sparse_header_t *) data; |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4365 | |
Mayank Grover | 48bd9bb | 2017-07-19 12:04:16 +0530 | [diff] [blame] | 4366 | if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){ |
| 4367 | fastboot_fail("Invalid block size\n"); |
| 4368 | return; |
| 4369 | } |
| 4370 | |
vijay kumar | 1321f34 | 2015-03-27 12:13:42 +0530 | [diff] [blame] | 4371 | 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] | 4372 | fastboot_fail("size too large"); |
| 4373 | return; |
| 4374 | } |
| 4375 | |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 4376 | data += sizeof(sparse_header_t); |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4377 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4378 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4379 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 4380 | return; |
| 4381 | } |
| 4382 | |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 4383 | if(sparse_header->file_hdr_sz != sizeof(sparse_header_t)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4384 | { |
vijay kumar | de4fcf6 | 2015-04-23 13:05:49 +0530 | [diff] [blame] | 4385 | fastboot_fail("sparse header size mismatch"); |
| 4386 | return; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4387 | } |
| 4388 | |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 4389 | dprintf (SPEW, "=== Sparse Image Header ===\n"); |
| 4390 | dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic); |
| 4391 | dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version); |
| 4392 | dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version); |
| 4393 | dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz); |
| 4394 | dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz); |
| 4395 | dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz); |
| 4396 | dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks); |
| 4397 | dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4398 | |
| 4399 | /* Start processing chunks */ |
| 4400 | for (chunk=0; chunk<sparse_header->total_chunks; chunk++) |
| 4401 | { |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4402 | /* Make sure the total image size does not exceed the partition size */ |
| 4403 | if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) { |
| 4404 | fastboot_fail("size too large"); |
| 4405 | return; |
| 4406 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4407 | /* Read and skip over chunk header */ |
| 4408 | chunk_header = (chunk_header_t *) data; |
| 4409 | data += sizeof(chunk_header_t); |
| 4410 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4411 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4412 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 4413 | return; |
| 4414 | } |
| 4415 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4416 | dprintf (SPEW, "=== Chunk Header ===\n"); |
| 4417 | dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type); |
| 4418 | dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz); |
| 4419 | dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz); |
| 4420 | |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4421 | if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4422 | { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4423 | fastboot_fail("chunk header size mismatch"); |
| 4424 | return; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4427 | chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz; |
| 4428 | |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4429 | /* Make sure that the chunk size calculated from sparse image does not |
| 4430 | * exceed partition size |
| 4431 | */ |
| 4432 | if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size) |
| 4433 | { |
| 4434 | fastboot_fail("Chunk data size exceeds partition size"); |
| 4435 | return; |
| 4436 | } |
| 4437 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4438 | switch (chunk_header->chunk_type) |
| 4439 | { |
| 4440 | case CHUNK_TYPE_RAW: |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4441 | 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] | 4442 | chunk_data_sz)) |
| 4443 | { |
| 4444 | fastboot_fail("Bogus chunk size for chunk type Raw"); |
| 4445 | return; |
| 4446 | } |
| 4447 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4448 | if (data_end < (uintptr_t)data + chunk_data_sz) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4449 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 4450 | return; |
| 4451 | } |
| 4452 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4453 | /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32 |
| 4454 | otherwise it will return in the line above |
| 4455 | */ |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 4456 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4457 | (uint32_t)chunk_data_sz, |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 4458 | (unsigned int*)data)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4459 | { |
| 4460 | fastboot_fail("flash write failure"); |
| 4461 | return; |
| 4462 | } |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4463 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 4464 | fastboot_fail("Bogus size for RAW chunk type"); |
| 4465 | return; |
| 4466 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4467 | total_blocks += chunk_header->chunk_sz; |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4468 | data += (uint32_t)chunk_data_sz; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4469 | break; |
| 4470 | |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4471 | case CHUNK_TYPE_FILL: |
| 4472 | if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz + |
| 4473 | sizeof(uint32_t))) |
| 4474 | { |
| 4475 | fastboot_fail("Bogus chunk size for chunk type FILL"); |
| 4476 | return; |
| 4477 | } |
| 4478 | |
Mayank Grover | 4f50bba | 2017-07-19 18:17:08 +0530 | [diff] [blame] | 4479 | blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE); |
| 4480 | /* Integer overflow detected */ |
| 4481 | if (blk_sz_actual < sparse_header->blk_sz) |
| 4482 | { |
| 4483 | fastboot_fail("Invalid block size"); |
| 4484 | return; |
| 4485 | } |
| 4486 | |
| 4487 | fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual); |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4488 | if (!fill_buf) |
| 4489 | { |
| 4490 | fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL"); |
| 4491 | return; |
| 4492 | } |
| 4493 | |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4494 | if (data_end < (uintptr_t)data + sizeof(uint32_t)) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4495 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
Gaurav Nebhwani | 163cbf8 | 2016-03-17 21:21:27 +0530 | [diff] [blame] | 4496 | free(fill_buf); |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4497 | return; |
| 4498 | } |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4499 | fill_val = *(uint32_t *)data; |
| 4500 | data = (char *) data + sizeof(uint32_t); |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4501 | |
| 4502 | for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++) |
| 4503 | { |
| 4504 | fill_buf[i] = fill_val; |
| 4505 | } |
| 4506 | |
Gaurav Nebhwani | e94cbe1 | 2016-03-17 21:16:34 +0530 | [diff] [blame] | 4507 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) |
| 4508 | { |
| 4509 | fastboot_fail("bogus size for chunk FILL type"); |
| 4510 | free(fill_buf); |
| 4511 | return; |
| 4512 | } |
| 4513 | |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4514 | for (i = 0; i < chunk_header->chunk_sz; i++) |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4515 | { |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4516 | /* Make sure that the data written to partition does not exceed partition size */ |
| 4517 | if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size) |
| 4518 | { |
| 4519 | fastboot_fail("Chunk data size for fill type exceeds partition size"); |
Gaurav Nebhwani | 163cbf8 | 2016-03-17 21:21:27 +0530 | [diff] [blame] | 4520 | free(fill_buf); |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4521 | return; |
| 4522 | } |
| 4523 | |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4524 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
| 4525 | sparse_header->blk_sz, |
| 4526 | fill_buf)) |
| 4527 | { |
| 4528 | fastboot_fail("flash write failure"); |
| 4529 | free(fill_buf); |
| 4530 | return; |
| 4531 | } |
| 4532 | |
| 4533 | total_blocks++; |
| 4534 | } |
| 4535 | |
| 4536 | free(fill_buf); |
| 4537 | break; |
| 4538 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4539 | case CHUNK_TYPE_DONT_CARE: |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4540 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 4541 | fastboot_fail("bogus size for chunk DONT CARE type"); |
| 4542 | return; |
| 4543 | } |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 4544 | total_blocks += chunk_header->chunk_sz; |
| 4545 | break; |
| 4546 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4547 | case CHUNK_TYPE_CRC: |
| 4548 | if(chunk_header->total_sz != sparse_header->chunk_hdr_sz) |
| 4549 | { |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4550 | fastboot_fail("Bogus chunk size for chunk type CRC"); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4551 | return; |
| 4552 | } |
Aparna Mallavarapu | 2ae741a | 2015-03-28 01:41:08 +0530 | [diff] [blame] | 4553 | if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) { |
| 4554 | fastboot_fail("bogus size for chunk CRC type"); |
| 4555 | return; |
| 4556 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4557 | total_blocks += chunk_header->chunk_sz; |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4558 | if ((uintptr_t)data > UINT_MAX - chunk_data_sz) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4559 | fastboot_fail("integer overflow occured"); |
| 4560 | return; |
| 4561 | } |
wufeng.jiang | 813dc35 | 2015-06-02 23:02:46 -0400 | [diff] [blame] | 4562 | data += (uint32_t)chunk_data_sz; |
Parth Dixit | 64b1a48 | 2016-03-07 16:31:26 +0530 | [diff] [blame] | 4563 | if (data_end < (uintptr_t)data) { |
vijay kumar | 800255e | 2015-04-24 20:26:19 +0530 | [diff] [blame] | 4564 | fastboot_fail("buffer overreads occured due to invalid sparse header"); |
| 4565 | return; |
| 4566 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4567 | break; |
| 4568 | |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 4569 | default: |
Channagoud Kadabi | f627cf8 | 2013-09-09 14:08:20 -0700 | [diff] [blame] | 4570 | dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4571 | fastboot_fail("Unknown chunk type"); |
| 4572 | return; |
| 4573 | } |
| 4574 | } |
| 4575 | |
Ajay Dudani | 0c6927b | 2011-05-18 11:12:16 -0700 | [diff] [blame] | 4576 | dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n", |
| 4577 | total_blocks, sparse_header->total_blks); |
| 4578 | |
| 4579 | if(total_blocks != sparse_header->total_blks) |
| 4580 | { |
| 4581 | fastboot_fail("sparse image write failure"); |
| 4582 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4583 | |
| 4584 | fastboot_okay(""); |
| 4585 | return; |
| 4586 | } |
| 4587 | |
| 4588 | void cmd_flash_mmc(const char *arg, void *data, unsigned sz) |
| 4589 | { |
| 4590 | sparse_header_t *sparse_header; |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4591 | meta_header_t *meta_header; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4592 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 4593 | #ifdef SSD_ENABLE |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 4594 | /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */ |
| 4595 | unsigned int *magic_number = (unsigned int *) data; |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4596 | int ret=0; |
| 4597 | uint32 major_version=0; |
| 4598 | uint32 minor_version=0; |
| 4599 | |
| 4600 | ret = scm_svc_version(&major_version,&minor_version); |
| 4601 | if(!ret) |
| 4602 | { |
| 4603 | if(major_version >= 2) |
| 4604 | { |
Amir Samuelov | bb65ce0 | 2013-05-05 12:20:18 +0300 | [diff] [blame] | 4605 | if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") ) |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4606 | { |
| 4607 | ret = encrypt_scm((uint32 **) &data, &sz); |
| 4608 | if (ret != 0) { |
| 4609 | dprintf(CRITICAL, "ERROR: Encryption Failure\n"); |
| 4610 | return; |
| 4611 | } |
| 4612 | |
Amir Samuelov | bb65ce0 | 2013-05-05 12:20:18 +0300 | [diff] [blame] | 4613 | /* Protect only for SSD */ |
| 4614 | if (!strcmp(arg, "ssd")) { |
| 4615 | ret = scm_protect_keystore((uint32 *) data, sz); |
| 4616 | if (ret != 0) { |
| 4617 | dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n"); |
| 4618 | return; |
| 4619 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4620 | } |
| 4621 | } |
| 4622 | else |
| 4623 | { |
| 4624 | ret = decrypt_scm_v2((uint32 **) &data, &sz); |
| 4625 | if(ret != 0) |
| 4626 | { |
| 4627 | dprintf(CRITICAL,"ERROR: Decryption Failure\n"); |
| 4628 | return; |
| 4629 | } |
| 4630 | } |
| 4631 | } |
| 4632 | else |
| 4633 | { |
| 4634 | if (magic_number[0] == DECRYPT_MAGIC_0 && |
| 4635 | magic_number[1] == DECRYPT_MAGIC_1) |
| 4636 | { |
| 4637 | ret = decrypt_scm((uint32 **) &data, &sz); |
| 4638 | if (ret != 0) { |
| 4639 | dprintf(CRITICAL, "ERROR: Invalid secure image\n"); |
| 4640 | return; |
| 4641 | } |
| 4642 | } |
| 4643 | else if (magic_number[0] == ENCRYPT_MAGIC_0 && |
| 4644 | magic_number[1] == ENCRYPT_MAGIC_1) |
| 4645 | { |
| 4646 | ret = encrypt_scm((uint32 **) &data, &sz); |
| 4647 | if (ret != 0) { |
| 4648 | dprintf(CRITICAL, "ERROR: Encryption Failure\n"); |
| 4649 | return; |
| 4650 | } |
| 4651 | } |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 4652 | } |
| 4653 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4654 | else |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 4655 | { |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4656 | dprintf(CRITICAL,"INVALID SVC Version\n"); |
| 4657 | return; |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 4658 | } |
sundarajan srinivasan | d4b1c48 | 2013-02-27 14:15:43 -0800 | [diff] [blame] | 4659 | #endif /* SSD_ENABLE */ |
Neeti Desai | 127b9e0 | 2012-03-20 16:11:23 -0700 | [diff] [blame] | 4660 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4661 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Channagoud Kadabi | 3529767 | 2015-06-13 11:09:49 -0700 | [diff] [blame] | 4662 | if (target_build_variant_user()) |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4663 | { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4664 | /* if device is locked: |
| 4665 | * common partition will not allow to be flashed |
| 4666 | * critical partition will allow to flash image. |
| 4667 | */ |
| 4668 | if(!device.is_unlocked && !critical_flash_allowed(arg)) { |
| 4669 | fastboot_fail("Partition flashing is not allowed"); |
| 4670 | return; |
| 4671 | } |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4672 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 4673 | /* if device critical is locked: |
| 4674 | * common partition will allow to be flashed |
| 4675 | * critical partition will not allow to flash image. |
| 4676 | */ |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 4677 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4678 | !device.is_unlock_critical && |
| 4679 | critical_flash_allowed(arg)) { |
| 4680 | fastboot_fail("Critical partition flashing is not allowed"); |
| 4681 | return; |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4682 | } |
| 4683 | } |
| 4684 | #endif |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4685 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 4686 | dprintf(INFO, "flashing avb_custom_key\n"); |
| 4687 | if (store_userkey(data, sz)) { |
| 4688 | fastboot_fail("Flashing avb_custom_key failed"); |
| 4689 | } else { |
| 4690 | fastboot_okay(""); |
| 4691 | } |
| 4692 | return; |
| 4693 | } |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4694 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 4695 | sparse_header = (sparse_header_t *) data; |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4696 | meta_header = (meta_header_t *) data; |
| 4697 | if (sparse_header->magic == SPARSE_HEADER_MAGIC) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4698 | cmd_flash_mmc_sparse_img(arg, data, sz); |
Ajay Dudani | de98479 | 2015-03-02 09:57:41 -0800 | [diff] [blame] | 4699 | else if (meta_header->magic == META_HEADER_MAGIC) |
| 4700 | cmd_flash_meta_img(arg, data, sz); |
| 4701 | else |
| 4702 | cmd_flash_mmc_img(arg, data, sz); |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 4703 | |
| 4704 | #if VERIFIED_BOOT |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 4705 | if (VB_M <= target_get_vb_version() && |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 4706 | (!strncmp(arg, "system", 6)) && |
| 4707 | !device.verity_mode) |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 4708 | // reset dm_verity mode to enforcing |
| 4709 | device.verity_mode = 1; |
| 4710 | write_device_info(&device); |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 4711 | #endif |
Sridhar Parasuram | d69f790 | 2015-07-10 13:31:17 -0700 | [diff] [blame] | 4712 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 4713 | return; |
| 4714 | } |
| 4715 | |
Tanya Brokhman | 84eaaf6 | 2015-02-24 21:57:06 +0200 | [diff] [blame] | 4716 | void cmd_updatevol(const char *vol_name, void *data, unsigned sz) |
| 4717 | { |
| 4718 | struct ptentry *sys_ptn; |
| 4719 | struct ptable *ptable; |
| 4720 | |
| 4721 | ptable = flash_get_ptable(); |
| 4722 | if (ptable == NULL) { |
| 4723 | fastboot_fail("partition table doesn't exist"); |
| 4724 | return; |
| 4725 | } |
| 4726 | |
| 4727 | sys_ptn = ptable_find(ptable, "system"); |
| 4728 | if (sys_ptn == NULL) { |
| 4729 | fastboot_fail("system partition not found"); |
| 4730 | return; |
| 4731 | } |
| 4732 | |
| 4733 | sz = ROUND_TO_PAGE(sz, page_mask); |
| 4734 | if (update_ubi_vol(sys_ptn, vol_name, data, sz)) |
| 4735 | fastboot_fail("update_ubi_vol failed"); |
| 4736 | else |
| 4737 | fastboot_okay(""); |
| 4738 | } |
| 4739 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 4740 | void cmd_flash_nand(const char *arg, void *data, unsigned sz) |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4741 | { |
| 4742 | struct ptentry *ptn; |
| 4743 | struct ptable *ptable; |
| 4744 | unsigned extra = 0; |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 4745 | uint64_t partition_size = 0; |
Vijay Kumar Pendoti | c6f22a7 | 2017-05-19 22:44:34 +0530 | [diff] [blame] | 4746 | unsigned bytes_to_round_page = 0; |
| 4747 | unsigned rounded_size = 0; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4748 | |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4749 | if((uintptr_t)data > (UINT_MAX - sz)) { |
| 4750 | fastboot_fail("Cannot flash: image header corrupt"); |
| 4751 | return; |
| 4752 | } |
| 4753 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4754 | ptable = flash_get_ptable(); |
| 4755 | if (ptable == NULL) { |
| 4756 | fastboot_fail("partition table doesn't exist"); |
| 4757 | return; |
| 4758 | } |
| 4759 | |
| 4760 | ptn = ptable_find(ptable, arg); |
| 4761 | if (ptn == NULL) { |
Tanya Brokhman | 84eaaf6 | 2015-02-24 21:57:06 +0200 | [diff] [blame] | 4762 | dprintf(INFO, "unknown partition name (%s). Trying updatevol\n", |
| 4763 | arg); |
| 4764 | cmd_updatevol(arg, data, sz); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4765 | return; |
| 4766 | } |
| 4767 | |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 4768 | if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) { |
| 4769 | dprintf(INFO, "flashing avb_custom_key\n"); |
| 4770 | if (store_userkey(data, sz)) { |
| 4771 | fastboot_fail("Flashing avb_custom_key failed"); |
| 4772 | } else { |
| 4773 | fastboot_okay(""); |
| 4774 | } |
| 4775 | return; |
| 4776 | } |
| 4777 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4778 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4779 | if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) { |
| 4780 | dprintf(INFO, "Verified the BOOT_MAGIC in image header \n"); |
| 4781 | } else { |
| 4782 | fastboot_fail("Image is not a boot image"); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4783 | return; |
| 4784 | } |
| 4785 | } |
| 4786 | |
Amol Jadi | 5c61a95 | 2012-05-04 17:05:35 -0700 | [diff] [blame] | 4787 | if (!strcmp(ptn->name, "system") |
Deepa Dinamani | 13e32c4 | 2012-03-12 14:34:17 -0700 | [diff] [blame] | 4788 | || !strcmp(ptn->name, "userdata") |
| 4789 | || !strcmp(ptn->name, "persist") |
Sundarajan Srinivasan | b063a85 | 2013-11-19 14:02:27 -0800 | [diff] [blame] | 4790 | || !strcmp(ptn->name, "recoveryfs") |
Sundarajan Srinivasan | 595b71e | 2013-11-05 12:44:34 -0800 | [diff] [blame] | 4791 | || !strcmp(ptn->name, "modem")) |
Tanya Brokhman | 1c94f1a | 2015-02-15 09:05:03 +0200 | [diff] [blame] | 4792 | extra = 1; |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4793 | else { |
Vijay Kumar Pendoti | c6f22a7 | 2017-05-19 22:44:34 +0530 | [diff] [blame] | 4794 | rounded_size = ROUNDUP(sz, page_size); |
| 4795 | bytes_to_round_page = rounded_size - sz; |
| 4796 | if (bytes_to_round_page) { |
| 4797 | if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) { |
| 4798 | fastboot_fail("Integer overflow detected"); |
| 4799 | return; |
| 4800 | } |
| 4801 | if (((uintptr_t)data + sz + bytes_to_round_page) > |
| 4802 | ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) { |
| 4803 | fastboot_fail("Buffer size is not aligned to page_size"); |
| 4804 | return; |
| 4805 | } |
| 4806 | else { |
| 4807 | memset(data + sz, 0, bytes_to_round_page); |
| 4808 | sz = rounded_size; |
| 4809 | } |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4810 | } |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 4811 | } |
| 4812 | |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4813 | /*Checking partition_size for the possible integer overflow */ |
| 4814 | partition_size = validate_partition_size(ptn); |
| 4815 | |
Gaurav Nebhwani | d973071 | 2016-05-06 14:28:29 +0530 | [diff] [blame] | 4816 | if (sz > partition_size) { |
| 4817 | fastboot_fail("Image size too large"); |
| 4818 | return; |
| 4819 | } |
| 4820 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4821 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
Mayank Grover | 6cde935 | 2017-06-06 18:45:23 +0530 | [diff] [blame] | 4822 | if ((sz > UBI_EC_HDR_SIZE) && |
| 4823 | (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) { |
Tanya Brokhman | 1c94f1a | 2015-02-15 09:05:03 +0200 | [diff] [blame] | 4824 | if (flash_ubi_img(ptn, data, sz)) { |
| 4825 | fastboot_fail("flash write failure"); |
| 4826 | return; |
| 4827 | } |
| 4828 | } else { |
| 4829 | if (flash_write(ptn, extra, data, sz)) { |
| 4830 | fastboot_fail("flash write failure"); |
| 4831 | return; |
| 4832 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4833 | } |
| 4834 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 4835 | fastboot_okay(""); |
| 4836 | } |
| 4837 | |
Kishor PK | 38ed93d | 2017-04-25 14:19:26 +0530 | [diff] [blame] | 4838 | |
| 4839 | static inline uint64_t validate_partition_size(struct ptentry *ptn) |
| 4840 | { |
| 4841 | if (ptn->length && flash_num_pages_per_blk() && page_size) { |
| 4842 | if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) { |
| 4843 | return ptn->length * flash_num_pages_per_blk() * page_size; |
| 4844 | } |
| 4845 | } |
| 4846 | return 0; |
| 4847 | } |
| 4848 | |
| 4849 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 4850 | void cmd_flash(const char *arg, void *data, unsigned sz) |
| 4851 | { |
| 4852 | if(target_is_emmc_boot()) |
| 4853 | cmd_flash_mmc(arg, data, sz); |
| 4854 | else |
| 4855 | cmd_flash_nand(arg, data, sz); |
| 4856 | } |
| 4857 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4858 | void cmd_continue(const char *arg, void *data, unsigned sz) |
| 4859 | { |
| 4860 | fastboot_okay(""); |
Amol Jadi | 7c4316c | 2013-10-07 14:19:26 -0700 | [diff] [blame] | 4861 | fastboot_stop(); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 4862 | |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4863 | if (target_is_emmc_boot()) |
| 4864 | { |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 4865 | #if FBCON_DISPLAY_MSG |
lijuang | de34d50 | 2016-02-26 16:04:50 +0800 | [diff] [blame] | 4866 | /* Exit keys' detection thread firstly */ |
| 4867 | exit_menu_keys_detection(); |
lijuang | a40d630 | 2015-07-20 20:10:13 +0800 | [diff] [blame] | 4868 | #endif |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4869 | boot_linux_from_mmc(); |
| 4870 | } |
| 4871 | else |
| 4872 | { |
| 4873 | boot_linux_from_flash(); |
| 4874 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 4875 | } |
| 4876 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 4877 | void cmd_reboot(const char *arg, void *data, unsigned sz) |
| 4878 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4879 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 4880 | fastboot_okay(""); |
| 4881 | reboot_device(0); |
| 4882 | } |
| 4883 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4884 | void cmd_set_active(const char *arg, void *data, unsigned sz) |
| 4885 | { |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4886 | char *p, *sp = NULL; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4887 | unsigned i,current_active_slot; |
| 4888 | const char *current_slot_suffix; |
| 4889 | |
| 4890 | if (!partition_multislot_is_supported()) |
| 4891 | { |
| 4892 | fastboot_fail("Command not supported"); |
| 4893 | return; |
| 4894 | } |
| 4895 | |
| 4896 | if (arg) |
| 4897 | { |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4898 | p = strtok_r((char *)arg, ":", &sp); |
Mayank Grover | 5eb5f69 | 2017-07-19 20:16:04 +0530 | [diff] [blame] | 4899 | if (p) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4900 | { |
| 4901 | current_active_slot = partition_find_active_slot(); |
| 4902 | |
| 4903 | /* Check if trying to make curent slot active */ |
| 4904 | current_slot_suffix = SUFFIX_SLOT(current_active_slot); |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4905 | current_slot_suffix = strtok_r((char *)current_slot_suffix, |
| 4906 | (char *)suffix_delimiter, &sp); |
| 4907 | |
Mayank Grover | 5eb5f69 | 2017-07-19 20:16:04 +0530 | [diff] [blame] | 4908 | if (current_slot_suffix && |
| 4909 | !strncmp(p, current_slot_suffix, strlen(current_slot_suffix))) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4910 | { |
| 4911 | fastboot_okay("Slot already set active"); |
| 4912 | return; |
| 4913 | } |
| 4914 | else |
| 4915 | { |
| 4916 | for (i = 0; i < AB_SUPPORTED_SLOTS; i++) |
| 4917 | { |
| 4918 | current_slot_suffix = SUFFIX_SLOT(i); |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 4919 | current_slot_suffix = strtok_r((char *)current_slot_suffix, |
| 4920 | (char *)suffix_delimiter, &sp); |
Mayank Grover | 5eb5f69 | 2017-07-19 20:16:04 +0530 | [diff] [blame] | 4921 | if (current_slot_suffix && |
| 4922 | !strncmp(p, current_slot_suffix, strlen(current_slot_suffix))) |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4923 | { |
lijuang | 8b03e5f | 2019-07-12 18:16:19 +0800 | [diff] [blame] | 4924 | partition_switch_slots(current_active_slot, i, true); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 4925 | publish_getvar_multislot_vars(); |
| 4926 | fastboot_okay(""); |
| 4927 | return; |
| 4928 | } |
| 4929 | } |
| 4930 | } |
| 4931 | } |
| 4932 | } |
| 4933 | fastboot_fail("Invalid slot suffix."); |
| 4934 | return; |
| 4935 | } |
| 4936 | |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 4937 | #if DYNAMIC_PARTITION_SUPPORT |
| 4938 | void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz) |
| 4939 | { |
| 4940 | dprintf(INFO, "rebooting the device - userspace fastboot\n"); |
| 4941 | if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) { |
| 4942 | dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n"); |
| 4943 | fastboot_fail("Failed to update recovery command"); |
| 4944 | return; |
| 4945 | } |
| 4946 | fastboot_okay(""); |
| 4947 | reboot_device(REBOOT_MODE_UNKNOWN); |
| 4948 | |
| 4949 | //shouldn't come here. |
| 4950 | dprintf(CRITICAL, "ERROR: Failed to reboot device\n"); |
| 4951 | return; |
| 4952 | } |
| 4953 | |
| 4954 | void cmd_reboot_recovery(const char *arg, void *data, unsigned sz) |
| 4955 | { |
| 4956 | dprintf(INFO, "rebooting the device - recovery\n"); |
| 4957 | if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) { |
| 4958 | dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n"); |
| 4959 | fastboot_fail("Failed to update recovery command"); |
| 4960 | return; |
| 4961 | } |
| 4962 | fastboot_okay(""); |
| 4963 | reboot_device(REBOOT_MODE_UNKNOWN); |
| 4964 | |
| 4965 | //shouldn't come here. |
| 4966 | dprintf(CRITICAL, "ERROR: Failed to reboot device\n"); |
| 4967 | return; |
| 4968 | } |
| 4969 | #endif |
| 4970 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 4971 | void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz) |
| 4972 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 4973 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 4974 | fastboot_okay(""); |
| 4975 | reboot_device(FASTBOOT_MODE); |
| 4976 | } |
| 4977 | |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 4978 | void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size) |
| 4979 | { |
| 4980 | dprintf(INFO, "Enabling charger screen check\n"); |
| 4981 | device.charger_screen_enabled = 1; |
| 4982 | write_device_info(&device); |
| 4983 | fastboot_okay(""); |
| 4984 | } |
| 4985 | |
| 4986 | void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size) |
| 4987 | { |
| 4988 | dprintf(INFO, "Disabling charger screen check\n"); |
| 4989 | device.charger_screen_enabled = 0; |
| 4990 | write_device_info(&device); |
| 4991 | fastboot_okay(""); |
| 4992 | } |
| 4993 | |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 4994 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 4995 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 4996 | void CmdOemEnableAdb(const char *arg, void *data, unsigned size) |
| 4997 | { |
| 4998 | dprintf(INFO, "Enabling Adb\n"); |
| 4999 | prod.is_adb_enabled = 1; |
| 5000 | write_prod_info(&prod); |
| 5001 | fastboot_okay(""); |
| 5002 | } |
| 5003 | #endif |
| 5004 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
| 5005 | |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5006 | void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size) |
| 5007 | { |
| 5008 | char *p = NULL; |
| 5009 | const char *delim = " \t\n\r"; |
Parth Dixit | 407f15b | 2016-01-07 14:47:37 +0530 | [diff] [blame] | 5010 | char *sp; |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5011 | |
| 5012 | if (arg) { |
Parth Dixit | 407f15b | 2016-01-07 14:47:37 +0530 | [diff] [blame] | 5013 | p = strtok_r((char *)arg, delim, &sp); |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5014 | if (p) { |
| 5015 | if (!strncmp(p, "0", 1)) { |
| 5016 | device.charger_screen_enabled = 0; |
| 5017 | } else if (!strncmp(p, "1", 1)) { |
| 5018 | device.charger_screen_enabled = 1; |
| 5019 | } |
| 5020 | } |
| 5021 | } |
| 5022 | |
| 5023 | /* update charger_screen_enabled value for getvar |
| 5024 | * command |
| 5025 | */ |
| 5026 | snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d", |
| 5027 | device.charger_screen_enabled); |
| 5028 | |
| 5029 | write_device_info(&device); |
| 5030 | fastboot_okay(""); |
| 5031 | } |
| 5032 | |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 5033 | void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size) |
| 5034 | { |
| 5035 | dprintf(INFO, "Selecting display panel %s\n", arg); |
| 5036 | if (arg) |
| 5037 | strlcpy(device.display_panel, arg, |
| 5038 | sizeof(device.display_panel)); |
| 5039 | write_device_info(&device); |
| 5040 | fastboot_okay(""); |
| 5041 | } |
| 5042 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 5043 | void cmd_oem_unlock(const char *arg, void *data, unsigned sz) |
| 5044 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5045 | set_device_unlock(UNLOCK, TRUE); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5046 | } |
| 5047 | |
tracychui | 8fd71dc | 2020-06-11 10:17:13 +0800 | [diff] [blame] | 5048 | /*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start*/ |
| 5049 | #if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK) |
| 5050 | void cmd_oem_unlock_skip_ui_check(const char *arg, void *data, unsigned sz) |
| 5051 | { |
| 5052 | set_device_unlock_skip_ui_check(UNLOCK, TRUE); |
| 5053 | } |
| 5054 | #endif |
| 5055 | /*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */ |
| 5056 | |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5057 | void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz) |
| 5058 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5059 | if(!device.is_unlocked) { |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5060 | if(!is_allow_unlock) { |
| 5061 | fastboot_fail("oem unlock is not allowed"); |
| 5062 | return; |
| 5063 | } |
| 5064 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5065 | set_device_unlock_value(UNLOCK, TRUE); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5066 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5067 | /* wipe data */ |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5068 | struct recovery_message msg; |
Kishor PK | 60a6821 | 2017-05-08 16:55:57 +0530 | [diff] [blame] | 5069 | memset(&msg, 0, sizeof(msg)); |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5070 | snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data"); |
| 5071 | write_misc(0, &msg, sizeof(msg)); |
| 5072 | |
| 5073 | fastboot_okay(""); |
| 5074 | reboot_device(RECOVERY_MODE); |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 5075 | } |
| 5076 | fastboot_okay(""); |
| 5077 | } |
| 5078 | |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 5079 | static int aboot_frp_unlock(char *pname, void *data, unsigned sz) |
| 5080 | { |
Mayank Grover | 70f8b0e | 2017-05-17 11:49:00 +0530 | [diff] [blame] | 5081 | int ret=1; |
| 5082 | bool authentication_success=false; |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 5083 | |
Mayank Grover | 70f8b0e | 2017-05-17 11:49:00 +0530 | [diff] [blame] | 5084 | /* |
| 5085 | Authentication method not implemented. |
| 5086 | |
| 5087 | OEM to implement, authentication system which on successful validataion, |
| 5088 | calls write_allow_oem_unlock() with is_allow_unlock. |
| 5089 | */ |
| 5090 | #if 0 |
| 5091 | authentication_success = oem_specific_auth_mthd(); |
| 5092 | #endif |
| 5093 | |
| 5094 | if (authentication_success) |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 5095 | { |
Mayank Grover | 70f8b0e | 2017-05-17 11:49:00 +0530 | [diff] [blame] | 5096 | is_allow_unlock = true; |
| 5097 | write_allow_oem_unlock(is_allow_unlock); |
| 5098 | ret = 0; |
Channagoud Kadabi | ad25983 | 2015-05-29 11:14:17 -0700 | [diff] [blame] | 5099 | } |
| 5100 | return ret; |
| 5101 | } |
| 5102 | |
Shashank Mittal | d3e54dd | 2014-08-28 15:24:02 -0700 | [diff] [blame] | 5103 | void cmd_oem_lock(const char *arg, void *data, unsigned sz) |
| 5104 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5105 | set_device_unlock(UNLOCK, FALSE); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 5106 | } |
| 5107 | |
tracychui | 8fd71dc | 2020-06-11 10:17:13 +0800 | [diff] [blame] | 5108 | /*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start*/ |
| 5109 | #if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK) |
| 5110 | void cmd_oem_lock_skip_ui_check(const char *arg, void *data, unsigned sz) |
| 5111 | { |
| 5112 | set_device_unlock_skip_ui_check(UNLOCK, FALSE); |
| 5113 | } |
| 5114 | #endif |
| 5115 | /*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */ |
| 5116 | |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5117 | void cmd_oem_devinfo(const char *arg, void *data, unsigned sz) |
| 5118 | { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5119 | char response[MAX_RSP_SIZE]; |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5120 | snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false")); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5121 | fastboot_info(response); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5122 | snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false")); |
| 5123 | fastboot_info(response); |
Monika Singh | 292b3e9 | 2018-03-17 22:40:23 +0530 | [diff] [blame] | 5124 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 5125 | if (VB_M <= target_get_vb_version()) |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5126 | { |
| 5127 | snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", |
| 5128 | (device.is_unlock_critical ? "true" : "false")); |
| 5129 | fastboot_info(response); |
| 5130 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 5131 | #endif |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5132 | 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] | 5133 | fastboot_info(response); |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 5134 | snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel)); |
| 5135 | fastboot_info(response); |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5136 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 5137 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5138 | snprintf(response, sizeof(response), "\tAdb enabled: %s", prod.is_adb_enabled ? "true" : "false"); |
| 5139 | fastboot_info(response); |
| 5140 | #endif |
| 5141 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5142 | fastboot_okay(""); |
| 5143 | } |
| 5144 | |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5145 | void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz) |
| 5146 | { |
| 5147 | char response[MAX_RSP_SIZE]; |
| 5148 | snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock); |
| 5149 | fastboot_info(response); |
| 5150 | fastboot_okay(""); |
| 5151 | } |
| 5152 | |
| 5153 | void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz) |
| 5154 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5155 | set_device_unlock(UNLOCK_CRITICAL, FALSE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5156 | } |
| 5157 | |
| 5158 | void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz) |
| 5159 | { |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5160 | set_device_unlock(UNLOCK_CRITICAL, TRUE); |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5161 | } |
| 5162 | |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 5163 | void cmd_preflash(const char *arg, void *data, unsigned sz) |
| 5164 | { |
| 5165 | fastboot_okay(""); |
| 5166 | } |
| 5167 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5168 | static uint8_t logo_header[LOGO_IMG_HEADER_SIZE]; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5169 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5170 | int splash_screen_check_header(logo_img_header *header) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5171 | { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5172 | if (memcmp(header->magic, LOGO_IMG_MAGIC, 8)) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5173 | return -1; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5174 | if (header->width == 0 || header->height == 0) |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5175 | return -1; |
| 5176 | return 0; |
| 5177 | } |
| 5178 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5179 | int splash_screen_flash() |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5180 | { |
| 5181 | struct ptentry *ptn; |
| 5182 | struct ptable *ptable; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5183 | struct logo_img_header *header; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5184 | struct fbcon_config *fb_display = NULL; |
Channagoud Kadabi | b3ccf5c | 2014-12-03 12:39:29 -0800 | [diff] [blame] | 5185 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5186 | ptable = flash_get_ptable(); |
| 5187 | if (ptable == NULL) { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5188 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 5189 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5190 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5191 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5192 | ptn = ptable_find(ptable, "splash"); |
| 5193 | if (ptn == NULL) { |
| 5194 | dprintf(CRITICAL, "ERROR: splash Partition not found\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5195 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5196 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5197 | if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5198 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5199 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5200 | } |
| 5201 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5202 | header = (struct logo_img_header *)logo_header; |
| 5203 | if (splash_screen_check_header(header)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5204 | dprintf(CRITICAL, "ERROR: Boot image header invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5205 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5206 | } |
| 5207 | |
| 5208 | fb_display = fbcon_display(); |
| 5209 | if (fb_display) { |
raghavendra ambadas | 554f4fd | 2018-10-09 15:50:40 +0530 | [diff] [blame] | 5210 | if (header->type && (header->blocks != 0) && |
| 5211 | (UINT_MAX >= header->blocks * 512) && |
| 5212 | ((header->blocks * 512) <= (fb_display->width * |
| 5213 | fb_display->height * (fb_display->bpp / 8)))) { |
| 5214 | /* RLE24 compressed data */ |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5215 | uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET; |
| 5216 | |
| 5217 | /* if the logo is full-screen size, remove "fbcon_clear()" */ |
| 5218 | if ((header->width != fb_display->width) |
| 5219 | || (header->height != fb_display->height)) |
| 5220 | fbcon_clear(); |
| 5221 | |
| 5222 | if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0, |
| 5223 | (uint32_t *)base, |
| 5224 | (header->blocks * 512))) { |
| 5225 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 5226 | return -1; |
| 5227 | } |
| 5228 | fbcon_extract_to_screen(header, base); |
| 5229 | return 0; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5230 | } |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5231 | |
Veera Sundaram Sankaran | c33416f | 2015-09-28 10:25:55 -0700 | [diff] [blame] | 5232 | if ((header->width > fb_display->width) || (header->height > fb_display->height)) { |
| 5233 | 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] | 5234 | return -1; |
| 5235 | } |
| 5236 | |
Vineet Bajaj | 99291ed | 2014-09-09 12:29:46 +0530 | [diff] [blame] | 5237 | uint8_t *base = (uint8_t *) fb_display->base; |
Sachin Bhayare | 619e9e4 | 2017-05-15 13:10:31 +0530 | [diff] [blame] | 5238 | uint32_t fb_size = ROUNDUP(fb_display->width * |
| 5239 | fb_display->height * |
| 5240 | (fb_display->bpp / 8), 4096); |
| 5241 | uint32_t splash_size = ((((header->width * header->height * |
| 5242 | fb_display->bpp/8) + 511) >> 9) << 9); |
| 5243 | |
| 5244 | if (splash_size > fb_size) { |
| 5245 | dprintf(CRITICAL, "ERROR: Splash image size invalid\n"); |
| 5246 | return -1; |
| 5247 | } |
| 5248 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5249 | if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0, |
| 5250 | (uint32_t *)base, |
| 5251 | ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5252 | fbcon_clear(); |
Vineet Bajaj | 99291ed | 2014-09-09 12:29:46 +0530 | [diff] [blame] | 5253 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5254 | return -1; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5255 | } |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5256 | } |
| 5257 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5258 | return 0; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5259 | } |
| 5260 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5261 | int splash_screen_mmc() |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5262 | { |
| 5263 | int index = INVALID_PTN; |
| 5264 | unsigned long long ptn = 0; |
| 5265 | struct fbcon_config *fb_display = NULL; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5266 | struct logo_img_header *header; |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5267 | uint32_t blocksize, realsize, readsize; |
| 5268 | uint8_t *base; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5269 | |
| 5270 | index = partition_get_index("splash"); |
| 5271 | if (index == 0) { |
| 5272 | dprintf(CRITICAL, "ERROR: splash Partition table not found\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5273 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5274 | } |
| 5275 | |
| 5276 | ptn = partition_get_offset(index); |
| 5277 | if (ptn == 0) { |
| 5278 | dprintf(CRITICAL, "ERROR: splash Partition invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5279 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5280 | } |
| 5281 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5282 | mmc_set_lun(partition_get_lun(index)); |
| 5283 | |
| 5284 | blocksize = mmc_get_device_blocksize(); |
| 5285 | if (blocksize == 0) { |
| 5286 | dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n"); |
| 5287 | return -1; |
| 5288 | } |
| 5289 | |
| 5290 | fb_display = fbcon_display(); |
Channagoud Kadabi | daf10a6 | 2015-07-22 11:51:55 -0700 | [diff] [blame] | 5291 | if (!fb_display) |
| 5292 | { |
| 5293 | dprintf(CRITICAL, "ERROR: fb config is not allocated\n"); |
| 5294 | return -1; |
| 5295 | } |
| 5296 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5297 | base = (uint8_t *) fb_display->base; |
| 5298 | |
jialongjhan | 1efba00 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5299 | //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 Start |
| 5300 | if (mmc_read(ptn , (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5301 | dprintf(CRITICAL, "ERROR: Cannot read splash image header\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5302 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5303 | } |
jialongjhan | 1efba00 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5304 | //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 End |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5305 | header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5306 | if (splash_screen_check_header(header)) { |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5307 | dprintf(CRITICAL, "ERROR: Splash image header invalid\n"); |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5308 | return -1; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5309 | } |
| 5310 | |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5311 | if (fb_display) { |
raghavendra ambadas | 554f4fd | 2018-10-09 15:50:40 +0530 | [diff] [blame] | 5312 | if (header->type && (header->blocks != 0) && |
| 5313 | (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) && |
| 5314 | ((header->blocks * 512) <= (fb_display->width * |
| 5315 | fb_display->height * (fb_display->bpp / 8)))) { |
| 5316 | /* 1 RLE24 compressed data */ |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5317 | base += LOGO_IMG_OFFSET; |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5318 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5319 | realsize = header->blocks * 512; |
| 5320 | readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize; |
| 5321 | |
| 5322 | /* if the logo is not full-screen size, clean screen */ |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5323 | if ((header->width != fb_display->width) |
| 5324 | || (header->height != fb_display->height)) |
| 5325 | fbcon_clear(); |
| 5326 | |
Sachin Bhayare | 619e9e4 | 2017-05-15 13:10:31 +0530 | [diff] [blame] | 5327 | uint32_t fb_size = ROUNDUP(fb_display->width * |
| 5328 | fb_display->height * |
| 5329 | (fb_display->bpp / 8), 4096); |
| 5330 | |
| 5331 | if (readsize > fb_size) { |
| 5332 | dprintf(CRITICAL, "ERROR: Splash image size invalid\n"); |
| 5333 | return -1; |
| 5334 | } |
| 5335 | |
jialongjhan | 1efba00 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5336 | //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 Start |
| 5337 | if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) { |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5338 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 5339 | return -1; |
| 5340 | } |
jialongjhan | 1efba00 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5341 | //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 End |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5342 | |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5343 | fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE)); |
| 5344 | } else { /* 2 Raw BGR data */ |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5345 | |
Veera Sundaram Sankaran | c33416f | 2015-09-28 10:25:55 -0700 | [diff] [blame] | 5346 | if ((header->width > fb_display->width) || (header->height > fb_display->height)) { |
| 5347 | 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] | 5348 | return -1; |
| 5349 | } |
| 5350 | |
| 5351 | realsize = header->width * header->height * fb_display->bpp / 8; |
| 5352 | readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize; |
| 5353 | |
| 5354 | if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */ |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 5355 | 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] | 5356 | fbcon_clear(); |
| 5357 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 5358 | return -1; |
| 5359 | } |
| 5360 | } else { |
Kishor PK | ee5c0a3 | 2018-03-06 16:49:46 +0530 | [diff] [blame] | 5361 | if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize , |
Mao Flynn | 1893a7f | 2015-06-03 12:03:36 +0800 | [diff] [blame] | 5362 | (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) { |
| 5363 | dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n"); |
| 5364 | return -1; |
| 5365 | } |
| 5366 | memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize); |
| 5367 | } |
| 5368 | } |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5369 | } |
| 5370 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5371 | return 0; |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5372 | } |
| 5373 | |
Mao Flynn | 7b379f3 | 2015-04-20 00:28:30 +0800 | [diff] [blame] | 5374 | int fetch_image_from_partition() |
Aparna Mallavarapu | ad8c1ec | 2013-08-23 17:55:47 +0530 | [diff] [blame] | 5375 | { |
| 5376 | if (target_is_emmc_boot()) { |
| 5377 | return splash_screen_mmc(); |
| 5378 | } else { |
| 5379 | return splash_screen_flash(); |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5380 | } |
| 5381 | } |
| 5382 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5383 | void publish_getvar_multislot_vars() |
| 5384 | { |
| 5385 | int i,count; |
| 5386 | static bool published = false; |
| 5387 | static char slot_count[MAX_RSP_SIZE]; |
| 5388 | static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS]; |
| 5389 | static char active_slot_suffix[MAX_RSP_SIZE]; |
| 5390 | static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE]; |
| 5391 | static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE]; |
| 5392 | const char *tmp; |
| 5393 | char tmpbuff[MAX_GET_VAR_NAME_SIZE]; |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5394 | signed active_slt; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5395 | |
| 5396 | if (!published) |
| 5397 | { |
| 5398 | /* Update slot meta info */ |
| 5399 | count = partition_fill_partition_meta(has_slot_pname, has_slot_reply, |
| 5400 | partition_get_partition_count()); |
| 5401 | for(i=0; i<count; i++) |
| 5402 | { |
| 5403 | memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE); |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5404 | snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s", |
| 5405 | has_slot_pname[i]); |
| 5406 | strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5407 | fastboot_publish(has_slot_pname[i], has_slot_reply[i]); |
| 5408 | } |
| 5409 | |
| 5410 | for (i=0; i<AB_SUPPORTED_SLOTS; i++) |
| 5411 | { |
| 5412 | tmp = SUFFIX_SLOT(i); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5413 | tmp++; // to remove "_" from slot_suffix. |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5414 | snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable), |
| 5415 | "slot-unbootable:%s", tmp); |
| 5416 | snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active), |
| 5417 | "slot-active:%s", tmp); |
| 5418 | snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful), |
| 5419 | "slot-success:%s", tmp); |
| 5420 | snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count), |
| 5421 | "slot-retry-count:%s", tmp); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5422 | fastboot_publish(slot_info[i].slot_is_unbootable, |
| 5423 | slot_info[i].slot_is_unbootable_rsp); |
| 5424 | fastboot_publish(slot_info[i].slot_is_active, |
| 5425 | slot_info[i].slot_is_active_rsp); |
| 5426 | fastboot_publish(slot_info[i].slot_is_succesful, |
| 5427 | slot_info[i].slot_is_succesful_rsp); |
| 5428 | fastboot_publish(slot_info[i].slot_retry_count, |
| 5429 | slot_info[i].slot_retry_count_rsp); |
| 5430 | } |
| 5431 | fastboot_publish("current-slot", active_slot_suffix); |
| 5432 | snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS); |
| 5433 | fastboot_publish("slot-count", slot_count); |
| 5434 | published = true; |
| 5435 | } |
| 5436 | |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5437 | active_slt = partition_find_active_slot(); |
| 5438 | if (active_slt != INVALID) |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5439 | { |
| 5440 | tmp = SUFFIX_SLOT(active_slt); |
| 5441 | tmp++; // to remove "_" from slot_suffix. |
| 5442 | snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp); |
| 5443 | } |
Mayank Grover | 2bd84bb | 2017-06-20 11:06:07 +0530 | [diff] [blame] | 5444 | else |
| 5445 | strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix)); |
| 5446 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5447 | /* Update partition meta information */ |
| 5448 | partition_fill_slot_meta(slot_info); |
| 5449 | return; |
| 5450 | } |
| 5451 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5452 | void get_product_name(unsigned char *buf) |
| 5453 | { |
| 5454 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD)); |
| 5455 | return; |
| 5456 | } |
| 5457 | |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5458 | #if PRODUCT_IOT |
| 5459 | void get_bootloader_version_iot(unsigned char *buf) |
| 5460 | { |
| 5461 | if (buf != NULL) |
| 5462 | { |
| 5463 | strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN); |
| 5464 | strlcat(buf, "-", MAX_VERSION_LEN); |
| 5465 | strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN); |
| 5466 | } |
| 5467 | return; |
| 5468 | } |
| 5469 | #endif |
| 5470 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5471 | void get_bootloader_version(unsigned char *buf) |
| 5472 | { |
jhchen | 7a504d1 | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 5473 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5474 | return; |
| 5475 | } |
| 5476 | |
| 5477 | void get_baseband_version(unsigned char *buf) |
| 5478 | { |
jhchen | 7a504d1 | 2020-04-24 15:45:57 +0800 | [diff] [blame] | 5479 | snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5480 | return; |
| 5481 | } |
| 5482 | |
Monika Singh | 32a09f7 | 2018-03-14 13:08:29 +0530 | [diff] [blame] | 5483 | bool is_device_locked_critical() |
| 5484 | { |
| 5485 | return device.is_unlock_critical ? false:true; |
| 5486 | } |
| 5487 | |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5488 | bool is_device_locked() |
| 5489 | { |
| 5490 | return device.is_unlocked ? false:true; |
| 5491 | } |
| 5492 | |
Monika Singh | 32a09f7 | 2018-03-14 13:08:29 +0530 | [diff] [blame] | 5493 | bool is_verity_enforcing() |
| 5494 | { |
| 5495 | return device.verity_mode ? true:false; |
| 5496 | } |
| 5497 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5498 | /* register commands and variables for fastboot */ |
| 5499 | void aboot_fastboot_register_commands(void) |
| 5500 | { |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5501 | int i; |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 5502 | char hw_platform_buf[MAX_RSP_SIZE]; |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5503 | |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5504 | struct fastboot_cmd_desc cmd_list[] = { |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5505 | /* By default the enabled list is empty. */ |
| 5506 | {"", NULL}, |
| 5507 | /* move commands enclosed within the below ifndef to here |
| 5508 | * if they need to be enabled in user build. |
| 5509 | */ |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5510 | #ifndef DISABLE_FASTBOOT_CMDS |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5511 | /* Register the following commands only for non-user builds */ |
| 5512 | {"flash:", cmd_flash}, |
| 5513 | {"erase:", cmd_erase}, |
| 5514 | {"boot", cmd_boot}, |
| 5515 | {"continue", cmd_continue}, |
| 5516 | {"reboot", cmd_reboot}, |
| 5517 | {"reboot-bootloader", cmd_reboot_bootloader}, |
| 5518 | {"oem unlock", cmd_oem_unlock}, |
| 5519 | {"oem unlock-go", cmd_oem_unlock_go}, |
| 5520 | {"oem lock", cmd_oem_lock}, |
tracychui | 8fd71dc | 2020-06-11 10:17:13 +0800 | [diff] [blame] | 5521 | /*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start */ |
| 5522 | #if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK) |
| 5523 | {"oem 8901_unlock", cmd_oem_unlock_skip_ui_check}, |
| 5524 | {"oem 8901_lock", cmd_oem_lock_skip_ui_check}, |
| 5525 | #endif |
| 5526 | /*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */ |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5527 | {"flashing unlock", cmd_oem_unlock}, |
| 5528 | {"flashing lock", cmd_oem_lock}, |
| 5529 | {"flashing lock_critical", cmd_flashing_lock_critical}, |
| 5530 | {"flashing unlock_critical", cmd_flashing_unlock_critical}, |
| 5531 | {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability}, |
| 5532 | {"oem device-info", cmd_oem_devinfo}, |
| 5533 | {"preflash", cmd_preflash}, |
| 5534 | {"oem enable-charger-screen", cmd_oem_enable_charger_screen}, |
| 5535 | {"oem disable-charger-screen", cmd_oem_disable_charger_screen}, |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5536 | {"oem off-mode-charge", cmd_oem_off_mode_charger}, |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5537 | {"oem select-display-panel", cmd_oem_select_display_panel}, |
Mayank Grover | 6ccc1c9 | 2017-07-04 17:36:46 +0530 | [diff] [blame] | 5538 | {"set_active",cmd_set_active}, |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 5539 | #if DYNAMIC_PARTITION_SUPPORT |
| 5540 | {"reboot-fastboot",cmd_reboot_fastboot}, |
| 5541 | {"reboot-recovery",cmd_reboot_recovery}, |
| 5542 | #endif |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 5543 | #if UNITTEST_FW_SUPPORT |
Channagoud Kadabi | d5ddf48 | 2015-09-28 10:31:35 -0700 | [diff] [blame] | 5544 | {"oem run-tests", cmd_oem_runtests}, |
Channagoud Kadabi | 736c496 | 2015-08-21 11:56:52 -0700 | [diff] [blame] | 5545 | #endif |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5546 | #endif |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5547 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 5548 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5549 | {"oem adb_enable", CmdOemEnableAdb}, |
| 5550 | #endif |
| 5551 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
lijuang | 511a2b5 | 2015-08-14 20:50:51 +0800 | [diff] [blame] | 5552 | }; |
Sridhar Parasuram | e94e815 | 2014-10-24 14:06:03 -0700 | [diff] [blame] | 5553 | |
| 5554 | int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]); |
| 5555 | for (i = 1; i < fastboot_cmds_count; i++) |
| 5556 | fastboot_register(cmd_list[i].name,cmd_list[i].cb); |
| 5557 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5558 | /* publish variables and their values */ |
| 5559 | fastboot_publish("product", TARGET(BOARD)); |
| 5560 | fastboot_publish("kernel", "lk"); |
| 5561 | fastboot_publish("serialno", sn_buf); |
| 5562 | |
Mayank Grover | a64dfbe | 2018-05-17 14:06:34 +0530 | [diff] [blame] | 5563 | /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/ |
| 5564 | snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version()); |
| 5565 | fastboot_publish("hw-revision", soc_version_str); |
| 5566 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5567 | /* |
| 5568 | * partition info is supported only for emmc partitions |
| 5569 | * Calling this for NAND prints some error messages which |
| 5570 | * is harmless but misleading. Avoid calling this for NAND |
| 5571 | * devices. |
| 5572 | */ |
| 5573 | if (target_is_emmc_boot()) |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5574 | publish_getvar_partition_info(part_info, partition_get_partition_count()); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5575 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5576 | if (partition_multislot_is_supported()) |
| 5577 | publish_getvar_multislot_vars(); |
| 5578 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5579 | /* Max download size supported */ |
lijuang | 2fbed91 | 2019-07-12 14:15:05 +0800 | [diff] [blame] | 5580 | #if !VERIFIED_BOOT_2 |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5581 | snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", |
| 5582 | target_get_max_flash_size()); |
lijuang | 2fbed91 | 2019-07-12 14:15:05 +0800 | [diff] [blame] | 5583 | #else |
| 5584 | snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", |
| 5585 | SUB_SALT_BUFF_OFFSET(target_get_max_flash_size())); |
| 5586 | #endif |
| 5587 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5588 | fastboot_publish("max-download-size", (const char *) max_download_size); |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5589 | /* Is the charger screen check enabled */ |
| 5590 | snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d", |
| 5591 | device.charger_screen_enabled); |
| 5592 | fastboot_publish("charger-screen-enabled", |
| 5593 | (const char *) charger_screen_enabled); |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5594 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 5595 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5596 | read_prod_info(&prod); |
| 5597 | snprintf(AdbEnable, MAX_RSP_SIZE, "%d", |
| 5598 | prod.is_adb_enabled); |
| 5599 | fastboot_publish("adb-enabled", |
| 5600 | (const char *) AdbEnable); |
| 5601 | #endif |
| 5602 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
lijuang | a25d8bb | 2015-09-16 13:11:52 +0800 | [diff] [blame] | 5603 | fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled); |
Unnati Gandhi | 62c8ab8 | 2014-01-24 11:01:01 +0530 | [diff] [blame] | 5604 | snprintf(panel_display_mode, MAX_RSP_SIZE, "%s", |
| 5605 | device.display_panel); |
| 5606 | fastboot_publish("display-panel", |
| 5607 | (const char *) panel_display_mode); |
Mukesh Ojha | bb6e7f3 | 2018-03-02 15:04:06 +0530 | [diff] [blame] | 5608 | |
| 5609 | if (target_is_emmc_boot()) |
| 5610 | { |
| 5611 | mmc_blocksize = mmc_get_device_blocksize(); |
| 5612 | } |
| 5613 | else |
| 5614 | { |
| 5615 | mmc_blocksize = flash_block_size(); |
| 5616 | } |
| 5617 | snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize); |
| 5618 | fastboot_publish("erase-block-size", (const char *) block_size_string); |
| 5619 | fastboot_publish("logical-block-size", (const char *) block_size_string); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5620 | #if PRODUCT_IOT |
| 5621 | get_bootloader_version_iot(&bootloader_version_string); |
| 5622 | fastboot_publish("version-bootloader", (const char *) bootloader_version_string); |
| 5623 | |
| 5624 | /* Version baseband is n/a for apq iot devices */ |
| 5625 | fastboot_publish("version-baseband", "N/A"); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5626 | #else |
Channagoud Kadabi | aafbbfc | 2015-03-04 17:52:56 -0800 | [diff] [blame] | 5627 | fastboot_publish("version-bootloader", (const char *) device.bootloader_version); |
| 5628 | fastboot_publish("version-baseband", (const char *) device.radio_version); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5629 | #endif |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 5630 | fastboot_publish("secure", is_secure_boot_enable()? "yes":"no"); |
Mayank Grover | 7b8a8f8 | 2017-10-27 13:07:59 +0530 | [diff] [blame] | 5631 | fastboot_publish("unlocked", device.is_unlocked ? "yes":"no"); |
lijuang | f16461c | 2015-08-03 17:09:34 +0800 | [diff] [blame] | 5632 | smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf)); |
| 5633 | snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf, |
| 5634 | target_is_emmc_boot()? "eMMC":"UFS"); |
| 5635 | fastboot_publish("variant", (const char *) get_variant); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 5636 | #if CHECK_BAT_VOLTAGE |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 5637 | update_battery_status(); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 5638 | fastboot_publish("battery-voltage", (const char *) battery_voltage); |
lijuang | 102dfa9 | 2015-10-09 18:31:03 +0800 | [diff] [blame] | 5639 | fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok); |
lijuang | 65c5a82 | 2015-08-29 16:35:36 +0800 | [diff] [blame] | 5640 | #endif |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 5641 | if (target_dynamic_partition_supported()) |
| 5642 | fastboot_publish("is-userspace", "no"); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5643 | } |
| 5644 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 5645 | void aboot_init(const struct app_descriptor *app) |
| 5646 | { |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 5647 | unsigned reboot_mode = 0; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5648 | int boot_err_type = 0; |
| 5649 | int boot_slot = INVALID; |
jessicatseng | 67c4fd0 | 2020-05-26 13:17:56 +0800 | [diff] [blame] | 5650 | //<2020/05/26-JessicaTseng, Add low battery icon |
| 5651 | int vbat = 0; |
| 5652 | char boot_vbat[MAX_RSP_SIZE]; |
| 5653 | //>2020/05/26-JessicaTseng |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 5654 | |
lijuang | 3983173 | 2016-01-08 17:49:02 +0800 | [diff] [blame] | 5655 | /* Initialise wdog to catch early lk crashes */ |
| 5656 | #if WDOG_SUPPORT |
| 5657 | msm_wdog_init(); |
| 5658 | #endif |
| 5659 | |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 5660 | /* Setup page size information for nv storage */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5661 | if (target_is_emmc_boot()) |
| 5662 | { |
Channagoud Kadabi | 749b0f8 | 2013-09-10 22:36:02 -0700 | [diff] [blame] | 5663 | page_size = mmc_page_size(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5664 | page_mask = page_size - 1; |
Gaurav Nebhwani | 4d2389c | 2016-03-17 21:10:05 +0530 | [diff] [blame] | 5665 | mmc_blocksize = mmc_get_device_blocksize(); |
| 5666 | mmc_blocksize_mask = mmc_blocksize - 1; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5667 | } |
| 5668 | else |
| 5669 | { |
| 5670 | page_size = flash_page_size(); |
| 5671 | page_mask = page_size - 1; |
| 5672 | } |
Deepa Dinamani | 0e163a4 | 2013-05-24 17:08:15 -0700 | [diff] [blame] | 5673 | ASSERT((MEMBASE + MEMSIZE) > MEMBASE); |
| 5674 | |
Ameya Thakur | 11cf1a6 | 2013-08-05 12:44:48 -0700 | [diff] [blame] | 5675 | read_device_info(&device); |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5676 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 5677 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5678 | read_prod_info(&prod); |
| 5679 | #endif |
| 5680 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
vijay kumar | c65876c | 2015-04-24 13:29:16 +0530 | [diff] [blame] | 5681 | read_allow_oem_unlock(&device); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 5682 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5683 | /* Detect multi-slot support */ |
| 5684 | if (partition_multislot_is_supported()) |
| 5685 | { |
| 5686 | boot_slot = partition_find_active_slot(); |
| 5687 | if (boot_slot == INVALID) |
| 5688 | { |
| 5689 | boot_into_fastboot = true; |
| 5690 | dprintf(INFO, "Active Slot: (INVALID)\n"); |
| 5691 | } |
| 5692 | else |
| 5693 | { |
| 5694 | /* Setting the state of system to boot active slot */ |
| 5695 | partition_mark_active_slot(boot_slot); |
| 5696 | dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot)); |
| 5697 | } |
| 5698 | } |
| 5699 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 5700 | target_serialno((unsigned char *) sn_buf); |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5701 | /*[20200605][TracyChui] Implement get Serial Number start*/ |
| 5702 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5703 | dprintf(CRITICAL,"serial number: %s\n",sn_buf); |
| 5704 | #else |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 5705 | dprintf(SPEW,"serial number: %s\n",sn_buf); |
tracychui | d184b91 | 2020-06-05 17:31:38 +0800 | [diff] [blame] | 5706 | #endif |
| 5707 | /*[20200605][TracyChui] Implement get Serial Number end*/ |
| 5708 | /*[20200605][TracyChui] Implement get Serial Number start */ |
| 5709 | #if defined(ENABLE_PRODINFO_ACCESS) |
| 5710 | read_prod_info(&prod); |
| 5711 | snprintf((char *)cust_sn_buf, PRODINFO_MAX_SSN_LEN + 1, "%s", prod.ssn); |
| 5712 | dprintf(CRITICAL,"customer serial number: %s\n", cust_sn_buf); |
| 5713 | snprintf((char *)factory_sn_buf, PRODINFO_MAX_ISN_LEN + 1, "%s", prod.isn); |
| 5714 | dprintf(CRITICAL,"factory serial number: %s\n", factory_sn_buf); |
| 5715 | #endif |
| 5716 | /*[20200605][TracyChui] Implement get Serial Number end */ |
Dhaval Patel | 223ec95 | 2013-07-18 14:49:44 -0700 | [diff] [blame] | 5717 | memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE); |
| 5718 | |
Matthew Qin | defd556 | 2014-07-11 18:02:40 +0800 | [diff] [blame] | 5719 | /* |
| 5720 | * Check power off reason if user force reset, |
| 5721 | * if yes phone will do normal boot. |
| 5722 | */ |
| 5723 | if (is_user_force_reset()) |
| 5724 | goto normal_boot; |
| 5725 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5726 | /* Check if we should do something other than booting up */ |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 5727 | if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN)) |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 5728 | { |
Pavel Nedev | a4c9d3a | 2013-05-15 14:42:34 +0300 | [diff] [blame] | 5729 | dprintf(ALWAYS,"dload mode key sequence detected\n"); |
lijuang | 395b5e6 | 2015-11-19 17:39:44 +0800 | [diff] [blame] | 5730 | reboot_device(EMERGENCY_DLOAD); |
| 5731 | dprintf(CRITICAL,"Failed to reboot into dload mode\n"); |
| 5732 | |
Ameya Thakur | 0b9c244 | 2013-05-10 13:22:56 -0700 | [diff] [blame] | 5733 | boot_into_fastboot = true; |
| 5734 | } |
| 5735 | if (!boot_into_fastboot) |
| 5736 | { |
| 5737 | if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP)) |
| 5738 | boot_into_recovery = 1; |
| 5739 | if (!boot_into_recovery && |
| 5740 | (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN))) |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5741 | boot_into_fastboot = true; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 5742 | } |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5743 | #if NO_KEYPAD_DRIVER |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 5744 | if (fastboot_trigger()) |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5745 | boot_into_fastboot = true; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5746 | #endif |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 5747 | |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5748 | #if USE_PON_REBOOT_REG |
| 5749 | reboot_mode = check_hard_reboot_mode(); |
| 5750 | #else |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 5751 | reboot_mode = check_reboot_mode(); |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5752 | #endif |
| 5753 | if (reboot_mode == RECOVERY_MODE) |
| 5754 | { |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 5755 | boot_into_recovery = 1; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5756 | } |
| 5757 | else if(reboot_mode == FASTBOOT_MODE) |
| 5758 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5759 | boot_into_fastboot = true; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5760 | } |
| 5761 | else if(reboot_mode == ALARM_BOOT) |
| 5762 | { |
Matthew Qin | d886f3c | 2014-01-17 16:52:01 +0800 | [diff] [blame] | 5763 | boot_reason_alarm = true; |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5764 | } |
Monika Singh | b0db4b8 | 2018-09-26 12:18:02 +0530 | [diff] [blame] | 5765 | #if VERIFIED_BOOT || VERIFIED_BOOT_2 |
Mayank Grover | 912eaa6 | 2017-10-26 12:08:53 +0530 | [diff] [blame] | 5766 | else if (VB_M <= target_get_vb_version()) |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5767 | { |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5768 | if (reboot_mode == DM_VERITY_ENFORCING) |
| 5769 | { |
| 5770 | device.verity_mode = 1; |
| 5771 | write_device_info(&device); |
| 5772 | } |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 5773 | #if ENABLE_VB_ATTEST |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5774 | else if (reboot_mode == DM_VERITY_EIO) |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 5775 | #else |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5776 | else if (reboot_mode == DM_VERITY_LOGGING) |
Channagoud Kadabi | 86b0c11 | 2016-03-16 19:23:16 -0700 | [diff] [blame] | 5777 | #endif |
Mayank Grover | 889be1b | 2017-09-12 20:12:23 +0530 | [diff] [blame] | 5778 | { |
| 5779 | device.verity_mode = 0; |
| 5780 | write_device_info(&device); |
| 5781 | } |
| 5782 | else if (reboot_mode == DM_VERITY_KEYSCLEAR) |
| 5783 | { |
| 5784 | if(send_delete_keys_to_tz()) |
| 5785 | ASSERT(0); |
| 5786 | } |
Channagoud Kadabi | cfcfcc5 | 2015-08-05 16:23:42 -0700 | [diff] [blame] | 5787 | } |
Parth Dixit | ddbc735 | 2015-10-18 03:13:31 +0530 | [diff] [blame] | 5788 | #endif |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 5789 | |
jialongjhan | 1efba00 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5790 | //[Arima][8901][JialongJhan][20200522]modify Display splash screen if enabled Start |
| 5791 | /* Display splash screen if enabled */ |
| 5792 | #if DISPLAY_SPLASH_SCREEN |
| 5793 | #if NO_ALARM_DISPLAY |
| 5794 | if (!check_alarm_boot()) { |
| 5795 | #endif |
| 5796 | dprintf(SPEW, "Display Init: Start\n"); |
| 5797 | #if DISPLAY_HDMI_PRIMARY |
| 5798 | if (!strlen(device.display_panel)) |
| 5799 | strlcpy(device.display_panel, DISPLAY_PANEL_HDMI, |
| 5800 | sizeof(device.display_panel)); |
| 5801 | #endif |
| 5802 | #if ENABLE_WBC |
| 5803 | /* Wait if the display shutdown is in progress */ |
| 5804 | while(pm_app_display_shutdown_in_prgs()); |
| 5805 | if (!pm_appsbl_display_init_done()) |
| 5806 | target_display_init(device.display_panel); |
| 5807 | else |
| 5808 | display_image_on_screen(); |
| 5809 | #else |
| 5810 | target_display_init(device.display_panel); |
| 5811 | #endif |
| 5812 | dprintf(SPEW, "Display Init: Done\n"); |
| 5813 | #if NO_ALARM_DISPLAY |
| 5814 | } |
| 5815 | #endif |
| 5816 | #endif |
| 5817 | |
| 5818 | //[Arima][8901][JialongJhan][20200522]modify Display splash screen if enabled End |
| 5819 | |
Matthew Qin | defd556 | 2014-07-11 18:02:40 +0800 | [diff] [blame] | 5820 | normal_boot: |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5821 | if (!boot_into_fastboot) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5822 | { |
jessicatseng | 67c4fd0 | 2020-05-26 13:17:56 +0800 | [diff] [blame] | 5823 | //<2020/05/26-JessicaTseng, Add low battery icon |
| 5824 | if(!target_pause_for_battery_charge()) |
| 5825 | { |
| 5826 | vbat = target_get_battery_voltage(); |
| 5827 | snprintf(boot_vbat, MAX_RSP_SIZE, "%d", vbat); |
| 5828 | dprintf(CRITICAL,"battery_voltage: %s\n", boot_vbat); |
| 5829 | if(vbat < 3500000) |
| 5830 | { |
| 5831 | display_lowbattery_image_on_screen(); |
| 5832 | //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 Start |
| 5833 | msm_display_flush(); |
| 5834 | //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 End |
| 5835 | dprintf(CRITICAL,"Low battery, cannot boot up...\n"); |
| 5836 | mdelay(3000); |
| 5837 | shutdown_device(); |
| 5838 | } |
| 5839 | } |
| 5840 | //>2020/05/26-JessicaTseng |
| 5841 | |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5842 | if (target_is_emmc_boot()) |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5843 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5844 | if(emmc_recovery_init()) |
| 5845 | dprintf(ALWAYS,"error in emmc_recovery_init\n"); |
| 5846 | if(target_use_signed_kernel()) |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5847 | { |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5848 | if((device.is_unlocked) || (device.is_tampered)) |
| 5849 | { |
| 5850 | #ifdef TZ_TAMPER_FUSE |
Monika Singh | 9825746 | 2018-06-06 11:28:49 +0530 | [diff] [blame] | 5851 | set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE); |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5852 | #endif |
| 5853 | #if USE_PCOM_SECBOOT |
| 5854 | set_tamper_flag(device.is_tampered); |
| 5855 | #endif |
| 5856 | } |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5857 | } |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 5858 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5859 | retry_boot: |
| 5860 | /* Trying to boot active partition */ |
| 5861 | if (partition_multislot_is_supported()) |
| 5862 | { |
| 5863 | boot_slot = partition_find_boot_slot(); |
| 5864 | partition_mark_active_slot(boot_slot); |
| 5865 | if (boot_slot == INVALID) |
| 5866 | goto fastboot; |
| 5867 | } |
| 5868 | |
| 5869 | boot_err_type = boot_linux_from_mmc(); |
| 5870 | switch (boot_err_type) |
| 5871 | { |
| 5872 | case ERR_INVALID_PAGE_SIZE: |
| 5873 | case ERR_DT_PARSE: |
| 5874 | case ERR_ABOOT_ADDR_OVERLAP: |
Mayank Grover | 10cabbe | 2017-10-30 19:11:05 +0530 | [diff] [blame] | 5875 | case ERR_INVALID_BOOT_MAGIC: |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5876 | if(partition_multislot_is_supported()) |
Mayank Grover | 10cabbe | 2017-10-30 19:11:05 +0530 | [diff] [blame] | 5877 | { |
| 5878 | /* |
| 5879 | * Deactivate current slot, as it failed to |
| 5880 | * boot, and retry next slot. |
| 5881 | */ |
| 5882 | partition_deactivate_slot(boot_slot); |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5883 | goto retry_boot; |
Mayank Grover | 10cabbe | 2017-10-30 19:11:05 +0530 | [diff] [blame] | 5884 | } |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5885 | else |
| 5886 | break; |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5887 | default: |
| 5888 | break; |
| 5889 | /* going to fastboot menu */ |
| 5890 | } |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 5891 | } |
Pavel Nedev | 5d91d41 | 2013-04-29 11:34:24 +0300 | [diff] [blame] | 5892 | else |
| 5893 | { |
| 5894 | recovery_init(); |
| 5895 | #if USE_PCOM_SECBOOT |
| 5896 | if((device.is_unlocked) || (device.is_tampered)) |
| 5897 | set_tamper_flag(device.is_tampered); |
| 5898 | #endif |
| 5899 | boot_linux_from_flash(); |
| 5900 | } |
| 5901 | dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting " |
| 5902 | "to fastboot mode.\n"); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 5903 | } |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 5904 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5905 | fastboot: |
jialongjhan | 1efba00 | 2020-05-22 18:25:56 +0800 | [diff] [blame] | 5906 | |
| 5907 | //[Arima][8901][JialongJhan]Show Fastboot logo 1 second 20190627 Start |
| 5908 | mdelay(1000); |
| 5909 | //[Arima][8901][JialongJhan]Show Fastboot logo 1 second 20190627 End |
| 5910 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5911 | /* We are here means regular boot did not happen. Start fastboot. */ |
Channagoud Kadabi | 4c4884e | 2013-04-09 17:12:13 -0700 | [diff] [blame] | 5912 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5913 | /* register aboot specific fastboot commands */ |
| 5914 | aboot_fastboot_register_commands(); |
Amol Jadi | 57abe4c | 2011-05-24 15:47:27 -0700 | [diff] [blame] | 5915 | |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5916 | /* dump partition table for debug info */ |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 5917 | partition_dump(); |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5918 | |
| 5919 | /* initialize and start fastboot */ |
Saranya Chidura | 41b3453 | 2018-10-16 12:29:52 +0530 | [diff] [blame] | 5920 | #if !VERIFIED_BOOT_2 |
Amol Jadi | 5edf355 | 2013-07-23 14:15:34 -0700 | [diff] [blame] | 5921 | fastboot_init(target_get_scratch_address(), target_get_max_flash_size()); |
Saranya Chidura | 41b3453 | 2018-10-16 12:29:52 +0530 | [diff] [blame] | 5922 | #else |
| 5923 | /* Add salt buffer offset at start of image address to copy VB salt */ |
| 5924 | fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()), |
| 5925 | SUB_SALT_BUFF_OFFSET(target_get_max_flash_size())); |
| 5926 | #endif |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5927 | #if FBCON_DISPLAY_MSG |
lijuang | 9a7d3b9 | 2015-11-30 14:41:24 +0800 | [diff] [blame] | 5928 | display_fastboot_menu(); |
lijuang | 4ece1e7 | 2015-08-14 21:02:36 +0800 | [diff] [blame] | 5929 | #endif |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 5930 | } |
| 5931 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 5932 | uint32_t get_page_size() |
| 5933 | { |
| 5934 | return page_size; |
| 5935 | } |
| 5936 | |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 5937 | /* |
| 5938 | * Calculated and save hash (SHA256) for non-signed boot image. |
| 5939 | * |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5940 | * @param image_addr - Boot image address |
| 5941 | * @param image_size - Size of the boot image |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 5942 | * |
| 5943 | * @return int - 0 on success, negative value on failure. |
| 5944 | */ |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5945 | 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] | 5946 | { |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5947 | unsigned int digest[8]; |
| 5948 | #if IMAGE_VERIF_ALGO_SHA1 |
| 5949 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1; |
| 5950 | #else |
| 5951 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256; |
| 5952 | #endif |
Amir Samuelov | 57a6fa2 | 2013-06-05 16:36:43 +0300 | [diff] [blame] | 5953 | |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5954 | target_crypto_init_params(); |
Veera Sundaram Sankaran | f991546 | 2014-12-09 11:54:59 -0800 | [diff] [blame] | 5955 | 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] | 5956 | |
| 5957 | save_kernel_hash_cmd(digest); |
Channagoud Kadabi | 82cf5c6 | 2014-08-24 22:17:34 -0700 | [diff] [blame] | 5958 | 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] | 5959 | |
| 5960 | return 0; |
| 5961 | } |
| 5962 | |
Mayank Grover | 351a75e | 2017-05-30 20:06:08 +0530 | [diff] [blame] | 5963 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 5964 | APP_START(aboot) |
| 5965 | .init = aboot_init, |
| 5966 | APP_END |