blob: 910cd08856129fbc93fd015981d2a1a679d21343 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Neeti Desaie245d492012-06-01 12:52:13 -07005 * Copyright (c) 2009-2012, 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
Neeti Desai17379b82012-06-04 18:42:53 -070052#if DEVICE_TREE
53#include <libfdt.h>
54#endif
55
Shashank Mittalcd98d472011-08-02 14:29:24 -070056#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080057#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080058#include "bootimg.h"
59#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070060#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070061#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070062#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070063#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080064
Shashank Mittal162244e2011-08-08 19:01:25 -070065#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070066
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070067#define EXPAND(NAME) #NAME
68#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080069#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070070
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080071#ifdef MEMBASE
72#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
73#else
David Ng183a7422009-12-07 14:55:21 -080074#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080075#endif
76
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080077#define RECOVERY_MODE 0x77665502
78#define FASTBOOT_MODE 0x77665500
79
Neeti Desai17379b82012-06-04 18:42:53 -070080#if DEVICE_TREE
Neeti Desai465491e2012-07-31 12:53:35 -070081#define DEV_TREE_SUCCESS 0
82#define DEV_TREE_MAGIC "QCDT"
83#define DEV_TREE_VERSION 1
84#define DEV_TREE_HEADER_SIZE 12
85
86
87struct dt_entry{
88 uint32_t platform_id;
89 uint32_t variant_id;
90 uint32_t soc_rev;
91 uint32_t offset;
92 uint32_t size;
93};
94
95struct dt_table{
96 uint32_t magic;
97 uint32_t version;
98 unsigned num_entries;
99};
100
101struct dt_entry * get_device_tree_ptr(struct dt_table *);
Neeti Desai17379b82012-06-04 18:42:53 -0700102int update_device_tree(const void *, char *, void *, unsigned);
103#endif
104
David Ng183a7422009-12-07 14:55:21 -0800105static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800106static const char *usb_sn_cmdline = " androidboot.serialno=";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800107static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700108static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -0800109
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800110static const char *baseband_apq = " androidboot.baseband=apq";
111static const char *baseband_msm = " androidboot.baseband=msm";
112static const char *baseband_csfb = " androidboot.baseband=csfb";
113static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700114static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700115static const char *baseband_sglte = " androidboot.baseband=sglte";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800116
Shashank Mittalcd98d472011-08-02 14:29:24 -0700117/* Assuming unauthorized kernel image by default */
118static int auth_kernel_img = 0;
119
Shashank Mittal162244e2011-08-08 19:01:25 -0700120static device_info device = {DEVICE_MAGIC, 0, 0};
121
Brian Swetland9c4c0752009-01-25 16:23:50 -0800122static struct udc_device surf_udc_device = {
123 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800124 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800125 .version_id = 0x0100,
126 .manufacturer = "Google",
127 .product = "Android",
128};
129
Dima Zavin42168f22009-01-30 11:52:22 -0800130struct atag_ptbl_entry
131{
132 char name[16];
133 unsigned offset;
134 unsigned size;
135 unsigned flags;
136};
137
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800138char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700139
Greg Griscod2471ef2011-07-14 13:00:42 -0700140extern int emmc_recovery_init(void);
141
Kinson Chik0b1c8162011-08-31 16:31:57 -0700142#if NO_KEYPAD_DRIVER
143extern int fastboot_trigger(void);
144#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700145
Dima Zavin42168f22009-01-30 11:52:22 -0800146static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
147{
148 struct atag_ptbl_entry atag_ptn;
149
150 memcpy(atag_ptn.name, ptn->name, 16);
151 atag_ptn.name[15] = '\0';
152 atag_ptn.offset = ptn->start;
153 atag_ptn.size = ptn->length;
154 atag_ptn.flags = ptn->flags;
155 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
156 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
157}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800158
Neeti Desaie245d492012-06-01 12:52:13 -0700159unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800160{
David Ng183a7422009-12-07 14:55:21 -0800161 int cmdline_len = 0;
162 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800163 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700164 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800165
Brian Swetland9c4c0752009-01-25 16:23:50 -0800166 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800167 cmdline_len = strlen(cmdline);
168 have_cmdline = 1;
169 }
170 if (target_is_emmc_boot()) {
171 cmdline_len += strlen(emmc_cmdline);
172 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800173
174 cmdline_len += strlen(usb_sn_cmdline);
175 cmdline_len += strlen(sn_buf);
176
David Ngf773dde2010-07-26 19:55:08 -0700177 if (target_pause_for_battery_charge()) {
178 pause_at_bootup = 1;
179 cmdline_len += strlen(battchg_pause);
180 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800181
Shashank Mittalcd98d472011-08-02 14:29:24 -0700182 if(target_use_signed_kernel() && auth_kernel_img) {
183 cmdline_len += strlen(auth_kernel);
184 }
185
Ajay Dudanid04110c2011-01-17 23:55:07 -0800186 /* Determine correct androidboot.baseband to use */
187 switch(target_baseband())
188 {
189 case BASEBAND_APQ:
190 cmdline_len += strlen(baseband_apq);
191 break;
192
193 case BASEBAND_MSM:
194 cmdline_len += strlen(baseband_msm);
195 break;
196
197 case BASEBAND_CSFB:
198 cmdline_len += strlen(baseband_csfb);
199 break;
200
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800201 case BASEBAND_SVLTE2A:
202 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800203 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700204
205 case BASEBAND_MDM:
206 cmdline_len += strlen(baseband_mdm);
207 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700208
209 case BASEBAND_SGLTE:
210 cmdline_len += strlen(baseband_sglte);
211 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800212 }
213
David Ng183a7422009-12-07 14:55:21 -0800214 if (cmdline_len > 0) {
215 const char *src;
Neeti Desaie245d492012-06-01 12:52:13 -0700216 char *dst = malloc((cmdline_len + 4) & (~3));
217 assert(dst != NULL);
218
Amol Jadi168b7712012-03-06 16:15:00 -0800219 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700220 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800221 if (have_cmdline) {
222 src = cmdline;
223 while ((*dst++ = *src++));
224 }
225 if (target_is_emmc_boot()) {
226 src = emmc_cmdline;
227 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700228 have_cmdline = 1;
229 while ((*dst++ = *src++));
230 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800231
232 src = usb_sn_cmdline;
233 if (have_cmdline) --dst;
234 have_cmdline = 1;
235 while ((*dst++ = *src++));
236 src = sn_buf;
237 if (have_cmdline) --dst;
238 have_cmdline = 1;
239 while ((*dst++ = *src++));
240
David Ngf773dde2010-07-26 19:55:08 -0700241 if (pause_at_bootup) {
242 src = battchg_pause;
243 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800244 while ((*dst++ = *src++));
245 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800246
Shashank Mittalcd98d472011-08-02 14:29:24 -0700247 if(target_use_signed_kernel() && auth_kernel_img) {
248 src = auth_kernel;
249 if (have_cmdline) --dst;
250 while ((*dst++ = *src++));
251 }
252
Ajay Dudanid04110c2011-01-17 23:55:07 -0800253 switch(target_baseband())
254 {
255 case BASEBAND_APQ:
256 src = baseband_apq;
257 if (have_cmdline) --dst;
258 while ((*dst++ = *src++));
259 break;
260
261 case BASEBAND_MSM:
262 src = baseband_msm;
263 if (have_cmdline) --dst;
264 while ((*dst++ = *src++));
265 break;
266
267 case BASEBAND_CSFB:
268 src = baseband_csfb;
269 if (have_cmdline) --dst;
270 while ((*dst++ = *src++));
271 break;
272
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800273 case BASEBAND_SVLTE2A:
274 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800275 if (have_cmdline) --dst;
276 while ((*dst++ = *src++));
277 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700278
279 case BASEBAND_MDM:
280 src = baseband_mdm;
281 if (have_cmdline) --dst;
282 while ((*dst++ = *src++));
283 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700284
285 case BASEBAND_SGLTE:
286 src = baseband_sglte;
287 if (have_cmdline) --dst;
288 while ((*dst++ = *src++));
289 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800290 }
Neeti Desaie245d492012-06-01 12:52:13 -0700291 }
292 return cmdline_final;
293}
294
295unsigned *atag_core(unsigned *ptr)
296{
297 /* CORE */
298 *ptr++ = 2;
299 *ptr++ = 0x54410001;
300
301 return ptr;
302
303}
304
305unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
306 unsigned ramdisk_size)
307{
308 if (ramdisk_size) {
309 *ptr++ = 4;
310 *ptr++ = 0x54420005;
311 *ptr++ = (unsigned)ramdisk;
312 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800313 }
314
Neeti Desaie245d492012-06-01 12:52:13 -0700315 return ptr;
316}
317
318unsigned *atag_ptable(unsigned **ptr_addr)
319{
320 int i;
321 struct ptable *ptable;
322
323 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
324 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
325 sizeof(unsigned)));
326 *(*ptr_addr)++ = 0x4d534d70;
327 for (i = 0; i < ptable->count; ++i)
328 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
329 }
330
331 return (*ptr_addr);
332}
333
334unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
335{
336 int cmdline_length = 0;
337 int n;
338 unsigned char *cmdline_final = NULL;
339 char *dest;
340
341 cmdline_final = update_cmdline(cmdline);
342 if (cmdline_final){
343 dprintf(INFO, "cmdline: %s\n", cmdline_final);
344 }
345
346 cmdline_length =strlen(cmdline_final);
347 n = (cmdline_length + 4) & (~3);
348
349 *ptr++ = (n / 4) + 2;
350 *ptr++ = 0x54410009;
351 dest = (char *) ptr;
352 while (*dest++ = *cmdline_final++);
353 ptr += (n / 4);
354
355 return ptr;
356}
357
358unsigned *atag_end(unsigned *ptr)
359{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800360 /* END */
361 *ptr++ = 0;
362 *ptr++ = 0;
363
Neeti Desaie245d492012-06-01 12:52:13 -0700364 return ptr;
365}
366
367void generate_atags(unsigned *ptr, const char *cmdline,
368 void *ramdisk, unsigned ramdisk_size)
369{
370
371 ptr = atag_core(ptr);
372 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
373 ptr = target_atag_mem(ptr);
374
375 /* Skip NAND partition ATAGS for eMMC boot */
376 if (!target_is_emmc_boot()){
377 ptr = atag_ptable(&ptr);
378 }
379
380 ptr = atag_cmdline(ptr, cmdline);
381 ptr = atag_end(ptr);
382}
383
384void boot_linux(void *kernel, unsigned *tags,
385 const char *cmdline, unsigned machtype,
386 void *ramdisk, unsigned ramdisk_size)
387{
Neeti Desai17379b82012-06-04 18:42:53 -0700388 int ret = 0;
Neeti Desaie245d492012-06-01 12:52:13 -0700389 void (*entry)(unsigned, unsigned, unsigned*) = kernel;
390
Neeti Desai17379b82012-06-04 18:42:53 -0700391#if DEVICE_TREE
392 /* Update the Device Tree */
393 ret = update_device_tree(tags, cmdline, ramdisk, ramdisk_size);
394 if(ret)
395 {
396 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
397 ASSERT(0);
398 }
399#else
Neeti Desaie245d492012-06-01 12:52:13 -0700400 /* Generating the Atags */
401 generate_atags(tags, cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700402#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700403
Brian Swetland9c4c0752009-01-25 16:23:50 -0800404 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
405 kernel, ramdisk, ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800406
407 enter_critical_section();
Amol Jadi4421e652011-06-16 15:00:48 -0700408 /* do any platform specific cleanup before kernel entry */
409 platform_uninit();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800410 arch_disable_cache(UCACHE);
Amol Jadi01c2c702012-07-19 23:03:45 -0700411 /* NOTE:
412 * The value of "entry" is getting corrupted at this point.
413 * The value is in R4 and gets pushed to stack on entry into
414 * disable_cache(), however, on return it is not the same.
415 * Not entirely sure why this dsb() seems to take of this.
416 * The stack pop operation on return from disable_cache()
417 * should restore R4 properly, but that is not happening.
418 * Will need to revisit to find the root cause.
419 */
420 dsb();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700421#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800422 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700423#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800424 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800425}
426
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800427unsigned page_size = 0;
428unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800429
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800430#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800431
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800432static unsigned char buf[4096]; //Equal to max-supported pagesize
Neeti Desai465491e2012-07-31 12:53:35 -0700433static unsigned char dt_buf[4096];
Dima Zavin214cc642009-01-26 11:16:21 -0800434
Shashank Mittal23b8f422010-04-16 19:27:21 -0700435int boot_linux_from_mmc(void)
436{
437 struct boot_img_hdr *hdr = (void*) buf;
438 struct boot_img_hdr *uhdr;
439 unsigned offset = 0;
440 unsigned long long ptn = 0;
441 unsigned n = 0;
442 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700443 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700444
Shashank Mittalcd98d472011-08-02 14:29:24 -0700445 unsigned char *image_addr = 0;
446 unsigned kernel_actual;
447 unsigned ramdisk_actual;
448 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700449 unsigned second_actual = 0;
450 unsigned dt_actual = 0;
451
452#if DEVICE_TREE
453 struct dt_table *table;
454 struct dt_entry *dt_entry_ptr;
455 unsigned dt_table_offset;
456#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700457
Shashank Mittal23b8f422010-04-16 19:27:21 -0700458 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
459 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
460 dprintf(INFO, "Unified boot method!\n");
461 hdr = uhdr;
462 goto unified_boot;
463 }
Greg Griscod6250552011-06-29 14:40:23 -0700464 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700465 index = partition_get_index("boot");
466 ptn = partition_get_offset(index);
467 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700468 dprintf(CRITICAL, "ERROR: No boot partition found\n");
469 return -1;
470 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700471 }
472 else {
473 index = partition_get_index("recovery");
474 ptn = partition_get_offset(index);
475 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700476 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
477 return -1;
478 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700479 }
480
Greg Griscod6250552011-06-29 14:40:23 -0700481 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700482 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
483 return -1;
484 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700485
486 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700487 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700488 return -1;
489 }
490
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700491 if (hdr->page_size && (hdr->page_size != page_size)) {
492 page_size = hdr->page_size;
493 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700494 }
495
Shashank Mittalcd98d472011-08-02 14:29:24 -0700496 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700497 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800498 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700499 image_addr = (unsigned char *)target_get_scratch_address();
500 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
501 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530502 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700503
504 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700505
506 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700507 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700508
Shashank Mittalcd98d472011-08-02 14:29:24 -0700509 /* Read image without signature */
510 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
511 {
512 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
513 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800514 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700515
516 offset = imagesize_actual;
517 /* Read signature */
518 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
519 {
520 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
521 }
522 else
523 {
524 auth_kernel_img = image_verify((unsigned char *)image_addr,
525 (unsigned char *)(image_addr + imagesize_actual),
526 imagesize_actual,
527 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700528
529 if(auth_kernel_img)
530 {
531 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700532 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700533 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700534 }
535
Neeti Desai465491e2012-07-31 12:53:35 -0700536 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700537 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
538 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700539
Neeti Desai465491e2012-07-31 12:53:35 -0700540 #if DEVICE_TREE
541 if(hdr->dt_size) {
542 table = (struct dt_table*) dt_buf;
543 dt_table_offset = (image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
544
545 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
546
547 /* Restriction that the device tree entry table should be less than a page*/
548 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
549
550 /* Validate the device tree table header */
551 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
552 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
553 return -1;
554 }
555
556 /* Find index of device tree within device tree table */
557 if((dt_entry_ptr = get_device_tree_ptr(table)) == NULL){
558 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
559 return -1;
560 }
561
562 /* Read device device tree in the "tags_add */
563 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
564 }
565 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700566 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700567 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700568 {
569 write_device_info_mmc(&device);
570 #ifdef TZ_TAMPER_FUSE
571 set_tamper_fuse_cmd();
572 #endif
573 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530574 #if USE_PCOM_SECBOOT
575 set_tamper_flag(device.is_tampered);
576 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700577 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700578 else
579 {
580 offset += page_size;
581
582 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
583 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
584 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
585 return -1;
586 }
587 offset += n;
588
589 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
590 if(n != 0)
591 {
592 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
593 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
594 return -1;
595 }
596 }
597 offset += n;
Neeti Desai465491e2012-07-31 12:53:35 -0700598
599 if(hdr->second_size != 0) {
600 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
601 offset += n;
602 }
603
604 #if DEVICE_TREE
605 if(hdr->dt_size != 0) {
606
607 /* Read the device tree table into buffer */
608 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
609 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
610 return -1;
611 }
612 table = (struct dt_table*) dt_buf;
613
614 /* Restriction that the device tree entry table should be less than a page*/
615 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
616
617 /* Validate the device tree table header */
618 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
619 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
620 return -1;
621 }
622
623 /* Calculate the offset of device tree within device tree table */
624 if((dt_entry_ptr = get_device_tree_ptr(table)) == NULL){
625 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
626 return -1;
627 }
628
629 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700630 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
631 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
632 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
633 return -1;
634 }
635 }
636 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700637 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700638
639unified_boot:
640 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
641 hdr->kernel_size);
642 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
643 hdr->ramdisk_size);
644
645 if(hdr->cmdline[0]) {
646 cmdline = (char*) hdr->cmdline;
647 } else {
648 cmdline = DEFAULT_CMDLINE;
649 }
650 dprintf(INFO, "cmdline = '%s'\n", cmdline);
651
652 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700653 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700654 (const char *)cmdline, board_machtype(),
655 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
656
657 return 0;
658}
659
Dima Zavin214cc642009-01-26 11:16:21 -0800660int boot_linux_from_flash(void)
661{
662 struct boot_img_hdr *hdr = (void*) buf;
663 unsigned n;
664 struct ptentry *ptn;
665 struct ptable *ptable;
666 unsigned offset = 0;
667 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800668
Shashank Mittalcd98d472011-08-02 14:29:24 -0700669 unsigned char *image_addr = 0;
670 unsigned kernel_actual;
671 unsigned ramdisk_actual;
672 unsigned imagesize_actual;
673
David Ng183a7422009-12-07 14:55:21 -0800674 if (target_is_emmc_boot()) {
675 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
676 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
677 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
678 return -1;
679 }
680 goto continue_boot;
681 }
682
Dima Zavin214cc642009-01-26 11:16:21 -0800683 ptable = flash_get_ptable();
684 if (ptable == NULL) {
685 dprintf(CRITICAL, "ERROR: Partition table not found\n");
686 return -1;
687 }
688
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800689 if(!boot_into_recovery)
690 {
691 ptn = ptable_find(ptable, "boot");
692 if (ptn == NULL) {
693 dprintf(CRITICAL, "ERROR: No boot partition found\n");
694 return -1;
695 }
696 }
697 else
698 {
699 ptn = ptable_find(ptable, "recovery");
700 if (ptn == NULL) {
701 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
702 return -1;
703 }
Dima Zavin214cc642009-01-26 11:16:21 -0800704 }
705
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800706 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800707 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
708 return -1;
709 }
Dima Zavin214cc642009-01-26 11:16:21 -0800710
711 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700712 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800713 return -1;
714 }
715
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800716 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700717 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 -0800718 return -1;
719 }
720
Shashank Mittalcd98d472011-08-02 14:29:24 -0700721 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700722 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700723 {
724 image_addr = (unsigned char *)target_get_scratch_address();
725 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
726 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
727 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800728
Shashank Mittalcd98d472011-08-02 14:29:24 -0700729 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700730
731 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700732 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700733
Shashank Mittalcd98d472011-08-02 14:29:24 -0700734 /* Read image without signature */
735 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
736 {
737 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
738 return -1;
739 }
Dima Zavin214cc642009-01-26 11:16:21 -0800740
Shashank Mittalcd98d472011-08-02 14:29:24 -0700741 offset = imagesize_actual;
742 /* Read signature */
743 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
744 {
745 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
746 }
747 else
748 {
749
750 /* Verify signature */
751 auth_kernel_img = image_verify((unsigned char *)image_addr,
752 (unsigned char *)(image_addr + imagesize_actual),
753 imagesize_actual,
754 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700755
756 if(auth_kernel_img)
757 {
758 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700759 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700760 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700761 }
762
763 /* Move kernel and ramdisk to correct address */
764 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
765 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700766
767 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700768 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700769 {
770 write_device_info_flash(&device);
771 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530772#if USE_PCOM_SECBOOT
773 set_tamper_flag(device.is_tampered);
774#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700775 }
776 else
777 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700778 offset = page_size;
779
Shashank Mittalcd98d472011-08-02 14:29:24 -0700780 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
781 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
782 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
783 return -1;
784 }
785 offset += n;
786
787 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
788 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
789 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
790 return -1;
791 }
792 offset += n;
793 }
David Ng183a7422009-12-07 14:55:21 -0800794continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800795 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
796 hdr->kernel_size);
797 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
798 hdr->ramdisk_size);
799
800 if(hdr->cmdline[0]) {
801 cmdline = (char*) hdr->cmdline;
802 } else {
803 cmdline = DEFAULT_CMDLINE;
804 }
805 dprintf(INFO, "cmdline = '%s'\n", cmdline);
806
807 /* TODO: create/pass atags to kernel */
808
809 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700810 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800811 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800812 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
813
814 return 0;
815}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800816
Shashank Mittal162244e2011-08-08 19:01:25 -0700817unsigned char info_buf[4096];
818void write_device_info_mmc(device_info *dev)
819{
820 struct device_info *info = (void*) info_buf;
821 unsigned long long ptn = 0;
822 unsigned long long size;
823 int index = INVALID_PTN;
824
825 index = partition_get_index("aboot");
826 ptn = partition_get_offset(index);
827 if(ptn == 0)
828 {
829 return;
830 }
831
832 size = partition_get_size(index);
833
834 memcpy(info, dev, sizeof(device_info));
835
836 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
837 {
838 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
839 return;
840 }
841}
842
843void read_device_info_mmc(device_info *dev)
844{
845 struct device_info *info = (void*) info_buf;
846 unsigned long long ptn = 0;
847 unsigned long long size;
848 int index = INVALID_PTN;
849
850 index = partition_get_index("aboot");
851 ptn = partition_get_offset(index);
852 if(ptn == 0)
853 {
854 return;
855 }
856
857 size = partition_get_size(index);
858
859 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
860 {
861 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
862 return;
863 }
864
865 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
866 {
867 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
868 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700869 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700870
871 write_device_info_mmc(info);
872 }
873 memcpy(dev, info, sizeof(device_info));
874}
875
876void write_device_info_flash(device_info *dev)
877{
878 struct device_info *info = (void *) info_buf;
879 struct ptentry *ptn;
880 struct ptable *ptable;
881
882 ptable = flash_get_ptable();
883 if (ptable == NULL)
884 {
885 dprintf(CRITICAL, "ERROR: Partition table not found\n");
886 return;
887 }
888
889 ptn = ptable_find(ptable, "devinfo");
890 if (ptn == NULL)
891 {
892 dprintf(CRITICAL, "ERROR: No boot partition found\n");
893 return;
894 }
895
896 memcpy(info, dev, sizeof(device_info));
897
898 if (flash_write(ptn, 0, (void *)info_buf, page_size))
899 {
900 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
901 return;
902 }
903}
904
905void read_device_info_flash(device_info *dev)
906{
907 struct device_info *info = (void*) info_buf;
908 struct ptentry *ptn;
909 struct ptable *ptable;
910
911 ptable = flash_get_ptable();
912 if (ptable == NULL)
913 {
914 dprintf(CRITICAL, "ERROR: Partition table not found\n");
915 return;
916 }
917
918 ptn = ptable_find(ptable, "devinfo");
919 if (ptn == NULL)
920 {
921 dprintf(CRITICAL, "ERROR: No boot partition found\n");
922 return;
923 }
924
925 if (flash_read(ptn, 0, (void *)info_buf, page_size))
926 {
927 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
928 return;
929 }
930
931 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
932 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700933 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
934 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700935 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700936 write_device_info_flash(info);
937 }
938 memcpy(dev, info, sizeof(device_info));
939}
940
941void write_device_info(device_info *dev)
942{
943 if(target_is_emmc_boot())
944 {
945 write_device_info_mmc(dev);
946 }
947 else
948 {
949 write_device_info_flash(dev);
950 }
951}
952
953void read_device_info(device_info *dev)
954{
955 if(target_is_emmc_boot())
956 {
957 read_device_info_mmc(dev);
958 }
959 else
960 {
961 read_device_info_flash(dev);
962 }
963}
964
965void reset_device_info()
966{
967 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700968 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700969 write_device_info(&device);
970}
971
972void set_device_root()
973{
974 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700975 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700976 write_device_info(&device);
977}
978
Amol Jadicb524072012-08-09 16:40:18 -0700979#if DEVICE_TREE
980int copy_dtb(uint8_t *boot_image_start)
981{
982 uint32 dt_image_offset = 0;
983 uint32_t n;
984 struct dt_table *table;
985 struct dt_entry *dt_entry_ptr;
986 unsigned dt_table_offset;
987
988 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
989
990
991 if(hdr->dt_size != 0) {
992
993 /* add kernel offset */
994 dt_image_offset += page_size;
995 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
996 dt_image_offset += n;
997
998 /* add ramdisk offset */
999 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1000 dt_image_offset += n;
1001
1002 /* add second offset */
1003 if(hdr->second_size != 0) {
1004 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1005 dt_image_offset += n;
1006 }
1007
1008 /* offset now point to start of dt.img */
1009 table = boot_image_start + dt_image_offset;
1010
1011 /* Restriction that the device tree entry table should be less than a page*/
1012 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1013
1014 /* Validate the device tree table header */
1015 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1016 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1017 return -1;
1018 }
1019
1020 /* Calculate the offset of device tree within device tree table */
1021 if((dt_entry_ptr = get_device_tree_ptr(table)) == NULL){
1022 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1023 return -1;
1024 }
1025
1026 /* Read device device tree in the "tags_add */
1027 memmove((void*) hdr->tags_addr, boot_image_start + dt_image_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
1028 }
1029
1030 /* Everything looks fine. Return success. */
1031 return 0;
1032}
1033#endif
1034
Brian Swetland9c4c0752009-01-25 16:23:50 -08001035void cmd_boot(const char *arg, void *data, unsigned sz)
1036{
1037 unsigned kernel_actual;
1038 unsigned ramdisk_actual;
1039 static struct boot_img_hdr hdr;
1040 char *ptr = ((char*) data);
1041
1042 if (sz < sizeof(hdr)) {
1043 fastboot_fail("invalid bootimage header");
1044 return;
1045 }
1046
1047 memcpy(&hdr, data, sizeof(hdr));
1048
1049 /* ensure commandline is terminated */
1050 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
1051
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001052 if(target_is_emmc_boot() && hdr.page_size) {
1053 page_size = hdr.page_size;
1054 page_mask = page_size - 1;
1055 }
1056
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001057 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
1058 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001059
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001060 /* sz should have atleast raw boot image */
1061 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001062 fastboot_fail("incomplete bootimage");
1063 return;
1064 }
1065
Amol Jadicb524072012-08-09 16:40:18 -07001066#if DEVICE_TREE
1067 /* find correct dtb and copy it to right location */
1068 if(copy_dtb(data))
1069 {
1070 fastboot_fail("dtb not found");
1071 return;
1072 }
1073#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001074
1075 fastboot_okay("");
1076 udc_stop();
1077
Amol Jadicb524072012-08-09 16:40:18 -07001078 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
1079 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
1080
Amol Jadie67872e2011-06-27 14:14:11 -07001081 boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -08001082 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -07001083 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001084}
1085
Dima Zavin214cc642009-01-26 11:16:21 -08001086void cmd_erase(const char *arg, void *data, unsigned sz)
1087{
1088 struct ptentry *ptn;
1089 struct ptable *ptable;
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 (flash_erase(ptn)) {
1104 fastboot_fail("failed to erase partition");
1105 return;
1106 }
1107 fastboot_okay("");
1108}
1109
Bikas Gurungd48bd242010-09-04 19:54:32 -07001110
1111void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1112{
1113 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -08001114 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -07001115 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001116
Kinson Chikf1a43512011-07-14 11:28:39 -07001117 index = partition_get_index(arg);
1118 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001119
Kinson Chikf1a43512011-07-14 11:28:39 -07001120 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001121 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001122 return;
1123 }
neetidb4b24d62012-01-20 12:13:09 -08001124 /* Simple inefficient version of erase. Just writing
1125 0 in first block */
1126 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1127 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001128 return;
1129 }
1130 fastboot_okay("");
1131}
1132
1133
Ajay Dudani5c761132011-04-07 20:19:04 -07001134void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001135{
1136 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001137 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001138 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001139
Greg Grisco6e754772011-06-23 12:19:39 -07001140 if (!strcmp(arg, "partition"))
1141 {
1142 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001143 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001144 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001145 return;
1146 }
1147 }
Greg Grisco6e754772011-06-23 12:19:39 -07001148 else
1149 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001150 index = partition_get_index(arg);
1151 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001152 if(ptn == 0) {
1153 fastboot_fail("partition table doesn't exist");
1154 return;
1155 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001156
Greg Grisco6e754772011-06-23 12:19:39 -07001157 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1158 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1159 fastboot_fail("image is not a boot image");
1160 return;
1161 }
1162 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001163
Kinson Chikf1a43512011-07-14 11:28:39 -07001164 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001165 if (ROUND_TO_PAGE(sz,511) > size) {
1166 fastboot_fail("size too large");
1167 return;
1168 }
1169 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1170 fastboot_fail("flash write failure");
1171 return;
1172 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001173 }
1174 fastboot_okay("");
1175 return;
1176}
1177
Ajay Dudani5c761132011-04-07 20:19:04 -07001178void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1179{
1180 unsigned int chunk;
1181 unsigned int chunk_data_sz;
1182 sparse_header_t *sparse_header;
1183 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001184 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001185 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301186 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001187 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001188
Kinson Chikf1a43512011-07-14 11:28:39 -07001189 index = partition_get_index(arg);
1190 ptn = partition_get_offset(index);
1191 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001192 fastboot_fail("partition table doesn't exist");
1193 return;
1194 }
1195
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301196 size = partition_get_size(index);
1197 if (ROUND_TO_PAGE(sz,511) > size) {
1198 fastboot_fail("size too large");
1199 return;
1200 }
1201
Ajay Dudani5c761132011-04-07 20:19:04 -07001202 /* Read and skip over sparse image header */
1203 sparse_header = (sparse_header_t *) data;
1204 data += sparse_header->file_hdr_sz;
1205 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1206 {
1207 /* Skip the remaining bytes in a header that is longer than
1208 * we expected.
1209 */
1210 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1211 }
1212
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001213 dprintf (SPEW, "=== Sparse Image Header ===\n");
1214 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1215 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1216 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1217 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1218 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1219 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1220 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1221 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001222
1223 /* Start processing chunks */
1224 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1225 {
1226 /* Read and skip over chunk header */
1227 chunk_header = (chunk_header_t *) data;
1228 data += sizeof(chunk_header_t);
1229
1230 dprintf (SPEW, "=== Chunk Header ===\n");
1231 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1232 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1233 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1234
1235 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1236 {
1237 /* Skip the remaining bytes in a header that is longer than
1238 * we expected.
1239 */
1240 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1241 }
1242
1243 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1244 switch (chunk_header->chunk_type)
1245 {
1246 case CHUNK_TYPE_RAW:
1247 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1248 chunk_data_sz))
1249 {
1250 fastboot_fail("Bogus chunk size for chunk type Raw");
1251 return;
1252 }
1253
Ajay Dudaniab18f022011-05-12 14:39:22 -07001254 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1255 chunk_data_sz,
1256 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001257 {
1258 fastboot_fail("flash write failure");
1259 return;
1260 }
1261 total_blocks += chunk_header->chunk_sz;
1262 data += chunk_data_sz;
1263 break;
1264
1265 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001266 total_blocks += chunk_header->chunk_sz;
1267 break;
1268
Ajay Dudani5c761132011-04-07 20:19:04 -07001269 case CHUNK_TYPE_CRC:
1270 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1271 {
1272 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1273 return;
1274 }
1275 total_blocks += chunk_header->chunk_sz;
1276 data += chunk_data_sz;
1277 break;
1278
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001279 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001280 fastboot_fail("Unknown chunk type");
1281 return;
1282 }
1283 }
1284
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001285 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1286 total_blocks, sparse_header->total_blks);
1287
1288 if(total_blocks != sparse_header->total_blks)
1289 {
1290 fastboot_fail("sparse image write failure");
1291 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001292
1293 fastboot_okay("");
1294 return;
1295}
1296
1297void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1298{
1299 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001300 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1301 unsigned int *magic_number = (unsigned int *) data;
1302 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001303
Neeti Desai127b9e02012-03-20 16:11:23 -07001304 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1305 magic_number[1] == DECRYPT_MAGIC_1)
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001306 {
1307#ifdef SSD_ENABLE
Neeti Desai127b9e02012-03-20 16:11:23 -07001308 ret = decrypt_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001309#endif
Greg Griscod6250552011-06-29 14:40:23 -07001310 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001311 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1312 return;
1313 }
1314 }
Neeti Desai127b9e02012-03-20 16:11:23 -07001315 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1316 magic_number[1] == ENCRYPT_MAGIC_1)
1317 {
1318#ifdef SSD_ENABLE
1319 ret = encrypt_scm((uint32 **) &data, &sz);
1320#endif
1321 if (ret != 0) {
1322 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1323 return;
1324 }
1325 }
1326
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001327 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001328 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1329 cmd_flash_mmc_img(arg, data, sz);
1330 else
1331 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001332 return;
1333}
1334
Dima Zavin214cc642009-01-26 11:16:21 -08001335void cmd_flash(const char *arg, void *data, unsigned sz)
1336{
1337 struct ptentry *ptn;
1338 struct ptable *ptable;
1339 unsigned extra = 0;
1340
1341 ptable = flash_get_ptable();
1342 if (ptable == NULL) {
1343 fastboot_fail("partition table doesn't exist");
1344 return;
1345 }
1346
1347 ptn = ptable_find(ptable, arg);
1348 if (ptn == NULL) {
1349 fastboot_fail("unknown partition name");
1350 return;
1351 }
1352
1353 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1354 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1355 fastboot_fail("image is not a boot image");
1356 return;
1357 }
1358 }
1359
Amol Jadi5c61a952012-05-04 17:05:35 -07001360 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001361 || !strcmp(ptn->name, "userdata")
1362 || !strcmp(ptn->name, "persist")
1363 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301364 if (flash_ecc_bch_enabled())
1365 /* Spare data bytes for 8 bit ECC increased by 4 */
1366 extra = ((page_size >> 9) * 20);
1367 else
1368 extra = ((page_size >> 9) * 16);
1369 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001370 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001371
1372 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1373 if (flash_write(ptn, extra, data, sz)) {
1374 fastboot_fail("flash write failure");
1375 return;
1376 }
1377 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1378 fastboot_okay("");
1379}
1380
1381void cmd_continue(const char *arg, void *data, unsigned sz)
1382{
1383 fastboot_okay("");
1384 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001385 if (target_is_emmc_boot())
1386 {
1387 boot_linux_from_mmc();
1388 }
1389 else
1390 {
1391 boot_linux_from_flash();
1392 }
Dima Zavin214cc642009-01-26 11:16:21 -08001393}
1394
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001395void cmd_reboot(const char *arg, void *data, unsigned sz)
1396{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001397 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001398 fastboot_okay("");
1399 reboot_device(0);
1400}
1401
1402void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1403{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001404 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001405 fastboot_okay("");
1406 reboot_device(FASTBOOT_MODE);
1407}
1408
Shashank Mittal162244e2011-08-08 19:01:25 -07001409void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1410{
1411 if(!device.is_unlocked)
1412 {
1413 device.is_unlocked = 1;
1414 write_device_info(&device);
1415 }
1416 fastboot_okay("");
1417}
1418
Shashank Mittala0032282011-08-26 14:50:11 -07001419void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1420{
1421 char response[64];
1422 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1423 fastboot_info(response);
1424 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1425 fastboot_info(response);
1426 fastboot_okay("");
1427}
1428
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001429void splash_screen ()
1430{
1431 struct ptentry *ptn;
1432 struct ptable *ptable;
1433 struct fbcon_config *fb_display = NULL;
1434
1435 if (!target_is_emmc_boot())
1436 {
1437 ptable = flash_get_ptable();
1438 if (ptable == NULL) {
1439 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001440 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001441 }
1442
1443 ptn = ptable_find(ptable, "splash");
1444 if (ptn == NULL) {
1445 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1446 } else {
1447 fb_display = fbcon_display();
1448 if (fb_display) {
1449 if (flash_read(ptn, 0, fb_display->base,
1450 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1451 fbcon_clear();
1452 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1453 }
1454 }
1455 }
1456 }
1457}
1458
Brian Swetland9c4c0752009-01-25 16:23:50 -08001459void aboot_init(const struct app_descriptor *app)
1460{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001461 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001462 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001463 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001464
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001465 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001466 if (target_is_emmc_boot())
1467 {
1468 page_size = 2048;
1469 page_mask = page_size - 1;
1470 }
1471 else
1472 {
1473 page_size = flash_page_size();
1474 page_mask = page_size - 1;
1475 }
1476
Shashank Mittal162244e2011-08-08 19:01:25 -07001477 if(target_use_signed_kernel())
1478 {
1479 read_device_info(&device);
1480
Shashank Mittal162244e2011-08-08 19:01:25 -07001481 }
1482
Greg Griscod6250552011-06-29 14:40:23 -07001483 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001484 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001485 surf_udc_device.serialno = sn_buf;
1486
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001487 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001488 if (keys_get_state(KEY_HOME) != 0)
1489 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001490 if (keys_get_state(KEY_VOLUMEUP) != 0)
1491 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001492 if(!boot_into_recovery)
1493 {
1494 if (keys_get_state(KEY_BACK) != 0)
1495 goto fastboot;
1496 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1497 goto fastboot;
1498 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001499
1500 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001501 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001502 goto fastboot;
1503 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001504
Ajay Dudani77421292010-10-27 19:34:06 -07001505 reboot_mode = check_reboot_mode();
1506 if (reboot_mode == RECOVERY_MODE) {
1507 boot_into_recovery = 1;
1508 } else if(reboot_mode == FASTBOOT_MODE) {
1509 goto fastboot;
1510 }
1511
Shashank Mittal23b8f422010-04-16 19:27:21 -07001512 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001513 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001514 if(emmc_recovery_init())
1515 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001516 if(target_use_signed_kernel())
1517 {
1518 if((device.is_unlocked) || (device.is_tampered))
1519 {
1520 #ifdef TZ_TAMPER_FUSE
1521 set_tamper_fuse_cmd();
1522 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301523 #if USE_PCOM_SECBOOT
1524 set_tamper_flag(device.is_tampered);
1525 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001526 }
1527 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001528 boot_linux_from_mmc();
1529 }
1530 else
1531 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001532 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301533#if USE_PCOM_SECBOOT
1534 if((device.is_unlocked) || (device.is_tampered))
1535 set_tamper_flag(device.is_tampered);
1536#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001537 boot_linux_from_flash();
1538 }
Dima Zavinb4283602009-01-26 16:36:57 -08001539 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1540 "to fastboot mode.\n");
1541
1542fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001543
Shashank Mittal162244e2011-08-08 19:01:25 -07001544 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001545
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001546 if(!usb_init)
1547 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001548
1549 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001550
Shashank Mittal23b8f422010-04-16 19:27:21 -07001551 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001552 {
1553 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001554 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001555 }
1556 else
1557 {
1558 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001559 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001560 }
1561
1562 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001563 fastboot_register("reboot", cmd_reboot);
1564 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001565 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001566 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001567 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001568 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001569 fastboot_publish("serialno", sn_buf);
Kinson Chikf1a43512011-07-14 11:28:39 -07001570 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001571 sz = target_get_max_flash_size();
1572 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001573 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001574}
1575
1576APP_START(aboot)
1577 .init = aboot_init,
1578APP_END
1579
Neeti Desai17379b82012-06-04 18:42:53 -07001580#if DEVICE_TREE
Neeti Desai465491e2012-07-31 12:53:35 -07001581struct dt_entry * get_device_tree_ptr(struct dt_table *table)
1582{
1583 unsigned i;
1584 struct dt_entry *dt_entry_ptr;
1585
1586 dt_entry_ptr = (char *)table + DEV_TREE_HEADER_SIZE ;
1587
1588 for(i = 0; i < table->num_entries; i++)
1589 {
1590 if((dt_entry_ptr->platform_id == board_platform_id()) &&
1591 (dt_entry_ptr->variant_id == board_hardware_id()) &&
1592 (dt_entry_ptr->soc_rev == 0)){
1593 return dt_entry_ptr;
1594 }
1595 dt_entry_ptr++;
1596 }
1597 return NULL;
1598}
1599
Neeti Desai17379b82012-06-04 18:42:53 -07001600int update_device_tree(const void * fdt, char *cmdline,
1601 void *ramdisk, unsigned ramdisk_size)
1602{
1603 int ret = 0;
1604 int offset;
1605 uint32_t *memory_reg;
1606 unsigned char *final_cmdline;
1607 uint32_t len;
1608
1609 /* Check the device tree header */
1610 ret = fdt_check_header(fdt);
1611 if(ret)
1612 {
1613 dprintf(CRITICAL, "Invalid device tree header \n");
1614 return ret;
1615 }
1616
1617 /* Get offset of the memory node */
1618 offset = fdt_path_offset(fdt,"/memory");
1619
1620 memory_reg = target_dev_tree_mem(&len);
1621
1622 /* Adding the memory values to the reg property */
1623 ret = fdt_setprop(fdt, offset, "reg", memory_reg, sizeof(uint32_t) * len * 2);
1624 if(ret)
1625 {
1626 dprintf(CRITICAL, "ERROR: Cannot update memory node\n");
1627 return ret;
1628 }
1629
1630 /* Get offset of the chosen node */
1631 offset = fdt_path_offset(fdt, "/chosen");
1632
1633 /* Adding the cmdline to the chosen node */
1634 final_cmdline = update_cmdline(cmdline);
1635 ret = fdt_setprop_string(fdt, offset, "bootargs", final_cmdline);
1636 if(ret)
1637 {
1638 dprintf(CRITICAL, "ERROR: Cannot update chosen node [bootargs]\n");
1639 return ret;
1640 }
1641
1642 /* Adding the initrd-start to the chosen node */
1643 ret = fdt_setprop_cell(fdt, offset, "linux,initrd-start", ramdisk);
1644 if(ret)
1645 {
1646 dprintf(CRITICAL, "ERROR: Cannot update chosen node [linux,initrd-start]\n");
1647 return ret;
1648 }
1649
1650 /* Adding the initrd-end to the chosen node */
1651 ret = fdt_setprop_cell(fdt, offset, "linux,initrd-end", (ramdisk + ramdisk_size));
1652 if(ret)
1653 {
1654 dprintf(CRITICAL, "ERROR: Cannot update chosen node [linux,initrd-end]\n");
1655 return ret;
1656 }
1657
1658 fdt_pack(fdt);
1659
1660 return ret;
1661}
1662#endif