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