blob: 5b0305278ba19cec86943048967c495e0bca1ce5 [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
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700380typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700381void boot_linux(void *kernel, unsigned *tags,
382 const char *cmdline, unsigned machtype,
383 void *ramdisk, unsigned ramdisk_size)
384{
Neeti Desai17379b82012-06-04 18:42:53 -0700385 int ret = 0;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700386 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800387 uint32_t tags_phys = PA((addr_t)tags);
388
389 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700390
Neeti Desai17379b82012-06-04 18:42:53 -0700391#if DEVICE_TREE
392 /* Update the Device Tree */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700393 ret = update_device_tree((void *)tags, cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700394 if(ret)
395 {
396 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
397 ASSERT(0);
398 }
399#else
Neeti Desaie245d492012-06-01 12:52:13 -0700400 /* Generating the Atags */
401 generate_atags(tags, cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700402#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700403
Brian Swetland9c4c0752009-01-25 16:23:50 -0800404 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
405 kernel, ramdisk, ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800406
407 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700408
Amol Jadi4421e652011-06-16 15:00:48 -0700409 /* do any platform specific cleanup before kernel entry */
410 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700411
Brian Swetland9c4c0752009-01-25 16:23:50 -0800412 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700413
Amol Jadi504f9fe2012-08-16 13:56:48 -0700414#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800415 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700416#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700417
418 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800419}
420
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800421unsigned page_size = 0;
422unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800423
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800424#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800425
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800426static unsigned char buf[4096]; //Equal to max-supported pagesize
Neeti Desai465491e2012-07-31 12:53:35 -0700427static unsigned char dt_buf[4096];
Dima Zavin214cc642009-01-26 11:16:21 -0800428
Shashank Mittal23b8f422010-04-16 19:27:21 -0700429int boot_linux_from_mmc(void)
430{
431 struct boot_img_hdr *hdr = (void*) buf;
432 struct boot_img_hdr *uhdr;
433 unsigned offset = 0;
434 unsigned long long ptn = 0;
435 unsigned n = 0;
436 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700437 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700438
Shashank Mittalcd98d472011-08-02 14:29:24 -0700439 unsigned char *image_addr = 0;
440 unsigned kernel_actual;
441 unsigned ramdisk_actual;
442 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700443 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700444
445#if DEVICE_TREE
446 struct dt_table *table;
447 struct dt_entry *dt_entry_ptr;
448 unsigned dt_table_offset;
449#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700450
Shashank Mittal23b8f422010-04-16 19:27:21 -0700451 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
452 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
453 dprintf(INFO, "Unified boot method!\n");
454 hdr = uhdr;
455 goto unified_boot;
456 }
Greg Griscod6250552011-06-29 14:40:23 -0700457 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700458 index = partition_get_index("boot");
459 ptn = partition_get_offset(index);
460 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700461 dprintf(CRITICAL, "ERROR: No boot partition found\n");
462 return -1;
463 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700464 }
465 else {
466 index = partition_get_index("recovery");
467 ptn = partition_get_offset(index);
468 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700469 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
470 return -1;
471 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700472 }
473
Greg Griscod6250552011-06-29 14:40:23 -0700474 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700475 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
476 return -1;
477 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700478
479 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700480 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700481 return -1;
482 }
483
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700484 if (hdr->page_size && (hdr->page_size != page_size)) {
485 page_size = hdr->page_size;
486 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700487 }
488
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700489 /* Get virtual addresses since the hdr saves physical addresses. */
490 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
491 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
492 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
493
Shashank Mittalcd98d472011-08-02 14:29:24 -0700494 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700495 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800496 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700497 image_addr = (unsigned char *)target_get_scratch_address();
498 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
499 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530500 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700501
502 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700503
504 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700505 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700506
Shashank Mittalcd98d472011-08-02 14:29:24 -0700507 /* Read image without signature */
508 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
509 {
510 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
511 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800512 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700513
514 offset = imagesize_actual;
515 /* Read signature */
516 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
517 {
518 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
519 }
520 else
521 {
522 auth_kernel_img = image_verify((unsigned char *)image_addr,
523 (unsigned char *)(image_addr + imagesize_actual),
524 imagesize_actual,
525 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700526
527 if(auth_kernel_img)
528 {
529 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700530 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700531 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700532 }
533
Neeti Desai465491e2012-07-31 12:53:35 -0700534 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700535 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
536 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700537
Neeti Desai465491e2012-07-31 12:53:35 -0700538 #if DEVICE_TREE
539 if(hdr->dt_size) {
540 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700541 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700542
543 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
544
545 /* Restriction that the device tree entry table should be less than a page*/
546 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
547
548 /* Validate the device tree table header */
549 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
550 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
551 return -1;
552 }
553
554 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700555 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700556 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
557 return -1;
558 }
559
560 /* Read device device tree in the "tags_add */
561 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
562 }
563 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700564 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700565 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700566 {
567 write_device_info_mmc(&device);
568 #ifdef TZ_TAMPER_FUSE
569 set_tamper_fuse_cmd();
570 #endif
571 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530572 #if USE_PCOM_SECBOOT
573 set_tamper_flag(device.is_tampered);
574 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700575 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700576 else
577 {
578 offset += page_size;
579
580 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
581 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
582 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
583 return -1;
584 }
585 offset += n;
586
587 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
588 if(n != 0)
589 {
590 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
591 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
592 return -1;
593 }
594 }
595 offset += n;
Neeti Desai465491e2012-07-31 12:53:35 -0700596
597 if(hdr->second_size != 0) {
598 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
599 offset += n;
600 }
601
602 #if DEVICE_TREE
603 if(hdr->dt_size != 0) {
604
605 /* Read the device tree table into buffer */
606 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
607 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
608 return -1;
609 }
610 table = (struct dt_table*) dt_buf;
611
612 /* Restriction that the device tree entry table should be less than a page*/
613 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
614
615 /* Validate the device tree table header */
616 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
617 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
618 return -1;
619 }
620
621 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700622 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700623 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
624 return -1;
625 }
626
627 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700628 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
629 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
630 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
631 return -1;
632 }
633 }
634 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700635 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700636
637unified_boot:
638 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
639 hdr->kernel_size);
640 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
641 hdr->ramdisk_size);
642
643 if(hdr->cmdline[0]) {
644 cmdline = (char*) hdr->cmdline;
645 } else {
646 cmdline = DEFAULT_CMDLINE;
647 }
648 dprintf(INFO, "cmdline = '%s'\n", cmdline);
649
650 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700651 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700652 (const char *)cmdline, board_machtype(),
653 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
654
655 return 0;
656}
657
Dima Zavin214cc642009-01-26 11:16:21 -0800658int boot_linux_from_flash(void)
659{
660 struct boot_img_hdr *hdr = (void*) buf;
661 unsigned n;
662 struct ptentry *ptn;
663 struct ptable *ptable;
664 unsigned offset = 0;
665 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800666
Shashank Mittalcd98d472011-08-02 14:29:24 -0700667 unsigned char *image_addr = 0;
668 unsigned kernel_actual;
669 unsigned ramdisk_actual;
670 unsigned imagesize_actual;
671
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700672#if DEVICE_TREE
673 struct dt_table *table;
674 struct dt_entry *dt_entry_ptr;
675#endif
676
677
David Ng183a7422009-12-07 14:55:21 -0800678 if (target_is_emmc_boot()) {
679 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
680 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
681 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
682 return -1;
683 }
684 goto continue_boot;
685 }
686
Dima Zavin214cc642009-01-26 11:16:21 -0800687 ptable = flash_get_ptable();
688 if (ptable == NULL) {
689 dprintf(CRITICAL, "ERROR: Partition table not found\n");
690 return -1;
691 }
692
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800693 if(!boot_into_recovery)
694 {
695 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700696
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800697 if (ptn == NULL) {
698 dprintf(CRITICAL, "ERROR: No boot partition found\n");
699 return -1;
700 }
701 }
702 else
703 {
704 ptn = ptable_find(ptable, "recovery");
705 if (ptn == NULL) {
706 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
707 return -1;
708 }
Dima Zavin214cc642009-01-26 11:16:21 -0800709 }
710
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800711 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800712 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
713 return -1;
714 }
Dima Zavin214cc642009-01-26 11:16:21 -0800715
716 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700717 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800718 return -1;
719 }
720
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800721 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700722 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 -0800723 return -1;
724 }
725
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700726 /* Get virtual addresses since the hdr saves physical addresses. */
727 hdr->kernel_addr = VA(hdr->kernel_addr);
728 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
729 hdr->tags_addr = VA(hdr->tags_addr);
730
Shashank Mittalcd98d472011-08-02 14:29:24 -0700731 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700732 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700733 {
734 image_addr = (unsigned char *)target_get_scratch_address();
735 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
736 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
737 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800738
Shashank Mittalcd98d472011-08-02 14:29:24 -0700739 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700740
741 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700742 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700743
Shashank Mittalcd98d472011-08-02 14:29:24 -0700744 /* Read image without signature */
745 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
746 {
747 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
748 return -1;
749 }
Dima Zavin214cc642009-01-26 11:16:21 -0800750
Shashank Mittalcd98d472011-08-02 14:29:24 -0700751 offset = imagesize_actual;
752 /* Read signature */
753 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
754 {
755 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
756 }
757 else
758 {
759
760 /* Verify signature */
761 auth_kernel_img = image_verify((unsigned char *)image_addr,
762 (unsigned char *)(image_addr + imagesize_actual),
763 imagesize_actual,
764 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700765
766 if(auth_kernel_img)
767 {
768 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700769 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700770 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700771 }
772
773 /* Move kernel and ramdisk to correct address */
774 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
775 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700776
777 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700778 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700779 {
780 write_device_info_flash(&device);
781 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530782#if USE_PCOM_SECBOOT
783 set_tamper_flag(device.is_tampered);
784#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700785 }
786 else
787 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700788 offset = page_size;
789
Shashank Mittalcd98d472011-08-02 14:29:24 -0700790 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
791 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
792 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
793 return -1;
794 }
795 offset += n;
796
797 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
798 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
799 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
800 return -1;
801 }
802 offset += n;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700803
804 if(hdr->second_size != 0) {
805 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
806 offset += n;
807 }
808
809#if DEVICE_TREE
810 if(hdr->dt_size != 0) {
811
812 /* Read the device tree table into buffer */
813 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
814 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
815 return -1;
816 }
817
818 table = (struct dt_table*) dt_buf;
819
820 /* Restriction that the device tree entry table should be less than a page*/
821 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
822
823 /* Validate the device tree table header */
824 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
825 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
826 return -1;
827 }
828
829 /* Calculate the offset of device tree within device tree table */
830 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
831 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
832 return -1;
833 }
834
835 /* Read device device tree in the "tags_add */
836 if(flash_read(ptn, offset + dt_entry_ptr->offset,
837 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
838 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
839 return -1;
840 }
841 }
842#endif
843
Shashank Mittalcd98d472011-08-02 14:29:24 -0700844 }
David Ng183a7422009-12-07 14:55:21 -0800845continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800846 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
847 hdr->kernel_size);
848 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
849 hdr->ramdisk_size);
850
851 if(hdr->cmdline[0]) {
852 cmdline = (char*) hdr->cmdline;
853 } else {
854 cmdline = DEFAULT_CMDLINE;
855 }
856 dprintf(INFO, "cmdline = '%s'\n", cmdline);
857
858 /* TODO: create/pass atags to kernel */
859
860 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700861 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800862 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800863 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
864
865 return 0;
866}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800867
Shashank Mittal162244e2011-08-08 19:01:25 -0700868unsigned char info_buf[4096];
869void write_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 memcpy(info, dev, sizeof(device_info));
886
887 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
888 {
889 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
890 return;
891 }
892}
893
894void read_device_info_mmc(device_info *dev)
895{
896 struct device_info *info = (void*) info_buf;
897 unsigned long long ptn = 0;
898 unsigned long long size;
899 int index = INVALID_PTN;
900
901 index = partition_get_index("aboot");
902 ptn = partition_get_offset(index);
903 if(ptn == 0)
904 {
905 return;
906 }
907
908 size = partition_get_size(index);
909
910 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
911 {
912 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
913 return;
914 }
915
916 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
917 {
918 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
919 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700920 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700921
922 write_device_info_mmc(info);
923 }
924 memcpy(dev, info, sizeof(device_info));
925}
926
927void write_device_info_flash(device_info *dev)
928{
929 struct device_info *info = (void *) info_buf;
930 struct ptentry *ptn;
931 struct ptable *ptable;
932
933 ptable = flash_get_ptable();
934 if (ptable == NULL)
935 {
936 dprintf(CRITICAL, "ERROR: Partition table not found\n");
937 return;
938 }
939
940 ptn = ptable_find(ptable, "devinfo");
941 if (ptn == NULL)
942 {
943 dprintf(CRITICAL, "ERROR: No boot partition found\n");
944 return;
945 }
946
947 memcpy(info, dev, sizeof(device_info));
948
949 if (flash_write(ptn, 0, (void *)info_buf, page_size))
950 {
951 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
952 return;
953 }
954}
955
956void read_device_info_flash(device_info *dev)
957{
958 struct device_info *info = (void*) info_buf;
959 struct ptentry *ptn;
960 struct ptable *ptable;
961
962 ptable = flash_get_ptable();
963 if (ptable == NULL)
964 {
965 dprintf(CRITICAL, "ERROR: Partition table not found\n");
966 return;
967 }
968
969 ptn = ptable_find(ptable, "devinfo");
970 if (ptn == NULL)
971 {
972 dprintf(CRITICAL, "ERROR: No boot partition found\n");
973 return;
974 }
975
976 if (flash_read(ptn, 0, (void *)info_buf, page_size))
977 {
978 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
979 return;
980 }
981
982 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
983 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700984 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
985 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700986 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700987 write_device_info_flash(info);
988 }
989 memcpy(dev, info, sizeof(device_info));
990}
991
992void write_device_info(device_info *dev)
993{
994 if(target_is_emmc_boot())
995 {
996 write_device_info_mmc(dev);
997 }
998 else
999 {
1000 write_device_info_flash(dev);
1001 }
1002}
1003
1004void read_device_info(device_info *dev)
1005{
1006 if(target_is_emmc_boot())
1007 {
1008 read_device_info_mmc(dev);
1009 }
1010 else
1011 {
1012 read_device_info_flash(dev);
1013 }
1014}
1015
1016void reset_device_info()
1017{
1018 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001019 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001020 write_device_info(&device);
1021}
1022
1023void set_device_root()
1024{
1025 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001026 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001027 write_device_info(&device);
1028}
1029
Amol Jadicb524072012-08-09 16:40:18 -07001030#if DEVICE_TREE
1031int copy_dtb(uint8_t *boot_image_start)
1032{
1033 uint32 dt_image_offset = 0;
1034 uint32_t n;
1035 struct dt_table *table;
1036 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001037
1038 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1039
Amol Jadicb524072012-08-09 16:40:18 -07001040 if(hdr->dt_size != 0) {
1041
1042 /* add kernel offset */
1043 dt_image_offset += page_size;
1044 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1045 dt_image_offset += n;
1046
1047 /* add ramdisk offset */
1048 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1049 dt_image_offset += n;
1050
1051 /* add second offset */
1052 if(hdr->second_size != 0) {
1053 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1054 dt_image_offset += n;
1055 }
1056
1057 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001058 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001059
1060 /* Restriction that the device tree entry table should be less than a page*/
1061 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1062
1063 /* Validate the device tree table header */
1064 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1065 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1066 return -1;
1067 }
1068
1069 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001070 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001071 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1072 return -1;
1073 }
1074
1075 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001076 memmove((void*) hdr->tags_addr,
1077 boot_image_start + dt_image_offset + dt_entry_ptr->offset,
1078 dt_entry_ptr->size);
Amol Jadicb524072012-08-09 16:40:18 -07001079 }
1080
1081 /* Everything looks fine. Return success. */
1082 return 0;
1083}
1084#endif
1085
Brian Swetland9c4c0752009-01-25 16:23:50 -08001086void cmd_boot(const char *arg, void *data, unsigned sz)
1087{
1088 unsigned kernel_actual;
1089 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001090 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001091 char *ptr = ((char*) data);
1092
1093 if (sz < sizeof(hdr)) {
1094 fastboot_fail("invalid bootimage header");
1095 return;
1096 }
1097
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001098 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001099
1100 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001101 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001102
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001103 if(target_is_emmc_boot() && hdr->page_size) {
1104 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001105 page_mask = page_size - 1;
1106 }
1107
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001108 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1109 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1110
1111 /* Get virtual addresses since the hdr saves physical addresses. */
1112 hdr->kernel_addr = VA(hdr->kernel_addr);
1113 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1114 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001115
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001116 /* sz should have atleast raw boot image */
1117 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001118 fastboot_fail("incomplete bootimage");
1119 return;
1120 }
1121
Amol Jadicb524072012-08-09 16:40:18 -07001122#if DEVICE_TREE
1123 /* find correct dtb and copy it to right location */
1124 if(copy_dtb(data))
1125 {
1126 fastboot_fail("dtb not found");
1127 return;
1128 }
1129#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001130
1131 fastboot_okay("");
1132 udc_stop();
1133
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001134 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1135 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
Amol Jadicb524072012-08-09 16:40:18 -07001136
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001137 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
1138 (const char*) hdr->cmdline, board_machtype(),
1139 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001140}
1141
Dima Zavin214cc642009-01-26 11:16:21 -08001142void cmd_erase(const char *arg, void *data, unsigned sz)
1143{
1144 struct ptentry *ptn;
1145 struct ptable *ptable;
1146
1147 ptable = flash_get_ptable();
1148 if (ptable == NULL) {
1149 fastboot_fail("partition table doesn't exist");
1150 return;
1151 }
1152
1153 ptn = ptable_find(ptable, arg);
1154 if (ptn == NULL) {
1155 fastboot_fail("unknown partition name");
1156 return;
1157 }
1158
1159 if (flash_erase(ptn)) {
1160 fastboot_fail("failed to erase partition");
1161 return;
1162 }
1163 fastboot_okay("");
1164}
1165
Bikas Gurungd48bd242010-09-04 19:54:32 -07001166
1167void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1168{
1169 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -08001170 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -07001171 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001172
Kinson Chikf1a43512011-07-14 11:28:39 -07001173 index = partition_get_index(arg);
1174 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001175
Kinson Chikf1a43512011-07-14 11:28:39 -07001176 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001177 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001178 return;
1179 }
neetidb4b24d62012-01-20 12:13:09 -08001180 /* Simple inefficient version of erase. Just writing
1181 0 in first block */
1182 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1183 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001184 return;
1185 }
1186 fastboot_okay("");
1187}
1188
1189
Ajay Dudani5c761132011-04-07 20:19:04 -07001190void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001191{
1192 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001193 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001194 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001195
Greg Grisco6e754772011-06-23 12:19:39 -07001196 if (!strcmp(arg, "partition"))
1197 {
1198 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001199 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001200 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001201 return;
1202 }
1203 }
Greg Grisco6e754772011-06-23 12:19:39 -07001204 else
1205 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001206 index = partition_get_index(arg);
1207 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001208 if(ptn == 0) {
1209 fastboot_fail("partition table doesn't exist");
1210 return;
1211 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001212
Greg Grisco6e754772011-06-23 12:19:39 -07001213 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1214 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1215 fastboot_fail("image is not a boot image");
1216 return;
1217 }
1218 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001219
Kinson Chikf1a43512011-07-14 11:28:39 -07001220 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001221 if (ROUND_TO_PAGE(sz,511) > size) {
1222 fastboot_fail("size too large");
1223 return;
1224 }
1225 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1226 fastboot_fail("flash write failure");
1227 return;
1228 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001229 }
1230 fastboot_okay("");
1231 return;
1232}
1233
Ajay Dudani5c761132011-04-07 20:19:04 -07001234void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1235{
1236 unsigned int chunk;
1237 unsigned int chunk_data_sz;
1238 sparse_header_t *sparse_header;
1239 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001240 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001241 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301242 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001243 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001244
Kinson Chikf1a43512011-07-14 11:28:39 -07001245 index = partition_get_index(arg);
1246 ptn = partition_get_offset(index);
1247 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001248 fastboot_fail("partition table doesn't exist");
1249 return;
1250 }
1251
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301252 size = partition_get_size(index);
1253 if (ROUND_TO_PAGE(sz,511) > size) {
1254 fastboot_fail("size too large");
1255 return;
1256 }
1257
Ajay Dudani5c761132011-04-07 20:19:04 -07001258 /* Read and skip over sparse image header */
1259 sparse_header = (sparse_header_t *) data;
1260 data += sparse_header->file_hdr_sz;
1261 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1262 {
1263 /* Skip the remaining bytes in a header that is longer than
1264 * we expected.
1265 */
1266 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1267 }
1268
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001269 dprintf (SPEW, "=== Sparse Image Header ===\n");
1270 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1271 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1272 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1273 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1274 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1275 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1276 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1277 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001278
1279 /* Start processing chunks */
1280 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1281 {
1282 /* Read and skip over chunk header */
1283 chunk_header = (chunk_header_t *) data;
1284 data += sizeof(chunk_header_t);
1285
1286 dprintf (SPEW, "=== Chunk Header ===\n");
1287 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1288 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1289 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1290
1291 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1292 {
1293 /* Skip the remaining bytes in a header that is longer than
1294 * we expected.
1295 */
1296 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1297 }
1298
1299 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1300 switch (chunk_header->chunk_type)
1301 {
1302 case CHUNK_TYPE_RAW:
1303 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1304 chunk_data_sz))
1305 {
1306 fastboot_fail("Bogus chunk size for chunk type Raw");
1307 return;
1308 }
1309
Ajay Dudaniab18f022011-05-12 14:39:22 -07001310 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1311 chunk_data_sz,
1312 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001313 {
1314 fastboot_fail("flash write failure");
1315 return;
1316 }
1317 total_blocks += chunk_header->chunk_sz;
1318 data += chunk_data_sz;
1319 break;
1320
1321 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001322 total_blocks += chunk_header->chunk_sz;
1323 break;
1324
Ajay Dudani5c761132011-04-07 20:19:04 -07001325 case CHUNK_TYPE_CRC:
1326 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1327 {
1328 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1329 return;
1330 }
1331 total_blocks += chunk_header->chunk_sz;
1332 data += chunk_data_sz;
1333 break;
1334
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001335 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001336 fastboot_fail("Unknown chunk type");
1337 return;
1338 }
1339 }
1340
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001341 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1342 total_blocks, sparse_header->total_blks);
1343
1344 if(total_blocks != sparse_header->total_blks)
1345 {
1346 fastboot_fail("sparse image write failure");
1347 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001348
1349 fastboot_okay("");
1350 return;
1351}
1352
1353void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1354{
1355 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001356 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1357 unsigned int *magic_number = (unsigned int *) data;
1358 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001359
Neeti Desai127b9e02012-03-20 16:11:23 -07001360 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1361 magic_number[1] == DECRYPT_MAGIC_1)
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001362 {
1363#ifdef SSD_ENABLE
Neeti Desai127b9e02012-03-20 16:11:23 -07001364 ret = decrypt_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001365#endif
Greg Griscod6250552011-06-29 14:40:23 -07001366 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001367 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1368 return;
1369 }
1370 }
Neeti Desai127b9e02012-03-20 16:11:23 -07001371 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1372 magic_number[1] == ENCRYPT_MAGIC_1)
1373 {
1374#ifdef SSD_ENABLE
1375 ret = encrypt_scm((uint32 **) &data, &sz);
1376#endif
1377 if (ret != 0) {
1378 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1379 return;
1380 }
1381 }
1382
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001383 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001384 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1385 cmd_flash_mmc_img(arg, data, sz);
1386 else
1387 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001388 return;
1389}
1390
Dima Zavin214cc642009-01-26 11:16:21 -08001391void cmd_flash(const char *arg, void *data, unsigned sz)
1392{
1393 struct ptentry *ptn;
1394 struct ptable *ptable;
1395 unsigned extra = 0;
1396
1397 ptable = flash_get_ptable();
1398 if (ptable == NULL) {
1399 fastboot_fail("partition table doesn't exist");
1400 return;
1401 }
1402
1403 ptn = ptable_find(ptable, arg);
1404 if (ptn == NULL) {
1405 fastboot_fail("unknown partition name");
1406 return;
1407 }
1408
1409 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1410 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1411 fastboot_fail("image is not a boot image");
1412 return;
1413 }
1414 }
1415
Amol Jadi5c61a952012-05-04 17:05:35 -07001416 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001417 || !strcmp(ptn->name, "userdata")
1418 || !strcmp(ptn->name, "persist")
1419 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301420 if (flash_ecc_bch_enabled())
1421 /* Spare data bytes for 8 bit ECC increased by 4 */
1422 extra = ((page_size >> 9) * 20);
1423 else
1424 extra = ((page_size >> 9) * 16);
1425 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001426 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001427
1428 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1429 if (flash_write(ptn, extra, data, sz)) {
1430 fastboot_fail("flash write failure");
1431 return;
1432 }
1433 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1434 fastboot_okay("");
1435}
1436
1437void cmd_continue(const char *arg, void *data, unsigned sz)
1438{
1439 fastboot_okay("");
1440 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001441 if (target_is_emmc_boot())
1442 {
1443 boot_linux_from_mmc();
1444 }
1445 else
1446 {
1447 boot_linux_from_flash();
1448 }
Dima Zavin214cc642009-01-26 11:16:21 -08001449}
1450
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001451void cmd_reboot(const char *arg, void *data, unsigned sz)
1452{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001453 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001454 fastboot_okay("");
1455 reboot_device(0);
1456}
1457
1458void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1459{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001460 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001461 fastboot_okay("");
1462 reboot_device(FASTBOOT_MODE);
1463}
1464
Shashank Mittal162244e2011-08-08 19:01:25 -07001465void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1466{
1467 if(!device.is_unlocked)
1468 {
1469 device.is_unlocked = 1;
1470 write_device_info(&device);
1471 }
1472 fastboot_okay("");
1473}
1474
Shashank Mittala0032282011-08-26 14:50:11 -07001475void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1476{
1477 char response[64];
1478 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1479 fastboot_info(response);
1480 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1481 fastboot_info(response);
1482 fastboot_okay("");
1483}
1484
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001485void splash_screen ()
1486{
1487 struct ptentry *ptn;
1488 struct ptable *ptable;
1489 struct fbcon_config *fb_display = NULL;
1490
1491 if (!target_is_emmc_boot())
1492 {
1493 ptable = flash_get_ptable();
1494 if (ptable == NULL) {
1495 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001496 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001497 }
1498
1499 ptn = ptable_find(ptable, "splash");
1500 if (ptn == NULL) {
1501 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1502 } else {
1503 fb_display = fbcon_display();
1504 if (fb_display) {
1505 if (flash_read(ptn, 0, fb_display->base,
1506 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1507 fbcon_clear();
1508 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1509 }
1510 }
1511 }
1512 }
1513}
1514
Brian Swetland9c4c0752009-01-25 16:23:50 -08001515void aboot_init(const struct app_descriptor *app)
1516{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001517 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001518 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001519 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001520
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001521 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001522 if (target_is_emmc_boot())
1523 {
1524 page_size = 2048;
1525 page_mask = page_size - 1;
1526 }
1527 else
1528 {
1529 page_size = flash_page_size();
1530 page_mask = page_size - 1;
1531 }
1532
Shashank Mittal162244e2011-08-08 19:01:25 -07001533 if(target_use_signed_kernel())
1534 {
1535 read_device_info(&device);
1536
Shashank Mittal162244e2011-08-08 19:01:25 -07001537 }
1538
Greg Griscod6250552011-06-29 14:40:23 -07001539 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001540 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001541 surf_udc_device.serialno = sn_buf;
1542
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001543 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001544 if (keys_get_state(KEY_HOME) != 0)
1545 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001546 if (keys_get_state(KEY_VOLUMEUP) != 0)
1547 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001548 if(!boot_into_recovery)
1549 {
1550 if (keys_get_state(KEY_BACK) != 0)
1551 goto fastboot;
1552 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1553 goto fastboot;
1554 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001555
1556 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001557 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001558 goto fastboot;
1559 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001560
Ajay Dudani77421292010-10-27 19:34:06 -07001561 reboot_mode = check_reboot_mode();
1562 if (reboot_mode == RECOVERY_MODE) {
1563 boot_into_recovery = 1;
1564 } else if(reboot_mode == FASTBOOT_MODE) {
1565 goto fastboot;
1566 }
1567
Shashank Mittal23b8f422010-04-16 19:27:21 -07001568 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001569 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001570 if(emmc_recovery_init())
1571 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001572 if(target_use_signed_kernel())
1573 {
1574 if((device.is_unlocked) || (device.is_tampered))
1575 {
1576 #ifdef TZ_TAMPER_FUSE
1577 set_tamper_fuse_cmd();
1578 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301579 #if USE_PCOM_SECBOOT
1580 set_tamper_flag(device.is_tampered);
1581 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001582 }
1583 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001584 boot_linux_from_mmc();
1585 }
1586 else
1587 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001588 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301589#if USE_PCOM_SECBOOT
1590 if((device.is_unlocked) || (device.is_tampered))
1591 set_tamper_flag(device.is_tampered);
1592#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001593 boot_linux_from_flash();
1594 }
Dima Zavinb4283602009-01-26 16:36:57 -08001595 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1596 "to fastboot mode.\n");
1597
1598fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001599
Shashank Mittal162244e2011-08-08 19:01:25 -07001600 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001601
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001602 if(!usb_init)
1603 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001604
1605 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001606
Shashank Mittal23b8f422010-04-16 19:27:21 -07001607 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001608 {
1609 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001610 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001611 }
1612 else
1613 {
1614 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001615 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001616 }
1617
1618 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001619 fastboot_register("reboot", cmd_reboot);
1620 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001621 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001622 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001623 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001624 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001625 fastboot_publish("serialno", sn_buf);
Kinson Chikf1a43512011-07-14 11:28:39 -07001626 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001627 sz = target_get_max_flash_size();
1628 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001629 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001630}
1631
1632APP_START(aboot)
1633 .init = aboot_init,
1634APP_END