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