blob: f6d6e509df7f72be79939ee8aeed88ba5c5cb1d2 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07005 * Copyright (c) 2009-2012, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
36#include <dev/udc.h>
37#include <string.h>
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>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070051#include <malloc.h>
Dima Zavin214cc642009-01-26 11:16:21 -080052
Neeti Desai17379b82012-06-04 18:42:53 -070053#if DEVICE_TREE
54#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070055#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070056#endif
57
Shashank Mittalcd98d472011-08-02 14:29:24 -070058#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080059#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080060#include "bootimg.h"
61#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070062#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070063#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070064#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070065#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080066
Shashank Mittal162244e2011-08-08 19:01:25 -070067#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070068
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070069extern bool target_use_signed_kernel(void);
70extern void dsb();
71extern void isb();
72extern void platform_uninit(void);
73
74void write_device_info_mmc(device_info *dev);
75void write_device_info_flash(device_info *dev);
76
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070077#define EXPAND(NAME) #NAME
78#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080079#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070080
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080081#ifdef MEMBASE
82#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
83#else
David Ng183a7422009-12-07 14:55:21 -080084#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080085#endif
86
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080087#define RECOVERY_MODE 0x77665502
88#define FASTBOOT_MODE 0x77665500
89
David Ng183a7422009-12-07 14:55:21 -080090static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080091static const char *usb_sn_cmdline = " androidboot.serialno=";
Ajay Dudanica3a33c2011-11-18 08:31:40 -080092static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -070093static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -080094
Ajay Dudani6cff85e2011-02-04 16:02:16 -080095static const char *baseband_apq = " androidboot.baseband=apq";
96static const char *baseband_msm = " androidboot.baseband=msm";
97static const char *baseband_csfb = " androidboot.baseband=csfb";
98static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -070099static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700100static const char *baseband_sglte = " androidboot.baseband=sglte";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800101
Shashank Mittalcd98d472011-08-02 14:29:24 -0700102/* Assuming unauthorized kernel image by default */
103static int auth_kernel_img = 0;
104
Shashank Mittal162244e2011-08-08 19:01:25 -0700105static device_info device = {DEVICE_MAGIC, 0, 0};
106
Brian Swetland9c4c0752009-01-25 16:23:50 -0800107static struct udc_device surf_udc_device = {
108 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800109 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800110 .version_id = 0x0100,
111 .manufacturer = "Google",
112 .product = "Android",
113};
114
Dima Zavin42168f22009-01-30 11:52:22 -0800115struct atag_ptbl_entry
116{
117 char name[16];
118 unsigned offset;
119 unsigned size;
120 unsigned flags;
121};
122
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800123char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700124
Greg Griscod2471ef2011-07-14 13:00:42 -0700125extern int emmc_recovery_init(void);
126
Kinson Chik0b1c8162011-08-31 16:31:57 -0700127#if NO_KEYPAD_DRIVER
128extern int fastboot_trigger(void);
129#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700130
Dima Zavin42168f22009-01-30 11:52:22 -0800131static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
132{
133 struct atag_ptbl_entry atag_ptn;
134
135 memcpy(atag_ptn.name, ptn->name, 16);
136 atag_ptn.name[15] = '\0';
137 atag_ptn.offset = ptn->start;
138 atag_ptn.size = ptn->length;
139 atag_ptn.flags = ptn->flags;
140 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
141 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
142}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800143
Neeti Desaie245d492012-06-01 12:52:13 -0700144unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800145{
David Ng183a7422009-12-07 14:55:21 -0800146 int cmdline_len = 0;
147 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800148 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700149 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800150
Brian Swetland9c4c0752009-01-25 16:23:50 -0800151 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800152 cmdline_len = strlen(cmdline);
153 have_cmdline = 1;
154 }
155 if (target_is_emmc_boot()) {
156 cmdline_len += strlen(emmc_cmdline);
157 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800158
159 cmdline_len += strlen(usb_sn_cmdline);
160 cmdline_len += strlen(sn_buf);
161
David Ngf773dde2010-07-26 19:55:08 -0700162 if (target_pause_for_battery_charge()) {
163 pause_at_bootup = 1;
164 cmdline_len += strlen(battchg_pause);
165 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800166
Shashank Mittalcd98d472011-08-02 14:29:24 -0700167 if(target_use_signed_kernel() && auth_kernel_img) {
168 cmdline_len += strlen(auth_kernel);
169 }
170
Ajay Dudanid04110c2011-01-17 23:55:07 -0800171 /* Determine correct androidboot.baseband to use */
172 switch(target_baseband())
173 {
174 case BASEBAND_APQ:
175 cmdline_len += strlen(baseband_apq);
176 break;
177
178 case BASEBAND_MSM:
179 cmdline_len += strlen(baseband_msm);
180 break;
181
182 case BASEBAND_CSFB:
183 cmdline_len += strlen(baseband_csfb);
184 break;
185
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800186 case BASEBAND_SVLTE2A:
187 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800188 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700189
190 case BASEBAND_MDM:
191 cmdline_len += strlen(baseband_mdm);
192 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700193
194 case BASEBAND_SGLTE:
195 cmdline_len += strlen(baseband_sglte);
196 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800197 }
198
David Ng183a7422009-12-07 14:55:21 -0800199 if (cmdline_len > 0) {
200 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700201 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
202 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700203
Amol Jadi168b7712012-03-06 16:15:00 -0800204 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700205 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800206 if (have_cmdline) {
207 src = cmdline;
208 while ((*dst++ = *src++));
209 }
210 if (target_is_emmc_boot()) {
211 src = emmc_cmdline;
212 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700213 have_cmdline = 1;
214 while ((*dst++ = *src++));
215 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800216
217 src = usb_sn_cmdline;
218 if (have_cmdline) --dst;
219 have_cmdline = 1;
220 while ((*dst++ = *src++));
221 src = sn_buf;
222 if (have_cmdline) --dst;
223 have_cmdline = 1;
224 while ((*dst++ = *src++));
225
David Ngf773dde2010-07-26 19:55:08 -0700226 if (pause_at_bootup) {
227 src = battchg_pause;
228 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800229 while ((*dst++ = *src++));
230 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800231
Shashank Mittalcd98d472011-08-02 14:29:24 -0700232 if(target_use_signed_kernel() && auth_kernel_img) {
233 src = auth_kernel;
234 if (have_cmdline) --dst;
235 while ((*dst++ = *src++));
236 }
237
Ajay Dudanid04110c2011-01-17 23:55:07 -0800238 switch(target_baseband())
239 {
240 case BASEBAND_APQ:
241 src = baseband_apq;
242 if (have_cmdline) --dst;
243 while ((*dst++ = *src++));
244 break;
245
246 case BASEBAND_MSM:
247 src = baseband_msm;
248 if (have_cmdline) --dst;
249 while ((*dst++ = *src++));
250 break;
251
252 case BASEBAND_CSFB:
253 src = baseband_csfb;
254 if (have_cmdline) --dst;
255 while ((*dst++ = *src++));
256 break;
257
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800258 case BASEBAND_SVLTE2A:
259 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800260 if (have_cmdline) --dst;
261 while ((*dst++ = *src++));
262 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700263
264 case BASEBAND_MDM:
265 src = baseband_mdm;
266 if (have_cmdline) --dst;
267 while ((*dst++ = *src++));
268 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700269
270 case BASEBAND_SGLTE:
271 src = baseband_sglte;
272 if (have_cmdline) --dst;
273 while ((*dst++ = *src++));
274 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800275 }
Neeti Desaie245d492012-06-01 12:52:13 -0700276 }
277 return cmdline_final;
278}
279
280unsigned *atag_core(unsigned *ptr)
281{
282 /* CORE */
283 *ptr++ = 2;
284 *ptr++ = 0x54410001;
285
286 return ptr;
287
288}
289
290unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
291 unsigned ramdisk_size)
292{
293 if (ramdisk_size) {
294 *ptr++ = 4;
295 *ptr++ = 0x54420005;
296 *ptr++ = (unsigned)ramdisk;
297 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800298 }
299
Neeti Desaie245d492012-06-01 12:52:13 -0700300 return ptr;
301}
302
303unsigned *atag_ptable(unsigned **ptr_addr)
304{
305 int i;
306 struct ptable *ptable;
307
308 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700309 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
310 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700311 *(*ptr_addr)++ = 0x4d534d70;
312 for (i = 0; i < ptable->count; ++i)
313 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
314 }
315
316 return (*ptr_addr);
317}
318
319unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
320{
321 int cmdline_length = 0;
322 int n;
323 unsigned char *cmdline_final = NULL;
324 char *dest;
325
326 cmdline_final = update_cmdline(cmdline);
327 if (cmdline_final){
328 dprintf(INFO, "cmdline: %s\n", cmdline_final);
329 }
330
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700331 cmdline_length =strlen((const char*)cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700332 n = (cmdline_length + 4) & (~3);
333
334 *ptr++ = (n / 4) + 2;
335 *ptr++ = 0x54410009;
336 dest = (char *) ptr;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700337 while ((*dest++ = *cmdline_final++));
Neeti Desaie245d492012-06-01 12:52:13 -0700338 ptr += (n / 4);
339
340 return ptr;
341}
342
343unsigned *atag_end(unsigned *ptr)
344{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800345 /* END */
346 *ptr++ = 0;
347 *ptr++ = 0;
348
Neeti Desaie245d492012-06-01 12:52:13 -0700349 return ptr;
350}
351
352void generate_atags(unsigned *ptr, const char *cmdline,
353 void *ramdisk, unsigned ramdisk_size)
354{
355
356 ptr = atag_core(ptr);
357 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
358 ptr = target_atag_mem(ptr);
359
360 /* Skip NAND partition ATAGS for eMMC boot */
361 if (!target_is_emmc_boot()){
362 ptr = atag_ptable(&ptr);
363 }
364
365 ptr = atag_cmdline(ptr, cmdline);
366 ptr = atag_end(ptr);
367}
368
369void boot_linux(void *kernel, unsigned *tags,
370 const char *cmdline, unsigned machtype,
371 void *ramdisk, unsigned ramdisk_size)
372{
Neeti Desai17379b82012-06-04 18:42:53 -0700373 int ret = 0;
Neeti Desaie245d492012-06-01 12:52:13 -0700374 void (*entry)(unsigned, unsigned, unsigned*) = kernel;
375
Neeti Desai17379b82012-06-04 18:42:53 -0700376#if DEVICE_TREE
377 /* Update the Device Tree */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700378 ret = update_device_tree((void *)tags, cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700379 if(ret)
380 {
381 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
382 ASSERT(0);
383 }
384#else
Neeti Desaie245d492012-06-01 12:52:13 -0700385 /* Generating the Atags */
386 generate_atags(tags, cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700387#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700388
Brian Swetland9c4c0752009-01-25 16:23:50 -0800389 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
390 kernel, ramdisk, ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800391
392 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700393
Amol Jadi4421e652011-06-16 15:00:48 -0700394 /* do any platform specific cleanup before kernel entry */
395 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700396
Brian Swetland9c4c0752009-01-25 16:23:50 -0800397 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700398
Amol Jadi504f9fe2012-08-16 13:56:48 -0700399#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800400 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700401#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800402 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800403}
404
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800405unsigned page_size = 0;
406unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800407
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800408#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800409
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800410static unsigned char buf[4096]; //Equal to max-supported pagesize
Neeti Desai465491e2012-07-31 12:53:35 -0700411static unsigned char dt_buf[4096];
Dima Zavin214cc642009-01-26 11:16:21 -0800412
Shashank Mittal23b8f422010-04-16 19:27:21 -0700413int boot_linux_from_mmc(void)
414{
415 struct boot_img_hdr *hdr = (void*) buf;
416 struct boot_img_hdr *uhdr;
417 unsigned offset = 0;
418 unsigned long long ptn = 0;
419 unsigned n = 0;
420 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700421 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700422
Shashank Mittalcd98d472011-08-02 14:29:24 -0700423 unsigned char *image_addr = 0;
424 unsigned kernel_actual;
425 unsigned ramdisk_actual;
426 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700427 unsigned second_actual = 0;
428 unsigned dt_actual = 0;
429
430#if DEVICE_TREE
431 struct dt_table *table;
432 struct dt_entry *dt_entry_ptr;
433 unsigned dt_table_offset;
434#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700435
Shashank Mittal23b8f422010-04-16 19:27:21 -0700436 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
437 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
438 dprintf(INFO, "Unified boot method!\n");
439 hdr = uhdr;
440 goto unified_boot;
441 }
Greg Griscod6250552011-06-29 14:40:23 -0700442 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700443 index = partition_get_index("boot");
444 ptn = partition_get_offset(index);
445 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700446 dprintf(CRITICAL, "ERROR: No boot partition found\n");
447 return -1;
448 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700449 }
450 else {
451 index = partition_get_index("recovery");
452 ptn = partition_get_offset(index);
453 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700454 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
455 return -1;
456 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700457 }
458
Greg Griscod6250552011-06-29 14:40:23 -0700459 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700460 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
461 return -1;
462 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700463
464 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700465 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700466 return -1;
467 }
468
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700469 if (hdr->page_size && (hdr->page_size != page_size)) {
470 page_size = hdr->page_size;
471 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700472 }
473
Shashank Mittalcd98d472011-08-02 14:29:24 -0700474 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700475 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800476 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700477 image_addr = (unsigned char *)target_get_scratch_address();
478 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
479 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530480 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700481
482 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700483
484 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700485 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700486
Shashank Mittalcd98d472011-08-02 14:29:24 -0700487 /* Read image without signature */
488 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
489 {
490 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
491 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800492 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700493
494 offset = imagesize_actual;
495 /* Read signature */
496 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
497 {
498 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
499 }
500 else
501 {
502 auth_kernel_img = image_verify((unsigned char *)image_addr,
503 (unsigned char *)(image_addr + imagesize_actual),
504 imagesize_actual,
505 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700506
507 if(auth_kernel_img)
508 {
509 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700510 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700511 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700512 }
513
Neeti Desai465491e2012-07-31 12:53:35 -0700514 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700515 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
516 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700517
Neeti Desai465491e2012-07-31 12:53:35 -0700518 #if DEVICE_TREE
519 if(hdr->dt_size) {
520 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700521 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700522
523 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
524
525 /* Restriction that the device tree entry table should be less than a page*/
526 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
527
528 /* Validate the device tree table header */
529 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
530 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
531 return -1;
532 }
533
534 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700535 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700536 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
537 return -1;
538 }
539
540 /* Read device device tree in the "tags_add */
541 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
542 }
543 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700544 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700545 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700546 {
547 write_device_info_mmc(&device);
548 #ifdef TZ_TAMPER_FUSE
549 set_tamper_fuse_cmd();
550 #endif
551 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530552 #if USE_PCOM_SECBOOT
553 set_tamper_flag(device.is_tampered);
554 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700555 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700556 else
557 {
558 offset += page_size;
559
560 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
561 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
562 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
563 return -1;
564 }
565 offset += n;
566
567 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
568 if(n != 0)
569 {
570 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
571 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
572 return -1;
573 }
574 }
575 offset += n;
Neeti Desai465491e2012-07-31 12:53:35 -0700576
577 if(hdr->second_size != 0) {
578 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
579 offset += n;
580 }
581
582 #if DEVICE_TREE
583 if(hdr->dt_size != 0) {
584
585 /* Read the device tree table into buffer */
586 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
587 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
588 return -1;
589 }
590 table = (struct dt_table*) dt_buf;
591
592 /* Restriction that the device tree entry table should be less than a page*/
593 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
594
595 /* Validate the device tree table header */
596 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
597 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
598 return -1;
599 }
600
601 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700602 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700603 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
604 return -1;
605 }
606
607 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700608 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
609 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
610 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
611 return -1;
612 }
613 }
614 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700615 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700616
617unified_boot:
618 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
619 hdr->kernel_size);
620 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
621 hdr->ramdisk_size);
622
623 if(hdr->cmdline[0]) {
624 cmdline = (char*) hdr->cmdline;
625 } else {
626 cmdline = DEFAULT_CMDLINE;
627 }
628 dprintf(INFO, "cmdline = '%s'\n", cmdline);
629
630 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700631 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700632 (const char *)cmdline, board_machtype(),
633 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
634
635 return 0;
636}
637
Dima Zavin214cc642009-01-26 11:16:21 -0800638int boot_linux_from_flash(void)
639{
640 struct boot_img_hdr *hdr = (void*) buf;
641 unsigned n;
642 struct ptentry *ptn;
643 struct ptable *ptable;
644 unsigned offset = 0;
645 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800646
Shashank Mittalcd98d472011-08-02 14:29:24 -0700647 unsigned char *image_addr = 0;
648 unsigned kernel_actual;
649 unsigned ramdisk_actual;
650 unsigned imagesize_actual;
651
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700652#if DEVICE_TREE
653 struct dt_table *table;
654 struct dt_entry *dt_entry_ptr;
655#endif
656
657
David Ng183a7422009-12-07 14:55:21 -0800658 if (target_is_emmc_boot()) {
659 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
660 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
661 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
662 return -1;
663 }
664 goto continue_boot;
665 }
666
Dima Zavin214cc642009-01-26 11:16:21 -0800667 ptable = flash_get_ptable();
668 if (ptable == NULL) {
669 dprintf(CRITICAL, "ERROR: Partition table not found\n");
670 return -1;
671 }
672
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800673 if(!boot_into_recovery)
674 {
675 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700676
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800677 if (ptn == NULL) {
678 dprintf(CRITICAL, "ERROR: No boot partition found\n");
679 return -1;
680 }
681 }
682 else
683 {
684 ptn = ptable_find(ptable, "recovery");
685 if (ptn == NULL) {
686 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
687 return -1;
688 }
Dima Zavin214cc642009-01-26 11:16:21 -0800689 }
690
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800691 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800692 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
693 return -1;
694 }
Dima Zavin214cc642009-01-26 11:16:21 -0800695
696 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700697 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800698 return -1;
699 }
700
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800701 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700702 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 -0800703 return -1;
704 }
705
Shashank Mittalcd98d472011-08-02 14:29:24 -0700706 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700707 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700708 {
709 image_addr = (unsigned char *)target_get_scratch_address();
710 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
711 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
712 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800713
Shashank Mittalcd98d472011-08-02 14:29:24 -0700714 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700715
716 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700717 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700718
Shashank Mittalcd98d472011-08-02 14:29:24 -0700719 /* Read image without signature */
720 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
721 {
722 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
723 return -1;
724 }
Dima Zavin214cc642009-01-26 11:16:21 -0800725
Shashank Mittalcd98d472011-08-02 14:29:24 -0700726 offset = imagesize_actual;
727 /* Read signature */
728 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
729 {
730 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
731 }
732 else
733 {
734
735 /* Verify signature */
736 auth_kernel_img = image_verify((unsigned char *)image_addr,
737 (unsigned char *)(image_addr + imagesize_actual),
738 imagesize_actual,
739 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700740
741 if(auth_kernel_img)
742 {
743 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700744 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700745 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700746 }
747
748 /* Move kernel and ramdisk to correct address */
749 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
750 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700751
752 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700753 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700754 {
755 write_device_info_flash(&device);
756 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530757#if USE_PCOM_SECBOOT
758 set_tamper_flag(device.is_tampered);
759#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700760 }
761 else
762 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700763 offset = page_size;
764
Shashank Mittalcd98d472011-08-02 14:29:24 -0700765 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
766 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
767 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
768 return -1;
769 }
770 offset += n;
771
772 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
773 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
774 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
775 return -1;
776 }
777 offset += n;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700778
779 if(hdr->second_size != 0) {
780 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
781 offset += n;
782 }
783
784#if DEVICE_TREE
785 if(hdr->dt_size != 0) {
786
787 /* Read the device tree table into buffer */
788 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
789 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
790 return -1;
791 }
792
793 table = (struct dt_table*) dt_buf;
794
795 /* Restriction that the device tree entry table should be less than a page*/
796 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
797
798 /* Validate the device tree table header */
799 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
800 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
801 return -1;
802 }
803
804 /* Calculate the offset of device tree within device tree table */
805 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
806 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
807 return -1;
808 }
809
810 /* Read device device tree in the "tags_add */
811 if(flash_read(ptn, offset + dt_entry_ptr->offset,
812 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
813 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
814 return -1;
815 }
816 }
817#endif
818
Shashank Mittalcd98d472011-08-02 14:29:24 -0700819 }
David Ng183a7422009-12-07 14:55:21 -0800820continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800821 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
822 hdr->kernel_size);
823 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
824 hdr->ramdisk_size);
825
826 if(hdr->cmdline[0]) {
827 cmdline = (char*) hdr->cmdline;
828 } else {
829 cmdline = DEFAULT_CMDLINE;
830 }
831 dprintf(INFO, "cmdline = '%s'\n", cmdline);
832
833 /* TODO: create/pass atags to kernel */
834
835 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700836 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800837 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800838 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
839
840 return 0;
841}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800842
Shashank Mittal162244e2011-08-08 19:01:25 -0700843unsigned char info_buf[4096];
844void write_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 memcpy(info, dev, sizeof(device_info));
861
862 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
863 {
864 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
865 return;
866 }
867}
868
869void read_device_info_mmc(device_info *dev)
870{
871 struct device_info *info = (void*) info_buf;
872 unsigned long long ptn = 0;
873 unsigned long long size;
874 int index = INVALID_PTN;
875
876 index = partition_get_index("aboot");
877 ptn = partition_get_offset(index);
878 if(ptn == 0)
879 {
880 return;
881 }
882
883 size = partition_get_size(index);
884
885 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
886 {
887 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
888 return;
889 }
890
891 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
892 {
893 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
894 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700895 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700896
897 write_device_info_mmc(info);
898 }
899 memcpy(dev, info, sizeof(device_info));
900}
901
902void write_device_info_flash(device_info *dev)
903{
904 struct device_info *info = (void *) info_buf;
905 struct ptentry *ptn;
906 struct ptable *ptable;
907
908 ptable = flash_get_ptable();
909 if (ptable == NULL)
910 {
911 dprintf(CRITICAL, "ERROR: Partition table not found\n");
912 return;
913 }
914
915 ptn = ptable_find(ptable, "devinfo");
916 if (ptn == NULL)
917 {
918 dprintf(CRITICAL, "ERROR: No boot partition found\n");
919 return;
920 }
921
922 memcpy(info, dev, sizeof(device_info));
923
924 if (flash_write(ptn, 0, (void *)info_buf, page_size))
925 {
926 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
927 return;
928 }
929}
930
931void read_device_info_flash(device_info *dev)
932{
933 struct device_info *info = (void*) info_buf;
934 struct ptentry *ptn;
935 struct ptable *ptable;
936
937 ptable = flash_get_ptable();
938 if (ptable == NULL)
939 {
940 dprintf(CRITICAL, "ERROR: Partition table not found\n");
941 return;
942 }
943
944 ptn = ptable_find(ptable, "devinfo");
945 if (ptn == NULL)
946 {
947 dprintf(CRITICAL, "ERROR: No boot partition found\n");
948 return;
949 }
950
951 if (flash_read(ptn, 0, (void *)info_buf, page_size))
952 {
953 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
954 return;
955 }
956
957 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
958 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700959 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
960 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700961 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700962 write_device_info_flash(info);
963 }
964 memcpy(dev, info, sizeof(device_info));
965}
966
967void write_device_info(device_info *dev)
968{
969 if(target_is_emmc_boot())
970 {
971 write_device_info_mmc(dev);
972 }
973 else
974 {
975 write_device_info_flash(dev);
976 }
977}
978
979void read_device_info(device_info *dev)
980{
981 if(target_is_emmc_boot())
982 {
983 read_device_info_mmc(dev);
984 }
985 else
986 {
987 read_device_info_flash(dev);
988 }
989}
990
991void reset_device_info()
992{
993 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -0700994 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700995 write_device_info(&device);
996}
997
998void set_device_root()
999{
1000 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001001 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001002 write_device_info(&device);
1003}
1004
Amol Jadicb524072012-08-09 16:40:18 -07001005#if DEVICE_TREE
1006int copy_dtb(uint8_t *boot_image_start)
1007{
1008 uint32 dt_image_offset = 0;
1009 uint32_t n;
1010 struct dt_table *table;
1011 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001012
1013 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1014
Amol Jadicb524072012-08-09 16:40:18 -07001015 if(hdr->dt_size != 0) {
1016
1017 /* add kernel offset */
1018 dt_image_offset += page_size;
1019 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1020 dt_image_offset += n;
1021
1022 /* add ramdisk offset */
1023 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1024 dt_image_offset += n;
1025
1026 /* add second offset */
1027 if(hdr->second_size != 0) {
1028 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1029 dt_image_offset += n;
1030 }
1031
1032 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001033 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001034
1035 /* Restriction that the device tree entry table should be less than a page*/
1036 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1037
1038 /* Validate the device tree table header */
1039 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1040 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1041 return -1;
1042 }
1043
1044 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001045 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001046 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1047 return -1;
1048 }
1049
1050 /* Read device device tree in the "tags_add */
1051 memmove((void*) hdr->tags_addr, boot_image_start + dt_image_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
1052 }
1053
1054 /* Everything looks fine. Return success. */
1055 return 0;
1056}
1057#endif
1058
Brian Swetland9c4c0752009-01-25 16:23:50 -08001059void cmd_boot(const char *arg, void *data, unsigned sz)
1060{
1061 unsigned kernel_actual;
1062 unsigned ramdisk_actual;
1063 static struct boot_img_hdr hdr;
1064 char *ptr = ((char*) data);
1065
1066 if (sz < sizeof(hdr)) {
1067 fastboot_fail("invalid bootimage header");
1068 return;
1069 }
1070
1071 memcpy(&hdr, data, sizeof(hdr));
1072
1073 /* ensure commandline is terminated */
1074 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
1075
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001076 if(target_is_emmc_boot() && hdr.page_size) {
1077 page_size = hdr.page_size;
1078 page_mask = page_size - 1;
1079 }
1080
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001081 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
1082 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001083
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001084 /* sz should have atleast raw boot image */
1085 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001086 fastboot_fail("incomplete bootimage");
1087 return;
1088 }
1089
Amol Jadicb524072012-08-09 16:40:18 -07001090#if DEVICE_TREE
1091 /* find correct dtb and copy it to right location */
1092 if(copy_dtb(data))
1093 {
1094 fastboot_fail("dtb not found");
1095 return;
1096 }
1097#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001098
1099 fastboot_okay("");
1100 udc_stop();
1101
Amol Jadicb524072012-08-09 16:40:18 -07001102 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001103 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
Amol Jadicb524072012-08-09 16:40:18 -07001104
Amol Jadie67872e2011-06-27 14:14:11 -07001105 boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -08001106 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -07001107 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001108}
1109
Dima Zavin214cc642009-01-26 11:16:21 -08001110void cmd_erase(const char *arg, void *data, unsigned sz)
1111{
1112 struct ptentry *ptn;
1113 struct ptable *ptable;
1114
1115 ptable = flash_get_ptable();
1116 if (ptable == NULL) {
1117 fastboot_fail("partition table doesn't exist");
1118 return;
1119 }
1120
1121 ptn = ptable_find(ptable, arg);
1122 if (ptn == NULL) {
1123 fastboot_fail("unknown partition name");
1124 return;
1125 }
1126
1127 if (flash_erase(ptn)) {
1128 fastboot_fail("failed to erase partition");
1129 return;
1130 }
1131 fastboot_okay("");
1132}
1133
Bikas Gurungd48bd242010-09-04 19:54:32 -07001134
1135void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1136{
1137 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -08001138 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -07001139 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001140
Kinson Chikf1a43512011-07-14 11:28:39 -07001141 index = partition_get_index(arg);
1142 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001143
Kinson Chikf1a43512011-07-14 11:28:39 -07001144 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001145 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001146 return;
1147 }
neetidb4b24d62012-01-20 12:13:09 -08001148 /* Simple inefficient version of erase. Just writing
1149 0 in first block */
1150 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1151 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001152 return;
1153 }
1154 fastboot_okay("");
1155}
1156
1157
Ajay Dudani5c761132011-04-07 20:19:04 -07001158void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001159{
1160 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001161 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001162 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001163
Greg Grisco6e754772011-06-23 12:19:39 -07001164 if (!strcmp(arg, "partition"))
1165 {
1166 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001167 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001168 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001169 return;
1170 }
1171 }
Greg Grisco6e754772011-06-23 12:19:39 -07001172 else
1173 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001174 index = partition_get_index(arg);
1175 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001176 if(ptn == 0) {
1177 fastboot_fail("partition table doesn't exist");
1178 return;
1179 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001180
Greg Grisco6e754772011-06-23 12:19:39 -07001181 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1182 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1183 fastboot_fail("image is not a boot image");
1184 return;
1185 }
1186 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001187
Kinson Chikf1a43512011-07-14 11:28:39 -07001188 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001189 if (ROUND_TO_PAGE(sz,511) > size) {
1190 fastboot_fail("size too large");
1191 return;
1192 }
1193 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1194 fastboot_fail("flash write failure");
1195 return;
1196 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001197 }
1198 fastboot_okay("");
1199 return;
1200}
1201
Ajay Dudani5c761132011-04-07 20:19:04 -07001202void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1203{
1204 unsigned int chunk;
1205 unsigned int chunk_data_sz;
1206 sparse_header_t *sparse_header;
1207 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001208 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001209 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301210 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001211 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001212
Kinson Chikf1a43512011-07-14 11:28:39 -07001213 index = partition_get_index(arg);
1214 ptn = partition_get_offset(index);
1215 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001216 fastboot_fail("partition table doesn't exist");
1217 return;
1218 }
1219
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301220 size = partition_get_size(index);
1221 if (ROUND_TO_PAGE(sz,511) > size) {
1222 fastboot_fail("size too large");
1223 return;
1224 }
1225
Ajay Dudani5c761132011-04-07 20:19:04 -07001226 /* Read and skip over sparse image header */
1227 sparse_header = (sparse_header_t *) data;
1228 data += sparse_header->file_hdr_sz;
1229 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1230 {
1231 /* Skip the remaining bytes in a header that is longer than
1232 * we expected.
1233 */
1234 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1235 }
1236
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001237 dprintf (SPEW, "=== Sparse Image Header ===\n");
1238 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1239 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1240 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1241 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1242 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1243 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1244 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1245 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001246
1247 /* Start processing chunks */
1248 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1249 {
1250 /* Read and skip over chunk header */
1251 chunk_header = (chunk_header_t *) data;
1252 data += sizeof(chunk_header_t);
1253
1254 dprintf (SPEW, "=== Chunk Header ===\n");
1255 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1256 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1257 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1258
1259 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1260 {
1261 /* Skip the remaining bytes in a header that is longer than
1262 * we expected.
1263 */
1264 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1265 }
1266
1267 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1268 switch (chunk_header->chunk_type)
1269 {
1270 case CHUNK_TYPE_RAW:
1271 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1272 chunk_data_sz))
1273 {
1274 fastboot_fail("Bogus chunk size for chunk type Raw");
1275 return;
1276 }
1277
Ajay Dudaniab18f022011-05-12 14:39:22 -07001278 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1279 chunk_data_sz,
1280 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001281 {
1282 fastboot_fail("flash write failure");
1283 return;
1284 }
1285 total_blocks += chunk_header->chunk_sz;
1286 data += chunk_data_sz;
1287 break;
1288
1289 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001290 total_blocks += chunk_header->chunk_sz;
1291 break;
1292
Ajay Dudani5c761132011-04-07 20:19:04 -07001293 case CHUNK_TYPE_CRC:
1294 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1295 {
1296 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1297 return;
1298 }
1299 total_blocks += chunk_header->chunk_sz;
1300 data += chunk_data_sz;
1301 break;
1302
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001303 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001304 fastboot_fail("Unknown chunk type");
1305 return;
1306 }
1307 }
1308
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001309 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1310 total_blocks, sparse_header->total_blks);
1311
1312 if(total_blocks != sparse_header->total_blks)
1313 {
1314 fastboot_fail("sparse image write failure");
1315 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001316
1317 fastboot_okay("");
1318 return;
1319}
1320
1321void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1322{
1323 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001324 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1325 unsigned int *magic_number = (unsigned int *) data;
1326 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001327
Neeti Desai127b9e02012-03-20 16:11:23 -07001328 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1329 magic_number[1] == DECRYPT_MAGIC_1)
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001330 {
1331#ifdef SSD_ENABLE
Neeti Desai127b9e02012-03-20 16:11:23 -07001332 ret = decrypt_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001333#endif
Greg Griscod6250552011-06-29 14:40:23 -07001334 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001335 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1336 return;
1337 }
1338 }
Neeti Desai127b9e02012-03-20 16:11:23 -07001339 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1340 magic_number[1] == ENCRYPT_MAGIC_1)
1341 {
1342#ifdef SSD_ENABLE
1343 ret = encrypt_scm((uint32 **) &data, &sz);
1344#endif
1345 if (ret != 0) {
1346 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1347 return;
1348 }
1349 }
1350
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001351 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001352 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1353 cmd_flash_mmc_img(arg, data, sz);
1354 else
1355 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001356 return;
1357}
1358
Dima Zavin214cc642009-01-26 11:16:21 -08001359void cmd_flash(const char *arg, void *data, unsigned sz)
1360{
1361 struct ptentry *ptn;
1362 struct ptable *ptable;
1363 unsigned extra = 0;
1364
1365 ptable = flash_get_ptable();
1366 if (ptable == NULL) {
1367 fastboot_fail("partition table doesn't exist");
1368 return;
1369 }
1370
1371 ptn = ptable_find(ptable, arg);
1372 if (ptn == NULL) {
1373 fastboot_fail("unknown partition name");
1374 return;
1375 }
1376
1377 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1378 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1379 fastboot_fail("image is not a boot image");
1380 return;
1381 }
1382 }
1383
Amol Jadi5c61a952012-05-04 17:05:35 -07001384 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001385 || !strcmp(ptn->name, "userdata")
1386 || !strcmp(ptn->name, "persist")
1387 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301388 if (flash_ecc_bch_enabled())
1389 /* Spare data bytes for 8 bit ECC increased by 4 */
1390 extra = ((page_size >> 9) * 20);
1391 else
1392 extra = ((page_size >> 9) * 16);
1393 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001394 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001395
1396 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1397 if (flash_write(ptn, extra, data, sz)) {
1398 fastboot_fail("flash write failure");
1399 return;
1400 }
1401 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1402 fastboot_okay("");
1403}
1404
1405void cmd_continue(const char *arg, void *data, unsigned sz)
1406{
1407 fastboot_okay("");
1408 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001409 if (target_is_emmc_boot())
1410 {
1411 boot_linux_from_mmc();
1412 }
1413 else
1414 {
1415 boot_linux_from_flash();
1416 }
Dima Zavin214cc642009-01-26 11:16:21 -08001417}
1418
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001419void cmd_reboot(const char *arg, void *data, unsigned sz)
1420{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001421 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001422 fastboot_okay("");
1423 reboot_device(0);
1424}
1425
1426void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1427{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001428 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001429 fastboot_okay("");
1430 reboot_device(FASTBOOT_MODE);
1431}
1432
Shashank Mittal162244e2011-08-08 19:01:25 -07001433void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1434{
1435 if(!device.is_unlocked)
1436 {
1437 device.is_unlocked = 1;
1438 write_device_info(&device);
1439 }
1440 fastboot_okay("");
1441}
1442
Shashank Mittala0032282011-08-26 14:50:11 -07001443void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1444{
1445 char response[64];
1446 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1447 fastboot_info(response);
1448 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1449 fastboot_info(response);
1450 fastboot_okay("");
1451}
1452
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001453void splash_screen ()
1454{
1455 struct ptentry *ptn;
1456 struct ptable *ptable;
1457 struct fbcon_config *fb_display = NULL;
1458
1459 if (!target_is_emmc_boot())
1460 {
1461 ptable = flash_get_ptable();
1462 if (ptable == NULL) {
1463 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001464 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001465 }
1466
1467 ptn = ptable_find(ptable, "splash");
1468 if (ptn == NULL) {
1469 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1470 } else {
1471 fb_display = fbcon_display();
1472 if (fb_display) {
1473 if (flash_read(ptn, 0, fb_display->base,
1474 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1475 fbcon_clear();
1476 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1477 }
1478 }
1479 }
1480 }
1481}
1482
Brian Swetland9c4c0752009-01-25 16:23:50 -08001483void aboot_init(const struct app_descriptor *app)
1484{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001485 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001486 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001487 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001488
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001489 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001490 if (target_is_emmc_boot())
1491 {
1492 page_size = 2048;
1493 page_mask = page_size - 1;
1494 }
1495 else
1496 {
1497 page_size = flash_page_size();
1498 page_mask = page_size - 1;
1499 }
1500
Shashank Mittal162244e2011-08-08 19:01:25 -07001501 if(target_use_signed_kernel())
1502 {
1503 read_device_info(&device);
1504
Shashank Mittal162244e2011-08-08 19:01:25 -07001505 }
1506
Greg Griscod6250552011-06-29 14:40:23 -07001507 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001508 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001509 surf_udc_device.serialno = sn_buf;
1510
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001511 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001512 if (keys_get_state(KEY_HOME) != 0)
1513 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001514 if (keys_get_state(KEY_VOLUMEUP) != 0)
1515 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001516 if(!boot_into_recovery)
1517 {
1518 if (keys_get_state(KEY_BACK) != 0)
1519 goto fastboot;
1520 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1521 goto fastboot;
1522 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001523
1524 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001525 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001526 goto fastboot;
1527 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001528
Ajay Dudani77421292010-10-27 19:34:06 -07001529 reboot_mode = check_reboot_mode();
1530 if (reboot_mode == RECOVERY_MODE) {
1531 boot_into_recovery = 1;
1532 } else if(reboot_mode == FASTBOOT_MODE) {
1533 goto fastboot;
1534 }
1535
Shashank Mittal23b8f422010-04-16 19:27:21 -07001536 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001537 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001538 if(emmc_recovery_init())
1539 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001540 if(target_use_signed_kernel())
1541 {
1542 if((device.is_unlocked) || (device.is_tampered))
1543 {
1544 #ifdef TZ_TAMPER_FUSE
1545 set_tamper_fuse_cmd();
1546 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301547 #if USE_PCOM_SECBOOT
1548 set_tamper_flag(device.is_tampered);
1549 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001550 }
1551 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001552 boot_linux_from_mmc();
1553 }
1554 else
1555 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001556 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301557#if USE_PCOM_SECBOOT
1558 if((device.is_unlocked) || (device.is_tampered))
1559 set_tamper_flag(device.is_tampered);
1560#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001561 boot_linux_from_flash();
1562 }
Dima Zavinb4283602009-01-26 16:36:57 -08001563 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1564 "to fastboot mode.\n");
1565
1566fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001567
Shashank Mittal162244e2011-08-08 19:01:25 -07001568 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001569
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001570 if(!usb_init)
1571 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001572
1573 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001574
Shashank Mittal23b8f422010-04-16 19:27:21 -07001575 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001576 {
1577 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001578 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001579 }
1580 else
1581 {
1582 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001583 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001584 }
1585
1586 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001587 fastboot_register("reboot", cmd_reboot);
1588 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001589 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001590 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001591 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001592 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001593 fastboot_publish("serialno", sn_buf);
Kinson Chikf1a43512011-07-14 11:28:39 -07001594 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001595 sz = target_get_max_flash_size();
1596 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001597 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001598}
1599
1600APP_START(aboot)
1601 .init = aboot_init,
1602APP_END