blob: 701095af57b5730316bc011234540040178228e1 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07005 * Copyright (c) 2009-2012, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
36#include <dev/udc.h>
37#include <string.h>
38#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070051#include <malloc.h>
Dima Zavin214cc642009-01-26 11:16:21 -080052
Neeti Desai17379b82012-06-04 18:42:53 -070053#if DEVICE_TREE
54#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070055#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070056#endif
57
Shashank Mittalcd98d472011-08-02 14:29:24 -070058#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080059#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080060#include "bootimg.h"
61#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070062#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070063#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070064#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070065#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080066
Shashank Mittal162244e2011-08-08 19:01:25 -070067#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070068
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070069extern bool target_use_signed_kernel(void);
70extern void dsb();
71extern void isb();
72extern void platform_uninit(void);
73
74void write_device_info_mmc(device_info *dev);
75void write_device_info_flash(device_info *dev);
76
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070077#define EXPAND(NAME) #NAME
78#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080079#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070080
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080081#ifdef MEMBASE
82#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
83#else
David Ng183a7422009-12-07 14:55:21 -080084#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080085#endif
86
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080087#define RECOVERY_MODE 0x77665502
88#define FASTBOOT_MODE 0x77665500
89
David Ng183a7422009-12-07 14:55:21 -080090static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080091static const char *usb_sn_cmdline = " androidboot.serialno=";
Ajay Dudanica3a33c2011-11-18 08:31:40 -080092static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -070093static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -080094
Ajay Dudani6cff85e2011-02-04 16:02:16 -080095static const char *baseband_apq = " androidboot.baseband=apq";
96static const char *baseband_msm = " androidboot.baseband=msm";
97static const char *baseband_csfb = " androidboot.baseband=csfb";
98static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -070099static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700100static const char *baseband_sglte = " androidboot.baseband=sglte";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800101
Shashank Mittalcd98d472011-08-02 14:29:24 -0700102/* Assuming unauthorized kernel image by default */
103static int auth_kernel_img = 0;
104
Shashank Mittal162244e2011-08-08 19:01:25 -0700105static device_info device = {DEVICE_MAGIC, 0, 0};
106
Brian Swetland9c4c0752009-01-25 16:23:50 -0800107static struct udc_device surf_udc_device = {
108 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800109 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800110 .version_id = 0x0100,
111 .manufacturer = "Google",
112 .product = "Android",
113};
114
Dima Zavin42168f22009-01-30 11:52:22 -0800115struct atag_ptbl_entry
116{
117 char name[16];
118 unsigned offset;
119 unsigned size;
120 unsigned flags;
121};
122
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800123char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700124
Greg Griscod2471ef2011-07-14 13:00:42 -0700125extern int emmc_recovery_init(void);
126
Kinson Chik0b1c8162011-08-31 16:31:57 -0700127#if NO_KEYPAD_DRIVER
128extern int fastboot_trigger(void);
129#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700130
Dima Zavin42168f22009-01-30 11:52:22 -0800131static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
132{
133 struct atag_ptbl_entry atag_ptn;
134
135 memcpy(atag_ptn.name, ptn->name, 16);
136 atag_ptn.name[15] = '\0';
137 atag_ptn.offset = ptn->start;
138 atag_ptn.size = ptn->length;
139 atag_ptn.flags = ptn->flags;
140 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
141 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
142}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800143
Neeti Desaie245d492012-06-01 12:52:13 -0700144unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800145{
David Ng183a7422009-12-07 14:55:21 -0800146 int cmdline_len = 0;
147 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800148 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700149 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800150
Brian Swetland9c4c0752009-01-25 16:23:50 -0800151 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800152 cmdline_len = strlen(cmdline);
153 have_cmdline = 1;
154 }
155 if (target_is_emmc_boot()) {
156 cmdline_len += strlen(emmc_cmdline);
157 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800158
159 cmdline_len += strlen(usb_sn_cmdline);
160 cmdline_len += strlen(sn_buf);
161
David Ngf773dde2010-07-26 19:55:08 -0700162 if (target_pause_for_battery_charge()) {
163 pause_at_bootup = 1;
164 cmdline_len += strlen(battchg_pause);
165 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800166
Shashank Mittalcd98d472011-08-02 14:29:24 -0700167 if(target_use_signed_kernel() && auth_kernel_img) {
168 cmdline_len += strlen(auth_kernel);
169 }
170
Ajay Dudanid04110c2011-01-17 23:55:07 -0800171 /* Determine correct androidboot.baseband to use */
172 switch(target_baseband())
173 {
174 case BASEBAND_APQ:
175 cmdline_len += strlen(baseband_apq);
176 break;
177
178 case BASEBAND_MSM:
179 cmdline_len += strlen(baseband_msm);
180 break;
181
182 case BASEBAND_CSFB:
183 cmdline_len += strlen(baseband_csfb);
184 break;
185
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800186 case BASEBAND_SVLTE2A:
187 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800188 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700189
190 case BASEBAND_MDM:
191 cmdline_len += strlen(baseband_mdm);
192 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700193
194 case BASEBAND_SGLTE:
195 cmdline_len += strlen(baseband_sglte);
196 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800197 }
198
David Ng183a7422009-12-07 14:55:21 -0800199 if (cmdline_len > 0) {
200 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700201 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
202 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700203
Amol Jadi168b7712012-03-06 16:15:00 -0800204 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700205 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800206 if (have_cmdline) {
207 src = cmdline;
208 while ((*dst++ = *src++));
209 }
210 if (target_is_emmc_boot()) {
211 src = emmc_cmdline;
212 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700213 have_cmdline = 1;
214 while ((*dst++ = *src++));
215 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800216
217 src = usb_sn_cmdline;
218 if (have_cmdline) --dst;
219 have_cmdline = 1;
220 while ((*dst++ = *src++));
221 src = sn_buf;
222 if (have_cmdline) --dst;
223 have_cmdline = 1;
224 while ((*dst++ = *src++));
225
David Ngf773dde2010-07-26 19:55:08 -0700226 if (pause_at_bootup) {
227 src = battchg_pause;
228 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800229 while ((*dst++ = *src++));
230 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800231
Shashank Mittalcd98d472011-08-02 14:29:24 -0700232 if(target_use_signed_kernel() && auth_kernel_img) {
233 src = auth_kernel;
234 if (have_cmdline) --dst;
235 while ((*dst++ = *src++));
236 }
237
Ajay Dudanid04110c2011-01-17 23:55:07 -0800238 switch(target_baseband())
239 {
240 case BASEBAND_APQ:
241 src = baseband_apq;
242 if (have_cmdline) --dst;
243 while ((*dst++ = *src++));
244 break;
245
246 case BASEBAND_MSM:
247 src = baseband_msm;
248 if (have_cmdline) --dst;
249 while ((*dst++ = *src++));
250 break;
251
252 case BASEBAND_CSFB:
253 src = baseband_csfb;
254 if (have_cmdline) --dst;
255 while ((*dst++ = *src++));
256 break;
257
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800258 case BASEBAND_SVLTE2A:
259 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800260 if (have_cmdline) --dst;
261 while ((*dst++ = *src++));
262 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700263
264 case BASEBAND_MDM:
265 src = baseband_mdm;
266 if (have_cmdline) --dst;
267 while ((*dst++ = *src++));
268 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700269
270 case BASEBAND_SGLTE:
271 src = baseband_sglte;
272 if (have_cmdline) --dst;
273 while ((*dst++ = *src++));
274 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800275 }
Neeti Desaie245d492012-06-01 12:52:13 -0700276 }
277 return cmdline_final;
278}
279
280unsigned *atag_core(unsigned *ptr)
281{
282 /* CORE */
283 *ptr++ = 2;
284 *ptr++ = 0x54410001;
285
286 return ptr;
287
288}
289
290unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
291 unsigned ramdisk_size)
292{
293 if (ramdisk_size) {
294 *ptr++ = 4;
295 *ptr++ = 0x54420005;
296 *ptr++ = (unsigned)ramdisk;
297 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800298 }
299
Neeti Desaie245d492012-06-01 12:52:13 -0700300 return ptr;
301}
302
303unsigned *atag_ptable(unsigned **ptr_addr)
304{
305 int i;
306 struct ptable *ptable;
307
308 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700309 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
310 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700311 *(*ptr_addr)++ = 0x4d534d70;
312 for (i = 0; i < ptable->count; ++i)
313 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
314 }
315
316 return (*ptr_addr);
317}
318
319unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
320{
321 int cmdline_length = 0;
322 int n;
323 unsigned char *cmdline_final = NULL;
324 char *dest;
325
326 cmdline_final = update_cmdline(cmdline);
327 if (cmdline_final){
328 dprintf(INFO, "cmdline: %s\n", cmdline_final);
329 }
330
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700331 cmdline_length =strlen((const char*)cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700332 n = (cmdline_length + 4) & (~3);
333
334 *ptr++ = (n / 4) + 2;
335 *ptr++ = 0x54410009;
336 dest = (char *) ptr;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700337 while ((*dest++ = *cmdline_final++));
Neeti Desaie245d492012-06-01 12:52:13 -0700338 ptr += (n / 4);
339
340 return ptr;
341}
342
343unsigned *atag_end(unsigned *ptr)
344{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800345 /* END */
346 *ptr++ = 0;
347 *ptr++ = 0;
348
Neeti Desaie245d492012-06-01 12:52:13 -0700349 return ptr;
350}
351
352void generate_atags(unsigned *ptr, const char *cmdline,
353 void *ramdisk, unsigned ramdisk_size)
354{
355
356 ptr = atag_core(ptr);
357 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
358 ptr = target_atag_mem(ptr);
359
360 /* Skip NAND partition ATAGS for eMMC boot */
361 if (!target_is_emmc_boot()){
362 ptr = atag_ptable(&ptr);
363 }
364
365 ptr = atag_cmdline(ptr, cmdline);
366 ptr = atag_end(ptr);
367}
368
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700369typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700370void boot_linux(void *kernel, unsigned *tags,
371 const char *cmdline, unsigned machtype,
372 void *ramdisk, unsigned ramdisk_size)
373{
Neeti Desai17379b82012-06-04 18:42:53 -0700374 int ret = 0;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700375 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
376 uint32_t tags_phys = platform_get_virt_to_phys_mapping((addr_t)tags);
Neeti Desaie245d492012-06-01 12:52:13 -0700377
Neeti Desai17379b82012-06-04 18:42:53 -0700378#if DEVICE_TREE
379 /* Update the Device Tree */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700380 ret = update_device_tree((void *)tags, cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700381 if(ret)
382 {
383 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
384 ASSERT(0);
385 }
386#else
Neeti Desaie245d492012-06-01 12:52:13 -0700387 /* Generating the Atags */
388 generate_atags(tags, cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700389#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700390
Brian Swetland9c4c0752009-01-25 16:23:50 -0800391 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
392 kernel, ramdisk, ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800393
394 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700395
Amol Jadi4421e652011-06-16 15:00:48 -0700396 /* do any platform specific cleanup before kernel entry */
397 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700398
Brian Swetland9c4c0752009-01-25 16:23:50 -0800399 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700400
Amol Jadi504f9fe2012-08-16 13:56:48 -0700401#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800402 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700403#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700404
405 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800406}
407
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800408unsigned page_size = 0;
409unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800410
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800411#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800412
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800413static unsigned char buf[4096]; //Equal to max-supported pagesize
Neeti Desai465491e2012-07-31 12:53:35 -0700414static unsigned char dt_buf[4096];
Dima Zavin214cc642009-01-26 11:16:21 -0800415
Shashank Mittal23b8f422010-04-16 19:27:21 -0700416int boot_linux_from_mmc(void)
417{
418 struct boot_img_hdr *hdr = (void*) buf;
419 struct boot_img_hdr *uhdr;
420 unsigned offset = 0;
421 unsigned long long ptn = 0;
422 unsigned n = 0;
423 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700424 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700425
Shashank Mittalcd98d472011-08-02 14:29:24 -0700426 unsigned char *image_addr = 0;
427 unsigned kernel_actual;
428 unsigned ramdisk_actual;
429 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700430 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700431
432#if DEVICE_TREE
433 struct dt_table *table;
434 struct dt_entry *dt_entry_ptr;
435 unsigned dt_table_offset;
436#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700437
Shashank Mittal23b8f422010-04-16 19:27:21 -0700438 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
439 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
440 dprintf(INFO, "Unified boot method!\n");
441 hdr = uhdr;
442 goto unified_boot;
443 }
Greg Griscod6250552011-06-29 14:40:23 -0700444 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700445 index = partition_get_index("boot");
446 ptn = partition_get_offset(index);
447 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700448 dprintf(CRITICAL, "ERROR: No boot partition found\n");
449 return -1;
450 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700451 }
452 else {
453 index = partition_get_index("recovery");
454 ptn = partition_get_offset(index);
455 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700456 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
457 return -1;
458 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700459 }
460
Greg Griscod6250552011-06-29 14:40:23 -0700461 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700462 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
463 return -1;
464 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700465
466 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700467 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700468 return -1;
469 }
470
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700471 if (hdr->page_size && (hdr->page_size != page_size)) {
472 page_size = hdr->page_size;
473 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700474 }
475
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700476 /* Get virtual addresses since the hdr saves physical addresses. */
477 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
478 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
479 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
480
Shashank Mittalcd98d472011-08-02 14:29:24 -0700481 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700482 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800483 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700484 image_addr = (unsigned char *)target_get_scratch_address();
485 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
486 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530487 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700488
489 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700490
491 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700492 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700493
Shashank Mittalcd98d472011-08-02 14:29:24 -0700494 /* Read image without signature */
495 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
496 {
497 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
498 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800499 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700500
501 offset = imagesize_actual;
502 /* Read signature */
503 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
504 {
505 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
506 }
507 else
508 {
509 auth_kernel_img = image_verify((unsigned char *)image_addr,
510 (unsigned char *)(image_addr + imagesize_actual),
511 imagesize_actual,
512 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700513
514 if(auth_kernel_img)
515 {
516 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700517 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700518 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700519 }
520
Neeti Desai465491e2012-07-31 12:53:35 -0700521 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700522 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
523 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700524
Neeti Desai465491e2012-07-31 12:53:35 -0700525 #if DEVICE_TREE
526 if(hdr->dt_size) {
527 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700528 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700529
530 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
531
532 /* Restriction that the device tree entry table should be less than a page*/
533 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
534
535 /* Validate the device tree table header */
536 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
537 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
538 return -1;
539 }
540
541 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700542 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700543 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
544 return -1;
545 }
546
547 /* Read device device tree in the "tags_add */
548 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
549 }
550 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700551 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700552 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700553 {
554 write_device_info_mmc(&device);
555 #ifdef TZ_TAMPER_FUSE
556 set_tamper_fuse_cmd();
557 #endif
558 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530559 #if USE_PCOM_SECBOOT
560 set_tamper_flag(device.is_tampered);
561 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700562 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700563 else
564 {
565 offset += page_size;
566
567 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
568 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
569 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
570 return -1;
571 }
572 offset += n;
573
574 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
575 if(n != 0)
576 {
577 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
578 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
579 return -1;
580 }
581 }
582 offset += n;
Neeti Desai465491e2012-07-31 12:53:35 -0700583
584 if(hdr->second_size != 0) {
585 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
586 offset += n;
587 }
588
589 #if DEVICE_TREE
590 if(hdr->dt_size != 0) {
591
592 /* Read the device tree table into buffer */
593 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
594 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
595 return -1;
596 }
597 table = (struct dt_table*) dt_buf;
598
599 /* Restriction that the device tree entry table should be less than a page*/
600 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
601
602 /* Validate the device tree table header */
603 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
604 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
605 return -1;
606 }
607
608 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700609 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700610 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
611 return -1;
612 }
613
614 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700615 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
616 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
617 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
618 return -1;
619 }
620 }
621 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700622 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700623
624unified_boot:
625 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
626 hdr->kernel_size);
627 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
628 hdr->ramdisk_size);
629
630 if(hdr->cmdline[0]) {
631 cmdline = (char*) hdr->cmdline;
632 } else {
633 cmdline = DEFAULT_CMDLINE;
634 }
635 dprintf(INFO, "cmdline = '%s'\n", cmdline);
636
637 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700638 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700639 (const char *)cmdline, board_machtype(),
640 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
641
642 return 0;
643}
644
Dima Zavin214cc642009-01-26 11:16:21 -0800645int boot_linux_from_flash(void)
646{
647 struct boot_img_hdr *hdr = (void*) buf;
648 unsigned n;
649 struct ptentry *ptn;
650 struct ptable *ptable;
651 unsigned offset = 0;
652 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800653
Shashank Mittalcd98d472011-08-02 14:29:24 -0700654 unsigned char *image_addr = 0;
655 unsigned kernel_actual;
656 unsigned ramdisk_actual;
657 unsigned imagesize_actual;
658
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700659#if DEVICE_TREE
660 struct dt_table *table;
661 struct dt_entry *dt_entry_ptr;
662#endif
663
664
David Ng183a7422009-12-07 14:55:21 -0800665 if (target_is_emmc_boot()) {
666 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
667 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
668 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
669 return -1;
670 }
671 goto continue_boot;
672 }
673
Dima Zavin214cc642009-01-26 11:16:21 -0800674 ptable = flash_get_ptable();
675 if (ptable == NULL) {
676 dprintf(CRITICAL, "ERROR: Partition table not found\n");
677 return -1;
678 }
679
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800680 if(!boot_into_recovery)
681 {
682 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700683
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800684 if (ptn == NULL) {
685 dprintf(CRITICAL, "ERROR: No boot partition found\n");
686 return -1;
687 }
688 }
689 else
690 {
691 ptn = ptable_find(ptable, "recovery");
692 if (ptn == NULL) {
693 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
694 return -1;
695 }
Dima Zavin214cc642009-01-26 11:16:21 -0800696 }
697
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800698 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800699 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
700 return -1;
701 }
Dima Zavin214cc642009-01-26 11:16:21 -0800702
703 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700704 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800705 return -1;
706 }
707
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800708 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700709 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 -0800710 return -1;
711 }
712
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700713 /* Get virtual addresses since the hdr saves physical addresses. */
714 hdr->kernel_addr = VA(hdr->kernel_addr);
715 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
716 hdr->tags_addr = VA(hdr->tags_addr);
717
Shashank Mittalcd98d472011-08-02 14:29:24 -0700718 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700719 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700720 {
721 image_addr = (unsigned char *)target_get_scratch_address();
722 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
723 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
724 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800725
Shashank Mittalcd98d472011-08-02 14:29:24 -0700726 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700727
728 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700729 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700730
Shashank Mittalcd98d472011-08-02 14:29:24 -0700731 /* Read image without signature */
732 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
733 {
734 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
735 return -1;
736 }
Dima Zavin214cc642009-01-26 11:16:21 -0800737
Shashank Mittalcd98d472011-08-02 14:29:24 -0700738 offset = imagesize_actual;
739 /* Read signature */
740 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
741 {
742 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
743 }
744 else
745 {
746
747 /* Verify signature */
748 auth_kernel_img = image_verify((unsigned char *)image_addr,
749 (unsigned char *)(image_addr + imagesize_actual),
750 imagesize_actual,
751 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700752
753 if(auth_kernel_img)
754 {
755 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700756 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700757 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700758 }
759
760 /* Move kernel and ramdisk to correct address */
761 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
762 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700763
764 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700765 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700766 {
767 write_device_info_flash(&device);
768 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530769#if USE_PCOM_SECBOOT
770 set_tamper_flag(device.is_tampered);
771#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700772 }
773 else
774 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700775 offset = page_size;
776
Shashank Mittalcd98d472011-08-02 14:29:24 -0700777 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
778 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
779 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
780 return -1;
781 }
782 offset += n;
783
784 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
785 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
786 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
787 return -1;
788 }
789 offset += n;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700790
791 if(hdr->second_size != 0) {
792 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
793 offset += n;
794 }
795
796#if DEVICE_TREE
797 if(hdr->dt_size != 0) {
798
799 /* Read the device tree table into buffer */
800 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
801 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
802 return -1;
803 }
804
805 table = (struct dt_table*) dt_buf;
806
807 /* Restriction that the device tree entry table should be less than a page*/
808 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
809
810 /* Validate the device tree table header */
811 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
812 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
813 return -1;
814 }
815
816 /* Calculate the offset of device tree within device tree table */
817 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
818 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
819 return -1;
820 }
821
822 /* Read device device tree in the "tags_add */
823 if(flash_read(ptn, offset + dt_entry_ptr->offset,
824 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
825 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
826 return -1;
827 }
828 }
829#endif
830
Shashank Mittalcd98d472011-08-02 14:29:24 -0700831 }
David Ng183a7422009-12-07 14:55:21 -0800832continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800833 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
834 hdr->kernel_size);
835 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
836 hdr->ramdisk_size);
837
838 if(hdr->cmdline[0]) {
839 cmdline = (char*) hdr->cmdline;
840 } else {
841 cmdline = DEFAULT_CMDLINE;
842 }
843 dprintf(INFO, "cmdline = '%s'\n", cmdline);
844
845 /* TODO: create/pass atags to kernel */
846
847 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700848 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800849 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800850 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
851
852 return 0;
853}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800854
Shashank Mittal162244e2011-08-08 19:01:25 -0700855unsigned char info_buf[4096];
856void write_device_info_mmc(device_info *dev)
857{
858 struct device_info *info = (void*) info_buf;
859 unsigned long long ptn = 0;
860 unsigned long long size;
861 int index = INVALID_PTN;
862
863 index = partition_get_index("aboot");
864 ptn = partition_get_offset(index);
865 if(ptn == 0)
866 {
867 return;
868 }
869
870 size = partition_get_size(index);
871
872 memcpy(info, dev, sizeof(device_info));
873
874 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
875 {
876 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
877 return;
878 }
879}
880
881void read_device_info_mmc(device_info *dev)
882{
883 struct device_info *info = (void*) info_buf;
884 unsigned long long ptn = 0;
885 unsigned long long size;
886 int index = INVALID_PTN;
887
888 index = partition_get_index("aboot");
889 ptn = partition_get_offset(index);
890 if(ptn == 0)
891 {
892 return;
893 }
894
895 size = partition_get_size(index);
896
897 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
898 {
899 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
900 return;
901 }
902
903 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
904 {
905 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
906 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700907 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700908
909 write_device_info_mmc(info);
910 }
911 memcpy(dev, info, sizeof(device_info));
912}
913
914void write_device_info_flash(device_info *dev)
915{
916 struct device_info *info = (void *) info_buf;
917 struct ptentry *ptn;
918 struct ptable *ptable;
919
920 ptable = flash_get_ptable();
921 if (ptable == NULL)
922 {
923 dprintf(CRITICAL, "ERROR: Partition table not found\n");
924 return;
925 }
926
927 ptn = ptable_find(ptable, "devinfo");
928 if (ptn == NULL)
929 {
930 dprintf(CRITICAL, "ERROR: No boot partition found\n");
931 return;
932 }
933
934 memcpy(info, dev, sizeof(device_info));
935
936 if (flash_write(ptn, 0, (void *)info_buf, page_size))
937 {
938 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
939 return;
940 }
941}
942
943void read_device_info_flash(device_info *dev)
944{
945 struct device_info *info = (void*) info_buf;
946 struct ptentry *ptn;
947 struct ptable *ptable;
948
949 ptable = flash_get_ptable();
950 if (ptable == NULL)
951 {
952 dprintf(CRITICAL, "ERROR: Partition table not found\n");
953 return;
954 }
955
956 ptn = ptable_find(ptable, "devinfo");
957 if (ptn == NULL)
958 {
959 dprintf(CRITICAL, "ERROR: No boot partition found\n");
960 return;
961 }
962
963 if (flash_read(ptn, 0, (void *)info_buf, page_size))
964 {
965 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
966 return;
967 }
968
969 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
970 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700971 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
972 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700973 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700974 write_device_info_flash(info);
975 }
976 memcpy(dev, info, sizeof(device_info));
977}
978
979void write_device_info(device_info *dev)
980{
981 if(target_is_emmc_boot())
982 {
983 write_device_info_mmc(dev);
984 }
985 else
986 {
987 write_device_info_flash(dev);
988 }
989}
990
991void read_device_info(device_info *dev)
992{
993 if(target_is_emmc_boot())
994 {
995 read_device_info_mmc(dev);
996 }
997 else
998 {
999 read_device_info_flash(dev);
1000 }
1001}
1002
1003void reset_device_info()
1004{
1005 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001006 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001007 write_device_info(&device);
1008}
1009
1010void set_device_root()
1011{
1012 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001013 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001014 write_device_info(&device);
1015}
1016
Amol Jadicb524072012-08-09 16:40:18 -07001017#if DEVICE_TREE
1018int copy_dtb(uint8_t *boot_image_start)
1019{
1020 uint32 dt_image_offset = 0;
1021 uint32_t n;
1022 struct dt_table *table;
1023 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001024
1025 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1026
Amol Jadicb524072012-08-09 16:40:18 -07001027 if(hdr->dt_size != 0) {
1028
1029 /* add kernel offset */
1030 dt_image_offset += page_size;
1031 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1032 dt_image_offset += n;
1033
1034 /* add ramdisk offset */
1035 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1036 dt_image_offset += n;
1037
1038 /* add second offset */
1039 if(hdr->second_size != 0) {
1040 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1041 dt_image_offset += n;
1042 }
1043
1044 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001045 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001046
1047 /* Restriction that the device tree entry table should be less than a page*/
1048 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1049
1050 /* Validate the device tree table header */
1051 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1052 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1053 return -1;
1054 }
1055
1056 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001057 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001058 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1059 return -1;
1060 }
1061
1062 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001063 memmove((void*) hdr->tags_addr,
1064 boot_image_start + dt_image_offset + dt_entry_ptr->offset,
1065 dt_entry_ptr->size);
Amol Jadicb524072012-08-09 16:40:18 -07001066 }
1067
1068 /* Everything looks fine. Return success. */
1069 return 0;
1070}
1071#endif
1072
Brian Swetland9c4c0752009-01-25 16:23:50 -08001073void cmd_boot(const char *arg, void *data, unsigned sz)
1074{
1075 unsigned kernel_actual;
1076 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001077 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001078 char *ptr = ((char*) data);
1079
1080 if (sz < sizeof(hdr)) {
1081 fastboot_fail("invalid bootimage header");
1082 return;
1083 }
1084
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001085 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001086
1087 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001088 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001089
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001090 if(target_is_emmc_boot() && hdr->page_size) {
1091 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001092 page_mask = page_size - 1;
1093 }
1094
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001095 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1096 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1097
1098 /* Get virtual addresses since the hdr saves physical addresses. */
1099 hdr->kernel_addr = VA(hdr->kernel_addr);
1100 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1101 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001102
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001103 /* sz should have atleast raw boot image */
1104 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001105 fastboot_fail("incomplete bootimage");
1106 return;
1107 }
1108
Amol Jadicb524072012-08-09 16:40:18 -07001109#if DEVICE_TREE
1110 /* find correct dtb and copy it to right location */
1111 if(copy_dtb(data))
1112 {
1113 fastboot_fail("dtb not found");
1114 return;
1115 }
1116#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001117
1118 fastboot_okay("");
1119 udc_stop();
1120
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001121 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1122 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
Amol Jadicb524072012-08-09 16:40:18 -07001123
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001124 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
1125 (const char*) hdr->cmdline, board_machtype(),
1126 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001127}
1128
Dima Zavin214cc642009-01-26 11:16:21 -08001129void cmd_erase(const char *arg, void *data, unsigned sz)
1130{
1131 struct ptentry *ptn;
1132 struct ptable *ptable;
1133
1134 ptable = flash_get_ptable();
1135 if (ptable == NULL) {
1136 fastboot_fail("partition table doesn't exist");
1137 return;
1138 }
1139
1140 ptn = ptable_find(ptable, arg);
1141 if (ptn == NULL) {
1142 fastboot_fail("unknown partition name");
1143 return;
1144 }
1145
1146 if (flash_erase(ptn)) {
1147 fastboot_fail("failed to erase partition");
1148 return;
1149 }
1150 fastboot_okay("");
1151}
1152
Bikas Gurungd48bd242010-09-04 19:54:32 -07001153
1154void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1155{
1156 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -08001157 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -07001158 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001159
Kinson Chikf1a43512011-07-14 11:28:39 -07001160 index = partition_get_index(arg);
1161 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001162
Kinson Chikf1a43512011-07-14 11:28:39 -07001163 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001164 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001165 return;
1166 }
neetidb4b24d62012-01-20 12:13:09 -08001167 /* Simple inefficient version of erase. Just writing
1168 0 in first block */
1169 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1170 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001171 return;
1172 }
1173 fastboot_okay("");
1174}
1175
1176
Ajay Dudani5c761132011-04-07 20:19:04 -07001177void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001178{
1179 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001180 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001181 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001182
Greg Grisco6e754772011-06-23 12:19:39 -07001183 if (!strcmp(arg, "partition"))
1184 {
1185 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001186 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001187 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001188 return;
1189 }
1190 }
Greg Grisco6e754772011-06-23 12:19:39 -07001191 else
1192 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001193 index = partition_get_index(arg);
1194 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001195 if(ptn == 0) {
1196 fastboot_fail("partition table doesn't exist");
1197 return;
1198 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001199
Greg Grisco6e754772011-06-23 12:19:39 -07001200 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1201 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1202 fastboot_fail("image is not a boot image");
1203 return;
1204 }
1205 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001206
Kinson Chikf1a43512011-07-14 11:28:39 -07001207 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001208 if (ROUND_TO_PAGE(sz,511) > size) {
1209 fastboot_fail("size too large");
1210 return;
1211 }
1212 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1213 fastboot_fail("flash write failure");
1214 return;
1215 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001216 }
1217 fastboot_okay("");
1218 return;
1219}
1220
Ajay Dudani5c761132011-04-07 20:19:04 -07001221void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1222{
1223 unsigned int chunk;
1224 unsigned int chunk_data_sz;
1225 sparse_header_t *sparse_header;
1226 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001227 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001228 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301229 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001230 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001231
Kinson Chikf1a43512011-07-14 11:28:39 -07001232 index = partition_get_index(arg);
1233 ptn = partition_get_offset(index);
1234 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001235 fastboot_fail("partition table doesn't exist");
1236 return;
1237 }
1238
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301239 size = partition_get_size(index);
1240 if (ROUND_TO_PAGE(sz,511) > size) {
1241 fastboot_fail("size too large");
1242 return;
1243 }
1244
Ajay Dudani5c761132011-04-07 20:19:04 -07001245 /* Read and skip over sparse image header */
1246 sparse_header = (sparse_header_t *) data;
1247 data += sparse_header->file_hdr_sz;
1248 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1249 {
1250 /* Skip the remaining bytes in a header that is longer than
1251 * we expected.
1252 */
1253 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1254 }
1255
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001256 dprintf (SPEW, "=== Sparse Image Header ===\n");
1257 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1258 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1259 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1260 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1261 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1262 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1263 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1264 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001265
1266 /* Start processing chunks */
1267 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1268 {
1269 /* Read and skip over chunk header */
1270 chunk_header = (chunk_header_t *) data;
1271 data += sizeof(chunk_header_t);
1272
1273 dprintf (SPEW, "=== Chunk Header ===\n");
1274 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1275 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1276 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1277
1278 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1279 {
1280 /* Skip the remaining bytes in a header that is longer than
1281 * we expected.
1282 */
1283 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1284 }
1285
1286 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1287 switch (chunk_header->chunk_type)
1288 {
1289 case CHUNK_TYPE_RAW:
1290 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1291 chunk_data_sz))
1292 {
1293 fastboot_fail("Bogus chunk size for chunk type Raw");
1294 return;
1295 }
1296
Ajay Dudaniab18f022011-05-12 14:39:22 -07001297 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1298 chunk_data_sz,
1299 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001300 {
1301 fastboot_fail("flash write failure");
1302 return;
1303 }
1304 total_blocks += chunk_header->chunk_sz;
1305 data += chunk_data_sz;
1306 break;
1307
1308 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001309 total_blocks += chunk_header->chunk_sz;
1310 break;
1311
Ajay Dudani5c761132011-04-07 20:19:04 -07001312 case CHUNK_TYPE_CRC:
1313 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1314 {
1315 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1316 return;
1317 }
1318 total_blocks += chunk_header->chunk_sz;
1319 data += chunk_data_sz;
1320 break;
1321
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001322 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001323 fastboot_fail("Unknown chunk type");
1324 return;
1325 }
1326 }
1327
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001328 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1329 total_blocks, sparse_header->total_blks);
1330
1331 if(total_blocks != sparse_header->total_blks)
1332 {
1333 fastboot_fail("sparse image write failure");
1334 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001335
1336 fastboot_okay("");
1337 return;
1338}
1339
1340void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1341{
1342 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001343 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1344 unsigned int *magic_number = (unsigned int *) data;
1345 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001346
Neeti Desai127b9e02012-03-20 16:11:23 -07001347 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1348 magic_number[1] == DECRYPT_MAGIC_1)
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001349 {
1350#ifdef SSD_ENABLE
Neeti Desai127b9e02012-03-20 16:11:23 -07001351 ret = decrypt_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001352#endif
Greg Griscod6250552011-06-29 14:40:23 -07001353 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001354 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1355 return;
1356 }
1357 }
Neeti Desai127b9e02012-03-20 16:11:23 -07001358 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1359 magic_number[1] == ENCRYPT_MAGIC_1)
1360 {
1361#ifdef SSD_ENABLE
1362 ret = encrypt_scm((uint32 **) &data, &sz);
1363#endif
1364 if (ret != 0) {
1365 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1366 return;
1367 }
1368 }
1369
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001370 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001371 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1372 cmd_flash_mmc_img(arg, data, sz);
1373 else
1374 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001375 return;
1376}
1377
Dima Zavin214cc642009-01-26 11:16:21 -08001378void cmd_flash(const char *arg, void *data, unsigned sz)
1379{
1380 struct ptentry *ptn;
1381 struct ptable *ptable;
1382 unsigned extra = 0;
1383
1384 ptable = flash_get_ptable();
1385 if (ptable == NULL) {
1386 fastboot_fail("partition table doesn't exist");
1387 return;
1388 }
1389
1390 ptn = ptable_find(ptable, arg);
1391 if (ptn == NULL) {
1392 fastboot_fail("unknown partition name");
1393 return;
1394 }
1395
1396 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1397 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1398 fastboot_fail("image is not a boot image");
1399 return;
1400 }
1401 }
1402
Amol Jadi5c61a952012-05-04 17:05:35 -07001403 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001404 || !strcmp(ptn->name, "userdata")
1405 || !strcmp(ptn->name, "persist")
1406 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301407 if (flash_ecc_bch_enabled())
1408 /* Spare data bytes for 8 bit ECC increased by 4 */
1409 extra = ((page_size >> 9) * 20);
1410 else
1411 extra = ((page_size >> 9) * 16);
1412 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001413 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001414
1415 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1416 if (flash_write(ptn, extra, data, sz)) {
1417 fastboot_fail("flash write failure");
1418 return;
1419 }
1420 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1421 fastboot_okay("");
1422}
1423
1424void cmd_continue(const char *arg, void *data, unsigned sz)
1425{
1426 fastboot_okay("");
1427 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001428 if (target_is_emmc_boot())
1429 {
1430 boot_linux_from_mmc();
1431 }
1432 else
1433 {
1434 boot_linux_from_flash();
1435 }
Dima Zavin214cc642009-01-26 11:16:21 -08001436}
1437
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001438void cmd_reboot(const char *arg, void *data, unsigned sz)
1439{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001440 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001441 fastboot_okay("");
1442 reboot_device(0);
1443}
1444
1445void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1446{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001447 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001448 fastboot_okay("");
1449 reboot_device(FASTBOOT_MODE);
1450}
1451
Shashank Mittal162244e2011-08-08 19:01:25 -07001452void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1453{
1454 if(!device.is_unlocked)
1455 {
1456 device.is_unlocked = 1;
1457 write_device_info(&device);
1458 }
1459 fastboot_okay("");
1460}
1461
Shashank Mittala0032282011-08-26 14:50:11 -07001462void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1463{
1464 char response[64];
1465 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1466 fastboot_info(response);
1467 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1468 fastboot_info(response);
1469 fastboot_okay("");
1470}
1471
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001472void splash_screen ()
1473{
1474 struct ptentry *ptn;
1475 struct ptable *ptable;
1476 struct fbcon_config *fb_display = NULL;
1477
1478 if (!target_is_emmc_boot())
1479 {
1480 ptable = flash_get_ptable();
1481 if (ptable == NULL) {
1482 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001483 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001484 }
1485
1486 ptn = ptable_find(ptable, "splash");
1487 if (ptn == NULL) {
1488 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1489 } else {
1490 fb_display = fbcon_display();
1491 if (fb_display) {
1492 if (flash_read(ptn, 0, fb_display->base,
1493 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1494 fbcon_clear();
1495 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1496 }
1497 }
1498 }
1499 }
1500}
1501
Brian Swetland9c4c0752009-01-25 16:23:50 -08001502void aboot_init(const struct app_descriptor *app)
1503{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001504 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001505 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001506 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001507
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001508 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001509 if (target_is_emmc_boot())
1510 {
1511 page_size = 2048;
1512 page_mask = page_size - 1;
1513 }
1514 else
1515 {
1516 page_size = flash_page_size();
1517 page_mask = page_size - 1;
1518 }
1519
Shashank Mittal162244e2011-08-08 19:01:25 -07001520 if(target_use_signed_kernel())
1521 {
1522 read_device_info(&device);
1523
Shashank Mittal162244e2011-08-08 19:01:25 -07001524 }
1525
Greg Griscod6250552011-06-29 14:40:23 -07001526 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001527 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001528 surf_udc_device.serialno = sn_buf;
1529
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001530 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001531 if (keys_get_state(KEY_HOME) != 0)
1532 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001533 if (keys_get_state(KEY_VOLUMEUP) != 0)
1534 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001535 if(!boot_into_recovery)
1536 {
1537 if (keys_get_state(KEY_BACK) != 0)
1538 goto fastboot;
1539 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1540 goto fastboot;
1541 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001542
1543 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001544 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001545 goto fastboot;
1546 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001547
Ajay Dudani77421292010-10-27 19:34:06 -07001548 reboot_mode = check_reboot_mode();
1549 if (reboot_mode == RECOVERY_MODE) {
1550 boot_into_recovery = 1;
1551 } else if(reboot_mode == FASTBOOT_MODE) {
1552 goto fastboot;
1553 }
1554
Shashank Mittal23b8f422010-04-16 19:27:21 -07001555 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001556 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001557 if(emmc_recovery_init())
1558 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001559 if(target_use_signed_kernel())
1560 {
1561 if((device.is_unlocked) || (device.is_tampered))
1562 {
1563 #ifdef TZ_TAMPER_FUSE
1564 set_tamper_fuse_cmd();
1565 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301566 #if USE_PCOM_SECBOOT
1567 set_tamper_flag(device.is_tampered);
1568 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001569 }
1570 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001571 boot_linux_from_mmc();
1572 }
1573 else
1574 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001575 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301576#if USE_PCOM_SECBOOT
1577 if((device.is_unlocked) || (device.is_tampered))
1578 set_tamper_flag(device.is_tampered);
1579#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001580 boot_linux_from_flash();
1581 }
Dima Zavinb4283602009-01-26 16:36:57 -08001582 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1583 "to fastboot mode.\n");
1584
1585fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001586
Shashank Mittal162244e2011-08-08 19:01:25 -07001587 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001588
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001589 if(!usb_init)
1590 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001591
1592 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001593
Shashank Mittal23b8f422010-04-16 19:27:21 -07001594 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001595 {
1596 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001597 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001598 }
1599 else
1600 {
1601 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001602 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001603 }
1604
1605 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001606 fastboot_register("reboot", cmd_reboot);
1607 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001608 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001609 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001610 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001611 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001612 fastboot_publish("serialno", sn_buf);
Kinson Chikf1a43512011-07-14 11:28:39 -07001613 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001614 sz = target_get_max_flash_size();
1615 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001616 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001617}
1618
1619APP_START(aboot)
1620 .init = aboot_init,
1621APP_END