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