Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 5 | * Copyright (c) 2009-2011, Code Aurora Forum. 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. |
| 14 | * * Neither the name of Code Aurora nor |
| 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> |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 51 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 52 | #include "image_verify.h" |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 53 | #include "recovery.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 54 | #include "bootimg.h" |
| 55 | #include "fastboot.h" |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 56 | #include "sparse_format.h" |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 57 | #include "mmc.h" |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 58 | #include "devinfo.h" |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 59 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 60 | #include "scm.h" |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 61 | |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 62 | #define EXPAND(NAME) #NAME |
| 63 | #define TARGET(NAME) EXPAND(NAME) |
Chandan Uddaraju | da91983 | 2009-11-17 01:06:11 -0800 | [diff] [blame] | 64 | #define DEFAULT_CMDLINE "mem=100M console=null"; |
Brian Swetland | 2defe16 | 2009-08-18 14:35:59 -0700 | [diff] [blame] | 65 | |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 66 | #ifdef MEMBASE |
| 67 | #define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE)) |
| 68 | #else |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 69 | #define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000 |
Ajay Dudani | cd01f9b | 2010-02-23 21:13:04 -0800 | [diff] [blame] | 70 | #endif |
| 71 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 72 | #define RECOVERY_MODE 0x77665502 |
| 73 | #define FASTBOOT_MODE 0x77665500 |
| 74 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 75 | static const char *emmc_cmdline = " androidboot.emmc=true"; |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 76 | static const char *usb_sn_cmdline = " androidboot.serialno="; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 77 | static const char *battchg_pause = " androidboot.battchg_pause=true"; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 78 | static const char *auth_kernel = " androidboot.authorized_kernel=true"; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 79 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 80 | static const char *baseband_apq = " androidboot.baseband=apq"; |
| 81 | static const char *baseband_msm = " androidboot.baseband=msm"; |
| 82 | static const char *baseband_csfb = " androidboot.baseband=csfb"; |
| 83 | static const char *baseband_svlte2a = " androidboot.baseband=svlte2a"; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 84 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 85 | /* Assuming unauthorized kernel image by default */ |
| 86 | static int auth_kernel_img = 0; |
| 87 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 88 | static device_info device = {DEVICE_MAGIC, 0, 0}; |
| 89 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 90 | static struct udc_device surf_udc_device = { |
| 91 | .vendor_id = 0x18d1, |
Chandan Uddaraju | c53a1a1 | 2009-11-18 14:53:40 -0800 | [diff] [blame] | 92 | .product_id = 0xD00D, |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 93 | .version_id = 0x0100, |
| 94 | .manufacturer = "Google", |
| 95 | .product = "Android", |
| 96 | }; |
| 97 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 98 | struct atag_ptbl_entry |
| 99 | { |
| 100 | char name[16]; |
| 101 | unsigned offset; |
| 102 | unsigned size; |
| 103 | unsigned flags; |
| 104 | }; |
| 105 | |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 106 | char sn_buf[13]; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 107 | |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 108 | extern int emmc_recovery_init(void); |
| 109 | |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 110 | #if NO_KEYPAD_DRIVER |
| 111 | extern int fastboot_trigger(void); |
| 112 | #endif |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 113 | |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 114 | static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn) |
| 115 | { |
| 116 | struct atag_ptbl_entry atag_ptn; |
| 117 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 118 | if (ptn->type == TYPE_MODEM_PARTITION) { |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 119 | return; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 120 | } |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 121 | memcpy(atag_ptn.name, ptn->name, 16); |
| 122 | atag_ptn.name[15] = '\0'; |
| 123 | atag_ptn.offset = ptn->start; |
| 124 | atag_ptn.size = ptn->length; |
| 125 | atag_ptn.flags = ptn->flags; |
| 126 | memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry)); |
| 127 | *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned); |
| 128 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 129 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 130 | void boot_linux(void *kernel, unsigned *tags, |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 131 | const char *cmdline, unsigned machtype, |
| 132 | void *ramdisk, unsigned ramdisk_size) |
| 133 | { |
| 134 | unsigned *ptr = tags; |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 135 | unsigned pcount = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 136 | void (*entry)(unsigned,unsigned,unsigned*) = kernel; |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 137 | struct ptable *ptable; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 138 | int cmdline_len = 0; |
| 139 | int have_cmdline = 0; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 140 | int pause_at_bootup = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 141 | |
| 142 | /* CORE */ |
| 143 | *ptr++ = 2; |
| 144 | *ptr++ = 0x54410001; |
| 145 | |
| 146 | if (ramdisk_size) { |
| 147 | *ptr++ = 4; |
| 148 | *ptr++ = 0x54420005; |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 149 | *ptr++ = (unsigned)ramdisk; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 150 | *ptr++ = ramdisk_size; |
| 151 | } |
| 152 | |
Chandan Uddaraju | c6860e1 | 2009-11-19 11:22:15 -0800 | [diff] [blame] | 153 | ptr = target_atag_mem(ptr); |
| 154 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 155 | if (!target_is_emmc_boot()) { |
| 156 | /* Skip NAND partition ATAGS for eMMC boot */ |
| 157 | if ((ptable = flash_get_ptable()) && (ptable->count != 0)) { |
| 158 | int i; |
Shashank Mittal | 8e49dec | 2010-03-01 15:19:04 -0800 | [diff] [blame] | 159 | for(i=0; i < ptable->count; i++) { |
| 160 | struct ptentry *ptn; |
| 161 | ptn = ptable_get(ptable, i); |
| 162 | if (ptn->type == TYPE_APPS_PARTITION) |
| 163 | pcount++; |
| 164 | } |
| 165 | *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) / |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 166 | sizeof(unsigned))); |
| 167 | *ptr++ = 0x4d534d70; |
| 168 | for (i = 0; i < ptable->count; ++i) |
| 169 | ptentry_to_tag(&ptr, ptable_get(ptable, i)); |
| 170 | } |
Dima Zavin | 42168f2 | 2009-01-30 11:52:22 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 173 | if (cmdline && cmdline[0]) { |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 174 | cmdline_len = strlen(cmdline); |
| 175 | have_cmdline = 1; |
| 176 | } |
| 177 | if (target_is_emmc_boot()) { |
| 178 | cmdline_len += strlen(emmc_cmdline); |
| 179 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 180 | |
| 181 | cmdline_len += strlen(usb_sn_cmdline); |
| 182 | cmdline_len += strlen(sn_buf); |
| 183 | |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 184 | if (target_pause_for_battery_charge()) { |
| 185 | pause_at_bootup = 1; |
| 186 | cmdline_len += strlen(battchg_pause); |
| 187 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 188 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 189 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 190 | cmdline_len += strlen(auth_kernel); |
| 191 | } |
| 192 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 193 | /* Determine correct androidboot.baseband to use */ |
| 194 | switch(target_baseband()) |
| 195 | { |
| 196 | case BASEBAND_APQ: |
| 197 | cmdline_len += strlen(baseband_apq); |
| 198 | break; |
| 199 | |
| 200 | case BASEBAND_MSM: |
| 201 | cmdline_len += strlen(baseband_msm); |
| 202 | break; |
| 203 | |
| 204 | case BASEBAND_CSFB: |
| 205 | cmdline_len += strlen(baseband_csfb); |
| 206 | break; |
| 207 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 208 | case BASEBAND_SVLTE2A: |
| 209 | cmdline_len += strlen(baseband_svlte2a); |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 210 | break; |
| 211 | } |
| 212 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 213 | if (cmdline_len > 0) { |
| 214 | const char *src; |
| 215 | char *dst; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 216 | unsigned n; |
| 217 | /* include terminating 0 and round up to a word multiple */ |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 218 | n = (cmdline_len + 4) & (~3); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 219 | *ptr++ = (n / 4) + 2; |
| 220 | *ptr++ = 0x54410009; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 221 | dst = (char *)ptr; |
| 222 | if (have_cmdline) { |
| 223 | src = cmdline; |
| 224 | while ((*dst++ = *src++)); |
| 225 | } |
| 226 | if (target_is_emmc_boot()) { |
| 227 | src = emmc_cmdline; |
| 228 | if (have_cmdline) --dst; |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 229 | have_cmdline = 1; |
| 230 | while ((*dst++ = *src++)); |
| 231 | } |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 232 | |
| 233 | src = usb_sn_cmdline; |
| 234 | if (have_cmdline) --dst; |
| 235 | have_cmdline = 1; |
| 236 | while ((*dst++ = *src++)); |
| 237 | src = sn_buf; |
| 238 | if (have_cmdline) --dst; |
| 239 | have_cmdline = 1; |
| 240 | while ((*dst++ = *src++)); |
| 241 | |
David Ng | f773dde | 2010-07-26 19:55:08 -0700 | [diff] [blame] | 242 | if (pause_at_bootup) { |
| 243 | src = battchg_pause; |
| 244 | if (have_cmdline) --dst; |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 245 | while ((*dst++ = *src++)); |
| 246 | } |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 247 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 248 | if(target_use_signed_kernel() && auth_kernel_img) { |
| 249 | src = auth_kernel; |
| 250 | if (have_cmdline) --dst; |
| 251 | while ((*dst++ = *src++)); |
| 252 | } |
| 253 | |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 254 | switch(target_baseband()) |
| 255 | { |
| 256 | case BASEBAND_APQ: |
| 257 | src = baseband_apq; |
| 258 | if (have_cmdline) --dst; |
| 259 | while ((*dst++ = *src++)); |
| 260 | break; |
| 261 | |
| 262 | case BASEBAND_MSM: |
| 263 | src = baseband_msm; |
| 264 | if (have_cmdline) --dst; |
| 265 | while ((*dst++ = *src++)); |
| 266 | break; |
| 267 | |
| 268 | case BASEBAND_CSFB: |
| 269 | src = baseband_csfb; |
| 270 | if (have_cmdline) --dst; |
| 271 | while ((*dst++ = *src++)); |
| 272 | break; |
| 273 | |
Ajay Dudani | 6cff85e | 2011-02-04 16:02:16 -0800 | [diff] [blame] | 274 | case BASEBAND_SVLTE2A: |
| 275 | src = baseband_svlte2a; |
Ajay Dudani | d04110c | 2011-01-17 23:55:07 -0800 | [diff] [blame] | 276 | if (have_cmdline) --dst; |
| 277 | while ((*dst++ = *src++)); |
| 278 | break; |
| 279 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 280 | ptr += (n / 4); |
| 281 | } |
| 282 | |
| 283 | /* END */ |
| 284 | *ptr++ = 0; |
| 285 | *ptr++ = 0; |
| 286 | |
| 287 | dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n", |
| 288 | kernel, ramdisk, ramdisk_size); |
| 289 | if (cmdline) |
| 290 | dprintf(INFO, "cmdline: %s\n", cmdline); |
| 291 | |
| 292 | enter_critical_section(); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 293 | /* do any platform specific cleanup before kernel entry */ |
| 294 | platform_uninit(); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 295 | arch_disable_cache(UCACHE); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 296 | arch_disable_mmu(); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 297 | entry(0, machtype, tags); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 300 | unsigned page_size = 0; |
| 301 | unsigned page_mask = 0; |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 302 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 303 | #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y))) |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 304 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 305 | static unsigned char buf[4096]; //Equal to max-supported pagesize |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 306 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 307 | int boot_linux_from_mmc(void) |
| 308 | { |
| 309 | struct boot_img_hdr *hdr = (void*) buf; |
| 310 | struct boot_img_hdr *uhdr; |
| 311 | unsigned offset = 0; |
| 312 | unsigned long long ptn = 0; |
| 313 | unsigned n = 0; |
| 314 | const char *cmdline; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 315 | int index = INVALID_PTN; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 316 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 317 | unsigned char *image_addr = 0; |
| 318 | unsigned kernel_actual; |
| 319 | unsigned ramdisk_actual; |
| 320 | unsigned imagesize_actual; |
| 321 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 322 | uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 323 | if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 324 | dprintf(INFO, "Unified boot method!\n"); |
| 325 | hdr = uhdr; |
| 326 | goto unified_boot; |
| 327 | } |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 328 | if (!boot_into_recovery) { |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 329 | index = partition_get_index("boot"); |
| 330 | ptn = partition_get_offset(index); |
| 331 | if(ptn == 0) { |
Shashank Mittal | 85b91f6 | 2010-10-30 10:12:38 -0700 | [diff] [blame] | 332 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 333 | return -1; |
| 334 | } |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 335 | } |
| 336 | else { |
| 337 | index = partition_get_index("recovery"); |
| 338 | ptn = partition_get_offset(index); |
| 339 | if(ptn == 0) { |
Shashank Mittal | 85b91f6 | 2010-10-30 10:12:38 -0700 | [diff] [blame] | 340 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 341 | return -1; |
| 342 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 345 | if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) { |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 346 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 347 | return -1; |
| 348 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 349 | |
| 350 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 351 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 352 | return -1; |
| 353 | } |
| 354 | |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 355 | if (hdr->page_size && (hdr->page_size != page_size)) { |
| 356 | page_size = hdr->page_size; |
| 357 | page_mask = page_size - 1; |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 360 | /* Authenticate Kernel */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 361 | if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered)) |
Subbaraman Narayanamurthy | 958fa24 | 2011-01-27 17:42:38 -0800 | [diff] [blame] | 362 | { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 363 | image_addr = (unsigned char *)target_get_scratch_address(); |
| 364 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 365 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 366 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual); |
| 367 | |
| 368 | offset = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 369 | |
| 370 | /* Assuming device rooted at this time */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 371 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 372 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 373 | /* Read image without signature */ |
| 374 | if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) |
| 375 | { |
| 376 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 377 | return -1; |
Subbaraman Narayanamurthy | 958fa24 | 2011-01-27 17:42:38 -0800 | [diff] [blame] | 378 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 379 | |
| 380 | offset = imagesize_actual; |
| 381 | /* Read signature */ |
| 382 | if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size)) |
| 383 | { |
| 384 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | auth_kernel_img = image_verify((unsigned char *)image_addr, |
| 389 | (unsigned char *)(image_addr + imagesize_actual), |
| 390 | imagesize_actual, |
| 391 | CRYPTO_AUTH_ALG_SHA256); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 392 | |
| 393 | if(auth_kernel_img) |
| 394 | { |
| 395 | /* Authorized kernel */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 396 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 397 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* Move kernel and ramdisk to correct address */ |
| 401 | memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size); |
| 402 | memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 403 | |
| 404 | /* Make sure everything from scratch address is read before next step!*/ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 405 | if(device.is_tampered) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 406 | { |
| 407 | write_device_info_mmc(&device); |
| 408 | #ifdef TZ_TAMPER_FUSE |
| 409 | set_tamper_fuse_cmd(); |
| 410 | #endif |
| 411 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 412 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 413 | else |
| 414 | { |
| 415 | offset += page_size; |
| 416 | |
| 417 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 418 | if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) { |
| 419 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 420 | return -1; |
| 421 | } |
| 422 | offset += n; |
| 423 | |
| 424 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 425 | if(n != 0) |
| 426 | { |
| 427 | if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) { |
| 428 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 429 | return -1; |
| 430 | } |
| 431 | } |
| 432 | offset += n; |
| 433 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 434 | |
| 435 | unified_boot: |
| 436 | dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr, |
| 437 | hdr->kernel_size); |
| 438 | dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr, |
| 439 | hdr->ramdisk_size); |
| 440 | |
| 441 | if(hdr->cmdline[0]) { |
| 442 | cmdline = (char*) hdr->cmdline; |
| 443 | } else { |
| 444 | cmdline = DEFAULT_CMDLINE; |
| 445 | } |
| 446 | dprintf(INFO, "cmdline = '%s'\n", cmdline); |
| 447 | |
| 448 | dprintf(INFO, "\nBooting Linux\n"); |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 449 | boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr, |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 450 | (const char *)cmdline, board_machtype(), |
| 451 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 456 | int boot_linux_from_flash(void) |
| 457 | { |
| 458 | struct boot_img_hdr *hdr = (void*) buf; |
| 459 | unsigned n; |
| 460 | struct ptentry *ptn; |
| 461 | struct ptable *ptable; |
| 462 | unsigned offset = 0; |
| 463 | const char *cmdline; |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 464 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 465 | unsigned char *image_addr = 0; |
| 466 | unsigned kernel_actual; |
| 467 | unsigned ramdisk_actual; |
| 468 | unsigned imagesize_actual; |
| 469 | |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 470 | if (target_is_emmc_boot()) { |
| 471 | hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR; |
| 472 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 473 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
| 474 | return -1; |
| 475 | } |
| 476 | goto continue_boot; |
| 477 | } |
| 478 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 479 | ptable = flash_get_ptable(); |
| 480 | if (ptable == NULL) { |
| 481 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 482 | return -1; |
| 483 | } |
| 484 | |
Chandan Uddaraju | de85d3f | 2010-01-05 16:32:33 -0800 | [diff] [blame] | 485 | if(!boot_into_recovery) |
| 486 | { |
| 487 | ptn = ptable_find(ptable, "boot"); |
| 488 | if (ptn == NULL) { |
| 489 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 490 | return -1; |
| 491 | } |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | ptn = ptable_find(ptable, "recovery"); |
| 496 | if (ptn == NULL) { |
| 497 | dprintf(CRITICAL, "ERROR: No recovery partition found\n"); |
| 498 | return -1; |
| 499 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 502 | if (flash_read(ptn, offset, buf, page_size)) { |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 503 | dprintf(CRITICAL, "ERROR: Cannot read boot image header\n"); |
| 504 | return -1; |
| 505 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 506 | |
| 507 | if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 508 | dprintf(CRITICAL, "ERROR: Invalid boot image header\n"); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 509 | return -1; |
| 510 | } |
| 511 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 512 | if (hdr->page_size != page_size) { |
Kinson Chik kchik@codeaurora.org | 82e4ae6 | 2011-04-12 17:42:07 -0700 | [diff] [blame] | 513 | 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] | 514 | return -1; |
| 515 | } |
| 516 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 517 | /* Authenticate Kernel */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 518 | if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered)) |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 519 | { |
| 520 | image_addr = (unsigned char *)target_get_scratch_address(); |
| 521 | kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 522 | ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 523 | imagesize_actual = (page_size + kernel_actual + ramdisk_actual); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 524 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 525 | offset = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 526 | |
| 527 | /* Assuming device rooted at this time */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 528 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 529 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 530 | /* Read image without signature */ |
| 531 | if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual)) |
| 532 | { |
| 533 | dprintf(CRITICAL, "ERROR: Cannot read boot image\n"); |
| 534 | return -1; |
| 535 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 536 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 537 | offset = imagesize_actual; |
| 538 | /* Read signature */ |
| 539 | if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size)) |
| 540 | { |
| 541 | dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n"); |
| 542 | } |
| 543 | else |
| 544 | { |
| 545 | |
| 546 | /* Verify signature */ |
| 547 | auth_kernel_img = image_verify((unsigned char *)image_addr, |
| 548 | (unsigned char *)(image_addr + imagesize_actual), |
| 549 | imagesize_actual, |
| 550 | CRYPTO_AUTH_ALG_SHA256); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 551 | |
| 552 | if(auth_kernel_img) |
| 553 | { |
| 554 | /* Authorized kernel */ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 555 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 556 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /* Move kernel and ramdisk to correct address */ |
| 560 | memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size); |
| 561 | memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 562 | |
| 563 | /* Make sure everything from scratch address is read before next step!*/ |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 564 | if(device.is_tampered) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 565 | { |
| 566 | write_device_info_flash(&device); |
| 567 | } |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 568 | } |
| 569 | else |
| 570 | { |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 571 | offset = page_size; |
| 572 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 573 | n = ROUND_TO_PAGE(hdr->kernel_size, page_mask); |
| 574 | if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) { |
| 575 | dprintf(CRITICAL, "ERROR: Cannot read kernel image\n"); |
| 576 | return -1; |
| 577 | } |
| 578 | offset += n; |
| 579 | |
| 580 | n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask); |
| 581 | if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) { |
| 582 | dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n"); |
| 583 | return -1; |
| 584 | } |
| 585 | offset += n; |
| 586 | } |
David Ng | 183a742 | 2009-12-07 14:55:21 -0800 | [diff] [blame] | 587 | continue_boot: |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 588 | dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr, |
| 589 | hdr->kernel_size); |
| 590 | dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr, |
| 591 | hdr->ramdisk_size); |
| 592 | |
| 593 | if(hdr->cmdline[0]) { |
| 594 | cmdline = (char*) hdr->cmdline; |
| 595 | } else { |
| 596 | cmdline = DEFAULT_CMDLINE; |
| 597 | } |
| 598 | dprintf(INFO, "cmdline = '%s'\n", cmdline); |
| 599 | |
| 600 | /* TODO: create/pass atags to kernel */ |
| 601 | |
| 602 | dprintf(INFO, "\nBooting Linux\n"); |
Ajay Dudani | e28a607 | 2011-07-01 13:59:46 -0700 | [diff] [blame] | 603 | boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr, |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 604 | (const char *)cmdline, board_machtype(), |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 605 | (void *)hdr->ramdisk_addr, hdr->ramdisk_size); |
| 606 | |
| 607 | return 0; |
| 608 | } |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 609 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 610 | unsigned char info_buf[4096]; |
| 611 | void write_device_info_mmc(device_info *dev) |
| 612 | { |
| 613 | struct device_info *info = (void*) info_buf; |
| 614 | unsigned long long ptn = 0; |
| 615 | unsigned long long size; |
| 616 | int index = INVALID_PTN; |
| 617 | |
| 618 | index = partition_get_index("aboot"); |
| 619 | ptn = partition_get_offset(index); |
| 620 | if(ptn == 0) |
| 621 | { |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | size = partition_get_size(index); |
| 626 | |
| 627 | memcpy(info, dev, sizeof(device_info)); |
| 628 | |
| 629 | if(mmc_write((ptn + size - 512), 512, (void *)info_buf)) |
| 630 | { |
| 631 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 632 | return; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | void read_device_info_mmc(device_info *dev) |
| 637 | { |
| 638 | struct device_info *info = (void*) info_buf; |
| 639 | unsigned long long ptn = 0; |
| 640 | unsigned long long size; |
| 641 | int index = INVALID_PTN; |
| 642 | |
| 643 | index = partition_get_index("aboot"); |
| 644 | ptn = partition_get_offset(index); |
| 645 | if(ptn == 0) |
| 646 | { |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | size = partition_get_size(index); |
| 651 | |
| 652 | if(mmc_read((ptn + size - 512), (void *)info_buf, 512)) |
| 653 | { |
| 654 | dprintf(CRITICAL, "ERROR: Cannot read device info\n"); |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 659 | { |
| 660 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
| 661 | info->is_unlocked = 0; |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 662 | info->is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 663 | |
| 664 | write_device_info_mmc(info); |
| 665 | } |
| 666 | memcpy(dev, info, sizeof(device_info)); |
| 667 | } |
| 668 | |
| 669 | void write_device_info_flash(device_info *dev) |
| 670 | { |
| 671 | struct device_info *info = (void *) info_buf; |
| 672 | struct ptentry *ptn; |
| 673 | struct ptable *ptable; |
| 674 | |
| 675 | ptable = flash_get_ptable(); |
| 676 | if (ptable == NULL) |
| 677 | { |
| 678 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | ptn = ptable_find(ptable, "devinfo"); |
| 683 | if (ptn == NULL) |
| 684 | { |
| 685 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | memcpy(info, dev, sizeof(device_info)); |
| 690 | |
| 691 | if (flash_write(ptn, 0, (void *)info_buf, page_size)) |
| 692 | { |
| 693 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 694 | return; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | void read_device_info_flash(device_info *dev) |
| 699 | { |
| 700 | struct device_info *info = (void*) info_buf; |
| 701 | struct ptentry *ptn; |
| 702 | struct ptable *ptable; |
| 703 | |
| 704 | ptable = flash_get_ptable(); |
| 705 | if (ptable == NULL) |
| 706 | { |
| 707 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | ptn = ptable_find(ptable, "devinfo"); |
| 712 | if (ptn == NULL) |
| 713 | { |
| 714 | dprintf(CRITICAL, "ERROR: No boot partition found\n"); |
| 715 | return; |
| 716 | } |
| 717 | |
| 718 | if (flash_read(ptn, 0, (void *)info_buf, page_size)) |
| 719 | { |
| 720 | dprintf(CRITICAL, "ERROR: Cannot write device info\n"); |
| 721 | return; |
| 722 | } |
| 723 | |
| 724 | if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE)) |
| 725 | { |
| 726 | while(1); |
| 727 | memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE); |
| 728 | info->is_unlocked = 0; |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 729 | info->is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 730 | write_device_info_flash(info); |
| 731 | } |
| 732 | memcpy(dev, info, sizeof(device_info)); |
| 733 | } |
| 734 | |
| 735 | void write_device_info(device_info *dev) |
| 736 | { |
| 737 | if(target_is_emmc_boot()) |
| 738 | { |
| 739 | write_device_info_mmc(dev); |
| 740 | } |
| 741 | else |
| 742 | { |
| 743 | write_device_info_flash(dev); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | void read_device_info(device_info *dev) |
| 748 | { |
| 749 | if(target_is_emmc_boot()) |
| 750 | { |
| 751 | read_device_info_mmc(dev); |
| 752 | } |
| 753 | else |
| 754 | { |
| 755 | read_device_info_flash(dev); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | void reset_device_info() |
| 760 | { |
| 761 | dprintf(ALWAYS, "reset_device_info called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 762 | device.is_tampered = 0; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 763 | write_device_info(&device); |
| 764 | } |
| 765 | |
| 766 | void set_device_root() |
| 767 | { |
| 768 | dprintf(ALWAYS, "set_device_root called."); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 769 | device.is_tampered = 1; |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 770 | write_device_info(&device); |
| 771 | } |
| 772 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 773 | void cmd_boot(const char *arg, void *data, unsigned sz) |
| 774 | { |
| 775 | unsigned kernel_actual; |
| 776 | unsigned ramdisk_actual; |
| 777 | static struct boot_img_hdr hdr; |
| 778 | char *ptr = ((char*) data); |
| 779 | |
| 780 | if (sz < sizeof(hdr)) { |
| 781 | fastboot_fail("invalid bootimage header"); |
| 782 | return; |
| 783 | } |
| 784 | |
| 785 | memcpy(&hdr, data, sizeof(hdr)); |
| 786 | |
| 787 | /* ensure commandline is terminated */ |
| 788 | hdr.cmdline[BOOT_ARGS_SIZE-1] = 0; |
| 789 | |
Subbaraman Narayanamurthy | fbe13a0 | 2010-09-10 11:51:12 -0700 | [diff] [blame] | 790 | if(target_is_emmc_boot() && hdr.page_size) { |
| 791 | page_size = hdr.page_size; |
| 792 | page_mask = page_size - 1; |
| 793 | } |
| 794 | |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 795 | kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask); |
| 796 | ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 797 | |
Shashank Mittal | 1f0e266 | 2011-09-01 15:06:00 -0700 | [diff] [blame] | 798 | /* sz should have atleast raw boot image */ |
| 799 | if (page_size + kernel_actual + ramdisk_actual > sz) { |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 800 | fastboot_fail("incomplete bootimage"); |
| 801 | return; |
| 802 | } |
| 803 | |
Ajay Dudani | e28a607 | 2011-07-01 13:59:46 -0700 | [diff] [blame] | 804 | memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size); |
| 805 | memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 806 | |
| 807 | fastboot_okay(""); |
| 808 | udc_stop(); |
| 809 | |
Amol Jadi | e67872e | 2011-06-27 14:14:11 -0700 | [diff] [blame] | 810 | boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr, |
Chandan Uddaraju | 885e4db | 2009-12-03 22:45:26 -0800 | [diff] [blame] | 811 | (const char*) hdr.cmdline, board_machtype(), |
Ajay Dudani | e28a607 | 2011-07-01 13:59:46 -0700 | [diff] [blame] | 812 | (void*) hdr.ramdisk_addr, hdr.ramdisk_size); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 813 | } |
| 814 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 815 | void cmd_erase(const char *arg, void *data, unsigned sz) |
| 816 | { |
| 817 | struct ptentry *ptn; |
| 818 | struct ptable *ptable; |
| 819 | |
| 820 | ptable = flash_get_ptable(); |
| 821 | if (ptable == NULL) { |
| 822 | fastboot_fail("partition table doesn't exist"); |
| 823 | return; |
| 824 | } |
| 825 | |
| 826 | ptn = ptable_find(ptable, arg); |
| 827 | if (ptn == NULL) { |
| 828 | fastboot_fail("unknown partition name"); |
| 829 | return; |
| 830 | } |
| 831 | |
| 832 | if (flash_erase(ptn)) { |
| 833 | fastboot_fail("failed to erase partition"); |
| 834 | return; |
| 835 | } |
| 836 | fastboot_okay(""); |
| 837 | } |
| 838 | |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 839 | |
| 840 | void cmd_erase_mmc(const char *arg, void *data, unsigned sz) |
| 841 | { |
| 842 | unsigned long long ptn = 0; |
| 843 | unsigned int out[512] = {0}; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 844 | int index = INVALID_PTN; |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 845 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 846 | index = partition_get_index(arg); |
| 847 | ptn = partition_get_offset(index); |
| 848 | if(ptn == 0) { |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 849 | fastboot_fail("partition table doesn't exist"); |
| 850 | return; |
| 851 | } |
| 852 | |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 853 | /* Simple inefficient version of erase. Just writing |
| 854 | 0 in first block */ |
| 855 | if (mmc_write(ptn , 512, (unsigned int *)out)) { |
| 856 | fastboot_fail("failed to erase partition"); |
| 857 | return; |
| 858 | } |
| 859 | fastboot_okay(""); |
| 860 | } |
| 861 | |
| 862 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 863 | void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz) |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 864 | { |
| 865 | unsigned long long ptn = 0; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 866 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 867 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 868 | |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 869 | if (!strcmp(arg, "partition")) |
| 870 | { |
| 871 | dprintf(INFO, "Attempt to write partition image.\n"); |
| 872 | if (mmc_write_partition(sz, (unsigned char *) data)) { |
| 873 | fastboot_fail("failed to write partition"); |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 874 | return; |
| 875 | } |
| 876 | } |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 877 | else |
| 878 | { |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 879 | index = partition_get_index(arg); |
| 880 | ptn = partition_get_offset(index); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 881 | if(ptn == 0) { |
| 882 | fastboot_fail("partition table doesn't exist"); |
| 883 | return; |
| 884 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 885 | |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 886 | if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) { |
| 887 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 888 | fastboot_fail("image is not a boot image"); |
| 889 | return; |
| 890 | } |
| 891 | } |
Subbaraman Narayanamurthy | c95b5b1 | 2010-08-31 13:19:48 -0700 | [diff] [blame] | 892 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 893 | size = partition_get_size(index); |
Greg Grisco | 6e75477 | 2011-06-23 12:19:39 -0700 | [diff] [blame] | 894 | if (ROUND_TO_PAGE(sz,511) > size) { |
| 895 | fastboot_fail("size too large"); |
| 896 | return; |
| 897 | } |
| 898 | else if (mmc_write(ptn , sz, (unsigned int *)data)) { |
| 899 | fastboot_fail("flash write failure"); |
| 900 | return; |
| 901 | } |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 902 | } |
| 903 | fastboot_okay(""); |
| 904 | return; |
| 905 | } |
| 906 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 907 | void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz) |
| 908 | { |
| 909 | unsigned int chunk; |
| 910 | unsigned int chunk_data_sz; |
| 911 | sparse_header_t *sparse_header; |
| 912 | chunk_header_t *chunk_header; |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 913 | uint32_t total_blocks = 0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 914 | unsigned long long ptn = 0; |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame^] | 915 | unsigned long long size = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 916 | int index = INVALID_PTN; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 917 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 918 | index = partition_get_index(arg); |
| 919 | ptn = partition_get_offset(index); |
| 920 | if(ptn == 0) { |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 921 | fastboot_fail("partition table doesn't exist"); |
| 922 | return; |
| 923 | } |
| 924 | |
Channagoud Kadabi | 65b9100 | 2011-10-11 17:34:33 +0530 | [diff] [blame^] | 925 | size = partition_get_size(index); |
| 926 | if (ROUND_TO_PAGE(sz,511) > size) { |
| 927 | fastboot_fail("size too large"); |
| 928 | return; |
| 929 | } |
| 930 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 931 | /* Read and skip over sparse image header */ |
| 932 | sparse_header = (sparse_header_t *) data; |
| 933 | data += sparse_header->file_hdr_sz; |
| 934 | if(sparse_header->file_hdr_sz > sizeof(sparse_header_t)) |
| 935 | { |
| 936 | /* Skip the remaining bytes in a header that is longer than |
| 937 | * we expected. |
| 938 | */ |
| 939 | data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t)); |
| 940 | } |
| 941 | |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 942 | dprintf (SPEW, "=== Sparse Image Header ===\n"); |
| 943 | dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic); |
| 944 | dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version); |
| 945 | dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version); |
| 946 | dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz); |
| 947 | dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz); |
| 948 | dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz); |
| 949 | dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks); |
| 950 | dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 951 | |
| 952 | /* Start processing chunks */ |
| 953 | for (chunk=0; chunk<sparse_header->total_chunks; chunk++) |
| 954 | { |
| 955 | /* Read and skip over chunk header */ |
| 956 | chunk_header = (chunk_header_t *) data; |
| 957 | data += sizeof(chunk_header_t); |
| 958 | |
| 959 | dprintf (SPEW, "=== Chunk Header ===\n"); |
| 960 | dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type); |
| 961 | dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz); |
| 962 | dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz); |
| 963 | |
| 964 | if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) |
| 965 | { |
| 966 | /* Skip the remaining bytes in a header that is longer than |
| 967 | * we expected. |
| 968 | */ |
| 969 | data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t)); |
| 970 | } |
| 971 | |
| 972 | chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz; |
| 973 | switch (chunk_header->chunk_type) |
| 974 | { |
| 975 | case CHUNK_TYPE_RAW: |
| 976 | if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz + |
| 977 | chunk_data_sz)) |
| 978 | { |
| 979 | fastboot_fail("Bogus chunk size for chunk type Raw"); |
| 980 | return; |
| 981 | } |
| 982 | |
Ajay Dudani | ab18f02 | 2011-05-12 14:39:22 -0700 | [diff] [blame] | 983 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
| 984 | chunk_data_sz, |
| 985 | (unsigned int*)data)) |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 986 | { |
| 987 | fastboot_fail("flash write failure"); |
| 988 | return; |
| 989 | } |
| 990 | total_blocks += chunk_header->chunk_sz; |
| 991 | data += chunk_data_sz; |
| 992 | break; |
| 993 | |
| 994 | case CHUNK_TYPE_DONT_CARE: |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 995 | total_blocks += chunk_header->chunk_sz; |
| 996 | break; |
| 997 | |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 998 | case CHUNK_TYPE_CRC: |
| 999 | if(chunk_header->total_sz != sparse_header->chunk_hdr_sz) |
| 1000 | { |
| 1001 | fastboot_fail("Bogus chunk size for chunk type Dont Care"); |
| 1002 | return; |
| 1003 | } |
| 1004 | total_blocks += chunk_header->chunk_sz; |
| 1005 | data += chunk_data_sz; |
| 1006 | break; |
| 1007 | |
Kinson Chik kchik@codeaurora.org | da29b1e | 2011-05-06 17:36:39 -0700 | [diff] [blame] | 1008 | default: |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1009 | fastboot_fail("Unknown chunk type"); |
| 1010 | return; |
| 1011 | } |
| 1012 | } |
| 1013 | |
Ajay Dudani | 0c6927b | 2011-05-18 11:12:16 -0700 | [diff] [blame] | 1014 | dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n", |
| 1015 | total_blocks, sparse_header->total_blks); |
| 1016 | |
| 1017 | if(total_blocks != sparse_header->total_blks) |
| 1018 | { |
| 1019 | fastboot_fail("sparse image write failure"); |
| 1020 | } |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1021 | |
| 1022 | fastboot_okay(""); |
| 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | void cmd_flash_mmc(const char *arg, void *data, unsigned sz) |
| 1027 | { |
| 1028 | sparse_header_t *sparse_header; |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1029 | /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */ |
| 1030 | unsigned int *magic_number = (unsigned int *) data; |
| 1031 | int ret=0; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1032 | |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1033 | if (magic_number[0] == SSD_HEADER_MAGIC_0 && |
| 1034 | magic_number[1] == SSD_HEADER_MAGIC_1) |
| 1035 | { |
| 1036 | #ifdef SSD_ENABLE |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 1037 | ret = decrypt_img_scm((uint32 **) &data, &sz); |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1038 | #endif |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 1039 | if (ret != 0) { |
kchik@codeaurora.org | bce18ea | 2011-04-18 20:22:28 -0700 | [diff] [blame] | 1040 | dprintf(CRITICAL, "ERROR: Invalid secure image\n"); |
| 1041 | return; |
| 1042 | } |
| 1043 | } |
| 1044 | sparse_header = (sparse_header_t *) data; |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1045 | if (sparse_header->magic != SPARSE_HEADER_MAGIC) |
| 1046 | cmd_flash_mmc_img(arg, data, sz); |
| 1047 | else |
| 1048 | cmd_flash_mmc_sparse_img(arg, data, sz); |
Ajay Dudani | 5c76113 | 2011-04-07 20:19:04 -0700 | [diff] [blame] | 1049 | return; |
| 1050 | } |
| 1051 | |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1052 | void cmd_flash(const char *arg, void *data, unsigned sz) |
| 1053 | { |
| 1054 | struct ptentry *ptn; |
| 1055 | struct ptable *ptable; |
| 1056 | unsigned extra = 0; |
| 1057 | |
| 1058 | ptable = flash_get_ptable(); |
| 1059 | if (ptable == NULL) { |
| 1060 | fastboot_fail("partition table doesn't exist"); |
| 1061 | return; |
| 1062 | } |
| 1063 | |
| 1064 | ptn = ptable_find(ptable, arg); |
| 1065 | if (ptn == NULL) { |
| 1066 | fastboot_fail("unknown partition name"); |
| 1067 | return; |
| 1068 | } |
| 1069 | |
| 1070 | if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) { |
| 1071 | if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 1072 | fastboot_fail("image is not a boot image"); |
| 1073 | return; |
| 1074 | } |
| 1075 | } |
| 1076 | |
Chandan Uddaraju | d6d4504 | 2010-02-24 21:12:45 -0800 | [diff] [blame] | 1077 | if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata") |
Channagoud Kadabi | 404a706 | 2011-03-21 19:27:50 +0530 | [diff] [blame] | 1078 | || !strcmp(ptn->name, "persist")) { |
| 1079 | if (flash_ecc_bch_enabled()) |
| 1080 | /* Spare data bytes for 8 bit ECC increased by 4 */ |
| 1081 | extra = ((page_size >> 9) * 20); |
| 1082 | else |
| 1083 | extra = ((page_size >> 9) * 16); |
| 1084 | } else |
Shashank Mittal | dcc2e35 | 2009-11-19 19:11:16 -0800 | [diff] [blame] | 1085 | sz = ROUND_TO_PAGE(sz, page_mask); |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1086 | |
| 1087 | dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name); |
| 1088 | if (flash_write(ptn, extra, data, sz)) { |
| 1089 | fastboot_fail("flash write failure"); |
| 1090 | return; |
| 1091 | } |
| 1092 | dprintf(INFO, "partition '%s' updated\n", ptn->name); |
| 1093 | fastboot_okay(""); |
| 1094 | } |
| 1095 | |
| 1096 | void cmd_continue(const char *arg, void *data, unsigned sz) |
| 1097 | { |
| 1098 | fastboot_okay(""); |
| 1099 | udc_stop(); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1100 | if (target_is_emmc_boot()) |
| 1101 | { |
| 1102 | boot_linux_from_mmc(); |
| 1103 | } |
| 1104 | else |
| 1105 | { |
| 1106 | boot_linux_from_flash(); |
| 1107 | } |
Dima Zavin | 214cc64 | 2009-01-26 11:16:21 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1110 | void cmd_reboot(const char *arg, void *data, unsigned sz) |
| 1111 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1112 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1113 | fastboot_okay(""); |
| 1114 | reboot_device(0); |
| 1115 | } |
| 1116 | |
| 1117 | void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz) |
| 1118 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1119 | dprintf(INFO, "rebooting the device\n"); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1120 | fastboot_okay(""); |
| 1121 | reboot_device(FASTBOOT_MODE); |
| 1122 | } |
| 1123 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1124 | void cmd_oem_unlock(const char *arg, void *data, unsigned sz) |
| 1125 | { |
| 1126 | if(!device.is_unlocked) |
| 1127 | { |
| 1128 | device.is_unlocked = 1; |
| 1129 | write_device_info(&device); |
| 1130 | } |
| 1131 | fastboot_okay(""); |
| 1132 | } |
| 1133 | |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1134 | void cmd_oem_devinfo(const char *arg, void *data, unsigned sz) |
| 1135 | { |
| 1136 | char response[64]; |
| 1137 | snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false")); |
| 1138 | fastboot_info(response); |
| 1139 | snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false")); |
| 1140 | fastboot_info(response); |
| 1141 | fastboot_okay(""); |
| 1142 | } |
| 1143 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1144 | void splash_screen () |
| 1145 | { |
| 1146 | struct ptentry *ptn; |
| 1147 | struct ptable *ptable; |
| 1148 | struct fbcon_config *fb_display = NULL; |
| 1149 | |
| 1150 | if (!target_is_emmc_boot()) |
| 1151 | { |
| 1152 | ptable = flash_get_ptable(); |
| 1153 | if (ptable == NULL) { |
| 1154 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 1155 | return; |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | ptn = ptable_find(ptable, "splash"); |
| 1159 | if (ptn == NULL) { |
| 1160 | dprintf(CRITICAL, "ERROR: No splash partition found\n"); |
| 1161 | } else { |
| 1162 | fb_display = fbcon_display(); |
| 1163 | if (fb_display) { |
| 1164 | if (flash_read(ptn, 0, fb_display->base, |
| 1165 | (fb_display->width * fb_display->height * fb_display->bpp/8))) { |
| 1166 | fbcon_clear(); |
| 1167 | dprintf(CRITICAL, "ERROR: Cannot read splash image\n"); |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1174 | void aboot_init(const struct app_descriptor *app) |
| 1175 | { |
Shashank Mittal | 4f99a88 | 2010-02-01 13:58:50 -0800 | [diff] [blame] | 1176 | unsigned reboot_mode = 0; |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1177 | unsigned usb_init = 0; |
Vivek Mehta | 5f1c9d4 | 2011-04-01 20:11:59 -0700 | [diff] [blame] | 1178 | unsigned sz = 0; |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 1179 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1180 | /* Setup page size information for nand/emmc reads */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1181 | if (target_is_emmc_boot()) |
| 1182 | { |
| 1183 | page_size = 2048; |
| 1184 | page_mask = page_size - 1; |
| 1185 | } |
| 1186 | else |
| 1187 | { |
| 1188 | page_size = flash_page_size(); |
| 1189 | page_mask = page_size - 1; |
| 1190 | } |
| 1191 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1192 | if(target_use_signed_kernel()) |
| 1193 | { |
| 1194 | read_device_info(&device); |
| 1195 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 1198 | target_serialno((unsigned char *) sn_buf); |
Ajay Dudani | b06c05f | 2011-05-12 14:46:10 -0700 | [diff] [blame] | 1199 | dprintf(SPEW,"serial number: %s\n",sn_buf); |
Subbaraman Narayanamurthy | f17b4ae | 2011-02-16 20:19:56 -0800 | [diff] [blame] | 1200 | surf_udc_device.serialno = sn_buf; |
| 1201 | |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1202 | /* Check if we should do something other than booting up */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1203 | if (keys_get_state(KEY_HOME) != 0) |
| 1204 | boot_into_recovery = 1; |
Wentao Xu | 153902c | 2010-12-20 16:20:52 -0500 | [diff] [blame] | 1205 | if (keys_get_state(KEY_VOLUMEUP) != 0) |
| 1206 | boot_into_recovery = 1; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 1207 | if(!boot_into_recovery) |
| 1208 | { |
| 1209 | if (keys_get_state(KEY_BACK) != 0) |
| 1210 | goto fastboot; |
| 1211 | if (keys_get_state(KEY_VOLUMEDOWN) != 0) |
| 1212 | goto fastboot; |
| 1213 | } |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1214 | |
| 1215 | #if NO_KEYPAD_DRIVER |
Kinson Chik | 0b1c816 | 2011-08-31 16:31:57 -0700 | [diff] [blame] | 1216 | if (fastboot_trigger()) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1217 | goto fastboot; |
| 1218 | #endif |
Chandan Uddaraju | bedca15 | 2010-06-02 23:05:15 -0700 | [diff] [blame] | 1219 | |
Ajay Dudani | 7742129 | 2010-10-27 19:34:06 -0700 | [diff] [blame] | 1220 | reboot_mode = check_reboot_mode(); |
| 1221 | if (reboot_mode == RECOVERY_MODE) { |
| 1222 | boot_into_recovery = 1; |
| 1223 | } else if(reboot_mode == FASTBOOT_MODE) { |
| 1224 | goto fastboot; |
| 1225 | } |
| 1226 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1227 | if (target_is_emmc_boot()) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1228 | { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 1229 | if(emmc_recovery_init()) |
| 1230 | dprintf(ALWAYS,"error in emmc_recovery_init\n"); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1231 | if(target_use_signed_kernel()) |
| 1232 | { |
| 1233 | if((device.is_unlocked) || (device.is_tampered)) |
| 1234 | { |
| 1235 | #ifdef TZ_TAMPER_FUSE |
| 1236 | set_tamper_fuse_cmd(); |
| 1237 | #endif |
| 1238 | } |
| 1239 | } |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1240 | boot_linux_from_mmc(); |
| 1241 | } |
| 1242 | else |
| 1243 | { |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1244 | recovery_init(); |
| 1245 | boot_linux_from_flash(); |
| 1246 | } |
Dima Zavin | b428360 | 2009-01-26 16:36:57 -0800 | [diff] [blame] | 1247 | dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting " |
| 1248 | "to fastboot mode.\n"); |
| 1249 | |
| 1250 | fastboot: |
Chandan Uddaraju | 2943fd6 | 2010-06-21 10:56:39 -0700 | [diff] [blame] | 1251 | |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1252 | target_fastboot_init(); |
Amol Jadi | 57abe4c | 2011-05-24 15:47:27 -0700 | [diff] [blame] | 1253 | |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1254 | if(!usb_init) |
| 1255 | udc_init(&surf_udc_device); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1256 | |
| 1257 | fastboot_register("boot", cmd_boot); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1258 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 1259 | if (target_is_emmc_boot()) |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1260 | { |
| 1261 | fastboot_register("flash:", cmd_flash_mmc); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1262 | fastboot_register("erase:", cmd_erase_mmc); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1263 | } |
| 1264 | else |
| 1265 | { |
| 1266 | fastboot_register("flash:", cmd_flash); |
Bikas Gurung | d48bd24 | 2010-09-04 19:54:32 -0700 | [diff] [blame] | 1267 | fastboot_register("erase:", cmd_erase); |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | fastboot_register("continue", cmd_continue); |
Chandan Uddaraju | 94183c0 | 2010-01-15 15:13:59 -0800 | [diff] [blame] | 1271 | fastboot_register("reboot", cmd_reboot); |
| 1272 | fastboot_register("reboot-bootloader", cmd_reboot_bootloader); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 1273 | fastboot_register("oem unlock", cmd_oem_unlock); |
Shashank Mittal | a003228 | 2011-08-26 14:50:11 -0700 | [diff] [blame] | 1274 | fastboot_register("oem device-info", cmd_oem_devinfo); |
Subbaraman Narayanamurthy | eb92bcc | 2010-07-20 14:32:46 -0700 | [diff] [blame] | 1275 | fastboot_publish("product", TARGET(BOARD)); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1276 | fastboot_publish("kernel", "lk"); |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 1277 | partition_dump(); |
Vivek Mehta | 5f1c9d4 | 2011-04-01 20:11:59 -0700 | [diff] [blame] | 1278 | sz = target_get_max_flash_size(); |
| 1279 | fastboot_init(target_get_scratch_address(), sz); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1280 | udc_start(); |
Brian Swetland | 9c4c075 | 2009-01-25 16:23:50 -0800 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | APP_START(aboot) |
| 1284 | .init = aboot_init, |
| 1285 | APP_END |
| 1286 | |