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