blob: d13ddecdb1ab4ac0770907ea0646a20f7b9074a3 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Amol Jadi2a15a272013-01-22 12:03:36 -08005 * Copyright (c) 2009-2013, The Linux Foundation. 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.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * 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>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070038#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
43#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080044#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080045#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080046#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070047#include <target.h>
48#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070049#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070050#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070051#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070052#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070053#include <boot_stats.h>
Dima Zavin214cc642009-01-26 11:16:21 -080054
Neeti Desai17379b82012-06-04 18:42:53 -070055#if DEVICE_TREE
56#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070057#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070058#endif
59
Shashank Mittalcd98d472011-08-02 14:29:24 -070060#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080061#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080062#include "bootimg.h"
63#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070064#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070065#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070066#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070067#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080068
Shashank Mittal162244e2011-08-08 19:01:25 -070069#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070070
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070071extern bool target_use_signed_kernel(void);
72extern void dsb();
73extern void isb();
74extern void platform_uninit(void);
75
76void write_device_info_mmc(device_info *dev);
77void write_device_info_flash(device_info *dev);
78
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070079#define EXPAND(NAME) #NAME
80#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070081
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080082#ifdef MEMBASE
83#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
84#else
David Ng183a7422009-12-07 14:55:21 -080085#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080086#endif
87
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080088#define RECOVERY_MODE 0x77665502
89#define FASTBOOT_MODE 0x77665500
90
David Ng183a7422009-12-07 14:55:21 -080091static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080092static const char *usb_sn_cmdline = " androidboot.serialno=";
Ajay Dudanica3a33c2011-11-18 08:31:40 -080093static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -070094static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -080095
Ajay Dudani6cff85e2011-02-04 16:02:16 -080096static const char *baseband_apq = " androidboot.baseband=apq";
97static const char *baseband_msm = " androidboot.baseband=msm";
98static const char *baseband_csfb = " androidboot.baseband=csfb";
99static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700100static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700101static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800102static const char *baseband_dsda = " androidboot.baseband=dsda";
103static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800104static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800105
Shashank Mittalcd98d472011-08-02 14:29:24 -0700106/* Assuming unauthorized kernel image by default */
107static int auth_kernel_img = 0;
108
Shashank Mittal162244e2011-08-08 19:01:25 -0700109static device_info device = {DEVICE_MAGIC, 0, 0};
110
Brian Swetland9c4c0752009-01-25 16:23:50 -0800111static struct udc_device surf_udc_device = {
112 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800113 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800114 .version_id = 0x0100,
115 .manufacturer = "Google",
116 .product = "Android",
117};
118
Dima Zavin42168f22009-01-30 11:52:22 -0800119struct atag_ptbl_entry
120{
121 char name[16];
122 unsigned offset;
123 unsigned size;
124 unsigned flags;
125};
126
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700127/*
128 * Partition info, required to be published
129 * for fastboot
130 */
131struct getvar_partition_info {
132 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
133 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
134 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
135 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
136 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
137};
138
139/*
140 * Right now, we are publishing the info for only
141 * three partitions
142 */
143struct getvar_partition_info part_info[] =
144{
145 { "system" , "partition-size:", "partition-type:", "", "ext4" },
146 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
147 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
148};
149
150char max_download_size[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800151char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700152
Greg Griscod2471ef2011-07-14 13:00:42 -0700153extern int emmc_recovery_init(void);
154
Kinson Chik0b1c8162011-08-31 16:31:57 -0700155#if NO_KEYPAD_DRIVER
156extern int fastboot_trigger(void);
157#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700158
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700159static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
160{
161 /* overwrite the destination of specified for the project */
162 /*
163 * Update the value to sane values only when the boot image
164 * header does not have sane values, this is added to make sure
165 * that we always use values from boot.img header and use the
166 * force values when boot image header has default values.
167 */
168#ifdef ABOOT_FORCE_KERNEL_ADDR
169 if (hdr->kernel_addr == ABOOT_DEFAULT_KERNEL_ADDR)
170 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
171#endif
172#ifdef ABOOT_FORCE_RAMDISK_ADDR
173 if (hdr->ramdisk_addr == ABOOT_DEFAULT_RAMDISK_ADDR)
174 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
175#endif
176#ifdef ABOOT_FORCE_TAGS_ADDR
177 if (hdr->tags_addr == ABOOT_DEFAULT_TAGS_ADDR)
178 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
179#endif
180}
181
Dima Zavin42168f22009-01-30 11:52:22 -0800182static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
183{
184 struct atag_ptbl_entry atag_ptn;
185
186 memcpy(atag_ptn.name, ptn->name, 16);
187 atag_ptn.name[15] = '\0';
188 atag_ptn.offset = ptn->start;
189 atag_ptn.size = ptn->length;
190 atag_ptn.flags = ptn->flags;
191 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
192 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
193}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800194
Neeti Desaie245d492012-06-01 12:52:13 -0700195unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800196{
David Ng183a7422009-12-07 14:55:21 -0800197 int cmdline_len = 0;
198 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800199 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700200 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800201
Brian Swetland9c4c0752009-01-25 16:23:50 -0800202 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800203 cmdline_len = strlen(cmdline);
204 have_cmdline = 1;
205 }
206 if (target_is_emmc_boot()) {
207 cmdline_len += strlen(emmc_cmdline);
208 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800209
210 cmdline_len += strlen(usb_sn_cmdline);
211 cmdline_len += strlen(sn_buf);
212
David Ngf773dde2010-07-26 19:55:08 -0700213 if (target_pause_for_battery_charge()) {
214 pause_at_bootup = 1;
215 cmdline_len += strlen(battchg_pause);
216 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800217
Shashank Mittalcd98d472011-08-02 14:29:24 -0700218 if(target_use_signed_kernel() && auth_kernel_img) {
219 cmdline_len += strlen(auth_kernel);
220 }
221
Ajay Dudanid04110c2011-01-17 23:55:07 -0800222 /* Determine correct androidboot.baseband to use */
223 switch(target_baseband())
224 {
225 case BASEBAND_APQ:
226 cmdline_len += strlen(baseband_apq);
227 break;
228
229 case BASEBAND_MSM:
230 cmdline_len += strlen(baseband_msm);
231 break;
232
233 case BASEBAND_CSFB:
234 cmdline_len += strlen(baseband_csfb);
235 break;
236
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800237 case BASEBAND_SVLTE2A:
238 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800239 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700240
241 case BASEBAND_MDM:
242 cmdline_len += strlen(baseband_mdm);
243 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700244
245 case BASEBAND_SGLTE:
246 cmdline_len += strlen(baseband_sglte);
247 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530248
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800249 case BASEBAND_SGLTE2:
250 cmdline_len += strlen(baseband_sglte2);
251 break;
252
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530253 case BASEBAND_DSDA:
254 cmdline_len += strlen(baseband_dsda);
255 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800256
257 case BASEBAND_DSDA2:
258 cmdline_len += strlen(baseband_dsda2);
259 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800260 }
261
David Ng183a7422009-12-07 14:55:21 -0800262 if (cmdline_len > 0) {
263 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700264 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
265 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700266
Amol Jadi168b7712012-03-06 16:15:00 -0800267 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700268 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800269 if (have_cmdline) {
270 src = cmdline;
271 while ((*dst++ = *src++));
272 }
273 if (target_is_emmc_boot()) {
274 src = emmc_cmdline;
275 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700276 have_cmdline = 1;
277 while ((*dst++ = *src++));
278 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800279
280 src = usb_sn_cmdline;
281 if (have_cmdline) --dst;
282 have_cmdline = 1;
283 while ((*dst++ = *src++));
284 src = sn_buf;
285 if (have_cmdline) --dst;
286 have_cmdline = 1;
287 while ((*dst++ = *src++));
288
David Ngf773dde2010-07-26 19:55:08 -0700289 if (pause_at_bootup) {
290 src = battchg_pause;
291 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800292 while ((*dst++ = *src++));
293 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800294
Shashank Mittalcd98d472011-08-02 14:29:24 -0700295 if(target_use_signed_kernel() && auth_kernel_img) {
296 src = auth_kernel;
297 if (have_cmdline) --dst;
298 while ((*dst++ = *src++));
299 }
300
Ajay Dudanid04110c2011-01-17 23:55:07 -0800301 switch(target_baseband())
302 {
303 case BASEBAND_APQ:
304 src = baseband_apq;
305 if (have_cmdline) --dst;
306 while ((*dst++ = *src++));
307 break;
308
309 case BASEBAND_MSM:
310 src = baseband_msm;
311 if (have_cmdline) --dst;
312 while ((*dst++ = *src++));
313 break;
314
315 case BASEBAND_CSFB:
316 src = baseband_csfb;
317 if (have_cmdline) --dst;
318 while ((*dst++ = *src++));
319 break;
320
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800321 case BASEBAND_SVLTE2A:
322 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800323 if (have_cmdline) --dst;
324 while ((*dst++ = *src++));
325 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700326
327 case BASEBAND_MDM:
328 src = baseband_mdm;
329 if (have_cmdline) --dst;
330 while ((*dst++ = *src++));
331 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700332
333 case BASEBAND_SGLTE:
334 src = baseband_sglte;
335 if (have_cmdline) --dst;
336 while ((*dst++ = *src++));
337 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530338
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800339 case BASEBAND_SGLTE2:
340 src = baseband_sglte2;
341 if (have_cmdline) --dst;
342 while ((*dst++ = *src++));
343 break;
344
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530345 case BASEBAND_DSDA:
346 src = baseband_dsda;
347 if (have_cmdline) --dst;
348 while ((*dst++ = *src++));
349 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800350
351 case BASEBAND_DSDA2:
352 src = baseband_dsda2;
353 if (have_cmdline) --dst;
354 while ((*dst++ = *src++));
355 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800356 }
Neeti Desaie245d492012-06-01 12:52:13 -0700357 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800358
359 dprintf(INFO, "cmdline: %s\n", cmdline_final);
360
Neeti Desaie245d492012-06-01 12:52:13 -0700361 return cmdline_final;
362}
363
364unsigned *atag_core(unsigned *ptr)
365{
366 /* CORE */
367 *ptr++ = 2;
368 *ptr++ = 0x54410001;
369
370 return ptr;
371
372}
373
374unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
375 unsigned ramdisk_size)
376{
377 if (ramdisk_size) {
378 *ptr++ = 4;
379 *ptr++ = 0x54420005;
380 *ptr++ = (unsigned)ramdisk;
381 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800382 }
383
Neeti Desaie245d492012-06-01 12:52:13 -0700384 return ptr;
385}
386
387unsigned *atag_ptable(unsigned **ptr_addr)
388{
389 int i;
390 struct ptable *ptable;
391
392 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700393 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
394 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700395 *(*ptr_addr)++ = 0x4d534d70;
396 for (i = 0; i < ptable->count; ++i)
397 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
398 }
399
400 return (*ptr_addr);
401}
402
403unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
404{
405 int cmdline_length = 0;
406 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700407 char *dest;
408
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800409 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700410 n = (cmdline_length + 4) & (~3);
411
412 *ptr++ = (n / 4) + 2;
413 *ptr++ = 0x54410009;
414 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800415 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700416 ptr += (n / 4);
417
418 return ptr;
419}
420
421unsigned *atag_end(unsigned *ptr)
422{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800423 /* END */
424 *ptr++ = 0;
425 *ptr++ = 0;
426
Neeti Desaie245d492012-06-01 12:52:13 -0700427 return ptr;
428}
429
430void generate_atags(unsigned *ptr, const char *cmdline,
431 void *ramdisk, unsigned ramdisk_size)
432{
433
434 ptr = atag_core(ptr);
435 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
436 ptr = target_atag_mem(ptr);
437
438 /* Skip NAND partition ATAGS for eMMC boot */
439 if (!target_is_emmc_boot()){
440 ptr = atag_ptable(&ptr);
441 }
442
443 ptr = atag_cmdline(ptr, cmdline);
444 ptr = atag_end(ptr);
445}
446
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700447typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700448void boot_linux(void *kernel, unsigned *tags,
449 const char *cmdline, unsigned machtype,
450 void *ramdisk, unsigned ramdisk_size)
451{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800452 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800453#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700454 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800455#endif
456
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700457 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800458 uint32_t tags_phys = PA((addr_t)tags);
459
460 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700461
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800462 final_cmdline = update_cmdline((const char*)cmdline);
463
Neeti Desai17379b82012-06-04 18:42:53 -0700464#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800465 dprintf(INFO, "Updating device tree: start\n");
466
Neeti Desai17379b82012-06-04 18:42:53 -0700467 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800468 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700469 if(ret)
470 {
471 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
472 ASSERT(0);
473 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800474 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700475#else
Neeti Desaie245d492012-06-01 12:52:13 -0700476 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800477 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700478#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700479
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800480 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
481 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800482
483 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700484
Amol Jadi4421e652011-06-16 15:00:48 -0700485 /* do any platform specific cleanup before kernel entry */
486 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700487
Brian Swetland9c4c0752009-01-25 16:23:50 -0800488 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700489
Amol Jadi504f9fe2012-08-16 13:56:48 -0700490#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800491 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700492#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700493 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700494 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800495}
496
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800497unsigned page_size = 0;
498unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800499
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800500#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800501
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700502BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800503#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700504BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800505#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800506
Shashank Mittal23b8f422010-04-16 19:27:21 -0700507int boot_linux_from_mmc(void)
508{
509 struct boot_img_hdr *hdr = (void*) buf;
510 struct boot_img_hdr *uhdr;
511 unsigned offset = 0;
512 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700513 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700514
Shashank Mittalcd98d472011-08-02 14:29:24 -0700515 unsigned char *image_addr = 0;
516 unsigned kernel_actual;
517 unsigned ramdisk_actual;
518 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700519 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700520
521#if DEVICE_TREE
522 struct dt_table *table;
523 struct dt_entry *dt_entry_ptr;
524 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800525 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700526#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700527
Shashank Mittal23b8f422010-04-16 19:27:21 -0700528 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
529 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
530 dprintf(INFO, "Unified boot method!\n");
531 hdr = uhdr;
532 goto unified_boot;
533 }
Greg Griscod6250552011-06-29 14:40:23 -0700534 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700535 index = partition_get_index("boot");
536 ptn = partition_get_offset(index);
537 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700538 dprintf(CRITICAL, "ERROR: No boot partition found\n");
539 return -1;
540 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700541 }
542 else {
543 index = partition_get_index("recovery");
544 ptn = partition_get_offset(index);
545 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700546 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
547 return -1;
548 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700549 }
550
Greg Griscod6250552011-06-29 14:40:23 -0700551 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700552 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
553 return -1;
554 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700555
556 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700557 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700558 return -1;
559 }
560
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700561 if (hdr->page_size && (hdr->page_size != page_size)) {
562 page_size = hdr->page_size;
563 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700564 }
565
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700566 /*
567 * Update the kernel/ramdisk/tags address if the boot image header
568 * has default values, these default values come from mkbootimg when
569 * the boot image is flashed using fastboot flash:raw
570 */
571 update_ker_tags_rdisk_addr(hdr);
572
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700573 /* Get virtual addresses since the hdr saves physical addresses. */
574 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
575 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
576 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
577
Shashank Mittalcd98d472011-08-02 14:29:24 -0700578 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700579 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800580 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700581 image_addr = (unsigned char *)target_get_scratch_address();
582 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
583 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800584#if DEVICE_TREE
585 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
586 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
587#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530588 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800589#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700590 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700591
592 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700593 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700594
Amol Jadib6be5c12012-11-14 13:39:51 -0800595 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700596 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800597
Shashank Mittalcd98d472011-08-02 14:29:24 -0700598 /* Read image without signature */
599 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
600 {
601 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
602 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800603 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700604
Amol Jadib6be5c12012-11-14 13:39:51 -0800605 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700606 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800607
Shashank Mittalcd98d472011-08-02 14:29:24 -0700608 offset = imagesize_actual;
609 /* Read signature */
610 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
611 {
612 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
613 }
614 else
615 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800616 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
617
Shashank Mittalcd98d472011-08-02 14:29:24 -0700618 auth_kernel_img = image_verify((unsigned char *)image_addr,
619 (unsigned char *)(image_addr + imagesize_actual),
620 imagesize_actual,
621 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700622
Amol Jadib6be5c12012-11-14 13:39:51 -0800623 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
624
Shashank Mittal162244e2011-08-08 19:01:25 -0700625 if(auth_kernel_img)
626 {
627 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700628 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700629 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700630 }
631
Neeti Desai465491e2012-07-31 12:53:35 -0700632 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700633 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
634 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800635#if DEVICE_TREE
636 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
637#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700638
Neeti Desai465491e2012-07-31 12:53:35 -0700639 #if DEVICE_TREE
640 if(hdr->dt_size) {
641 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700642 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700643
644 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
645
646 /* Restriction that the device tree entry table should be less than a page*/
647 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
648
649 /* Validate the device tree table header */
650 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
651 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
652 return -1;
653 }
654
655 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700656 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700657 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
658 return -1;
659 }
660
661 /* Read device device tree in the "tags_add */
662 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800663 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800664 /*
665 * If appended dev tree is found, update the atags with
666 * memory address to the DTB appended location on RAM.
667 * Else update with the atags address in the kernel header
668 */
669 void *dtb;
670 dtb = dev_tree_appended((void*) hdr->kernel_addr,
671 (void *)hdr->tags_addr);
672 if (!dtb) {
673 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
674 return -1;
675 }
Neeti Desai465491e2012-07-31 12:53:35 -0700676 }
677 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700678 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700679 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700680 {
681 write_device_info_mmc(&device);
682 #ifdef TZ_TAMPER_FUSE
683 set_tamper_fuse_cmd();
684 #endif
685 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530686 #if USE_PCOM_SECBOOT
687 set_tamper_flag(device.is_tampered);
688 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700689 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700690 else
691 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800692 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
693 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
694 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700695
Amol Jadib6be5c12012-11-14 13:39:51 -0800696 dprintf(INFO, "Loading boot image (%d): start\n",
697 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700698 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800699
700 offset = page_size;
701
702 /* Load kernel */
703 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700704 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
705 return -1;
706 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800707 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700708
Amol Jadib6be5c12012-11-14 13:39:51 -0800709 /* Load ramdisk */
710 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700711 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800712 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700713 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
714 return -1;
715 }
716 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800717 offset += ramdisk_actual;
718
719 dprintf(INFO, "Loading boot image (%d): done\n",
720 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700721 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700722
723 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800724 offset += second_actual;
725 /* Second image loading not implemented. */
726 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700727 }
728
729 #if DEVICE_TREE
730 if(hdr->dt_size != 0) {
731
732 /* Read the device tree table into buffer */
733 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
734 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
735 return -1;
736 }
737 table = (struct dt_table*) dt_buf;
738
739 /* Restriction that the device tree entry table should be less than a page*/
740 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
741
742 /* Validate the device tree table header */
743 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
744 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
745 return -1;
746 }
747
748 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700749 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700750 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
751 return -1;
752 }
753
754 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700755 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
756 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
757 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
758 return -1;
759 }
Channagoud Kadabi35095622013-03-01 13:53:05 -0800760 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800761 /*
762 * If appended dev tree is found, update the atags with
763 * memory address to the DTB appended location on RAM.
764 * Else update with the atags address in the kernel header
765 */
766 void *dtb;
767 dtb = dev_tree_appended((void*) hdr->kernel_addr,
768 (void *)hdr->tags_addr);
769 if (!dtb) {
770 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
771 return -1;
772 }
Neeti Desai465491e2012-07-31 12:53:35 -0700773 }
774 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700775 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700776
777unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700778
Dima Zavin77e41f32013-03-06 16:10:43 -0800779 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700780 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700781 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
782
783 return 0;
784}
785
Dima Zavin214cc642009-01-26 11:16:21 -0800786int boot_linux_from_flash(void)
787{
788 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800789 struct ptentry *ptn;
790 struct ptable *ptable;
791 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800792
Shashank Mittalcd98d472011-08-02 14:29:24 -0700793 unsigned char *image_addr = 0;
794 unsigned kernel_actual;
795 unsigned ramdisk_actual;
796 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800797 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700798
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700799#if DEVICE_TREE
800 struct dt_table *table;
801 struct dt_entry *dt_entry_ptr;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800802 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700803#endif
804
David Ng183a7422009-12-07 14:55:21 -0800805 if (target_is_emmc_boot()) {
806 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
807 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
808 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
809 return -1;
810 }
811 goto continue_boot;
812 }
813
Dima Zavin214cc642009-01-26 11:16:21 -0800814 ptable = flash_get_ptable();
815 if (ptable == NULL) {
816 dprintf(CRITICAL, "ERROR: Partition table not found\n");
817 return -1;
818 }
819
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800820 if(!boot_into_recovery)
821 {
822 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700823
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800824 if (ptn == NULL) {
825 dprintf(CRITICAL, "ERROR: No boot partition found\n");
826 return -1;
827 }
828 }
829 else
830 {
831 ptn = ptable_find(ptable, "recovery");
832 if (ptn == NULL) {
833 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
834 return -1;
835 }
Dima Zavin214cc642009-01-26 11:16:21 -0800836 }
837
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800838 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800839 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
840 return -1;
841 }
Dima Zavin214cc642009-01-26 11:16:21 -0800842
843 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700844 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800845 return -1;
846 }
847
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800848 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700849 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 -0800850 return -1;
851 }
852
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700853 /*
854 * Update the kernel/ramdisk/tags address if the boot image header
855 * has default values, these default values come from mkbootimg when
856 * the boot image is flashed using fastboot flash:raw
857 */
858 update_ker_tags_rdisk_addr(hdr);
859
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700860 /* Get virtual addresses since the hdr saves physical addresses. */
861 hdr->kernel_addr = VA(hdr->kernel_addr);
862 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
863 hdr->tags_addr = VA(hdr->tags_addr);
864
Shashank Mittalcd98d472011-08-02 14:29:24 -0700865 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700866 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700867 {
868 image_addr = (unsigned char *)target_get_scratch_address();
869 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
870 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800871#if DEVICE_TREE
872 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
873 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
874#else
Shashank Mittalcd98d472011-08-02 14:29:24 -0700875 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800876#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700877 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700878
879 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700880 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700881
Amol Jadib6be5c12012-11-14 13:39:51 -0800882 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700883 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800884
Shashank Mittalcd98d472011-08-02 14:29:24 -0700885 /* Read image without signature */
886 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
887 {
888 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
889 return -1;
890 }
Dima Zavin214cc642009-01-26 11:16:21 -0800891
Amol Jadib6be5c12012-11-14 13:39:51 -0800892 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700893 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800894
Shashank Mittalcd98d472011-08-02 14:29:24 -0700895 offset = imagesize_actual;
896 /* Read signature */
897 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
898 {
899 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
900 }
901 else
902 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800903 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700904
905 /* Verify signature */
906 auth_kernel_img = image_verify((unsigned char *)image_addr,
907 (unsigned char *)(image_addr + imagesize_actual),
908 imagesize_actual,
909 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700910
Amol Jadib6be5c12012-11-14 13:39:51 -0800911 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
912
Shashank Mittal162244e2011-08-08 19:01:25 -0700913 if(auth_kernel_img)
914 {
915 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700916 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700917 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700918 }
919
920 /* Move kernel and ramdisk to correct address */
921 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
922 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800923#if DEVICE_TREE
924 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
925#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700926
927 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700928 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700929 {
930 write_device_info_flash(&device);
931 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530932#if USE_PCOM_SECBOOT
933 set_tamper_flag(device.is_tampered);
934#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700935 }
936 else
937 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700938 offset = page_size;
939
Amol Jadib6be5c12012-11-14 13:39:51 -0800940 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
941 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
942 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
943
944 dprintf(INFO, "Loading boot image (%d): start\n",
945 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700946 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800947
948 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700949 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
950 return -1;
951 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800952 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700953
Amol Jadib6be5c12012-11-14 13:39:51 -0800954 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700955 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
956 return -1;
957 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800958 offset += ramdisk_actual;
959
960 dprintf(INFO, "Loading boot image (%d): done\n",
961 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700962 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700963
964 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800965 offset += second_actual;
966 /* Second image loading not implemented. */
967 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700968 }
969
970#if DEVICE_TREE
971 if(hdr->dt_size != 0) {
972
973 /* Read the device tree table into buffer */
974 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
975 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
976 return -1;
977 }
978
979 table = (struct dt_table*) dt_buf;
980
981 /* Restriction that the device tree entry table should be less than a page*/
982 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
983
984 /* Validate the device tree table header */
985 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
986 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
987 return -1;
988 }
989
990 /* Calculate the offset of device tree within device tree table */
991 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
992 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
993 return -1;
994 }
995
996 /* Read device device tree in the "tags_add */
997 if(flash_read(ptn, offset + dt_entry_ptr->offset,
998 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
999 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1000 return -1;
1001 }
1002 }
1003#endif
1004
Shashank Mittalcd98d472011-08-02 14:29:24 -07001005 }
David Ng183a7422009-12-07 14:55:21 -08001006continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001007
Dima Zavin214cc642009-01-26 11:16:21 -08001008 /* TODO: create/pass atags to kernel */
1009
Ajay Dudanie28a6072011-07-01 13:59:46 -07001010 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001011 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001012 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1013
1014 return 0;
1015}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001016
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001017BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001018void write_device_info_mmc(device_info *dev)
1019{
1020 struct device_info *info = (void*) info_buf;
1021 unsigned long long ptn = 0;
1022 unsigned long long size;
1023 int index = INVALID_PTN;
1024
1025 index = partition_get_index("aboot");
1026 ptn = partition_get_offset(index);
1027 if(ptn == 0)
1028 {
1029 return;
1030 }
1031
1032 size = partition_get_size(index);
1033
1034 memcpy(info, dev, sizeof(device_info));
1035
1036 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1037 {
1038 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1039 return;
1040 }
1041}
1042
1043void read_device_info_mmc(device_info *dev)
1044{
1045 struct device_info *info = (void*) info_buf;
1046 unsigned long long ptn = 0;
1047 unsigned long long size;
1048 int index = INVALID_PTN;
1049
1050 index = partition_get_index("aboot");
1051 ptn = partition_get_offset(index);
1052 if(ptn == 0)
1053 {
1054 return;
1055 }
1056
1057 size = partition_get_size(index);
1058
1059 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1060 {
1061 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1062 return;
1063 }
1064
1065 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1066 {
1067 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1068 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001069 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001070
1071 write_device_info_mmc(info);
1072 }
1073 memcpy(dev, info, sizeof(device_info));
1074}
1075
1076void write_device_info_flash(device_info *dev)
1077{
1078 struct device_info *info = (void *) info_buf;
1079 struct ptentry *ptn;
1080 struct ptable *ptable;
1081
1082 ptable = flash_get_ptable();
1083 if (ptable == NULL)
1084 {
1085 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1086 return;
1087 }
1088
1089 ptn = ptable_find(ptable, "devinfo");
1090 if (ptn == NULL)
1091 {
1092 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1093 return;
1094 }
1095
1096 memcpy(info, dev, sizeof(device_info));
1097
1098 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1099 {
1100 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1101 return;
1102 }
1103}
1104
1105void read_device_info_flash(device_info *dev)
1106{
1107 struct device_info *info = (void*) info_buf;
1108 struct ptentry *ptn;
1109 struct ptable *ptable;
1110
1111 ptable = flash_get_ptable();
1112 if (ptable == NULL)
1113 {
1114 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1115 return;
1116 }
1117
1118 ptn = ptable_find(ptable, "devinfo");
1119 if (ptn == NULL)
1120 {
1121 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1122 return;
1123 }
1124
1125 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1126 {
1127 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1128 return;
1129 }
1130
1131 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1132 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001133 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1134 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001135 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001136 write_device_info_flash(info);
1137 }
1138 memcpy(dev, info, sizeof(device_info));
1139}
1140
1141void write_device_info(device_info *dev)
1142{
1143 if(target_is_emmc_boot())
1144 {
1145 write_device_info_mmc(dev);
1146 }
1147 else
1148 {
1149 write_device_info_flash(dev);
1150 }
1151}
1152
1153void read_device_info(device_info *dev)
1154{
1155 if(target_is_emmc_boot())
1156 {
1157 read_device_info_mmc(dev);
1158 }
1159 else
1160 {
1161 read_device_info_flash(dev);
1162 }
1163}
1164
1165void reset_device_info()
1166{
1167 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001168 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001169 write_device_info(&device);
1170}
1171
1172void set_device_root()
1173{
1174 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001175 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001176 write_device_info(&device);
1177}
1178
Amol Jadicb524072012-08-09 16:40:18 -07001179#if DEVICE_TREE
1180int copy_dtb(uint8_t *boot_image_start)
1181{
1182 uint32 dt_image_offset = 0;
1183 uint32_t n;
1184 struct dt_table *table;
1185 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001186
1187 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1188
Amol Jadicb524072012-08-09 16:40:18 -07001189 if(hdr->dt_size != 0) {
1190
1191 /* add kernel offset */
1192 dt_image_offset += page_size;
1193 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1194 dt_image_offset += n;
1195
1196 /* add ramdisk offset */
1197 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1198 dt_image_offset += n;
1199
1200 /* add second offset */
1201 if(hdr->second_size != 0) {
1202 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1203 dt_image_offset += n;
1204 }
1205
1206 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001207 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001208
1209 /* Restriction that the device tree entry table should be less than a page*/
1210 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1211
1212 /* Validate the device tree table header */
1213 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1214 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1215 return -1;
1216 }
1217
1218 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001219 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001220 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1221 return -1;
1222 }
1223
1224 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001225 memmove((void*) hdr->tags_addr,
1226 boot_image_start + dt_image_offset + dt_entry_ptr->offset,
1227 dt_entry_ptr->size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001228 } else
1229 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001230
1231 /* Everything looks fine. Return success. */
1232 return 0;
1233}
1234#endif
1235
Brian Swetland9c4c0752009-01-25 16:23:50 -08001236void cmd_boot(const char *arg, void *data, unsigned sz)
1237{
1238 unsigned kernel_actual;
1239 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001240 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001241 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001242 int ret = 0;
1243 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001244
1245 if (sz < sizeof(hdr)) {
1246 fastboot_fail("invalid bootimage header");
1247 return;
1248 }
1249
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001250 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001251
1252 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001253 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001254
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001255 if(target_is_emmc_boot() && hdr->page_size) {
1256 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001257 page_mask = page_size - 1;
1258 }
1259
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001260 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1261 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1262
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001263 /*
1264 * Update the kernel/ramdisk/tags address if the boot image header
1265 * has default values, these default values come from mkbootimg when
1266 * the boot image is flashed using fastboot flash:raw
1267 */
1268 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001269
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001270 /* Get virtual addresses since the hdr saves physical addresses. */
1271 hdr->kernel_addr = VA(hdr->kernel_addr);
1272 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1273 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001274
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001275 /* sz should have atleast raw boot image */
1276 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001277 fastboot_fail("incomplete bootimage");
1278 return;
1279 }
1280
Amol Jadicb524072012-08-09 16:40:18 -07001281#if DEVICE_TREE
1282 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001283 ret = copy_dtb(data);
1284
1285 dtb_copied = !ret ? 1 : 0;
1286#endif
1287
1288 /* Load ramdisk & kernel */
1289 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1290 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1291
1292#if DEVICE_TREE
1293 /*
1294 * If dtb is not found look for appended DTB in the kernel.
1295 * If appended dev tree is found, update the atags with
1296 * memory address to the DTB appended location on RAM.
1297 * Else update with the atags address in the kernel header
1298 */
1299 if (!dtb_copied) {
1300 void *dtb;
1301 dtb = dev_tree_appended((void *)hdr->kernel_addr, (void *)hdr->tags_addr);
1302 if (!dtb) {
1303 fastboot_fail("dtb not found");
1304 return;
1305 }
Amol Jadicb524072012-08-09 16:40:18 -07001306 }
1307#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001308
1309 fastboot_okay("");
1310 udc_stop();
1311
Dima Zavin77e41f32013-03-06 16:10:43 -08001312 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001313 (const char*) hdr->cmdline, board_machtype(),
1314 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001315}
1316
Dima Zavin214cc642009-01-26 11:16:21 -08001317void cmd_erase(const char *arg, void *data, unsigned sz)
1318{
1319 struct ptentry *ptn;
1320 struct ptable *ptable;
1321
1322 ptable = flash_get_ptable();
1323 if (ptable == NULL) {
1324 fastboot_fail("partition table doesn't exist");
1325 return;
1326 }
1327
1328 ptn = ptable_find(ptable, arg);
1329 if (ptn == NULL) {
1330 fastboot_fail("unknown partition name");
1331 return;
1332 }
1333
1334 if (flash_erase(ptn)) {
1335 fastboot_fail("failed to erase partition");
1336 return;
1337 }
1338 fastboot_okay("");
1339}
1340
Bikas Gurungd48bd242010-09-04 19:54:32 -07001341
1342void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1343{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001344 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001345 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001346 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001347
Kinson Chikf1a43512011-07-14 11:28:39 -07001348 index = partition_get_index(arg);
1349 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001350
Kinson Chikf1a43512011-07-14 11:28:39 -07001351 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001352 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001353 return;
1354 }
neetidb4b24d62012-01-20 12:13:09 -08001355 /* Simple inefficient version of erase. Just writing
1356 0 in first block */
1357 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1358 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001359 return;
1360 }
1361 fastboot_okay("");
1362}
1363
1364
Ajay Dudani5c761132011-04-07 20:19:04 -07001365void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001366{
1367 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001368 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001369 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001370
Greg Grisco6e754772011-06-23 12:19:39 -07001371 if (!strcmp(arg, "partition"))
1372 {
1373 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001374 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001375 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001376 return;
1377 }
1378 }
Greg Grisco6e754772011-06-23 12:19:39 -07001379 else
1380 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001381 index = partition_get_index(arg);
1382 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001383 if(ptn == 0) {
1384 fastboot_fail("partition table doesn't exist");
1385 return;
1386 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001387
Greg Grisco6e754772011-06-23 12:19:39 -07001388 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1389 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1390 fastboot_fail("image is not a boot image");
1391 return;
1392 }
1393 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001394
Kinson Chikf1a43512011-07-14 11:28:39 -07001395 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001396 if (ROUND_TO_PAGE(sz,511) > size) {
1397 fastboot_fail("size too large");
1398 return;
1399 }
1400 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1401 fastboot_fail("flash write failure");
1402 return;
1403 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001404 }
1405 fastboot_okay("");
1406 return;
1407}
1408
Ajay Dudani5c761132011-04-07 20:19:04 -07001409void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1410{
1411 unsigned int chunk;
1412 unsigned int chunk_data_sz;
1413 sparse_header_t *sparse_header;
1414 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001415 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001416 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301417 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001418 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001419
Kinson Chikf1a43512011-07-14 11:28:39 -07001420 index = partition_get_index(arg);
1421 ptn = partition_get_offset(index);
1422 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001423 fastboot_fail("partition table doesn't exist");
1424 return;
1425 }
1426
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301427 size = partition_get_size(index);
1428 if (ROUND_TO_PAGE(sz,511) > size) {
1429 fastboot_fail("size too large");
1430 return;
1431 }
1432
Ajay Dudani5c761132011-04-07 20:19:04 -07001433 /* Read and skip over sparse image header */
1434 sparse_header = (sparse_header_t *) data;
1435 data += sparse_header->file_hdr_sz;
1436 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1437 {
1438 /* Skip the remaining bytes in a header that is longer than
1439 * we expected.
1440 */
1441 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1442 }
1443
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001444 dprintf (SPEW, "=== Sparse Image Header ===\n");
1445 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1446 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1447 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1448 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1449 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1450 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1451 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1452 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001453
1454 /* Start processing chunks */
1455 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1456 {
1457 /* Read and skip over chunk header */
1458 chunk_header = (chunk_header_t *) data;
1459 data += sizeof(chunk_header_t);
1460
1461 dprintf (SPEW, "=== Chunk Header ===\n");
1462 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1463 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1464 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1465
1466 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1467 {
1468 /* Skip the remaining bytes in a header that is longer than
1469 * we expected.
1470 */
1471 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1472 }
1473
1474 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1475 switch (chunk_header->chunk_type)
1476 {
1477 case CHUNK_TYPE_RAW:
1478 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1479 chunk_data_sz))
1480 {
1481 fastboot_fail("Bogus chunk size for chunk type Raw");
1482 return;
1483 }
1484
Ajay Dudaniab18f022011-05-12 14:39:22 -07001485 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1486 chunk_data_sz,
1487 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001488 {
1489 fastboot_fail("flash write failure");
1490 return;
1491 }
1492 total_blocks += chunk_header->chunk_sz;
1493 data += chunk_data_sz;
1494 break;
1495
1496 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001497 total_blocks += chunk_header->chunk_sz;
1498 break;
1499
Ajay Dudani5c761132011-04-07 20:19:04 -07001500 case CHUNK_TYPE_CRC:
1501 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1502 {
1503 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1504 return;
1505 }
1506 total_blocks += chunk_header->chunk_sz;
1507 data += chunk_data_sz;
1508 break;
1509
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001510 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001511 fastboot_fail("Unknown chunk type");
1512 return;
1513 }
1514 }
1515
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001516 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1517 total_blocks, sparse_header->total_blks);
1518
1519 if(total_blocks != sparse_header->total_blks)
1520 {
1521 fastboot_fail("sparse image write failure");
1522 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001523
1524 fastboot_okay("");
1525 return;
1526}
1527
1528void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1529{
1530 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001531 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1532 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001533
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001534#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001535 int ret=0;
1536 uint32 major_version=0;
1537 uint32 minor_version=0;
1538
1539 ret = scm_svc_version(&major_version,&minor_version);
1540 if(!ret)
1541 {
1542 if(major_version >= 2)
1543 {
1544 if( !strcmp(arg,"ssd") || !strcmp(arg,"tqs") )
1545 {
1546 ret = encrypt_scm((uint32 **) &data, &sz);
1547 if (ret != 0) {
1548 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1549 return;
1550 }
1551
1552 ret = scm_protect_keystore((uint32 *) data, sz);
1553 if (ret != 0) {
1554 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1555 return;
1556 }
1557 }
1558 else
1559 {
1560 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1561 if(ret != 0)
1562 {
1563 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1564 return;
1565 }
1566 }
1567 }
1568 else
1569 {
1570 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1571 magic_number[1] == DECRYPT_MAGIC_1)
1572 {
1573 ret = decrypt_scm((uint32 **) &data, &sz);
1574 if (ret != 0) {
1575 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1576 return;
1577 }
1578 }
1579 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1580 magic_number[1] == ENCRYPT_MAGIC_1)
1581 {
1582 ret = encrypt_scm((uint32 **) &data, &sz);
1583 if (ret != 0) {
1584 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1585 return;
1586 }
1587 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001588 }
1589 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001590 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001591 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001592 dprintf(CRITICAL,"INVALID SVC Version\n");
1593 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001594 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001595#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001596
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001597 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001598 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1599 cmd_flash_mmc_img(arg, data, sz);
1600 else
1601 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001602 return;
1603}
1604
Dima Zavin214cc642009-01-26 11:16:21 -08001605void cmd_flash(const char *arg, void *data, unsigned sz)
1606{
1607 struct ptentry *ptn;
1608 struct ptable *ptable;
1609 unsigned extra = 0;
1610
1611 ptable = flash_get_ptable();
1612 if (ptable == NULL) {
1613 fastboot_fail("partition table doesn't exist");
1614 return;
1615 }
1616
1617 ptn = ptable_find(ptable, arg);
1618 if (ptn == NULL) {
1619 fastboot_fail("unknown partition name");
1620 return;
1621 }
1622
1623 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1624 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1625 fastboot_fail("image is not a boot image");
1626 return;
1627 }
1628 }
1629
Amol Jadi5c61a952012-05-04 17:05:35 -07001630 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001631 || !strcmp(ptn->name, "userdata")
1632 || !strcmp(ptn->name, "persist")
1633 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301634 if (flash_ecc_bch_enabled())
1635 /* Spare data bytes for 8 bit ECC increased by 4 */
1636 extra = ((page_size >> 9) * 20);
1637 else
1638 extra = ((page_size >> 9) * 16);
1639 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001640 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001641
1642 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1643 if (flash_write(ptn, extra, data, sz)) {
1644 fastboot_fail("flash write failure");
1645 return;
1646 }
1647 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1648 fastboot_okay("");
1649}
1650
1651void cmd_continue(const char *arg, void *data, unsigned sz)
1652{
1653 fastboot_okay("");
1654 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001655 if (target_is_emmc_boot())
1656 {
1657 boot_linux_from_mmc();
1658 }
1659 else
1660 {
1661 boot_linux_from_flash();
1662 }
Dima Zavin214cc642009-01-26 11:16:21 -08001663}
1664
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001665void cmd_reboot(const char *arg, void *data, unsigned sz)
1666{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001667 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001668 fastboot_okay("");
1669 reboot_device(0);
1670}
1671
1672void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1673{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001674 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001675 fastboot_okay("");
1676 reboot_device(FASTBOOT_MODE);
1677}
1678
Shashank Mittal162244e2011-08-08 19:01:25 -07001679void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1680{
1681 if(!device.is_unlocked)
1682 {
1683 device.is_unlocked = 1;
1684 write_device_info(&device);
1685 }
1686 fastboot_okay("");
1687}
1688
Shashank Mittala0032282011-08-26 14:50:11 -07001689void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1690{
1691 char response[64];
1692 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1693 fastboot_info(response);
1694 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1695 fastboot_info(response);
1696 fastboot_okay("");
1697}
1698
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001699void cmd_preflash(const char *arg, void *data, unsigned sz)
1700{
1701 fastboot_okay("");
1702}
1703
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001704void splash_screen ()
1705{
1706 struct ptentry *ptn;
1707 struct ptable *ptable;
1708 struct fbcon_config *fb_display = NULL;
1709
1710 if (!target_is_emmc_boot())
1711 {
1712 ptable = flash_get_ptable();
1713 if (ptable == NULL) {
1714 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001715 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001716 }
1717
1718 ptn = ptable_find(ptable, "splash");
1719 if (ptn == NULL) {
1720 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1721 } else {
1722 fb_display = fbcon_display();
1723 if (fb_display) {
1724 if (flash_read(ptn, 0, fb_display->base,
1725 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1726 fbcon_clear();
1727 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1728 }
1729 }
1730 }
1731 }
1732}
1733
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001734/* Get the size from partiton name */
1735static void get_partition_size(const char *arg, char *response)
1736{
1737 uint64_t ptn = 0;
1738 uint64_t size;
1739 int index = INVALID_PTN;
1740
1741 index = partition_get_index(arg);
1742
1743 if (index == INVALID_PTN)
1744 {
1745 dprintf(CRITICAL, "Invalid partition index\n");
1746 return;
1747 }
1748
1749 ptn = partition_get_offset(index);
1750
1751 if(!ptn)
1752 {
1753 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1754 return;
1755 }
1756
1757 size = partition_get_size(index);
1758
1759 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1760 return;
1761}
1762
1763/*
1764 * Publish the partition type & size info
1765 * fastboot getvar will publish the required information.
1766 * fastboot getvar partition_size:<partition_name>: partition size in hex
1767 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1768 */
1769static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1770{
1771 uint8_t i;
1772
1773 for (i = 0; i < num_parts; i++) {
1774 get_partition_size(info[i].part_name, info[i].size_response);
1775
1776 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1777 {
1778 dprintf(CRITICAL, "partition size name truncated\n");
1779 return;
1780 }
1781 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1782 {
1783 dprintf(CRITICAL, "partition type name truncated\n");
1784 return;
1785 }
1786
1787 /* publish partition size & type info */
1788 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1789 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1790 }
1791}
1792
Brian Swetland9c4c0752009-01-25 16:23:50 -08001793void aboot_init(const struct app_descriptor *app)
1794{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001795 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001796 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001797 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001798
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001799 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001800 if (target_is_emmc_boot())
1801 {
1802 page_size = 2048;
1803 page_mask = page_size - 1;
1804 }
1805 else
1806 {
1807 page_size = flash_page_size();
1808 page_mask = page_size - 1;
1809 }
1810
Shashank Mittal162244e2011-08-08 19:01:25 -07001811 if(target_use_signed_kernel())
1812 {
1813 read_device_info(&device);
1814
Shashank Mittal162244e2011-08-08 19:01:25 -07001815 }
1816
Greg Griscod6250552011-06-29 14:40:23 -07001817 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001818 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001819 surf_udc_device.serialno = sn_buf;
1820
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001821 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001822 if (keys_get_state(KEY_HOME) != 0)
1823 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001824 if (keys_get_state(KEY_VOLUMEUP) != 0)
1825 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001826 if(!boot_into_recovery)
1827 {
1828 if (keys_get_state(KEY_BACK) != 0)
1829 goto fastboot;
1830 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1831 goto fastboot;
1832 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001833
1834 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001835 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001836 goto fastboot;
1837 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001838
Ajay Dudani77421292010-10-27 19:34:06 -07001839 reboot_mode = check_reboot_mode();
1840 if (reboot_mode == RECOVERY_MODE) {
1841 boot_into_recovery = 1;
1842 } else if(reboot_mode == FASTBOOT_MODE) {
1843 goto fastboot;
1844 }
1845
Shashank Mittal23b8f422010-04-16 19:27:21 -07001846 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001847 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001848 if(emmc_recovery_init())
1849 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001850 if(target_use_signed_kernel())
1851 {
1852 if((device.is_unlocked) || (device.is_tampered))
1853 {
1854 #ifdef TZ_TAMPER_FUSE
1855 set_tamper_fuse_cmd();
1856 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301857 #if USE_PCOM_SECBOOT
1858 set_tamper_flag(device.is_tampered);
1859 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001860 }
1861 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001862 boot_linux_from_mmc();
1863 }
1864 else
1865 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001866 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301867#if USE_PCOM_SECBOOT
1868 if((device.is_unlocked) || (device.is_tampered))
1869 set_tamper_flag(device.is_tampered);
1870#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001871 boot_linux_from_flash();
1872 }
Dima Zavinb4283602009-01-26 16:36:57 -08001873 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1874 "to fastboot mode.\n");
1875
1876fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001877
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001878 sz = target_get_max_flash_size();
1879
Shashank Mittal162244e2011-08-08 19:01:25 -07001880 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001881
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001882 if(!usb_init)
1883 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001884
1885 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001886
Shashank Mittal23b8f422010-04-16 19:27:21 -07001887 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001888 {
1889 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001890 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001891 }
1892 else
1893 {
1894 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001895 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001896 }
1897
1898 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001899 fastboot_register("reboot", cmd_reboot);
1900 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001901 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001902 fastboot_register("oem device-info", cmd_oem_devinfo);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001903 fastboot_register("preflash", cmd_preflash);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001904 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001905 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001906 fastboot_publish("serialno", sn_buf);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001907 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
1908 /* Max download size supported */
1909 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", sz);
1910 fastboot_publish("max-download-size", (const char *) max_download_size);
Kinson Chikf1a43512011-07-14 11:28:39 -07001911 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001912 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001913 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001914}
1915
1916APP_START(aboot)
1917 .init = aboot_init,
1918APP_END