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