blob: 99857cc0898a5210718273292eeb10439a49e1cf [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";
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530101static const char *baseband_dsda = " androidboot.baseband=dsda";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800102
Shashank Mittalcd98d472011-08-02 14:29:24 -0700103/* Assuming unauthorized kernel image by default */
104static int auth_kernel_img = 0;
105
Shashank Mittal162244e2011-08-08 19:01:25 -0700106static device_info device = {DEVICE_MAGIC, 0, 0};
107
Brian Swetland9c4c0752009-01-25 16:23:50 -0800108static struct udc_device surf_udc_device = {
109 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800110 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800111 .version_id = 0x0100,
112 .manufacturer = "Google",
113 .product = "Android",
114};
115
Dima Zavin42168f22009-01-30 11:52:22 -0800116struct atag_ptbl_entry
117{
118 char name[16];
119 unsigned offset;
120 unsigned size;
121 unsigned flags;
122};
123
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800124char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700125
Greg Griscod2471ef2011-07-14 13:00:42 -0700126extern int emmc_recovery_init(void);
127
Kinson Chik0b1c8162011-08-31 16:31:57 -0700128#if NO_KEYPAD_DRIVER
129extern int fastboot_trigger(void);
130#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700131
Dima Zavin42168f22009-01-30 11:52:22 -0800132static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
133{
134 struct atag_ptbl_entry atag_ptn;
135
136 memcpy(atag_ptn.name, ptn->name, 16);
137 atag_ptn.name[15] = '\0';
138 atag_ptn.offset = ptn->start;
139 atag_ptn.size = ptn->length;
140 atag_ptn.flags = ptn->flags;
141 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
142 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
143}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800144
Neeti Desaie245d492012-06-01 12:52:13 -0700145unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800146{
David Ng183a7422009-12-07 14:55:21 -0800147 int cmdline_len = 0;
148 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800149 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700150 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800151
Brian Swetland9c4c0752009-01-25 16:23:50 -0800152 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800153 cmdline_len = strlen(cmdline);
154 have_cmdline = 1;
155 }
156 if (target_is_emmc_boot()) {
157 cmdline_len += strlen(emmc_cmdline);
158 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800159
160 cmdline_len += strlen(usb_sn_cmdline);
161 cmdline_len += strlen(sn_buf);
162
David Ngf773dde2010-07-26 19:55:08 -0700163 if (target_pause_for_battery_charge()) {
164 pause_at_bootup = 1;
165 cmdline_len += strlen(battchg_pause);
166 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800167
Shashank Mittalcd98d472011-08-02 14:29:24 -0700168 if(target_use_signed_kernel() && auth_kernel_img) {
169 cmdline_len += strlen(auth_kernel);
170 }
171
Ajay Dudanid04110c2011-01-17 23:55:07 -0800172 /* Determine correct androidboot.baseband to use */
173 switch(target_baseband())
174 {
175 case BASEBAND_APQ:
176 cmdline_len += strlen(baseband_apq);
177 break;
178
179 case BASEBAND_MSM:
180 cmdline_len += strlen(baseband_msm);
181 break;
182
183 case BASEBAND_CSFB:
184 cmdline_len += strlen(baseband_csfb);
185 break;
186
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800187 case BASEBAND_SVLTE2A:
188 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800189 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700190
191 case BASEBAND_MDM:
192 cmdline_len += strlen(baseband_mdm);
193 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700194
195 case BASEBAND_SGLTE:
196 cmdline_len += strlen(baseband_sglte);
197 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530198
199 case BASEBAND_DSDA:
200 cmdline_len += strlen(baseband_dsda);
201 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800202 }
203
David Ng183a7422009-12-07 14:55:21 -0800204 if (cmdline_len > 0) {
205 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700206 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
207 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700208
Amol Jadi168b7712012-03-06 16:15:00 -0800209 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700210 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800211 if (have_cmdline) {
212 src = cmdline;
213 while ((*dst++ = *src++));
214 }
215 if (target_is_emmc_boot()) {
216 src = emmc_cmdline;
217 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700218 have_cmdline = 1;
219 while ((*dst++ = *src++));
220 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800221
222 src = usb_sn_cmdline;
223 if (have_cmdline) --dst;
224 have_cmdline = 1;
225 while ((*dst++ = *src++));
226 src = sn_buf;
227 if (have_cmdline) --dst;
228 have_cmdline = 1;
229 while ((*dst++ = *src++));
230
David Ngf773dde2010-07-26 19:55:08 -0700231 if (pause_at_bootup) {
232 src = battchg_pause;
233 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800234 while ((*dst++ = *src++));
235 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800236
Shashank Mittalcd98d472011-08-02 14:29:24 -0700237 if(target_use_signed_kernel() && auth_kernel_img) {
238 src = auth_kernel;
239 if (have_cmdline) --dst;
240 while ((*dst++ = *src++));
241 }
242
Ajay Dudanid04110c2011-01-17 23:55:07 -0800243 switch(target_baseband())
244 {
245 case BASEBAND_APQ:
246 src = baseband_apq;
247 if (have_cmdline) --dst;
248 while ((*dst++ = *src++));
249 break;
250
251 case BASEBAND_MSM:
252 src = baseband_msm;
253 if (have_cmdline) --dst;
254 while ((*dst++ = *src++));
255 break;
256
257 case BASEBAND_CSFB:
258 src = baseband_csfb;
259 if (have_cmdline) --dst;
260 while ((*dst++ = *src++));
261 break;
262
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800263 case BASEBAND_SVLTE2A:
264 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800265 if (have_cmdline) --dst;
266 while ((*dst++ = *src++));
267 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700268
269 case BASEBAND_MDM:
270 src = baseband_mdm;
271 if (have_cmdline) --dst;
272 while ((*dst++ = *src++));
273 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700274
275 case BASEBAND_SGLTE:
276 src = baseband_sglte;
277 if (have_cmdline) --dst;
278 while ((*dst++ = *src++));
279 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530280
281 case BASEBAND_DSDA:
282 src = baseband_dsda;
283 if (have_cmdline) --dst;
284 while ((*dst++ = *src++));
285 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800286 }
Neeti Desaie245d492012-06-01 12:52:13 -0700287 }
288 return cmdline_final;
289}
290
291unsigned *atag_core(unsigned *ptr)
292{
293 /* CORE */
294 *ptr++ = 2;
295 *ptr++ = 0x54410001;
296
297 return ptr;
298
299}
300
301unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
302 unsigned ramdisk_size)
303{
304 if (ramdisk_size) {
305 *ptr++ = 4;
306 *ptr++ = 0x54420005;
307 *ptr++ = (unsigned)ramdisk;
308 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800309 }
310
Neeti Desaie245d492012-06-01 12:52:13 -0700311 return ptr;
312}
313
314unsigned *atag_ptable(unsigned **ptr_addr)
315{
316 int i;
317 struct ptable *ptable;
318
319 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700320 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
321 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700322 *(*ptr_addr)++ = 0x4d534d70;
323 for (i = 0; i < ptable->count; ++i)
324 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
325 }
326
327 return (*ptr_addr);
328}
329
330unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
331{
332 int cmdline_length = 0;
333 int n;
334 unsigned char *cmdline_final = NULL;
335 char *dest;
336
337 cmdline_final = update_cmdline(cmdline);
338 if (cmdline_final){
339 dprintf(INFO, "cmdline: %s\n", cmdline_final);
340 }
341
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700342 cmdline_length =strlen((const char*)cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700343 n = (cmdline_length + 4) & (~3);
344
345 *ptr++ = (n / 4) + 2;
346 *ptr++ = 0x54410009;
347 dest = (char *) ptr;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700348 while ((*dest++ = *cmdline_final++));
Neeti Desaie245d492012-06-01 12:52:13 -0700349 ptr += (n / 4);
350
351 return ptr;
352}
353
354unsigned *atag_end(unsigned *ptr)
355{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800356 /* END */
357 *ptr++ = 0;
358 *ptr++ = 0;
359
Neeti Desaie245d492012-06-01 12:52:13 -0700360 return ptr;
361}
362
363void generate_atags(unsigned *ptr, const char *cmdline,
364 void *ramdisk, unsigned ramdisk_size)
365{
366
367 ptr = atag_core(ptr);
368 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
369 ptr = target_atag_mem(ptr);
370
371 /* Skip NAND partition ATAGS for eMMC boot */
372 if (!target_is_emmc_boot()){
373 ptr = atag_ptable(&ptr);
374 }
375
376 ptr = atag_cmdline(ptr, cmdline);
377 ptr = atag_end(ptr);
378}
379
380void boot_linux(void *kernel, unsigned *tags,
381 const char *cmdline, unsigned machtype,
382 void *ramdisk, unsigned ramdisk_size)
383{
Neeti Desai17379b82012-06-04 18:42:53 -0700384 int ret = 0;
Neeti Desaie245d492012-06-01 12:52:13 -0700385 void (*entry)(unsigned, unsigned, unsigned*) = kernel;
386
Neeti Desai17379b82012-06-04 18:42:53 -0700387#if DEVICE_TREE
388 /* Update the Device Tree */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700389 ret = update_device_tree((void *)tags, cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700390 if(ret)
391 {
392 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
393 ASSERT(0);
394 }
395#else
Neeti Desaie245d492012-06-01 12:52:13 -0700396 /* Generating the Atags */
397 generate_atags(tags, cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700398#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700399
Brian Swetland9c4c0752009-01-25 16:23:50 -0800400 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
401 kernel, ramdisk, ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800402
403 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700404
Amol Jadi4421e652011-06-16 15:00:48 -0700405 /* do any platform specific cleanup before kernel entry */
406 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700407
Brian Swetland9c4c0752009-01-25 16:23:50 -0800408 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700409
Amol Jadi504f9fe2012-08-16 13:56:48 -0700410#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800411 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700412#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800413 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800414}
415
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800416unsigned page_size = 0;
417unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800418
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800419#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800420
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800421static unsigned char buf[4096]; //Equal to max-supported pagesize
Neeti Desai465491e2012-07-31 12:53:35 -0700422static unsigned char dt_buf[4096];
Dima Zavin214cc642009-01-26 11:16:21 -0800423
Shashank Mittal23b8f422010-04-16 19:27:21 -0700424int boot_linux_from_mmc(void)
425{
426 struct boot_img_hdr *hdr = (void*) buf;
427 struct boot_img_hdr *uhdr;
428 unsigned offset = 0;
429 unsigned long long ptn = 0;
430 unsigned n = 0;
431 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700432 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700433
Shashank Mittalcd98d472011-08-02 14:29:24 -0700434 unsigned char *image_addr = 0;
435 unsigned kernel_actual;
436 unsigned ramdisk_actual;
437 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700438 unsigned second_actual = 0;
439 unsigned dt_actual = 0;
440
441#if DEVICE_TREE
442 struct dt_table *table;
443 struct dt_entry *dt_entry_ptr;
444 unsigned dt_table_offset;
445#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700446
Shashank Mittal23b8f422010-04-16 19:27:21 -0700447 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
448 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
449 dprintf(INFO, "Unified boot method!\n");
450 hdr = uhdr;
451 goto unified_boot;
452 }
Greg Griscod6250552011-06-29 14:40:23 -0700453 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700454 index = partition_get_index("boot");
455 ptn = partition_get_offset(index);
456 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700457 dprintf(CRITICAL, "ERROR: No boot partition found\n");
458 return -1;
459 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700460 }
461 else {
462 index = partition_get_index("recovery");
463 ptn = partition_get_offset(index);
464 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700465 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
466 return -1;
467 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700468 }
469
Greg Griscod6250552011-06-29 14:40:23 -0700470 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700471 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
472 return -1;
473 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700474
475 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700476 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700477 return -1;
478 }
479
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700480 if (hdr->page_size && (hdr->page_size != page_size)) {
481 page_size = hdr->page_size;
482 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700483 }
484
Shashank Mittalcd98d472011-08-02 14:29:24 -0700485 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700486 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800487 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700488 image_addr = (unsigned char *)target_get_scratch_address();
489 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
490 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530491 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700492
493 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700494
495 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700496 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700497
Shashank Mittalcd98d472011-08-02 14:29:24 -0700498 /* Read image without signature */
499 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
500 {
501 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
502 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800503 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700504
505 offset = imagesize_actual;
506 /* Read signature */
507 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
508 {
509 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
510 }
511 else
512 {
513 auth_kernel_img = image_verify((unsigned char *)image_addr,
514 (unsigned char *)(image_addr + imagesize_actual),
515 imagesize_actual,
516 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700517
518 if(auth_kernel_img)
519 {
520 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700521 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700522 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700523 }
524
Neeti Desai465491e2012-07-31 12:53:35 -0700525 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700526 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
527 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700528
Neeti Desai465491e2012-07-31 12:53:35 -0700529 #if DEVICE_TREE
530 if(hdr->dt_size) {
531 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700532 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700533
534 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
535
536 /* Restriction that the device tree entry table should be less than a page*/
537 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
538
539 /* Validate the device tree table header */
540 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
541 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
542 return -1;
543 }
544
545 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700546 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700547 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
548 return -1;
549 }
550
551 /* Read device device tree in the "tags_add */
552 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
553 }
554 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700555 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700556 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700557 {
558 write_device_info_mmc(&device);
559 #ifdef TZ_TAMPER_FUSE
560 set_tamper_fuse_cmd();
561 #endif
562 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530563 #if USE_PCOM_SECBOOT
564 set_tamper_flag(device.is_tampered);
565 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700566 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700567 else
568 {
569 offset += page_size;
570
571 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
572 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
573 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
574 return -1;
575 }
576 offset += n;
577
578 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
579 if(n != 0)
580 {
581 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
582 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
583 return -1;
584 }
585 }
586 offset += n;
Neeti Desai465491e2012-07-31 12:53:35 -0700587
588 if(hdr->second_size != 0) {
589 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
590 offset += n;
591 }
592
593 #if DEVICE_TREE
594 if(hdr->dt_size != 0) {
595
596 /* Read the device tree table into buffer */
597 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
598 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
599 return -1;
600 }
601 table = (struct dt_table*) dt_buf;
602
603 /* Restriction that the device tree entry table should be less than a page*/
604 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
605
606 /* Validate the device tree table header */
607 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
608 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
609 return -1;
610 }
611
612 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700613 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700614 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
615 return -1;
616 }
617
618 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700619 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
620 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
621 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
622 return -1;
623 }
624 }
625 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700626 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700627
628unified_boot:
629 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
630 hdr->kernel_size);
631 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
632 hdr->ramdisk_size);
633
634 if(hdr->cmdline[0]) {
635 cmdline = (char*) hdr->cmdline;
636 } else {
637 cmdline = DEFAULT_CMDLINE;
638 }
639 dprintf(INFO, "cmdline = '%s'\n", cmdline);
640
641 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700642 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700643 (const char *)cmdline, board_machtype(),
644 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
645
646 return 0;
647}
648
Dima Zavin214cc642009-01-26 11:16:21 -0800649int boot_linux_from_flash(void)
650{
651 struct boot_img_hdr *hdr = (void*) buf;
652 unsigned n;
653 struct ptentry *ptn;
654 struct ptable *ptable;
655 unsigned offset = 0;
656 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800657
Shashank Mittalcd98d472011-08-02 14:29:24 -0700658 unsigned char *image_addr = 0;
659 unsigned kernel_actual;
660 unsigned ramdisk_actual;
661 unsigned imagesize_actual;
662
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700663#if DEVICE_TREE
664 struct dt_table *table;
665 struct dt_entry *dt_entry_ptr;
666#endif
667
668
David Ng183a7422009-12-07 14:55:21 -0800669 if (target_is_emmc_boot()) {
670 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
671 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
672 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
673 return -1;
674 }
675 goto continue_boot;
676 }
677
Dima Zavin214cc642009-01-26 11:16:21 -0800678 ptable = flash_get_ptable();
679 if (ptable == NULL) {
680 dprintf(CRITICAL, "ERROR: Partition table not found\n");
681 return -1;
682 }
683
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800684 if(!boot_into_recovery)
685 {
686 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700687
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800688 if (ptn == NULL) {
689 dprintf(CRITICAL, "ERROR: No boot partition found\n");
690 return -1;
691 }
692 }
693 else
694 {
695 ptn = ptable_find(ptable, "recovery");
696 if (ptn == NULL) {
697 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
698 return -1;
699 }
Dima Zavin214cc642009-01-26 11:16:21 -0800700 }
701
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800702 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800703 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
704 return -1;
705 }
Dima Zavin214cc642009-01-26 11:16:21 -0800706
707 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700708 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800709 return -1;
710 }
711
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800712 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700713 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 -0800714 return -1;
715 }
716
Shashank Mittalcd98d472011-08-02 14:29:24 -0700717 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700718 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700719 {
720 image_addr = (unsigned char *)target_get_scratch_address();
721 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
722 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
723 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800724
Shashank Mittalcd98d472011-08-02 14:29:24 -0700725 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700726
727 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700728 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700729
Shashank Mittalcd98d472011-08-02 14:29:24 -0700730 /* Read image without signature */
731 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
732 {
733 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
734 return -1;
735 }
Dima Zavin214cc642009-01-26 11:16:21 -0800736
Shashank Mittalcd98d472011-08-02 14:29:24 -0700737 offset = imagesize_actual;
738 /* Read signature */
739 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
740 {
741 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
742 }
743 else
744 {
745
746 /* Verify signature */
747 auth_kernel_img = image_verify((unsigned char *)image_addr,
748 (unsigned char *)(image_addr + imagesize_actual),
749 imagesize_actual,
750 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700751
752 if(auth_kernel_img)
753 {
754 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700755 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700756 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700757 }
758
759 /* Move kernel and ramdisk to correct address */
760 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
761 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700762
763 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700764 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700765 {
766 write_device_info_flash(&device);
767 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530768#if USE_PCOM_SECBOOT
769 set_tamper_flag(device.is_tampered);
770#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700771 }
772 else
773 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700774 offset = page_size;
775
Shashank Mittalcd98d472011-08-02 14:29:24 -0700776 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
777 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
778 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
779 return -1;
780 }
781 offset += n;
782
783 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
784 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
785 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
786 return -1;
787 }
788 offset += n;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700789
790 if(hdr->second_size != 0) {
791 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
792 offset += n;
793 }
794
795#if DEVICE_TREE
796 if(hdr->dt_size != 0) {
797
798 /* Read the device tree table into buffer */
799 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
800 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
801 return -1;
802 }
803
804 table = (struct dt_table*) dt_buf;
805
806 /* Restriction that the device tree entry table should be less than a page*/
807 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
808
809 /* Validate the device tree table header */
810 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
811 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
812 return -1;
813 }
814
815 /* Calculate the offset of device tree within device tree table */
816 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
817 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
818 return -1;
819 }
820
821 /* Read device device tree in the "tags_add */
822 if(flash_read(ptn, offset + dt_entry_ptr->offset,
823 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
824 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
825 return -1;
826 }
827 }
828#endif
829
Shashank Mittalcd98d472011-08-02 14:29:24 -0700830 }
David Ng183a7422009-12-07 14:55:21 -0800831continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800832 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
833 hdr->kernel_size);
834 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
835 hdr->ramdisk_size);
836
837 if(hdr->cmdline[0]) {
838 cmdline = (char*) hdr->cmdline;
839 } else {
840 cmdline = DEFAULT_CMDLINE;
841 }
842 dprintf(INFO, "cmdline = '%s'\n", cmdline);
843
844 /* TODO: create/pass atags to kernel */
845
846 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700847 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800848 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800849 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
850
851 return 0;
852}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800853
Shashank Mittal162244e2011-08-08 19:01:25 -0700854unsigned char info_buf[4096];
855void write_device_info_mmc(device_info *dev)
856{
857 struct device_info *info = (void*) info_buf;
858 unsigned long long ptn = 0;
859 unsigned long long size;
860 int index = INVALID_PTN;
861
862 index = partition_get_index("aboot");
863 ptn = partition_get_offset(index);
864 if(ptn == 0)
865 {
866 return;
867 }
868
869 size = partition_get_size(index);
870
871 memcpy(info, dev, sizeof(device_info));
872
873 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
874 {
875 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
876 return;
877 }
878}
879
880void read_device_info_mmc(device_info *dev)
881{
882 struct device_info *info = (void*) info_buf;
883 unsigned long long ptn = 0;
884 unsigned long long size;
885 int index = INVALID_PTN;
886
887 index = partition_get_index("aboot");
888 ptn = partition_get_offset(index);
889 if(ptn == 0)
890 {
891 return;
892 }
893
894 size = partition_get_size(index);
895
896 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
897 {
898 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
899 return;
900 }
901
902 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
903 {
904 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
905 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700906 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700907
908 write_device_info_mmc(info);
909 }
910 memcpy(dev, info, sizeof(device_info));
911}
912
913void write_device_info_flash(device_info *dev)
914{
915 struct device_info *info = (void *) info_buf;
916 struct ptentry *ptn;
917 struct ptable *ptable;
918
919 ptable = flash_get_ptable();
920 if (ptable == NULL)
921 {
922 dprintf(CRITICAL, "ERROR: Partition table not found\n");
923 return;
924 }
925
926 ptn = ptable_find(ptable, "devinfo");
927 if (ptn == NULL)
928 {
929 dprintf(CRITICAL, "ERROR: No boot partition found\n");
930 return;
931 }
932
933 memcpy(info, dev, sizeof(device_info));
934
935 if (flash_write(ptn, 0, (void *)info_buf, page_size))
936 {
937 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
938 return;
939 }
940}
941
942void read_device_info_flash(device_info *dev)
943{
944 struct device_info *info = (void*) info_buf;
945 struct ptentry *ptn;
946 struct ptable *ptable;
947
948 ptable = flash_get_ptable();
949 if (ptable == NULL)
950 {
951 dprintf(CRITICAL, "ERROR: Partition table not found\n");
952 return;
953 }
954
955 ptn = ptable_find(ptable, "devinfo");
956 if (ptn == NULL)
957 {
958 dprintf(CRITICAL, "ERROR: No boot partition found\n");
959 return;
960 }
961
962 if (flash_read(ptn, 0, (void *)info_buf, page_size))
963 {
964 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
965 return;
966 }
967
968 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
969 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700970 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
971 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700972 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700973 write_device_info_flash(info);
974 }
975 memcpy(dev, info, sizeof(device_info));
976}
977
978void write_device_info(device_info *dev)
979{
980 if(target_is_emmc_boot())
981 {
982 write_device_info_mmc(dev);
983 }
984 else
985 {
986 write_device_info_flash(dev);
987 }
988}
989
990void read_device_info(device_info *dev)
991{
992 if(target_is_emmc_boot())
993 {
994 read_device_info_mmc(dev);
995 }
996 else
997 {
998 read_device_info_flash(dev);
999 }
1000}
1001
1002void reset_device_info()
1003{
1004 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001005 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001006 write_device_info(&device);
1007}
1008
1009void set_device_root()
1010{
1011 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001012 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001013 write_device_info(&device);
1014}
1015
Amol Jadicb524072012-08-09 16:40:18 -07001016#if DEVICE_TREE
1017int copy_dtb(uint8_t *boot_image_start)
1018{
1019 uint32 dt_image_offset = 0;
1020 uint32_t n;
1021 struct dt_table *table;
1022 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001023
1024 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1025
Amol Jadicb524072012-08-09 16:40:18 -07001026 if(hdr->dt_size != 0) {
1027
1028 /* add kernel offset */
1029 dt_image_offset += page_size;
1030 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1031 dt_image_offset += n;
1032
1033 /* add ramdisk offset */
1034 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1035 dt_image_offset += n;
1036
1037 /* add second offset */
1038 if(hdr->second_size != 0) {
1039 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1040 dt_image_offset += n;
1041 }
1042
1043 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001044 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001045
1046 /* Restriction that the device tree entry table should be less than a page*/
1047 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1048
1049 /* Validate the device tree table header */
1050 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1051 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1052 return -1;
1053 }
1054
1055 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001056 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001057 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1058 return -1;
1059 }
1060
1061 /* Read device device tree in the "tags_add */
1062 memmove((void*) hdr->tags_addr, boot_image_start + dt_image_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
1063 }
1064
1065 /* Everything looks fine. Return success. */
1066 return 0;
1067}
1068#endif
1069
Brian Swetland9c4c0752009-01-25 16:23:50 -08001070void cmd_boot(const char *arg, void *data, unsigned sz)
1071{
1072 unsigned kernel_actual;
1073 unsigned ramdisk_actual;
1074 static struct boot_img_hdr hdr;
1075 char *ptr = ((char*) data);
1076
1077 if (sz < sizeof(hdr)) {
1078 fastboot_fail("invalid bootimage header");
1079 return;
1080 }
1081
1082 memcpy(&hdr, data, sizeof(hdr));
1083
1084 /* ensure commandline is terminated */
1085 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
1086
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001087 if(target_is_emmc_boot() && hdr.page_size) {
1088 page_size = hdr.page_size;
1089 page_mask = page_size - 1;
1090 }
1091
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001092 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
1093 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001094
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001095 /* sz should have atleast raw boot image */
1096 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001097 fastboot_fail("incomplete bootimage");
1098 return;
1099 }
1100
Amol Jadicb524072012-08-09 16:40:18 -07001101#if DEVICE_TREE
1102 /* find correct dtb and copy it to right location */
1103 if(copy_dtb(data))
1104 {
1105 fastboot_fail("dtb not found");
1106 return;
1107 }
1108#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001109
1110 fastboot_okay("");
1111 udc_stop();
1112
Amol Jadicb524072012-08-09 16:40:18 -07001113 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001114 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
Amol Jadicb524072012-08-09 16:40:18 -07001115
Amol Jadie67872e2011-06-27 14:14:11 -07001116 boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -08001117 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -07001118 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001119}
1120
Dima Zavin214cc642009-01-26 11:16:21 -08001121void cmd_erase(const char *arg, void *data, unsigned sz)
1122{
1123 struct ptentry *ptn;
1124 struct ptable *ptable;
1125
1126 ptable = flash_get_ptable();
1127 if (ptable == NULL) {
1128 fastboot_fail("partition table doesn't exist");
1129 return;
1130 }
1131
1132 ptn = ptable_find(ptable, arg);
1133 if (ptn == NULL) {
1134 fastboot_fail("unknown partition name");
1135 return;
1136 }
1137
1138 if (flash_erase(ptn)) {
1139 fastboot_fail("failed to erase partition");
1140 return;
1141 }
1142 fastboot_okay("");
1143}
1144
Bikas Gurungd48bd242010-09-04 19:54:32 -07001145
1146void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1147{
1148 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -08001149 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -07001150 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001151
Kinson Chikf1a43512011-07-14 11:28:39 -07001152 index = partition_get_index(arg);
1153 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001154
Kinson Chikf1a43512011-07-14 11:28:39 -07001155 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001156 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001157 return;
1158 }
neetidb4b24d62012-01-20 12:13:09 -08001159 /* Simple inefficient version of erase. Just writing
1160 0 in first block */
1161 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1162 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001163 return;
1164 }
1165 fastboot_okay("");
1166}
1167
1168
Ajay Dudani5c761132011-04-07 20:19:04 -07001169void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001170{
1171 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001172 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001173 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001174
Greg Grisco6e754772011-06-23 12:19:39 -07001175 if (!strcmp(arg, "partition"))
1176 {
1177 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001178 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001179 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001180 return;
1181 }
1182 }
Greg Grisco6e754772011-06-23 12:19:39 -07001183 else
1184 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001185 index = partition_get_index(arg);
1186 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001187 if(ptn == 0) {
1188 fastboot_fail("partition table doesn't exist");
1189 return;
1190 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001191
Greg Grisco6e754772011-06-23 12:19:39 -07001192 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1193 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1194 fastboot_fail("image is not a boot image");
1195 return;
1196 }
1197 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001198
Kinson Chikf1a43512011-07-14 11:28:39 -07001199 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001200 if (ROUND_TO_PAGE(sz,511) > size) {
1201 fastboot_fail("size too large");
1202 return;
1203 }
1204 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1205 fastboot_fail("flash write failure");
1206 return;
1207 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001208 }
1209 fastboot_okay("");
1210 return;
1211}
1212
Ajay Dudani5c761132011-04-07 20:19:04 -07001213void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1214{
1215 unsigned int chunk;
1216 unsigned int chunk_data_sz;
1217 sparse_header_t *sparse_header;
1218 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001219 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001220 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301221 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001222 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001223
Kinson Chikf1a43512011-07-14 11:28:39 -07001224 index = partition_get_index(arg);
1225 ptn = partition_get_offset(index);
1226 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001227 fastboot_fail("partition table doesn't exist");
1228 return;
1229 }
1230
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301231 size = partition_get_size(index);
1232 if (ROUND_TO_PAGE(sz,511) > size) {
1233 fastboot_fail("size too large");
1234 return;
1235 }
1236
Ajay Dudani5c761132011-04-07 20:19:04 -07001237 /* Read and skip over sparse image header */
1238 sparse_header = (sparse_header_t *) data;
1239 data += sparse_header->file_hdr_sz;
1240 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1241 {
1242 /* Skip the remaining bytes in a header that is longer than
1243 * we expected.
1244 */
1245 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1246 }
1247
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001248 dprintf (SPEW, "=== Sparse Image Header ===\n");
1249 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1250 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1251 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1252 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1253 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1254 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1255 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1256 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001257
1258 /* Start processing chunks */
1259 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1260 {
1261 /* Read and skip over chunk header */
1262 chunk_header = (chunk_header_t *) data;
1263 data += sizeof(chunk_header_t);
1264
1265 dprintf (SPEW, "=== Chunk Header ===\n");
1266 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1267 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1268 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1269
1270 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1271 {
1272 /* Skip the remaining bytes in a header that is longer than
1273 * we expected.
1274 */
1275 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1276 }
1277
1278 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1279 switch (chunk_header->chunk_type)
1280 {
1281 case CHUNK_TYPE_RAW:
1282 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1283 chunk_data_sz))
1284 {
1285 fastboot_fail("Bogus chunk size for chunk type Raw");
1286 return;
1287 }
1288
Ajay Dudaniab18f022011-05-12 14:39:22 -07001289 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1290 chunk_data_sz,
1291 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001292 {
1293 fastboot_fail("flash write failure");
1294 return;
1295 }
1296 total_blocks += chunk_header->chunk_sz;
1297 data += chunk_data_sz;
1298 break;
1299
1300 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001301 total_blocks += chunk_header->chunk_sz;
1302 break;
1303
Ajay Dudani5c761132011-04-07 20:19:04 -07001304 case CHUNK_TYPE_CRC:
1305 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1306 {
1307 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1308 return;
1309 }
1310 total_blocks += chunk_header->chunk_sz;
1311 data += chunk_data_sz;
1312 break;
1313
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001314 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001315 fastboot_fail("Unknown chunk type");
1316 return;
1317 }
1318 }
1319
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001320 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1321 total_blocks, sparse_header->total_blks);
1322
1323 if(total_blocks != sparse_header->total_blks)
1324 {
1325 fastboot_fail("sparse image write failure");
1326 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001327
1328 fastboot_okay("");
1329 return;
1330}
1331
1332void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1333{
1334 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001335 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1336 unsigned int *magic_number = (unsigned int *) data;
1337 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001338
Neeti Desai127b9e02012-03-20 16:11:23 -07001339 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1340 magic_number[1] == DECRYPT_MAGIC_1)
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001341 {
1342#ifdef SSD_ENABLE
Neeti Desai127b9e02012-03-20 16:11:23 -07001343 ret = decrypt_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001344#endif
Greg Griscod6250552011-06-29 14:40:23 -07001345 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001346 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1347 return;
1348 }
1349 }
Neeti Desai127b9e02012-03-20 16:11:23 -07001350 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1351 magic_number[1] == ENCRYPT_MAGIC_1)
1352 {
1353#ifdef SSD_ENABLE
1354 ret = encrypt_scm((uint32 **) &data, &sz);
1355#endif
1356 if (ret != 0) {
1357 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1358 return;
1359 }
1360 }
1361
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001362 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001363 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1364 cmd_flash_mmc_img(arg, data, sz);
1365 else
1366 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001367 return;
1368}
1369
Dima Zavin214cc642009-01-26 11:16:21 -08001370void cmd_flash(const char *arg, void *data, unsigned sz)
1371{
1372 struct ptentry *ptn;
1373 struct ptable *ptable;
1374 unsigned extra = 0;
1375
1376 ptable = flash_get_ptable();
1377 if (ptable == NULL) {
1378 fastboot_fail("partition table doesn't exist");
1379 return;
1380 }
1381
1382 ptn = ptable_find(ptable, arg);
1383 if (ptn == NULL) {
1384 fastboot_fail("unknown partition name");
1385 return;
1386 }
1387
1388 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1389 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1390 fastboot_fail("image is not a boot image");
1391 return;
1392 }
1393 }
1394
Amol Jadi5c61a952012-05-04 17:05:35 -07001395 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001396 || !strcmp(ptn->name, "userdata")
1397 || !strcmp(ptn->name, "persist")
1398 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301399 if (flash_ecc_bch_enabled())
1400 /* Spare data bytes for 8 bit ECC increased by 4 */
1401 extra = ((page_size >> 9) * 20);
1402 else
1403 extra = ((page_size >> 9) * 16);
1404 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001405 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001406
1407 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1408 if (flash_write(ptn, extra, data, sz)) {
1409 fastboot_fail("flash write failure");
1410 return;
1411 }
1412 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1413 fastboot_okay("");
1414}
1415
1416void cmd_continue(const char *arg, void *data, unsigned sz)
1417{
1418 fastboot_okay("");
1419 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001420 if (target_is_emmc_boot())
1421 {
1422 boot_linux_from_mmc();
1423 }
1424 else
1425 {
1426 boot_linux_from_flash();
1427 }
Dima Zavin214cc642009-01-26 11:16:21 -08001428}
1429
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001430void cmd_reboot(const char *arg, void *data, unsigned sz)
1431{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001432 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001433 fastboot_okay("");
1434 reboot_device(0);
1435}
1436
1437void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1438{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001439 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001440 fastboot_okay("");
1441 reboot_device(FASTBOOT_MODE);
1442}
1443
Shashank Mittal162244e2011-08-08 19:01:25 -07001444void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1445{
1446 if(!device.is_unlocked)
1447 {
1448 device.is_unlocked = 1;
1449 write_device_info(&device);
1450 }
1451 fastboot_okay("");
1452}
1453
Shashank Mittala0032282011-08-26 14:50:11 -07001454void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1455{
1456 char response[64];
1457 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1458 fastboot_info(response);
1459 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1460 fastboot_info(response);
1461 fastboot_okay("");
1462}
1463
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001464void splash_screen ()
1465{
1466 struct ptentry *ptn;
1467 struct ptable *ptable;
1468 struct fbcon_config *fb_display = NULL;
1469
1470 if (!target_is_emmc_boot())
1471 {
1472 ptable = flash_get_ptable();
1473 if (ptable == NULL) {
1474 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001475 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001476 }
1477
1478 ptn = ptable_find(ptable, "splash");
1479 if (ptn == NULL) {
1480 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1481 } else {
1482 fb_display = fbcon_display();
1483 if (fb_display) {
1484 if (flash_read(ptn, 0, fb_display->base,
1485 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1486 fbcon_clear();
1487 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1488 }
1489 }
1490 }
1491 }
1492}
1493
Brian Swetland9c4c0752009-01-25 16:23:50 -08001494void aboot_init(const struct app_descriptor *app)
1495{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001496 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001497 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001498 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001499
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001500 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001501 if (target_is_emmc_boot())
1502 {
1503 page_size = 2048;
1504 page_mask = page_size - 1;
1505 }
1506 else
1507 {
1508 page_size = flash_page_size();
1509 page_mask = page_size - 1;
1510 }
1511
Shashank Mittal162244e2011-08-08 19:01:25 -07001512 if(target_use_signed_kernel())
1513 {
1514 read_device_info(&device);
1515
Shashank Mittal162244e2011-08-08 19:01:25 -07001516 }
1517
Greg Griscod6250552011-06-29 14:40:23 -07001518 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001519 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001520 surf_udc_device.serialno = sn_buf;
1521
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001522 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001523 if (keys_get_state(KEY_HOME) != 0)
1524 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001525 if (keys_get_state(KEY_VOLUMEUP) != 0)
1526 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001527 if(!boot_into_recovery)
1528 {
1529 if (keys_get_state(KEY_BACK) != 0)
1530 goto fastboot;
1531 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1532 goto fastboot;
1533 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001534
1535 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001536 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001537 goto fastboot;
1538 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001539
Ajay Dudani77421292010-10-27 19:34:06 -07001540 reboot_mode = check_reboot_mode();
1541 if (reboot_mode == RECOVERY_MODE) {
1542 boot_into_recovery = 1;
1543 } else if(reboot_mode == FASTBOOT_MODE) {
1544 goto fastboot;
1545 }
1546
Shashank Mittal23b8f422010-04-16 19:27:21 -07001547 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001548 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001549 if(emmc_recovery_init())
1550 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001551 if(target_use_signed_kernel())
1552 {
1553 if((device.is_unlocked) || (device.is_tampered))
1554 {
1555 #ifdef TZ_TAMPER_FUSE
1556 set_tamper_fuse_cmd();
1557 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301558 #if USE_PCOM_SECBOOT
1559 set_tamper_flag(device.is_tampered);
1560 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001561 }
1562 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001563 boot_linux_from_mmc();
1564 }
1565 else
1566 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001567 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301568#if USE_PCOM_SECBOOT
1569 if((device.is_unlocked) || (device.is_tampered))
1570 set_tamper_flag(device.is_tampered);
1571#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001572 boot_linux_from_flash();
1573 }
Dima Zavinb4283602009-01-26 16:36:57 -08001574 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1575 "to fastboot mode.\n");
1576
1577fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001578
Shashank Mittal162244e2011-08-08 19:01:25 -07001579 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001580
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001581 if(!usb_init)
1582 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001583
1584 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001585
Shashank Mittal23b8f422010-04-16 19:27:21 -07001586 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001587 {
1588 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001589 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001590 }
1591 else
1592 {
1593 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001594 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001595 }
1596
1597 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001598 fastboot_register("reboot", cmd_reboot);
1599 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001600 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001601 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001602 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001603 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001604 fastboot_publish("serialno", sn_buf);
Kinson Chikf1a43512011-07-14 11:28:39 -07001605 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001606 sz = target_get_max_flash_size();
1607 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001608 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001609}
1610
1611APP_START(aboot)
1612 .init = aboot_init,
1613APP_END