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