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