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