blob: dbcd8dd5520fdffe6fe6151b688f8d7ee81588b8 [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();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800421 arch_disable_mmu();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800422 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800423}
424
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800425unsigned page_size = 0;
426unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800427
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800428#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800429
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800430static unsigned char buf[4096]; //Equal to max-supported pagesize
Neeti Desai465491e2012-07-31 12:53:35 -0700431static unsigned char dt_buf[4096];
Dima Zavin214cc642009-01-26 11:16:21 -0800432
Shashank Mittal23b8f422010-04-16 19:27:21 -0700433int boot_linux_from_mmc(void)
434{
435 struct boot_img_hdr *hdr = (void*) buf;
436 struct boot_img_hdr *uhdr;
437 unsigned offset = 0;
438 unsigned long long ptn = 0;
439 unsigned n = 0;
440 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700441 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700442
Shashank Mittalcd98d472011-08-02 14:29:24 -0700443 unsigned char *image_addr = 0;
444 unsigned kernel_actual;
445 unsigned ramdisk_actual;
446 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700447 unsigned second_actual = 0;
448 unsigned dt_actual = 0;
449
450#if DEVICE_TREE
451 struct dt_table *table;
452 struct dt_entry *dt_entry_ptr;
453 unsigned dt_table_offset;
454#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700455
Shashank Mittal23b8f422010-04-16 19:27:21 -0700456 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
457 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
458 dprintf(INFO, "Unified boot method!\n");
459 hdr = uhdr;
460 goto unified_boot;
461 }
Greg Griscod6250552011-06-29 14:40:23 -0700462 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700463 index = partition_get_index("boot");
464 ptn = partition_get_offset(index);
465 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700466 dprintf(CRITICAL, "ERROR: No boot partition found\n");
467 return -1;
468 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700469 }
470 else {
471 index = partition_get_index("recovery");
472 ptn = partition_get_offset(index);
473 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700474 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
475 return -1;
476 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700477 }
478
Greg Griscod6250552011-06-29 14:40:23 -0700479 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700480 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
481 return -1;
482 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700483
484 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700485 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700486 return -1;
487 }
488
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700489 if (hdr->page_size && (hdr->page_size != page_size)) {
490 page_size = hdr->page_size;
491 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700492 }
493
Shashank Mittalcd98d472011-08-02 14:29:24 -0700494 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700495 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800496 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700497 image_addr = (unsigned char *)target_get_scratch_address();
498 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
499 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Neeti Desai465491e2012-07-31 12:53:35 -0700500 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
501 dt_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
502 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual +
503 dt_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700504
505 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700506
507 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700508 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700509
Shashank Mittalcd98d472011-08-02 14:29:24 -0700510 /* Read image without signature */
511 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
512 {
513 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
514 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800515 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700516
517 offset = imagesize_actual;
518 /* Read signature */
519 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
520 {
521 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
522 }
523 else
524 {
525 auth_kernel_img = image_verify((unsigned char *)image_addr,
526 (unsigned char *)(image_addr + imagesize_actual),
527 imagesize_actual,
528 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700529
530 if(auth_kernel_img)
531 {
532 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700533 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700534 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700535 }
536
Neeti Desai465491e2012-07-31 12:53:35 -0700537 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700538 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
539 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700540
Neeti Desai465491e2012-07-31 12:53:35 -0700541 #if DEVICE_TREE
542 if(hdr->dt_size) {
543 table = (struct dt_table*) dt_buf;
544 dt_table_offset = (image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
545
546 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
547
548 /* Restriction that the device tree entry table should be less than a page*/
549 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
550
551 /* Validate the device tree table header */
552 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
553 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
554 return -1;
555 }
556
557 /* Find index of device tree within device tree table */
558 if((dt_entry_ptr = get_device_tree_ptr(table)) == NULL){
559 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
560 return -1;
561 }
562
563 /* Read device device tree in the "tags_add */
564 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
565 }
566 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700567 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700568 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700569 {
570 write_device_info_mmc(&device);
571 #ifdef TZ_TAMPER_FUSE
572 set_tamper_fuse_cmd();
573 #endif
574 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530575 #if USE_PCOM_SECBOOT
576 set_tamper_flag(device.is_tampered);
577 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700578 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700579 else
580 {
581 offset += page_size;
582
583 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
584 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
585 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
586 return -1;
587 }
588 offset += n;
589
590 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
591 if(n != 0)
592 {
593 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
594 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
595 return -1;
596 }
597 }
598 offset += n;
Neeti Desai465491e2012-07-31 12:53:35 -0700599
600 if(hdr->second_size != 0) {
601 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
602 offset += n;
603 }
604
605 #if DEVICE_TREE
606 if(hdr->dt_size != 0) {
607
608 /* Read the device tree table into buffer */
609 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
610 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
611 return -1;
612 }
613 table = (struct dt_table*) dt_buf;
614
615 /* Restriction that the device tree entry table should be less than a page*/
616 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
617
618 /* Validate the device tree table header */
619 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
620 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
621 return -1;
622 }
623
624 /* Calculate the offset of device tree within device tree table */
625 if((dt_entry_ptr = get_device_tree_ptr(table)) == NULL){
626 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
627 return -1;
628 }
629
630 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700631 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
632 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
633 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
634 return -1;
635 }
636 }
637 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700638 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700639
640unified_boot:
641 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
642 hdr->kernel_size);
643 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
644 hdr->ramdisk_size);
645
646 if(hdr->cmdline[0]) {
647 cmdline = (char*) hdr->cmdline;
648 } else {
649 cmdline = DEFAULT_CMDLINE;
650 }
651 dprintf(INFO, "cmdline = '%s'\n", cmdline);
652
653 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700654 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700655 (const char *)cmdline, board_machtype(),
656 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
657
658 return 0;
659}
660
Dima Zavin214cc642009-01-26 11:16:21 -0800661int boot_linux_from_flash(void)
662{
663 struct boot_img_hdr *hdr = (void*) buf;
664 unsigned n;
665 struct ptentry *ptn;
666 struct ptable *ptable;
667 unsigned offset = 0;
668 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800669
Shashank Mittalcd98d472011-08-02 14:29:24 -0700670 unsigned char *image_addr = 0;
671 unsigned kernel_actual;
672 unsigned ramdisk_actual;
673 unsigned imagesize_actual;
674
David Ng183a7422009-12-07 14:55:21 -0800675 if (target_is_emmc_boot()) {
676 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
677 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
678 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
679 return -1;
680 }
681 goto continue_boot;
682 }
683
Dima Zavin214cc642009-01-26 11:16:21 -0800684 ptable = flash_get_ptable();
685 if (ptable == NULL) {
686 dprintf(CRITICAL, "ERROR: Partition table not found\n");
687 return -1;
688 }
689
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800690 if(!boot_into_recovery)
691 {
692 ptn = ptable_find(ptable, "boot");
693 if (ptn == NULL) {
694 dprintf(CRITICAL, "ERROR: No boot partition found\n");
695 return -1;
696 }
697 }
698 else
699 {
700 ptn = ptable_find(ptable, "recovery");
701 if (ptn == NULL) {
702 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
703 return -1;
704 }
Dima Zavin214cc642009-01-26 11:16:21 -0800705 }
706
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800707 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800708 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
709 return -1;
710 }
Dima Zavin214cc642009-01-26 11:16:21 -0800711
712 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700713 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800714 return -1;
715 }
716
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800717 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700718 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 -0800719 return -1;
720 }
721
Shashank Mittalcd98d472011-08-02 14:29:24 -0700722 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700723 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700724 {
725 image_addr = (unsigned char *)target_get_scratch_address();
726 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
727 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
728 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800729
Shashank Mittalcd98d472011-08-02 14:29:24 -0700730 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700731
732 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700733 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700734
Shashank Mittalcd98d472011-08-02 14:29:24 -0700735 /* Read image without signature */
736 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
737 {
738 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
739 return -1;
740 }
Dima Zavin214cc642009-01-26 11:16:21 -0800741
Shashank Mittalcd98d472011-08-02 14:29:24 -0700742 offset = imagesize_actual;
743 /* Read signature */
744 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
745 {
746 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
747 }
748 else
749 {
750
751 /* Verify signature */
752 auth_kernel_img = image_verify((unsigned char *)image_addr,
753 (unsigned char *)(image_addr + imagesize_actual),
754 imagesize_actual,
755 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700756
757 if(auth_kernel_img)
758 {
759 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700760 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700761 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700762 }
763
764 /* Move kernel and ramdisk to correct address */
765 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
766 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700767
768 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700769 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700770 {
771 write_device_info_flash(&device);
772 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530773#if USE_PCOM_SECBOOT
774 set_tamper_flag(device.is_tampered);
775#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700776 }
777 else
778 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700779 offset = page_size;
780
Shashank Mittalcd98d472011-08-02 14:29:24 -0700781 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
782 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
783 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
784 return -1;
785 }
786 offset += n;
787
788 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
789 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
790 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
791 return -1;
792 }
793 offset += n;
794 }
David Ng183a7422009-12-07 14:55:21 -0800795continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800796 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
797 hdr->kernel_size);
798 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
799 hdr->ramdisk_size);
800
801 if(hdr->cmdline[0]) {
802 cmdline = (char*) hdr->cmdline;
803 } else {
804 cmdline = DEFAULT_CMDLINE;
805 }
806 dprintf(INFO, "cmdline = '%s'\n", cmdline);
807
808 /* TODO: create/pass atags to kernel */
809
810 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700811 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800812 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800813 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
814
815 return 0;
816}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800817
Shashank Mittal162244e2011-08-08 19:01:25 -0700818unsigned char info_buf[4096];
819void write_device_info_mmc(device_info *dev)
820{
821 struct device_info *info = (void*) info_buf;
822 unsigned long long ptn = 0;
823 unsigned long long size;
824 int index = INVALID_PTN;
825
826 index = partition_get_index("aboot");
827 ptn = partition_get_offset(index);
828 if(ptn == 0)
829 {
830 return;
831 }
832
833 size = partition_get_size(index);
834
835 memcpy(info, dev, sizeof(device_info));
836
837 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
838 {
839 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
840 return;
841 }
842}
843
844void read_device_info_mmc(device_info *dev)
845{
846 struct device_info *info = (void*) info_buf;
847 unsigned long long ptn = 0;
848 unsigned long long size;
849 int index = INVALID_PTN;
850
851 index = partition_get_index("aboot");
852 ptn = partition_get_offset(index);
853 if(ptn == 0)
854 {
855 return;
856 }
857
858 size = partition_get_size(index);
859
860 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
861 {
862 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
863 return;
864 }
865
866 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
867 {
868 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
869 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700870 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700871
872 write_device_info_mmc(info);
873 }
874 memcpy(dev, info, sizeof(device_info));
875}
876
877void write_device_info_flash(device_info *dev)
878{
879 struct device_info *info = (void *) info_buf;
880 struct ptentry *ptn;
881 struct ptable *ptable;
882
883 ptable = flash_get_ptable();
884 if (ptable == NULL)
885 {
886 dprintf(CRITICAL, "ERROR: Partition table not found\n");
887 return;
888 }
889
890 ptn = ptable_find(ptable, "devinfo");
891 if (ptn == NULL)
892 {
893 dprintf(CRITICAL, "ERROR: No boot partition found\n");
894 return;
895 }
896
897 memcpy(info, dev, sizeof(device_info));
898
899 if (flash_write(ptn, 0, (void *)info_buf, page_size))
900 {
901 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
902 return;
903 }
904}
905
906void read_device_info_flash(device_info *dev)
907{
908 struct device_info *info = (void*) info_buf;
909 struct ptentry *ptn;
910 struct ptable *ptable;
911
912 ptable = flash_get_ptable();
913 if (ptable == NULL)
914 {
915 dprintf(CRITICAL, "ERROR: Partition table not found\n");
916 return;
917 }
918
919 ptn = ptable_find(ptable, "devinfo");
920 if (ptn == NULL)
921 {
922 dprintf(CRITICAL, "ERROR: No boot partition found\n");
923 return;
924 }
925
926 if (flash_read(ptn, 0, (void *)info_buf, page_size))
927 {
928 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
929 return;
930 }
931
932 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
933 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700934 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
935 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700936 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700937 write_device_info_flash(info);
938 }
939 memcpy(dev, info, sizeof(device_info));
940}
941
942void write_device_info(device_info *dev)
943{
944 if(target_is_emmc_boot())
945 {
946 write_device_info_mmc(dev);
947 }
948 else
949 {
950 write_device_info_flash(dev);
951 }
952}
953
954void read_device_info(device_info *dev)
955{
956 if(target_is_emmc_boot())
957 {
958 read_device_info_mmc(dev);
959 }
960 else
961 {
962 read_device_info_flash(dev);
963 }
964}
965
966void reset_device_info()
967{
968 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700969 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700970 write_device_info(&device);
971}
972
973void set_device_root()
974{
975 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700976 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700977 write_device_info(&device);
978}
979
Amol Jadicb524072012-08-09 16:40:18 -0700980#if DEVICE_TREE
981int copy_dtb(uint8_t *boot_image_start)
982{
983 uint32 dt_image_offset = 0;
984 uint32_t n;
985 struct dt_table *table;
986 struct dt_entry *dt_entry_ptr;
987 unsigned dt_table_offset;
988
989 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
990
991
992 if(hdr->dt_size != 0) {
993
994 /* add kernel offset */
995 dt_image_offset += page_size;
996 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
997 dt_image_offset += n;
998
999 /* add ramdisk offset */
1000 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1001 dt_image_offset += n;
1002
1003 /* add second offset */
1004 if(hdr->second_size != 0) {
1005 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1006 dt_image_offset += n;
1007 }
1008
1009 /* offset now point to start of dt.img */
1010 table = boot_image_start + dt_image_offset;
1011
1012 /* Restriction that the device tree entry table should be less than a page*/
1013 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1014
1015 /* Validate the device tree table header */
1016 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1017 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1018 return -1;
1019 }
1020
1021 /* Calculate the offset of device tree within device tree table */
1022 if((dt_entry_ptr = get_device_tree_ptr(table)) == NULL){
1023 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1024 return -1;
1025 }
1026
1027 /* Read device device tree in the "tags_add */
1028 memmove((void*) hdr->tags_addr, boot_image_start + dt_image_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
1029 }
1030
1031 /* Everything looks fine. Return success. */
1032 return 0;
1033}
1034#endif
1035
Brian Swetland9c4c0752009-01-25 16:23:50 -08001036void cmd_boot(const char *arg, void *data, unsigned sz)
1037{
1038 unsigned kernel_actual;
1039 unsigned ramdisk_actual;
1040 static struct boot_img_hdr hdr;
1041 char *ptr = ((char*) data);
1042
1043 if (sz < sizeof(hdr)) {
1044 fastboot_fail("invalid bootimage header");
1045 return;
1046 }
1047
1048 memcpy(&hdr, data, sizeof(hdr));
1049
1050 /* ensure commandline is terminated */
1051 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
1052
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001053 if(target_is_emmc_boot() && hdr.page_size) {
1054 page_size = hdr.page_size;
1055 page_mask = page_size - 1;
1056 }
1057
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001058 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
1059 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001060
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001061 /* sz should have atleast raw boot image */
1062 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001063 fastboot_fail("incomplete bootimage");
1064 return;
1065 }
1066
Amol Jadicb524072012-08-09 16:40:18 -07001067#if DEVICE_TREE
1068 /* find correct dtb and copy it to right location */
1069 if(copy_dtb(data))
1070 {
1071 fastboot_fail("dtb not found");
1072 return;
1073 }
1074#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001075
1076 fastboot_okay("");
1077 udc_stop();
1078
Amol Jadicb524072012-08-09 16:40:18 -07001079 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
1080 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
1081
Amol Jadie67872e2011-06-27 14:14:11 -07001082 boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -08001083 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -07001084 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001085}
1086
Dima Zavin214cc642009-01-26 11:16:21 -08001087void cmd_erase(const char *arg, void *data, unsigned sz)
1088{
1089 struct ptentry *ptn;
1090 struct ptable *ptable;
1091
1092 ptable = flash_get_ptable();
1093 if (ptable == NULL) {
1094 fastboot_fail("partition table doesn't exist");
1095 return;
1096 }
1097
1098 ptn = ptable_find(ptable, arg);
1099 if (ptn == NULL) {
1100 fastboot_fail("unknown partition name");
1101 return;
1102 }
1103
1104 if (flash_erase(ptn)) {
1105 fastboot_fail("failed to erase partition");
1106 return;
1107 }
1108 fastboot_okay("");
1109}
1110
Bikas Gurungd48bd242010-09-04 19:54:32 -07001111
1112void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1113{
1114 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -08001115 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -07001116 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001117
Kinson Chikf1a43512011-07-14 11:28:39 -07001118 index = partition_get_index(arg);
1119 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001120
Kinson Chikf1a43512011-07-14 11:28:39 -07001121 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001122 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001123 return;
1124 }
neetidb4b24d62012-01-20 12:13:09 -08001125 /* Simple inefficient version of erase. Just writing
1126 0 in first block */
1127 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1128 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001129 return;
1130 }
1131 fastboot_okay("");
1132}
1133
1134
Ajay Dudani5c761132011-04-07 20:19:04 -07001135void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001136{
1137 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001138 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001139 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001140
Greg Grisco6e754772011-06-23 12:19:39 -07001141 if (!strcmp(arg, "partition"))
1142 {
1143 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001144 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001145 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001146 return;
1147 }
1148 }
Greg Grisco6e754772011-06-23 12:19:39 -07001149 else
1150 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001151 index = partition_get_index(arg);
1152 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001153 if(ptn == 0) {
1154 fastboot_fail("partition table doesn't exist");
1155 return;
1156 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001157
Greg Grisco6e754772011-06-23 12:19:39 -07001158 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1159 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1160 fastboot_fail("image is not a boot image");
1161 return;
1162 }
1163 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001164
Kinson Chikf1a43512011-07-14 11:28:39 -07001165 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001166 if (ROUND_TO_PAGE(sz,511) > size) {
1167 fastboot_fail("size too large");
1168 return;
1169 }
1170 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1171 fastboot_fail("flash write failure");
1172 return;
1173 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001174 }
1175 fastboot_okay("");
1176 return;
1177}
1178
Ajay Dudani5c761132011-04-07 20:19:04 -07001179void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1180{
1181 unsigned int chunk;
1182 unsigned int chunk_data_sz;
1183 sparse_header_t *sparse_header;
1184 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001185 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001186 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301187 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001188 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001189
Kinson Chikf1a43512011-07-14 11:28:39 -07001190 index = partition_get_index(arg);
1191 ptn = partition_get_offset(index);
1192 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001193 fastboot_fail("partition table doesn't exist");
1194 return;
1195 }
1196
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301197 size = partition_get_size(index);
1198 if (ROUND_TO_PAGE(sz,511) > size) {
1199 fastboot_fail("size too large");
1200 return;
1201 }
1202
Ajay Dudani5c761132011-04-07 20:19:04 -07001203 /* Read and skip over sparse image header */
1204 sparse_header = (sparse_header_t *) data;
1205 data += sparse_header->file_hdr_sz;
1206 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1207 {
1208 /* Skip the remaining bytes in a header that is longer than
1209 * we expected.
1210 */
1211 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1212 }
1213
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001214 dprintf (SPEW, "=== Sparse Image Header ===\n");
1215 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1216 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1217 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1218 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1219 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1220 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1221 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1222 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001223
1224 /* Start processing chunks */
1225 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1226 {
1227 /* Read and skip over chunk header */
1228 chunk_header = (chunk_header_t *) data;
1229 data += sizeof(chunk_header_t);
1230
1231 dprintf (SPEW, "=== Chunk Header ===\n");
1232 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1233 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1234 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1235
1236 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1237 {
1238 /* Skip the remaining bytes in a header that is longer than
1239 * we expected.
1240 */
1241 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1242 }
1243
1244 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1245 switch (chunk_header->chunk_type)
1246 {
1247 case CHUNK_TYPE_RAW:
1248 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1249 chunk_data_sz))
1250 {
1251 fastboot_fail("Bogus chunk size for chunk type Raw");
1252 return;
1253 }
1254
Ajay Dudaniab18f022011-05-12 14:39:22 -07001255 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1256 chunk_data_sz,
1257 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001258 {
1259 fastboot_fail("flash write failure");
1260 return;
1261 }
1262 total_blocks += chunk_header->chunk_sz;
1263 data += chunk_data_sz;
1264 break;
1265
1266 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001267 total_blocks += chunk_header->chunk_sz;
1268 break;
1269
Ajay Dudani5c761132011-04-07 20:19:04 -07001270 case CHUNK_TYPE_CRC:
1271 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1272 {
1273 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1274 return;
1275 }
1276 total_blocks += chunk_header->chunk_sz;
1277 data += chunk_data_sz;
1278 break;
1279
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001280 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001281 fastboot_fail("Unknown chunk type");
1282 return;
1283 }
1284 }
1285
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001286 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1287 total_blocks, sparse_header->total_blks);
1288
1289 if(total_blocks != sparse_header->total_blks)
1290 {
1291 fastboot_fail("sparse image write failure");
1292 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001293
1294 fastboot_okay("");
1295 return;
1296}
1297
1298void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1299{
1300 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001301 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1302 unsigned int *magic_number = (unsigned int *) data;
1303 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001304
Neeti Desai127b9e02012-03-20 16:11:23 -07001305 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1306 magic_number[1] == DECRYPT_MAGIC_1)
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001307 {
1308#ifdef SSD_ENABLE
Neeti Desai127b9e02012-03-20 16:11:23 -07001309 ret = decrypt_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001310#endif
Greg Griscod6250552011-06-29 14:40:23 -07001311 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001312 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1313 return;
1314 }
1315 }
Neeti Desai127b9e02012-03-20 16:11:23 -07001316 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1317 magic_number[1] == ENCRYPT_MAGIC_1)
1318 {
1319#ifdef SSD_ENABLE
1320 ret = encrypt_scm((uint32 **) &data, &sz);
1321#endif
1322 if (ret != 0) {
1323 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1324 return;
1325 }
1326 }
1327
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001328 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001329 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1330 cmd_flash_mmc_img(arg, data, sz);
1331 else
1332 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001333 return;
1334}
1335
Dima Zavin214cc642009-01-26 11:16:21 -08001336void cmd_flash(const char *arg, void *data, unsigned sz)
1337{
1338 struct ptentry *ptn;
1339 struct ptable *ptable;
1340 unsigned extra = 0;
1341
1342 ptable = flash_get_ptable();
1343 if (ptable == NULL) {
1344 fastboot_fail("partition table doesn't exist");
1345 return;
1346 }
1347
1348 ptn = ptable_find(ptable, arg);
1349 if (ptn == NULL) {
1350 fastboot_fail("unknown partition name");
1351 return;
1352 }
1353
1354 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1355 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1356 fastboot_fail("image is not a boot image");
1357 return;
1358 }
1359 }
1360
Amol Jadi5c61a952012-05-04 17:05:35 -07001361 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001362 || !strcmp(ptn->name, "userdata")
1363 || !strcmp(ptn->name, "persist")
1364 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301365 if (flash_ecc_bch_enabled())
1366 /* Spare data bytes for 8 bit ECC increased by 4 */
1367 extra = ((page_size >> 9) * 20);
1368 else
1369 extra = ((page_size >> 9) * 16);
1370 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001371 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001372
1373 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1374 if (flash_write(ptn, extra, data, sz)) {
1375 fastboot_fail("flash write failure");
1376 return;
1377 }
1378 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1379 fastboot_okay("");
1380}
1381
1382void cmd_continue(const char *arg, void *data, unsigned sz)
1383{
1384 fastboot_okay("");
1385 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001386 if (target_is_emmc_boot())
1387 {
1388 boot_linux_from_mmc();
1389 }
1390 else
1391 {
1392 boot_linux_from_flash();
1393 }
Dima Zavin214cc642009-01-26 11:16:21 -08001394}
1395
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001396void cmd_reboot(const char *arg, void *data, unsigned sz)
1397{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001398 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001399 fastboot_okay("");
1400 reboot_device(0);
1401}
1402
1403void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1404{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001405 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001406 fastboot_okay("");
1407 reboot_device(FASTBOOT_MODE);
1408}
1409
Shashank Mittal162244e2011-08-08 19:01:25 -07001410void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1411{
1412 if(!device.is_unlocked)
1413 {
1414 device.is_unlocked = 1;
1415 write_device_info(&device);
1416 }
1417 fastboot_okay("");
1418}
1419
Shashank Mittala0032282011-08-26 14:50:11 -07001420void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1421{
1422 char response[64];
1423 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1424 fastboot_info(response);
1425 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1426 fastboot_info(response);
1427 fastboot_okay("");
1428}
1429
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001430void splash_screen ()
1431{
1432 struct ptentry *ptn;
1433 struct ptable *ptable;
1434 struct fbcon_config *fb_display = NULL;
1435
1436 if (!target_is_emmc_boot())
1437 {
1438 ptable = flash_get_ptable();
1439 if (ptable == NULL) {
1440 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001441 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001442 }
1443
1444 ptn = ptable_find(ptable, "splash");
1445 if (ptn == NULL) {
1446 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1447 } else {
1448 fb_display = fbcon_display();
1449 if (fb_display) {
1450 if (flash_read(ptn, 0, fb_display->base,
1451 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1452 fbcon_clear();
1453 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1454 }
1455 }
1456 }
1457 }
1458}
1459
Brian Swetland9c4c0752009-01-25 16:23:50 -08001460void aboot_init(const struct app_descriptor *app)
1461{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001462 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001463 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001464 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001465
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001466 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001467 if (target_is_emmc_boot())
1468 {
1469 page_size = 2048;
1470 page_mask = page_size - 1;
1471 }
1472 else
1473 {
1474 page_size = flash_page_size();
1475 page_mask = page_size - 1;
1476 }
1477
Shashank Mittal162244e2011-08-08 19:01:25 -07001478 if(target_use_signed_kernel())
1479 {
1480 read_device_info(&device);
1481
Shashank Mittal162244e2011-08-08 19:01:25 -07001482 }
1483
Greg Griscod6250552011-06-29 14:40:23 -07001484 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001485 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001486 surf_udc_device.serialno = sn_buf;
1487
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001488 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001489 if (keys_get_state(KEY_HOME) != 0)
1490 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001491 if (keys_get_state(KEY_VOLUMEUP) != 0)
1492 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001493 if(!boot_into_recovery)
1494 {
1495 if (keys_get_state(KEY_BACK) != 0)
1496 goto fastboot;
1497 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1498 goto fastboot;
1499 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001500
1501 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001502 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001503 goto fastboot;
1504 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001505
Ajay Dudani77421292010-10-27 19:34:06 -07001506 reboot_mode = check_reboot_mode();
1507 if (reboot_mode == RECOVERY_MODE) {
1508 boot_into_recovery = 1;
1509 } else if(reboot_mode == FASTBOOT_MODE) {
1510 goto fastboot;
1511 }
1512
Shashank Mittal23b8f422010-04-16 19:27:21 -07001513 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001514 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001515 if(emmc_recovery_init())
1516 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001517 if(target_use_signed_kernel())
1518 {
1519 if((device.is_unlocked) || (device.is_tampered))
1520 {
1521 #ifdef TZ_TAMPER_FUSE
1522 set_tamper_fuse_cmd();
1523 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301524 #if USE_PCOM_SECBOOT
1525 set_tamper_flag(device.is_tampered);
1526 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001527 }
1528 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001529 boot_linux_from_mmc();
1530 }
1531 else
1532 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001533 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301534#if USE_PCOM_SECBOOT
1535 if((device.is_unlocked) || (device.is_tampered))
1536 set_tamper_flag(device.is_tampered);
1537#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001538 boot_linux_from_flash();
1539 }
Dima Zavinb4283602009-01-26 16:36:57 -08001540 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1541 "to fastboot mode.\n");
1542
1543fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001544
Shashank Mittal162244e2011-08-08 19:01:25 -07001545 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001546
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001547 if(!usb_init)
1548 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001549
1550 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001551
Shashank Mittal23b8f422010-04-16 19:27:21 -07001552 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001553 {
1554 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001555 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001556 }
1557 else
1558 {
1559 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001560 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001561 }
1562
1563 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001564 fastboot_register("reboot", cmd_reboot);
1565 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001566 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001567 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001568 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001569 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001570 fastboot_publish("serialno", sn_buf);
Kinson Chikf1a43512011-07-14 11:28:39 -07001571 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001572 sz = target_get_max_flash_size();
1573 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001574 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001575}
1576
1577APP_START(aboot)
1578 .init = aboot_init,
1579APP_END
1580
Neeti Desai17379b82012-06-04 18:42:53 -07001581#if DEVICE_TREE
Neeti Desai465491e2012-07-31 12:53:35 -07001582struct dt_entry * get_device_tree_ptr(struct dt_table *table)
1583{
1584 unsigned i;
1585 struct dt_entry *dt_entry_ptr;
1586
1587 dt_entry_ptr = (char *)table + DEV_TREE_HEADER_SIZE ;
1588
1589 for(i = 0; i < table->num_entries; i++)
1590 {
1591 if((dt_entry_ptr->platform_id == board_platform_id()) &&
1592 (dt_entry_ptr->variant_id == board_hardware_id()) &&
1593 (dt_entry_ptr->soc_rev == 0)){
1594 return dt_entry_ptr;
1595 }
1596 dt_entry_ptr++;
1597 }
1598 return NULL;
1599}
1600
Neeti Desai17379b82012-06-04 18:42:53 -07001601int update_device_tree(const void * fdt, char *cmdline,
1602 void *ramdisk, unsigned ramdisk_size)
1603{
1604 int ret = 0;
1605 int offset;
1606 uint32_t *memory_reg;
1607 unsigned char *final_cmdline;
1608 uint32_t len;
1609
1610 /* Check the device tree header */
1611 ret = fdt_check_header(fdt);
1612 if(ret)
1613 {
1614 dprintf(CRITICAL, "Invalid device tree header \n");
1615 return ret;
1616 }
1617
1618 /* Get offset of the memory node */
1619 offset = fdt_path_offset(fdt,"/memory");
1620
1621 memory_reg = target_dev_tree_mem(&len);
1622
1623 /* Adding the memory values to the reg property */
1624 ret = fdt_setprop(fdt, offset, "reg", memory_reg, sizeof(uint32_t) * len * 2);
1625 if(ret)
1626 {
1627 dprintf(CRITICAL, "ERROR: Cannot update memory node\n");
1628 return ret;
1629 }
1630
1631 /* Get offset of the chosen node */
1632 offset = fdt_path_offset(fdt, "/chosen");
1633
1634 /* Adding the cmdline to the chosen node */
1635 final_cmdline = update_cmdline(cmdline);
1636 ret = fdt_setprop_string(fdt, offset, "bootargs", final_cmdline);
1637 if(ret)
1638 {
1639 dprintf(CRITICAL, "ERROR: Cannot update chosen node [bootargs]\n");
1640 return ret;
1641 }
1642
1643 /* Adding the initrd-start to the chosen node */
1644 ret = fdt_setprop_cell(fdt, offset, "linux,initrd-start", ramdisk);
1645 if(ret)
1646 {
1647 dprintf(CRITICAL, "ERROR: Cannot update chosen node [linux,initrd-start]\n");
1648 return ret;
1649 }
1650
1651 /* Adding the initrd-end to the chosen node */
1652 ret = fdt_setprop_cell(fdt, offset, "linux,initrd-end", (ramdisk + ramdisk_size));
1653 if(ret)
1654 {
1655 dprintf(CRITICAL, "ERROR: Cannot update chosen node [linux,initrd-end]\n");
1656 return ret;
1657 }
1658
1659 fdt_pack(fdt);
1660
1661 return ret;
1662}
1663#endif