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