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