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