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