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