blob: 5a68a4bd541af1d9e069dd3385183b53c1f5c7e4 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Ajay Dudanid04110c2011-01-17 23:55:07 -08005 * Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * 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 Swetland9c4c0752009-01-25 16:23:50 -080031 */
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 Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Dima Zavin214cc642009-01-26 11:16:21 -080051
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080053#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080054#include "bootimg.h"
55#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070056#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070057#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070058#include "devinfo.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080059
Shashank Mittal162244e2011-08-08 19:01:25 -070060#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070061
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070062#define EXPAND(NAME) #NAME
63#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080064#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070065
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080066#ifdef MEMBASE
67#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
68#else
David Ng183a7422009-12-07 14:55:21 -080069#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080070#endif
71
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080072#define RECOVERY_MODE 0x77665502
73#define FASTBOOT_MODE 0x77665500
74
David Ng183a7422009-12-07 14:55:21 -080075static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080076static const char *usb_sn_cmdline = " androidboot.serialno=";
Ajay Dudanica3a33c2011-11-18 08:31:40 -080077static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -070078static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -080079
Ajay Dudani6cff85e2011-02-04 16:02:16 -080080static const char *baseband_apq = " androidboot.baseband=apq";
81static const char *baseband_msm = " androidboot.baseband=msm";
82static const char *baseband_csfb = " androidboot.baseband=csfb";
83static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -070084static const char *baseband_mdm = " androidboot.baseband=mdm";
Ajay Dudanid04110c2011-01-17 23:55:07 -080085
Shashank Mittalcd98d472011-08-02 14:29:24 -070086/* Assuming unauthorized kernel image by default */
87static int auth_kernel_img = 0;
88
Shashank Mittal162244e2011-08-08 19:01:25 -070089static device_info device = {DEVICE_MAGIC, 0, 0};
90
Brian Swetland9c4c0752009-01-25 16:23:50 -080091static struct udc_device surf_udc_device = {
92 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080093 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080094 .version_id = 0x0100,
95 .manufacturer = "Google",
96 .product = "Android",
97};
98
Dima Zavin42168f22009-01-30 11:52:22 -080099struct atag_ptbl_entry
100{
101 char name[16];
102 unsigned offset;
103 unsigned size;
104 unsigned flags;
105};
106
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800107char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700108
Greg Griscod2471ef2011-07-14 13:00:42 -0700109extern int emmc_recovery_init(void);
110
Kinson Chik0b1c8162011-08-31 16:31:57 -0700111#if NO_KEYPAD_DRIVER
112extern int fastboot_trigger(void);
113#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700114
Dima Zavin42168f22009-01-30 11:52:22 -0800115static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
116{
117 struct atag_ptbl_entry atag_ptn;
118
119 memcpy(atag_ptn.name, ptn->name, 16);
120 atag_ptn.name[15] = '\0';
121 atag_ptn.offset = ptn->start;
122 atag_ptn.size = ptn->length;
123 atag_ptn.flags = ptn->flags;
124 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
125 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
126}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800127
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700128void boot_linux(void *kernel, unsigned *tags,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800129 const char *cmdline, unsigned machtype,
130 void *ramdisk, unsigned ramdisk_size)
131{
132 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800133 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800134 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800135 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800136 int cmdline_len = 0;
137 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700138 int pause_at_bootup = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800139 unsigned char *cmdline_final = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800140
141 /* CORE */
142 *ptr++ = 2;
143 *ptr++ = 0x54410001;
144
145 if (ramdisk_size) {
146 *ptr++ = 4;
147 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800148 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800149 *ptr++ = ramdisk_size;
150 }
151
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800152 ptr = target_atag_mem(ptr);
153
David Ng183a7422009-12-07 14:55:21 -0800154 if (!target_is_emmc_boot()) {
155 /* Skip NAND partition ATAGS for eMMC boot */
156 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
157 int i;
Mekala Natarajanc43d8842011-10-26 15:09:10 -0700158 *ptr++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800159 sizeof(unsigned)));
160 *ptr++ = 0x4d534d70;
161 for (i = 0; i < ptable->count; ++i)
162 ptentry_to_tag(&ptr, ptable_get(ptable, i));
163 }
Dima Zavin42168f22009-01-30 11:52:22 -0800164 }
165
Brian Swetland9c4c0752009-01-25 16:23:50 -0800166 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800167 cmdline_len = strlen(cmdline);
168 have_cmdline = 1;
169 }
170 if (target_is_emmc_boot()) {
171 cmdline_len += strlen(emmc_cmdline);
172 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800173
174 cmdline_len += strlen(usb_sn_cmdline);
175 cmdline_len += strlen(sn_buf);
176
David Ngf773dde2010-07-26 19:55:08 -0700177 if (target_pause_for_battery_charge()) {
178 pause_at_bootup = 1;
179 cmdline_len += strlen(battchg_pause);
180 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800181
Shashank Mittalcd98d472011-08-02 14:29:24 -0700182 if(target_use_signed_kernel() && auth_kernel_img) {
183 cmdline_len += strlen(auth_kernel);
184 }
185
Ajay Dudanid04110c2011-01-17 23:55:07 -0800186 /* Determine correct androidboot.baseband to use */
187 switch(target_baseband())
188 {
189 case BASEBAND_APQ:
190 cmdline_len += strlen(baseband_apq);
191 break;
192
193 case BASEBAND_MSM:
194 cmdline_len += strlen(baseband_msm);
195 break;
196
197 case BASEBAND_CSFB:
198 cmdline_len += strlen(baseband_csfb);
199 break;
200
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800201 case BASEBAND_SVLTE2A:
202 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800203 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700204
205 case BASEBAND_MDM:
206 cmdline_len += strlen(baseband_mdm);
207 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800208 }
209
David Ng183a7422009-12-07 14:55:21 -0800210 if (cmdline_len > 0) {
211 const char *src;
212 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800213 unsigned n;
214 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800215 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800216 *ptr++ = (n / 4) + 2;
217 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800218 dst = (char *)ptr;
Amol Jadi168b7712012-03-06 16:15:00 -0800219 /* Save start ptr for debug print */
220 cmdline_final = (char *)ptr;
David Ng183a7422009-12-07 14:55:21 -0800221 if (have_cmdline) {
222 src = cmdline;
223 while ((*dst++ = *src++));
224 }
225 if (target_is_emmc_boot()) {
226 src = emmc_cmdline;
227 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700228 have_cmdline = 1;
229 while ((*dst++ = *src++));
230 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800231
232 src = usb_sn_cmdline;
233 if (have_cmdline) --dst;
234 have_cmdline = 1;
235 while ((*dst++ = *src++));
236 src = sn_buf;
237 if (have_cmdline) --dst;
238 have_cmdline = 1;
239 while ((*dst++ = *src++));
240
David Ngf773dde2010-07-26 19:55:08 -0700241 if (pause_at_bootup) {
242 src = battchg_pause;
243 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800244 while ((*dst++ = *src++));
245 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800246
Shashank Mittalcd98d472011-08-02 14:29:24 -0700247 if(target_use_signed_kernel() && auth_kernel_img) {
248 src = auth_kernel;
249 if (have_cmdline) --dst;
250 while ((*dst++ = *src++));
251 }
252
Ajay Dudanid04110c2011-01-17 23:55:07 -0800253 switch(target_baseband())
254 {
255 case BASEBAND_APQ:
256 src = baseband_apq;
257 if (have_cmdline) --dst;
258 while ((*dst++ = *src++));
259 break;
260
261 case BASEBAND_MSM:
262 src = baseband_msm;
263 if (have_cmdline) --dst;
264 while ((*dst++ = *src++));
265 break;
266
267 case BASEBAND_CSFB:
268 src = baseband_csfb;
269 if (have_cmdline) --dst;
270 while ((*dst++ = *src++));
271 break;
272
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800273 case BASEBAND_SVLTE2A:
274 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800275 if (have_cmdline) --dst;
276 while ((*dst++ = *src++));
277 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700278
279 case BASEBAND_MDM:
280 src = baseband_mdm;
281 if (have_cmdline) --dst;
282 while ((*dst++ = *src++));
283 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800284 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800285 ptr += (n / 4);
286 }
287
288 /* END */
289 *ptr++ = 0;
290 *ptr++ = 0;
291
292 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
293 kernel, ramdisk, ramdisk_size);
Amol Jadi168b7712012-03-06 16:15:00 -0800294 if (cmdline_final)
295 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800296
297 enter_critical_section();
Amol Jadi4421e652011-06-16 15:00:48 -0700298 /* do any platform specific cleanup before kernel entry */
299 platform_uninit();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800300 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800301 arch_disable_mmu();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800302 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800303}
304
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800305unsigned page_size = 0;
306unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800307
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800308#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800309
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800310static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800311
Shashank Mittal23b8f422010-04-16 19:27:21 -0700312int boot_linux_from_mmc(void)
313{
314 struct boot_img_hdr *hdr = (void*) buf;
315 struct boot_img_hdr *uhdr;
316 unsigned offset = 0;
317 unsigned long long ptn = 0;
318 unsigned n = 0;
319 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700320 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700321
Shashank Mittalcd98d472011-08-02 14:29:24 -0700322 unsigned char *image_addr = 0;
323 unsigned kernel_actual;
324 unsigned ramdisk_actual;
325 unsigned imagesize_actual;
326
Shashank Mittal23b8f422010-04-16 19:27:21 -0700327 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
328 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
329 dprintf(INFO, "Unified boot method!\n");
330 hdr = uhdr;
331 goto unified_boot;
332 }
Greg Griscod6250552011-06-29 14:40:23 -0700333 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700334 index = partition_get_index("boot");
335 ptn = partition_get_offset(index);
336 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700337 dprintf(CRITICAL, "ERROR: No boot partition found\n");
338 return -1;
339 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700340 }
341 else {
342 index = partition_get_index("recovery");
343 ptn = partition_get_offset(index);
344 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700345 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
346 return -1;
347 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700348 }
349
Greg Griscod6250552011-06-29 14:40:23 -0700350 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700351 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
352 return -1;
353 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700354
355 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700356 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700357 return -1;
358 }
359
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700360 if (hdr->page_size && (hdr->page_size != page_size)) {
361 page_size = hdr->page_size;
362 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700363 }
364
Shashank Mittalcd98d472011-08-02 14:29:24 -0700365 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700366 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800367 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700368 image_addr = (unsigned char *)target_get_scratch_address();
369 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
370 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
371 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
372
373 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700374
375 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700376 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700377
Shashank Mittalcd98d472011-08-02 14:29:24 -0700378 /* Read image without signature */
379 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
380 {
381 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
382 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800383 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700384
385 offset = imagesize_actual;
386 /* Read signature */
387 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
388 {
389 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
390 }
391 else
392 {
393 auth_kernel_img = image_verify((unsigned char *)image_addr,
394 (unsigned char *)(image_addr + imagesize_actual),
395 imagesize_actual,
396 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700397
398 if(auth_kernel_img)
399 {
400 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700401 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700402 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700403 }
404
405 /* Move kernel and ramdisk to correct address */
406 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
407 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700408
409 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700410 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700411 {
412 write_device_info_mmc(&device);
413 #ifdef TZ_TAMPER_FUSE
414 set_tamper_fuse_cmd();
415 #endif
416 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530417 #if USE_PCOM_SECBOOT
418 set_tamper_flag(device.is_tampered);
419 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700420 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700421 else
422 {
423 offset += page_size;
424
425 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
426 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
427 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
428 return -1;
429 }
430 offset += n;
431
432 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
433 if(n != 0)
434 {
435 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
436 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
437 return -1;
438 }
439 }
440 offset += n;
441 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700442
443unified_boot:
444 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
445 hdr->kernel_size);
446 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
447 hdr->ramdisk_size);
448
449 if(hdr->cmdline[0]) {
450 cmdline = (char*) hdr->cmdline;
451 } else {
452 cmdline = DEFAULT_CMDLINE;
453 }
454 dprintf(INFO, "cmdline = '%s'\n", cmdline);
455
456 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700457 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700458 (const char *)cmdline, board_machtype(),
459 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
460
461 return 0;
462}
463
Dima Zavin214cc642009-01-26 11:16:21 -0800464int boot_linux_from_flash(void)
465{
466 struct boot_img_hdr *hdr = (void*) buf;
467 unsigned n;
468 struct ptentry *ptn;
469 struct ptable *ptable;
470 unsigned offset = 0;
471 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800472
Shashank Mittalcd98d472011-08-02 14:29:24 -0700473 unsigned char *image_addr = 0;
474 unsigned kernel_actual;
475 unsigned ramdisk_actual;
476 unsigned imagesize_actual;
477
David Ng183a7422009-12-07 14:55:21 -0800478 if (target_is_emmc_boot()) {
479 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
480 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
481 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
482 return -1;
483 }
484 goto continue_boot;
485 }
486
Dima Zavin214cc642009-01-26 11:16:21 -0800487 ptable = flash_get_ptable();
488 if (ptable == NULL) {
489 dprintf(CRITICAL, "ERROR: Partition table not found\n");
490 return -1;
491 }
492
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800493 if(!boot_into_recovery)
494 {
495 ptn = ptable_find(ptable, "boot");
496 if (ptn == NULL) {
497 dprintf(CRITICAL, "ERROR: No boot partition found\n");
498 return -1;
499 }
500 }
501 else
502 {
503 ptn = ptable_find(ptable, "recovery");
504 if (ptn == NULL) {
505 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
506 return -1;
507 }
Dima Zavin214cc642009-01-26 11:16:21 -0800508 }
509
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800510 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800511 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
512 return -1;
513 }
Dima Zavin214cc642009-01-26 11:16:21 -0800514
515 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700516 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800517 return -1;
518 }
519
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800520 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700521 dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800522 return -1;
523 }
524
Shashank Mittalcd98d472011-08-02 14:29:24 -0700525 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700526 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700527 {
528 image_addr = (unsigned char *)target_get_scratch_address();
529 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
530 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
531 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800532
Shashank Mittalcd98d472011-08-02 14:29:24 -0700533 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700534
535 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700536 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700537
Shashank Mittalcd98d472011-08-02 14:29:24 -0700538 /* Read image without signature */
539 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
540 {
541 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
542 return -1;
543 }
Dima Zavin214cc642009-01-26 11:16:21 -0800544
Shashank Mittalcd98d472011-08-02 14:29:24 -0700545 offset = imagesize_actual;
546 /* Read signature */
547 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
548 {
549 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
550 }
551 else
552 {
553
554 /* Verify signature */
555 auth_kernel_img = image_verify((unsigned char *)image_addr,
556 (unsigned char *)(image_addr + imagesize_actual),
557 imagesize_actual,
558 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700559
560 if(auth_kernel_img)
561 {
562 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700563 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700564 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700565 }
566
567 /* Move kernel and ramdisk to correct address */
568 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
569 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700570
571 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700572 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700573 {
574 write_device_info_flash(&device);
575 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530576#if USE_PCOM_SECBOOT
577 set_tamper_flag(device.is_tampered);
578#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700579 }
580 else
581 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700582 offset = page_size;
583
Shashank Mittalcd98d472011-08-02 14:29:24 -0700584 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
585 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
586 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
587 return -1;
588 }
589 offset += n;
590
591 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
592 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
593 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
594 return -1;
595 }
596 offset += n;
597 }
David Ng183a7422009-12-07 14:55:21 -0800598continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800599 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
600 hdr->kernel_size);
601 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
602 hdr->ramdisk_size);
603
604 if(hdr->cmdline[0]) {
605 cmdline = (char*) hdr->cmdline;
606 } else {
607 cmdline = DEFAULT_CMDLINE;
608 }
609 dprintf(INFO, "cmdline = '%s'\n", cmdline);
610
611 /* TODO: create/pass atags to kernel */
612
613 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700614 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800615 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800616 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
617
618 return 0;
619}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800620
Shashank Mittal162244e2011-08-08 19:01:25 -0700621unsigned char info_buf[4096];
622void write_device_info_mmc(device_info *dev)
623{
624 struct device_info *info = (void*) info_buf;
625 unsigned long long ptn = 0;
626 unsigned long long size;
627 int index = INVALID_PTN;
628
629 index = partition_get_index("aboot");
630 ptn = partition_get_offset(index);
631 if(ptn == 0)
632 {
633 return;
634 }
635
636 size = partition_get_size(index);
637
638 memcpy(info, dev, sizeof(device_info));
639
640 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
641 {
642 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
643 return;
644 }
645}
646
647void read_device_info_mmc(device_info *dev)
648{
649 struct device_info *info = (void*) info_buf;
650 unsigned long long ptn = 0;
651 unsigned long long size;
652 int index = INVALID_PTN;
653
654 index = partition_get_index("aboot");
655 ptn = partition_get_offset(index);
656 if(ptn == 0)
657 {
658 return;
659 }
660
661 size = partition_get_size(index);
662
663 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
664 {
665 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
666 return;
667 }
668
669 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
670 {
671 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
672 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700673 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700674
675 write_device_info_mmc(info);
676 }
677 memcpy(dev, info, sizeof(device_info));
678}
679
680void write_device_info_flash(device_info *dev)
681{
682 struct device_info *info = (void *) info_buf;
683 struct ptentry *ptn;
684 struct ptable *ptable;
685
686 ptable = flash_get_ptable();
687 if (ptable == NULL)
688 {
689 dprintf(CRITICAL, "ERROR: Partition table not found\n");
690 return;
691 }
692
693 ptn = ptable_find(ptable, "devinfo");
694 if (ptn == NULL)
695 {
696 dprintf(CRITICAL, "ERROR: No boot partition found\n");
697 return;
698 }
699
700 memcpy(info, dev, sizeof(device_info));
701
702 if (flash_write(ptn, 0, (void *)info_buf, page_size))
703 {
704 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
705 return;
706 }
707}
708
709void read_device_info_flash(device_info *dev)
710{
711 struct device_info *info = (void*) info_buf;
712 struct ptentry *ptn;
713 struct ptable *ptable;
714
715 ptable = flash_get_ptable();
716 if (ptable == NULL)
717 {
718 dprintf(CRITICAL, "ERROR: Partition table not found\n");
719 return;
720 }
721
722 ptn = ptable_find(ptable, "devinfo");
723 if (ptn == NULL)
724 {
725 dprintf(CRITICAL, "ERROR: No boot partition found\n");
726 return;
727 }
728
729 if (flash_read(ptn, 0, (void *)info_buf, page_size))
730 {
731 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
732 return;
733 }
734
735 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
736 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700737 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
738 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700739 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700740 write_device_info_flash(info);
741 }
742 memcpy(dev, info, sizeof(device_info));
743}
744
745void write_device_info(device_info *dev)
746{
747 if(target_is_emmc_boot())
748 {
749 write_device_info_mmc(dev);
750 }
751 else
752 {
753 write_device_info_flash(dev);
754 }
755}
756
757void read_device_info(device_info *dev)
758{
759 if(target_is_emmc_boot())
760 {
761 read_device_info_mmc(dev);
762 }
763 else
764 {
765 read_device_info_flash(dev);
766 }
767}
768
769void reset_device_info()
770{
771 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700772 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700773 write_device_info(&device);
774}
775
776void set_device_root()
777{
778 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700779 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700780 write_device_info(&device);
781}
782
Brian Swetland9c4c0752009-01-25 16:23:50 -0800783void cmd_boot(const char *arg, void *data, unsigned sz)
784{
785 unsigned kernel_actual;
786 unsigned ramdisk_actual;
787 static struct boot_img_hdr hdr;
788 char *ptr = ((char*) data);
789
790 if (sz < sizeof(hdr)) {
791 fastboot_fail("invalid bootimage header");
792 return;
793 }
794
795 memcpy(&hdr, data, sizeof(hdr));
796
797 /* ensure commandline is terminated */
798 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
799
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700800 if(target_is_emmc_boot() && hdr.page_size) {
801 page_size = hdr.page_size;
802 page_mask = page_size - 1;
803 }
804
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800805 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
806 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800807
Shashank Mittal1f0e2662011-09-01 15:06:00 -0700808 /* sz should have atleast raw boot image */
809 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800810 fastboot_fail("incomplete bootimage");
811 return;
812 }
813
Ajay Dudanie28a6072011-07-01 13:59:46 -0700814 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
815 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800816
817 fastboot_okay("");
818 udc_stop();
819
Amol Jadie67872e2011-06-27 14:14:11 -0700820 boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800821 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -0700822 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800823}
824
Dima Zavin214cc642009-01-26 11:16:21 -0800825void cmd_erase(const char *arg, void *data, unsigned sz)
826{
827 struct ptentry *ptn;
828 struct ptable *ptable;
829
830 ptable = flash_get_ptable();
831 if (ptable == NULL) {
832 fastboot_fail("partition table doesn't exist");
833 return;
834 }
835
836 ptn = ptable_find(ptable, arg);
837 if (ptn == NULL) {
838 fastboot_fail("unknown partition name");
839 return;
840 }
841
842 if (flash_erase(ptn)) {
843 fastboot_fail("failed to erase partition");
844 return;
845 }
846 fastboot_okay("");
847}
848
Bikas Gurungd48bd242010-09-04 19:54:32 -0700849
850void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
851{
852 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -0800853 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -0700854 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -0700855
Kinson Chikf1a43512011-07-14 11:28:39 -0700856 index = partition_get_index(arg);
857 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -0700858
Kinson Chikf1a43512011-07-14 11:28:39 -0700859 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -0700860 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -0700861 return;
862 }
neetidb4b24d62012-01-20 12:13:09 -0800863 /* Simple inefficient version of erase. Just writing
864 0 in first block */
865 if (mmc_write(ptn , 512, (unsigned int *)out)) {
866 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -0700867 return;
868 }
869 fastboot_okay("");
870}
871
872
Ajay Dudani5c761132011-04-07 20:19:04 -0700873void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -0700874{
875 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700876 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700877 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700878
Greg Grisco6e754772011-06-23 12:19:39 -0700879 if (!strcmp(arg, "partition"))
880 {
881 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -0700882 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -0700883 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700884 return;
885 }
886 }
Greg Grisco6e754772011-06-23 12:19:39 -0700887 else
888 {
Kinson Chikf1a43512011-07-14 11:28:39 -0700889 index = partition_get_index(arg);
890 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700891 if(ptn == 0) {
892 fastboot_fail("partition table doesn't exist");
893 return;
894 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700895
Greg Grisco6e754772011-06-23 12:19:39 -0700896 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
897 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
898 fastboot_fail("image is not a boot image");
899 return;
900 }
901 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700902
Kinson Chikf1a43512011-07-14 11:28:39 -0700903 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700904 if (ROUND_TO_PAGE(sz,511) > size) {
905 fastboot_fail("size too large");
906 return;
907 }
908 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
909 fastboot_fail("flash write failure");
910 return;
911 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700912 }
913 fastboot_okay("");
914 return;
915}
916
Ajay Dudani5c761132011-04-07 20:19:04 -0700917void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
918{
919 unsigned int chunk;
920 unsigned int chunk_data_sz;
921 sparse_header_t *sparse_header;
922 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -0700923 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700924 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +0530925 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700926 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -0700927
Kinson Chikf1a43512011-07-14 11:28:39 -0700928 index = partition_get_index(arg);
929 ptn = partition_get_offset(index);
930 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -0700931 fastboot_fail("partition table doesn't exist");
932 return;
933 }
934
Channagoud Kadabi65b91002011-10-11 17:34:33 +0530935 size = partition_get_size(index);
936 if (ROUND_TO_PAGE(sz,511) > size) {
937 fastboot_fail("size too large");
938 return;
939 }
940
Ajay Dudani5c761132011-04-07 20:19:04 -0700941 /* Read and skip over sparse image header */
942 sparse_header = (sparse_header_t *) data;
943 data += sparse_header->file_hdr_sz;
944 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
945 {
946 /* Skip the remaining bytes in a header that is longer than
947 * we expected.
948 */
949 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
950 }
951
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700952 dprintf (SPEW, "=== Sparse Image Header ===\n");
953 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
954 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
955 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
956 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
957 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
958 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
959 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
960 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -0700961
962 /* Start processing chunks */
963 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
964 {
965 /* Read and skip over chunk header */
966 chunk_header = (chunk_header_t *) data;
967 data += sizeof(chunk_header_t);
968
969 dprintf (SPEW, "=== Chunk Header ===\n");
970 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
971 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
972 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
973
974 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
975 {
976 /* Skip the remaining bytes in a header that is longer than
977 * we expected.
978 */
979 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
980 }
981
982 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
983 switch (chunk_header->chunk_type)
984 {
985 case CHUNK_TYPE_RAW:
986 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
987 chunk_data_sz))
988 {
989 fastboot_fail("Bogus chunk size for chunk type Raw");
990 return;
991 }
992
Ajay Dudaniab18f022011-05-12 14:39:22 -0700993 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
994 chunk_data_sz,
995 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -0700996 {
997 fastboot_fail("flash write failure");
998 return;
999 }
1000 total_blocks += chunk_header->chunk_sz;
1001 data += chunk_data_sz;
1002 break;
1003
1004 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001005 total_blocks += chunk_header->chunk_sz;
1006 break;
1007
Ajay Dudani5c761132011-04-07 20:19:04 -07001008 case CHUNK_TYPE_CRC:
1009 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1010 {
1011 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1012 return;
1013 }
1014 total_blocks += chunk_header->chunk_sz;
1015 data += chunk_data_sz;
1016 break;
1017
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001018 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001019 fastboot_fail("Unknown chunk type");
1020 return;
1021 }
1022 }
1023
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001024 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1025 total_blocks, sparse_header->total_blks);
1026
1027 if(total_blocks != sparse_header->total_blks)
1028 {
1029 fastboot_fail("sparse image write failure");
1030 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001031
1032 fastboot_okay("");
1033 return;
1034}
1035
1036void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1037{
1038 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001039 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1040 unsigned int *magic_number = (unsigned int *) data;
1041 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001042
Neeti Desai127b9e02012-03-20 16:11:23 -07001043 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1044 magic_number[1] == DECRYPT_MAGIC_1)
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001045 {
1046#ifdef SSD_ENABLE
Neeti Desai127b9e02012-03-20 16:11:23 -07001047 ret = decrypt_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001048#endif
Greg Griscod6250552011-06-29 14:40:23 -07001049 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001050 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1051 return;
1052 }
1053 }
Neeti Desai127b9e02012-03-20 16:11:23 -07001054 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1055 magic_number[1] == ENCRYPT_MAGIC_1)
1056 {
1057#ifdef SSD_ENABLE
1058 ret = encrypt_scm((uint32 **) &data, &sz);
1059#endif
1060 if (ret != 0) {
1061 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1062 return;
1063 }
1064 }
1065
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001066 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001067 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1068 cmd_flash_mmc_img(arg, data, sz);
1069 else
1070 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001071 return;
1072}
1073
Dima Zavin214cc642009-01-26 11:16:21 -08001074void cmd_flash(const char *arg, void *data, unsigned sz)
1075{
1076 struct ptentry *ptn;
1077 struct ptable *ptable;
1078 unsigned extra = 0;
1079
1080 ptable = flash_get_ptable();
1081 if (ptable == NULL) {
1082 fastboot_fail("partition table doesn't exist");
1083 return;
1084 }
1085
1086 ptn = ptable_find(ptable, arg);
1087 if (ptn == NULL) {
1088 fastboot_fail("unknown partition name");
1089 return;
1090 }
1091
1092 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1093 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1094 fastboot_fail("image is not a boot image");
1095 return;
1096 }
1097 }
1098
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001099 if (!strcmp(ptn->name, "system")
1100 || !strcmp(ptn->name, "userdata")
1101 || !strcmp(ptn->name, "persist")
1102 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301103 if (flash_ecc_bch_enabled())
1104 /* Spare data bytes for 8 bit ECC increased by 4 */
1105 extra = ((page_size >> 9) * 20);
1106 else
1107 extra = ((page_size >> 9) * 16);
1108 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001109 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001110
1111 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1112 if (flash_write(ptn, extra, data, sz)) {
1113 fastboot_fail("flash write failure");
1114 return;
1115 }
1116 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1117 fastboot_okay("");
1118}
1119
1120void cmd_continue(const char *arg, void *data, unsigned sz)
1121{
1122 fastboot_okay("");
1123 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001124 if (target_is_emmc_boot())
1125 {
1126 boot_linux_from_mmc();
1127 }
1128 else
1129 {
1130 boot_linux_from_flash();
1131 }
Dima Zavin214cc642009-01-26 11:16:21 -08001132}
1133
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001134void cmd_reboot(const char *arg, void *data, unsigned sz)
1135{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001136 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001137 fastboot_okay("");
1138 reboot_device(0);
1139}
1140
1141void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1142{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001143 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001144 fastboot_okay("");
1145 reboot_device(FASTBOOT_MODE);
1146}
1147
Shashank Mittal162244e2011-08-08 19:01:25 -07001148void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1149{
1150 if(!device.is_unlocked)
1151 {
1152 device.is_unlocked = 1;
1153 write_device_info(&device);
1154 }
1155 fastboot_okay("");
1156}
1157
Shashank Mittala0032282011-08-26 14:50:11 -07001158void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1159{
1160 char response[64];
1161 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1162 fastboot_info(response);
1163 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1164 fastboot_info(response);
1165 fastboot_okay("");
1166}
1167
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001168void splash_screen ()
1169{
1170 struct ptentry *ptn;
1171 struct ptable *ptable;
1172 struct fbcon_config *fb_display = NULL;
1173
1174 if (!target_is_emmc_boot())
1175 {
1176 ptable = flash_get_ptable();
1177 if (ptable == NULL) {
1178 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001179 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001180 }
1181
1182 ptn = ptable_find(ptable, "splash");
1183 if (ptn == NULL) {
1184 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1185 } else {
1186 fb_display = fbcon_display();
1187 if (fb_display) {
1188 if (flash_read(ptn, 0, fb_display->base,
1189 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1190 fbcon_clear();
1191 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1192 }
1193 }
1194 }
1195 }
1196}
1197
Brian Swetland9c4c0752009-01-25 16:23:50 -08001198void aboot_init(const struct app_descriptor *app)
1199{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001200 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001201 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001202 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001203
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001204 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001205 if (target_is_emmc_boot())
1206 {
1207 page_size = 2048;
1208 page_mask = page_size - 1;
1209 }
1210 else
1211 {
1212 page_size = flash_page_size();
1213 page_mask = page_size - 1;
1214 }
1215
Shashank Mittal162244e2011-08-08 19:01:25 -07001216 if(target_use_signed_kernel())
1217 {
1218 read_device_info(&device);
1219
Shashank Mittal162244e2011-08-08 19:01:25 -07001220 }
1221
Greg Griscod6250552011-06-29 14:40:23 -07001222 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001223 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001224 surf_udc_device.serialno = sn_buf;
1225
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001226 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001227 if (keys_get_state(KEY_HOME) != 0)
1228 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001229 if (keys_get_state(KEY_VOLUMEUP) != 0)
1230 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001231 if(!boot_into_recovery)
1232 {
1233 if (keys_get_state(KEY_BACK) != 0)
1234 goto fastboot;
1235 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1236 goto fastboot;
1237 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001238
1239 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001240 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001241 goto fastboot;
1242 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001243
Ajay Dudani77421292010-10-27 19:34:06 -07001244 reboot_mode = check_reboot_mode();
1245 if (reboot_mode == RECOVERY_MODE) {
1246 boot_into_recovery = 1;
1247 } else if(reboot_mode == FASTBOOT_MODE) {
1248 goto fastboot;
1249 }
1250
Shashank Mittal23b8f422010-04-16 19:27:21 -07001251 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001252 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001253 if(emmc_recovery_init())
1254 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001255 if(target_use_signed_kernel())
1256 {
1257 if((device.is_unlocked) || (device.is_tampered))
1258 {
1259 #ifdef TZ_TAMPER_FUSE
1260 set_tamper_fuse_cmd();
1261 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301262 #if USE_PCOM_SECBOOT
1263 set_tamper_flag(device.is_tampered);
1264 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001265 }
1266 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001267 boot_linux_from_mmc();
1268 }
1269 else
1270 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001271 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301272#if USE_PCOM_SECBOOT
1273 if((device.is_unlocked) || (device.is_tampered))
1274 set_tamper_flag(device.is_tampered);
1275#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001276 boot_linux_from_flash();
1277 }
Dima Zavinb4283602009-01-26 16:36:57 -08001278 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1279 "to fastboot mode.\n");
1280
1281fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001282
Shashank Mittal162244e2011-08-08 19:01:25 -07001283 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001284
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001285 if(!usb_init)
1286 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001287
1288 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001289
Shashank Mittal23b8f422010-04-16 19:27:21 -07001290 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001291 {
1292 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001293 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001294 }
1295 else
1296 {
1297 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001298 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001299 }
1300
1301 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001302 fastboot_register("reboot", cmd_reboot);
1303 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001304 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001305 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001306 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001307 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001308 fastboot_publish("serialno", sn_buf);
Kinson Chikf1a43512011-07-14 11:28:39 -07001309 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001310 sz = target_get_max_flash_size();
1311 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001312 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001313}
1314
1315APP_START(aboot)
1316 .init = aboot_init,
1317APP_END
1318