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