blob: 9ca4bce4bc60e058bd6ebd2b91c95da7743e68fb [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";
Amol Jadi5c61a952012-05-04 17:05:35 -070085static const char *baseband_sglte = " androidboot.baseband=sglte";
Ajay Dudanid04110c2011-01-17 23:55:07 -080086
Shashank Mittalcd98d472011-08-02 14:29:24 -070087/* Assuming unauthorized kernel image by default */
88static int auth_kernel_img = 0;
89
Shashank Mittal162244e2011-08-08 19:01:25 -070090static device_info device = {DEVICE_MAGIC, 0, 0};
91
Brian Swetland9c4c0752009-01-25 16:23:50 -080092static struct udc_device surf_udc_device = {
93 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080094 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080095 .version_id = 0x0100,
96 .manufacturer = "Google",
97 .product = "Android",
98};
99
Dima Zavin42168f22009-01-30 11:52:22 -0800100struct atag_ptbl_entry
101{
102 char name[16];
103 unsigned offset;
104 unsigned size;
105 unsigned flags;
106};
107
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800108char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700109
Greg Griscod2471ef2011-07-14 13:00:42 -0700110extern int emmc_recovery_init(void);
111
Kinson Chik0b1c8162011-08-31 16:31:57 -0700112#if NO_KEYPAD_DRIVER
113extern int fastboot_trigger(void);
114#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700115
Dima Zavin42168f22009-01-30 11:52:22 -0800116static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
117{
118 struct atag_ptbl_entry atag_ptn;
119
120 memcpy(atag_ptn.name, ptn->name, 16);
121 atag_ptn.name[15] = '\0';
122 atag_ptn.offset = ptn->start;
123 atag_ptn.size = ptn->length;
124 atag_ptn.flags = ptn->flags;
125 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
126 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
127}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800128
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700129void boot_linux(void *kernel, unsigned *tags,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800130 const char *cmdline, unsigned machtype,
131 void *ramdisk, unsigned ramdisk_size)
132{
133 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800134 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800135 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800136 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800137 int cmdline_len = 0;
138 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700139 int pause_at_bootup = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800140 unsigned char *cmdline_final = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800141
142 /* CORE */
143 *ptr++ = 2;
144 *ptr++ = 0x54410001;
145
146 if (ramdisk_size) {
147 *ptr++ = 4;
148 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800149 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800150 *ptr++ = ramdisk_size;
151 }
152
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800153 ptr = target_atag_mem(ptr);
154
David Ng183a7422009-12-07 14:55:21 -0800155 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;
Mekala Natarajanc43d8842011-10-26 15:09:10 -0700159 *ptr++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800160 sizeof(unsigned)));
161 *ptr++ = 0x4d534d70;
162 for (i = 0; i < ptable->count; ++i)
163 ptentry_to_tag(&ptr, ptable_get(ptable, i));
164 }
Dima Zavin42168f22009-01-30 11:52:22 -0800165 }
166
Brian Swetland9c4c0752009-01-25 16:23:50 -0800167 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800168 cmdline_len = strlen(cmdline);
169 have_cmdline = 1;
170 }
171 if (target_is_emmc_boot()) {
172 cmdline_len += strlen(emmc_cmdline);
173 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800174
175 cmdline_len += strlen(usb_sn_cmdline);
176 cmdline_len += strlen(sn_buf);
177
David Ngf773dde2010-07-26 19:55:08 -0700178 if (target_pause_for_battery_charge()) {
179 pause_at_bootup = 1;
180 cmdline_len += strlen(battchg_pause);
181 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800182
Shashank Mittalcd98d472011-08-02 14:29:24 -0700183 if(target_use_signed_kernel() && auth_kernel_img) {
184 cmdline_len += strlen(auth_kernel);
185 }
186
Ajay Dudanid04110c2011-01-17 23:55:07 -0800187 /* Determine correct androidboot.baseband to use */
188 switch(target_baseband())
189 {
190 case BASEBAND_APQ:
191 cmdline_len += strlen(baseband_apq);
192 break;
193
194 case BASEBAND_MSM:
195 cmdline_len += strlen(baseband_msm);
196 break;
197
198 case BASEBAND_CSFB:
199 cmdline_len += strlen(baseband_csfb);
200 break;
201
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800202 case BASEBAND_SVLTE2A:
203 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800204 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700205
206 case BASEBAND_MDM:
207 cmdline_len += strlen(baseband_mdm);
208 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700209
210 case BASEBAND_SGLTE:
211 cmdline_len += strlen(baseband_sglte);
212 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800213 }
214
David Ng183a7422009-12-07 14:55:21 -0800215 if (cmdline_len > 0) {
216 const char *src;
217 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800218 unsigned n;
219 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800220 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800221 *ptr++ = (n / 4) + 2;
222 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800223 dst = (char *)ptr;
Amol Jadi168b7712012-03-06 16:15:00 -0800224 /* Save start ptr for debug print */
225 cmdline_final = (char *)ptr;
David Ng183a7422009-12-07 14:55:21 -0800226 if (have_cmdline) {
227 src = cmdline;
228 while ((*dst++ = *src++));
229 }
230 if (target_is_emmc_boot()) {
231 src = emmc_cmdline;
232 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700233 have_cmdline = 1;
234 while ((*dst++ = *src++));
235 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800236
237 src = usb_sn_cmdline;
238 if (have_cmdline) --dst;
239 have_cmdline = 1;
240 while ((*dst++ = *src++));
241 src = sn_buf;
242 if (have_cmdline) --dst;
243 have_cmdline = 1;
244 while ((*dst++ = *src++));
245
David Ngf773dde2010-07-26 19:55:08 -0700246 if (pause_at_bootup) {
247 src = battchg_pause;
248 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800249 while ((*dst++ = *src++));
250 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800251
Shashank Mittalcd98d472011-08-02 14:29:24 -0700252 if(target_use_signed_kernel() && auth_kernel_img) {
253 src = auth_kernel;
254 if (have_cmdline) --dst;
255 while ((*dst++ = *src++));
256 }
257
Ajay Dudanid04110c2011-01-17 23:55:07 -0800258 switch(target_baseband())
259 {
260 case BASEBAND_APQ:
261 src = baseband_apq;
262 if (have_cmdline) --dst;
263 while ((*dst++ = *src++));
264 break;
265
266 case BASEBAND_MSM:
267 src = baseband_msm;
268 if (have_cmdline) --dst;
269 while ((*dst++ = *src++));
270 break;
271
272 case BASEBAND_CSFB:
273 src = baseband_csfb;
274 if (have_cmdline) --dst;
275 while ((*dst++ = *src++));
276 break;
277
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800278 case BASEBAND_SVLTE2A:
279 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800280 if (have_cmdline) --dst;
281 while ((*dst++ = *src++));
282 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700283
284 case BASEBAND_MDM:
285 src = baseband_mdm;
286 if (have_cmdline) --dst;
287 while ((*dst++ = *src++));
288 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700289
290 case BASEBAND_SGLTE:
291 src = baseband_sglte;
292 if (have_cmdline) --dst;
293 while ((*dst++ = *src++));
294 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800295 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800296 ptr += (n / 4);
297 }
298
299 /* END */
300 *ptr++ = 0;
301 *ptr++ = 0;
302
303 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
304 kernel, ramdisk, ramdisk_size);
Amol Jadi168b7712012-03-06 16:15:00 -0800305 if (cmdline_final)
306 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800307
308 enter_critical_section();
Amol Jadi4421e652011-06-16 15:00:48 -0700309 /* do any platform specific cleanup before kernel entry */
310 platform_uninit();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800311 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800312 arch_disable_mmu();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800313 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800314}
315
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800316unsigned page_size = 0;
317unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800318
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800319#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800320
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800321static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800322
Shashank Mittal23b8f422010-04-16 19:27:21 -0700323int boot_linux_from_mmc(void)
324{
325 struct boot_img_hdr *hdr = (void*) buf;
326 struct boot_img_hdr *uhdr;
327 unsigned offset = 0;
328 unsigned long long ptn = 0;
329 unsigned n = 0;
330 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700331 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700332
Shashank Mittalcd98d472011-08-02 14:29:24 -0700333 unsigned char *image_addr = 0;
334 unsigned kernel_actual;
335 unsigned ramdisk_actual;
336 unsigned imagesize_actual;
337
Shashank Mittal23b8f422010-04-16 19:27:21 -0700338 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
339 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
340 dprintf(INFO, "Unified boot method!\n");
341 hdr = uhdr;
342 goto unified_boot;
343 }
Greg Griscod6250552011-06-29 14:40:23 -0700344 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700345 index = partition_get_index("boot");
346 ptn = partition_get_offset(index);
347 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700348 dprintf(CRITICAL, "ERROR: No boot partition found\n");
349 return -1;
350 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700351 }
352 else {
353 index = partition_get_index("recovery");
354 ptn = partition_get_offset(index);
355 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700356 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
357 return -1;
358 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700359 }
360
Greg Griscod6250552011-06-29 14:40:23 -0700361 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700362 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
363 return -1;
364 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700365
366 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700367 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700368 return -1;
369 }
370
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700371 if (hdr->page_size && (hdr->page_size != page_size)) {
372 page_size = hdr->page_size;
373 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700374 }
375
Shashank Mittalcd98d472011-08-02 14:29:24 -0700376 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700377 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800378 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700379 image_addr = (unsigned char *)target_get_scratch_address();
380 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
381 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
382 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
383
384 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700385
386 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700387 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700388
Shashank Mittalcd98d472011-08-02 14:29:24 -0700389 /* Read image without signature */
390 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
391 {
392 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
393 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800394 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700395
396 offset = imagesize_actual;
397 /* Read signature */
398 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
399 {
400 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
401 }
402 else
403 {
404 auth_kernel_img = image_verify((unsigned char *)image_addr,
405 (unsigned char *)(image_addr + imagesize_actual),
406 imagesize_actual,
407 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700408
409 if(auth_kernel_img)
410 {
411 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700412 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700413 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700414 }
415
416 /* Move kernel and ramdisk to correct address */
417 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
418 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700419
420 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700421 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700422 {
423 write_device_info_mmc(&device);
424 #ifdef TZ_TAMPER_FUSE
425 set_tamper_fuse_cmd();
426 #endif
427 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530428 #if USE_PCOM_SECBOOT
429 set_tamper_flag(device.is_tampered);
430 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700431 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700432 else
433 {
434 offset += page_size;
435
436 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
437 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
438 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
439 return -1;
440 }
441 offset += n;
442
443 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
444 if(n != 0)
445 {
446 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
447 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
448 return -1;
449 }
450 }
451 offset += n;
452 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700453
454unified_boot:
455 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
456 hdr->kernel_size);
457 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
458 hdr->ramdisk_size);
459
460 if(hdr->cmdline[0]) {
461 cmdline = (char*) hdr->cmdline;
462 } else {
463 cmdline = DEFAULT_CMDLINE;
464 }
465 dprintf(INFO, "cmdline = '%s'\n", cmdline);
466
467 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700468 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700469 (const char *)cmdline, board_machtype(),
470 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
471
472 return 0;
473}
474
Dima Zavin214cc642009-01-26 11:16:21 -0800475int boot_linux_from_flash(void)
476{
477 struct boot_img_hdr *hdr = (void*) buf;
478 unsigned n;
479 struct ptentry *ptn;
480 struct ptable *ptable;
481 unsigned offset = 0;
482 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800483
Shashank Mittalcd98d472011-08-02 14:29:24 -0700484 unsigned char *image_addr = 0;
485 unsigned kernel_actual;
486 unsigned ramdisk_actual;
487 unsigned imagesize_actual;
488
David Ng183a7422009-12-07 14:55:21 -0800489 if (target_is_emmc_boot()) {
490 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
491 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
492 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
493 return -1;
494 }
495 goto continue_boot;
496 }
497
Dima Zavin214cc642009-01-26 11:16:21 -0800498 ptable = flash_get_ptable();
499 if (ptable == NULL) {
500 dprintf(CRITICAL, "ERROR: Partition table not found\n");
501 return -1;
502 }
503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800504 if(!boot_into_recovery)
505 {
506 ptn = ptable_find(ptable, "boot");
507 if (ptn == NULL) {
508 dprintf(CRITICAL, "ERROR: No boot partition found\n");
509 return -1;
510 }
511 }
512 else
513 {
514 ptn = ptable_find(ptable, "recovery");
515 if (ptn == NULL) {
516 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
517 return -1;
518 }
Dima Zavin214cc642009-01-26 11:16:21 -0800519 }
520
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800521 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800522 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
523 return -1;
524 }
Dima Zavin214cc642009-01-26 11:16:21 -0800525
526 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700527 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800528 return -1;
529 }
530
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800531 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700532 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 -0800533 return -1;
534 }
535
Shashank Mittalcd98d472011-08-02 14:29:24 -0700536 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700537 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700538 {
539 image_addr = (unsigned char *)target_get_scratch_address();
540 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
541 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
542 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800543
Shashank Mittalcd98d472011-08-02 14:29:24 -0700544 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700545
546 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700547 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700548
Shashank Mittalcd98d472011-08-02 14:29:24 -0700549 /* Read image without signature */
550 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
551 {
552 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
553 return -1;
554 }
Dima Zavin214cc642009-01-26 11:16:21 -0800555
Shashank Mittalcd98d472011-08-02 14:29:24 -0700556 offset = imagesize_actual;
557 /* Read signature */
558 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
559 {
560 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
561 }
562 else
563 {
564
565 /* Verify signature */
566 auth_kernel_img = image_verify((unsigned char *)image_addr,
567 (unsigned char *)(image_addr + imagesize_actual),
568 imagesize_actual,
569 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700570
571 if(auth_kernel_img)
572 {
573 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700574 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700575 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700576 }
577
578 /* Move kernel and ramdisk to correct address */
579 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
580 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700581
582 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700583 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700584 {
585 write_device_info_flash(&device);
586 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530587#if USE_PCOM_SECBOOT
588 set_tamper_flag(device.is_tampered);
589#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700590 }
591 else
592 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700593 offset = page_size;
594
Shashank Mittalcd98d472011-08-02 14:29:24 -0700595 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
596 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
597 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
598 return -1;
599 }
600 offset += n;
601
602 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
603 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
604 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
605 return -1;
606 }
607 offset += n;
608 }
David Ng183a7422009-12-07 14:55:21 -0800609continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800610 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
611 hdr->kernel_size);
612 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
613 hdr->ramdisk_size);
614
615 if(hdr->cmdline[0]) {
616 cmdline = (char*) hdr->cmdline;
617 } else {
618 cmdline = DEFAULT_CMDLINE;
619 }
620 dprintf(INFO, "cmdline = '%s'\n", cmdline);
621
622 /* TODO: create/pass atags to kernel */
623
624 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700625 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800626 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800627 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
628
629 return 0;
630}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800631
Shashank Mittal162244e2011-08-08 19:01:25 -0700632unsigned char info_buf[4096];
633void write_device_info_mmc(device_info *dev)
634{
635 struct device_info *info = (void*) info_buf;
636 unsigned long long ptn = 0;
637 unsigned long long size;
638 int index = INVALID_PTN;
639
640 index = partition_get_index("aboot");
641 ptn = partition_get_offset(index);
642 if(ptn == 0)
643 {
644 return;
645 }
646
647 size = partition_get_size(index);
648
649 memcpy(info, dev, sizeof(device_info));
650
651 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
652 {
653 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
654 return;
655 }
656}
657
658void read_device_info_mmc(device_info *dev)
659{
660 struct device_info *info = (void*) info_buf;
661 unsigned long long ptn = 0;
662 unsigned long long size;
663 int index = INVALID_PTN;
664
665 index = partition_get_index("aboot");
666 ptn = partition_get_offset(index);
667 if(ptn == 0)
668 {
669 return;
670 }
671
672 size = partition_get_size(index);
673
674 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
675 {
676 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
677 return;
678 }
679
680 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
681 {
682 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
683 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700684 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700685
686 write_device_info_mmc(info);
687 }
688 memcpy(dev, info, sizeof(device_info));
689}
690
691void write_device_info_flash(device_info *dev)
692{
693 struct device_info *info = (void *) info_buf;
694 struct ptentry *ptn;
695 struct ptable *ptable;
696
697 ptable = flash_get_ptable();
698 if (ptable == NULL)
699 {
700 dprintf(CRITICAL, "ERROR: Partition table not found\n");
701 return;
702 }
703
704 ptn = ptable_find(ptable, "devinfo");
705 if (ptn == NULL)
706 {
707 dprintf(CRITICAL, "ERROR: No boot partition found\n");
708 return;
709 }
710
711 memcpy(info, dev, sizeof(device_info));
712
713 if (flash_write(ptn, 0, (void *)info_buf, page_size))
714 {
715 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
716 return;
717 }
718}
719
720void read_device_info_flash(device_info *dev)
721{
722 struct device_info *info = (void*) info_buf;
723 struct ptentry *ptn;
724 struct ptable *ptable;
725
726 ptable = flash_get_ptable();
727 if (ptable == NULL)
728 {
729 dprintf(CRITICAL, "ERROR: Partition table not found\n");
730 return;
731 }
732
733 ptn = ptable_find(ptable, "devinfo");
734 if (ptn == NULL)
735 {
736 dprintf(CRITICAL, "ERROR: No boot partition found\n");
737 return;
738 }
739
740 if (flash_read(ptn, 0, (void *)info_buf, page_size))
741 {
742 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
743 return;
744 }
745
746 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
747 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700748 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
749 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700750 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700751 write_device_info_flash(info);
752 }
753 memcpy(dev, info, sizeof(device_info));
754}
755
756void write_device_info(device_info *dev)
757{
758 if(target_is_emmc_boot())
759 {
760 write_device_info_mmc(dev);
761 }
762 else
763 {
764 write_device_info_flash(dev);
765 }
766}
767
768void read_device_info(device_info *dev)
769{
770 if(target_is_emmc_boot())
771 {
772 read_device_info_mmc(dev);
773 }
774 else
775 {
776 read_device_info_flash(dev);
777 }
778}
779
780void reset_device_info()
781{
782 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700783 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700784 write_device_info(&device);
785}
786
787void set_device_root()
788{
789 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700790 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700791 write_device_info(&device);
792}
793
Brian Swetland9c4c0752009-01-25 16:23:50 -0800794void cmd_boot(const char *arg, void *data, unsigned sz)
795{
796 unsigned kernel_actual;
797 unsigned ramdisk_actual;
798 static struct boot_img_hdr hdr;
799 char *ptr = ((char*) data);
800
801 if (sz < sizeof(hdr)) {
802 fastboot_fail("invalid bootimage header");
803 return;
804 }
805
806 memcpy(&hdr, data, sizeof(hdr));
807
808 /* ensure commandline is terminated */
809 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
810
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700811 if(target_is_emmc_boot() && hdr.page_size) {
812 page_size = hdr.page_size;
813 page_mask = page_size - 1;
814 }
815
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800816 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
817 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800818
Shashank Mittal1f0e2662011-09-01 15:06:00 -0700819 /* sz should have atleast raw boot image */
820 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800821 fastboot_fail("incomplete bootimage");
822 return;
823 }
824
Ajay Dudanie28a6072011-07-01 13:59:46 -0700825 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
826 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800827
828 fastboot_okay("");
829 udc_stop();
830
Amol Jadie67872e2011-06-27 14:14:11 -0700831 boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800832 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -0700833 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800834}
835
Dima Zavin214cc642009-01-26 11:16:21 -0800836void cmd_erase(const char *arg, void *data, unsigned sz)
837{
838 struct ptentry *ptn;
839 struct ptable *ptable;
840
841 ptable = flash_get_ptable();
842 if (ptable == NULL) {
843 fastboot_fail("partition table doesn't exist");
844 return;
845 }
846
847 ptn = ptable_find(ptable, arg);
848 if (ptn == NULL) {
849 fastboot_fail("unknown partition name");
850 return;
851 }
852
853 if (flash_erase(ptn)) {
854 fastboot_fail("failed to erase partition");
855 return;
856 }
857 fastboot_okay("");
858}
859
Bikas Gurungd48bd242010-09-04 19:54:32 -0700860
861void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
862{
863 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -0800864 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -0700865 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -0700866
Kinson Chikf1a43512011-07-14 11:28:39 -0700867 index = partition_get_index(arg);
868 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -0700869
Kinson Chikf1a43512011-07-14 11:28:39 -0700870 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -0700871 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -0700872 return;
873 }
neetidb4b24d62012-01-20 12:13:09 -0800874 /* Simple inefficient version of erase. Just writing
875 0 in first block */
876 if (mmc_write(ptn , 512, (unsigned int *)out)) {
877 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -0700878 return;
879 }
880 fastboot_okay("");
881}
882
883
Ajay Dudani5c761132011-04-07 20:19:04 -0700884void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -0700885{
886 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700887 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700888 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700889
Greg Grisco6e754772011-06-23 12:19:39 -0700890 if (!strcmp(arg, "partition"))
891 {
892 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -0700893 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -0700894 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700895 return;
896 }
897 }
Greg Grisco6e754772011-06-23 12:19:39 -0700898 else
899 {
Kinson Chikf1a43512011-07-14 11:28:39 -0700900 index = partition_get_index(arg);
901 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700902 if(ptn == 0) {
903 fastboot_fail("partition table doesn't exist");
904 return;
905 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700906
Greg Grisco6e754772011-06-23 12:19:39 -0700907 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
908 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
909 fastboot_fail("image is not a boot image");
910 return;
911 }
912 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700913
Kinson Chikf1a43512011-07-14 11:28:39 -0700914 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700915 if (ROUND_TO_PAGE(sz,511) > size) {
916 fastboot_fail("size too large");
917 return;
918 }
919 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
920 fastboot_fail("flash write failure");
921 return;
922 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700923 }
924 fastboot_okay("");
925 return;
926}
927
Ajay Dudani5c761132011-04-07 20:19:04 -0700928void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
929{
930 unsigned int chunk;
931 unsigned int chunk_data_sz;
932 sparse_header_t *sparse_header;
933 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -0700934 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700935 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +0530936 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700937 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -0700938
Kinson Chikf1a43512011-07-14 11:28:39 -0700939 index = partition_get_index(arg);
940 ptn = partition_get_offset(index);
941 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -0700942 fastboot_fail("partition table doesn't exist");
943 return;
944 }
945
Channagoud Kadabi65b91002011-10-11 17:34:33 +0530946 size = partition_get_size(index);
947 if (ROUND_TO_PAGE(sz,511) > size) {
948 fastboot_fail("size too large");
949 return;
950 }
951
Ajay Dudani5c761132011-04-07 20:19:04 -0700952 /* Read and skip over sparse image header */
953 sparse_header = (sparse_header_t *) data;
954 data += sparse_header->file_hdr_sz;
955 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
956 {
957 /* Skip the remaining bytes in a header that is longer than
958 * we expected.
959 */
960 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
961 }
962
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700963 dprintf (SPEW, "=== Sparse Image Header ===\n");
964 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
965 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
966 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
967 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
968 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
969 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
970 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
971 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -0700972
973 /* Start processing chunks */
974 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
975 {
976 /* Read and skip over chunk header */
977 chunk_header = (chunk_header_t *) data;
978 data += sizeof(chunk_header_t);
979
980 dprintf (SPEW, "=== Chunk Header ===\n");
981 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
982 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
983 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
984
985 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
986 {
987 /* Skip the remaining bytes in a header that is longer than
988 * we expected.
989 */
990 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
991 }
992
993 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
994 switch (chunk_header->chunk_type)
995 {
996 case CHUNK_TYPE_RAW:
997 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
998 chunk_data_sz))
999 {
1000 fastboot_fail("Bogus chunk size for chunk type Raw");
1001 return;
1002 }
1003
Ajay Dudaniab18f022011-05-12 14:39:22 -07001004 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1005 chunk_data_sz,
1006 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001007 {
1008 fastboot_fail("flash write failure");
1009 return;
1010 }
1011 total_blocks += chunk_header->chunk_sz;
1012 data += chunk_data_sz;
1013 break;
1014
1015 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001016 total_blocks += chunk_header->chunk_sz;
1017 break;
1018
Ajay Dudani5c761132011-04-07 20:19:04 -07001019 case CHUNK_TYPE_CRC:
1020 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1021 {
1022 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1023 return;
1024 }
1025 total_blocks += chunk_header->chunk_sz;
1026 data += chunk_data_sz;
1027 break;
1028
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001029 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001030 fastboot_fail("Unknown chunk type");
1031 return;
1032 }
1033 }
1034
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001035 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1036 total_blocks, sparse_header->total_blks);
1037
1038 if(total_blocks != sparse_header->total_blks)
1039 {
1040 fastboot_fail("sparse image write failure");
1041 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001042
1043 fastboot_okay("");
1044 return;
1045}
1046
1047void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1048{
1049 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001050 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1051 unsigned int *magic_number = (unsigned int *) data;
1052 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001053
Neeti Desai127b9e02012-03-20 16:11:23 -07001054 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1055 magic_number[1] == DECRYPT_MAGIC_1)
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001056 {
1057#ifdef SSD_ENABLE
Neeti Desai127b9e02012-03-20 16:11:23 -07001058 ret = decrypt_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001059#endif
Greg Griscod6250552011-06-29 14:40:23 -07001060 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001061 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1062 return;
1063 }
1064 }
Neeti Desai127b9e02012-03-20 16:11:23 -07001065 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1066 magic_number[1] == ENCRYPT_MAGIC_1)
1067 {
1068#ifdef SSD_ENABLE
1069 ret = encrypt_scm((uint32 **) &data, &sz);
1070#endif
1071 if (ret != 0) {
1072 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1073 return;
1074 }
1075 }
1076
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001077 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001078 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1079 cmd_flash_mmc_img(arg, data, sz);
1080 else
1081 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001082 return;
1083}
1084
Dima Zavin214cc642009-01-26 11:16:21 -08001085void cmd_flash(const char *arg, void *data, unsigned sz)
1086{
1087 struct ptentry *ptn;
1088 struct ptable *ptable;
1089 unsigned extra = 0;
1090
1091 ptable = flash_get_ptable();
1092 if (ptable == NULL) {
1093 fastboot_fail("partition table doesn't exist");
1094 return;
1095 }
1096
1097 ptn = ptable_find(ptable, arg);
1098 if (ptn == NULL) {
1099 fastboot_fail("unknown partition name");
1100 return;
1101 }
1102
1103 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1104 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1105 fastboot_fail("image is not a boot image");
1106 return;
1107 }
1108 }
1109
Amol Jadi5c61a952012-05-04 17:05:35 -07001110 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001111 || !strcmp(ptn->name, "userdata")
1112 || !strcmp(ptn->name, "persist")
1113 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301114 if (flash_ecc_bch_enabled())
1115 /* Spare data bytes for 8 bit ECC increased by 4 */
1116 extra = ((page_size >> 9) * 20);
1117 else
1118 extra = ((page_size >> 9) * 16);
1119 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001120 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001121
1122 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1123 if (flash_write(ptn, extra, data, sz)) {
1124 fastboot_fail("flash write failure");
1125 return;
1126 }
1127 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1128 fastboot_okay("");
1129}
1130
1131void cmd_continue(const char *arg, void *data, unsigned sz)
1132{
1133 fastboot_okay("");
1134 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001135 if (target_is_emmc_boot())
1136 {
1137 boot_linux_from_mmc();
1138 }
1139 else
1140 {
1141 boot_linux_from_flash();
1142 }
Dima Zavin214cc642009-01-26 11:16:21 -08001143}
1144
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001145void cmd_reboot(const char *arg, void *data, unsigned sz)
1146{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001147 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001148 fastboot_okay("");
1149 reboot_device(0);
1150}
1151
1152void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1153{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001154 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001155 fastboot_okay("");
1156 reboot_device(FASTBOOT_MODE);
1157}
1158
Shashank Mittal162244e2011-08-08 19:01:25 -07001159void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1160{
1161 if(!device.is_unlocked)
1162 {
1163 device.is_unlocked = 1;
1164 write_device_info(&device);
1165 }
1166 fastboot_okay("");
1167}
1168
Shashank Mittala0032282011-08-26 14:50:11 -07001169void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1170{
1171 char response[64];
1172 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1173 fastboot_info(response);
1174 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1175 fastboot_info(response);
1176 fastboot_okay("");
1177}
1178
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001179void splash_screen ()
1180{
1181 struct ptentry *ptn;
1182 struct ptable *ptable;
1183 struct fbcon_config *fb_display = NULL;
1184
1185 if (!target_is_emmc_boot())
1186 {
1187 ptable = flash_get_ptable();
1188 if (ptable == NULL) {
1189 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001190 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001191 }
1192
1193 ptn = ptable_find(ptable, "splash");
1194 if (ptn == NULL) {
1195 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1196 } else {
1197 fb_display = fbcon_display();
1198 if (fb_display) {
1199 if (flash_read(ptn, 0, fb_display->base,
1200 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1201 fbcon_clear();
1202 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1203 }
1204 }
1205 }
1206 }
1207}
1208
Brian Swetland9c4c0752009-01-25 16:23:50 -08001209void aboot_init(const struct app_descriptor *app)
1210{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001211 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001212 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001213 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001214
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001215 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001216 if (target_is_emmc_boot())
1217 {
1218 page_size = 2048;
1219 page_mask = page_size - 1;
1220 }
1221 else
1222 {
1223 page_size = flash_page_size();
1224 page_mask = page_size - 1;
1225 }
1226
Shashank Mittal162244e2011-08-08 19:01:25 -07001227 if(target_use_signed_kernel())
1228 {
1229 read_device_info(&device);
1230
Shashank Mittal162244e2011-08-08 19:01:25 -07001231 }
1232
Greg Griscod6250552011-06-29 14:40:23 -07001233 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001234 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001235 surf_udc_device.serialno = sn_buf;
1236
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001237 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001238 if (keys_get_state(KEY_HOME) != 0)
1239 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001240 if (keys_get_state(KEY_VOLUMEUP) != 0)
1241 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001242 if(!boot_into_recovery)
1243 {
1244 if (keys_get_state(KEY_BACK) != 0)
1245 goto fastboot;
1246 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1247 goto fastboot;
1248 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001249
1250 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001251 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001252 goto fastboot;
1253 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001254
Ajay Dudani77421292010-10-27 19:34:06 -07001255 reboot_mode = check_reboot_mode();
1256 if (reboot_mode == RECOVERY_MODE) {
1257 boot_into_recovery = 1;
1258 } else if(reboot_mode == FASTBOOT_MODE) {
1259 goto fastboot;
1260 }
1261
Shashank Mittal23b8f422010-04-16 19:27:21 -07001262 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001263 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001264 if(emmc_recovery_init())
1265 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001266 if(target_use_signed_kernel())
1267 {
1268 if((device.is_unlocked) || (device.is_tampered))
1269 {
1270 #ifdef TZ_TAMPER_FUSE
1271 set_tamper_fuse_cmd();
1272 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301273 #if USE_PCOM_SECBOOT
1274 set_tamper_flag(device.is_tampered);
1275 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001276 }
1277 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001278 boot_linux_from_mmc();
1279 }
1280 else
1281 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001282 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301283#if USE_PCOM_SECBOOT
1284 if((device.is_unlocked) || (device.is_tampered))
1285 set_tamper_flag(device.is_tampered);
1286#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001287 boot_linux_from_flash();
1288 }
Dima Zavinb4283602009-01-26 16:36:57 -08001289 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1290 "to fastboot mode.\n");
1291
1292fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001293
Shashank Mittal162244e2011-08-08 19:01:25 -07001294 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001295
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001296 if(!usb_init)
1297 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001298
1299 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001300
Shashank Mittal23b8f422010-04-16 19:27:21 -07001301 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001302 {
1303 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001304 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001305 }
1306 else
1307 {
1308 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001309 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001310 }
1311
1312 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001313 fastboot_register("reboot", cmd_reboot);
1314 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001315 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001316 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001317 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001318 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001319 fastboot_publish("serialno", sn_buf);
Kinson Chikf1a43512011-07-14 11:28:39 -07001320 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001321 sz = target_get_max_flash_size();
1322 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001323 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001324}
1325
1326APP_START(aboot)
1327 .init = aboot_init,
1328APP_END
1329