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