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