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