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