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