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