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