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