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