blob: 07485878858bb894b26379d042547f7ddc2595b4 [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{
Amol Jadib6be5c12012-11-14 13:39:51 -0800385#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700386 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800387#endif
388
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700389 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
390 uint32_t tags_phys = platform_get_virt_to_phys_mapping((addr_t)tags);
Neeti Desaie245d492012-06-01 12:52:13 -0700391
Neeti Desai17379b82012-06-04 18:42:53 -0700392#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800393 dprintf(INFO, "Updating device tree: start\n");
394
Neeti Desai17379b82012-06-04 18:42:53 -0700395 /* Update the Device Tree */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700396 ret = update_device_tree((void *)tags, cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700397 if(ret)
398 {
399 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
400 ASSERT(0);
401 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800402
403 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700404#else
Neeti Desaie245d492012-06-01 12:52:13 -0700405 /* Generating the Atags */
406 generate_atags(tags, cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700407#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700408
Brian Swetland9c4c0752009-01-25 16:23:50 -0800409 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
Amol Jadib6be5c12012-11-14 13:39:51 -0800410 entry, ramdisk, ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800411
412 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700413
Amol Jadi4421e652011-06-16 15:00:48 -0700414 /* do any platform specific cleanup before kernel entry */
415 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700416
Brian Swetland9c4c0752009-01-25 16:23:50 -0800417 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700418
Amol Jadi504f9fe2012-08-16 13:56:48 -0700419#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800420 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700421#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700422
423 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800424}
425
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800426unsigned page_size = 0;
427unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800428
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800429#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800430
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800431static unsigned char buf[4096]; //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800432#if DEVICE_TREE
Neeti Desai465491e2012-07-31 12:53:35 -0700433static unsigned char dt_buf[4096];
Amol Jadib6be5c12012-11-14 13:39:51 -0800434#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800435
Shashank Mittal23b8f422010-04-16 19:27:21 -0700436int boot_linux_from_mmc(void)
437{
438 struct boot_img_hdr *hdr = (void*) buf;
439 struct boot_img_hdr *uhdr;
440 unsigned offset = 0;
441 unsigned long long ptn = 0;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700442 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700443 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700444
Shashank Mittalcd98d472011-08-02 14:29:24 -0700445 unsigned char *image_addr = 0;
446 unsigned kernel_actual;
447 unsigned ramdisk_actual;
448 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700449 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700450
451#if DEVICE_TREE
452 struct dt_table *table;
453 struct dt_entry *dt_entry_ptr;
454 unsigned dt_table_offset;
455#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700456
Shashank Mittal23b8f422010-04-16 19:27:21 -0700457 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
458 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
459 dprintf(INFO, "Unified boot method!\n");
460 hdr = uhdr;
461 goto unified_boot;
462 }
Greg Griscod6250552011-06-29 14:40:23 -0700463 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700464 index = partition_get_index("boot");
465 ptn = partition_get_offset(index);
466 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700467 dprintf(CRITICAL, "ERROR: No boot partition found\n");
468 return -1;
469 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700470 }
471 else {
472 index = partition_get_index("recovery");
473 ptn = partition_get_offset(index);
474 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700475 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
476 return -1;
477 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700478 }
479
Greg Griscod6250552011-06-29 14:40:23 -0700480 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700481 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
482 return -1;
483 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700484
485 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700486 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700487 return -1;
488 }
489
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700490 if (hdr->page_size && (hdr->page_size != page_size)) {
491 page_size = hdr->page_size;
492 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700493 }
494
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700495 /* Get virtual addresses since the hdr saves physical addresses. */
496 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
497 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
498 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
499
Shashank Mittalcd98d472011-08-02 14:29:24 -0700500 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700501 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800502 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700503 image_addr = (unsigned char *)target_get_scratch_address();
504 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
505 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530506 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700507
508 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700509
510 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700511 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700512
Amol Jadib6be5c12012-11-14 13:39:51 -0800513 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
514
Shashank Mittalcd98d472011-08-02 14:29:24 -0700515 /* Read image without signature */
516 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
517 {
518 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
519 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800520 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700521
Amol Jadib6be5c12012-11-14 13:39:51 -0800522 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
523
Shashank Mittalcd98d472011-08-02 14:29:24 -0700524 offset = imagesize_actual;
525 /* Read signature */
526 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
527 {
528 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
529 }
530 else
531 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800532 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
533
Shashank Mittalcd98d472011-08-02 14:29:24 -0700534 auth_kernel_img = image_verify((unsigned char *)image_addr,
535 (unsigned char *)(image_addr + imagesize_actual),
536 imagesize_actual,
537 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700538
Amol Jadib6be5c12012-11-14 13:39:51 -0800539 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
540
Shashank Mittal162244e2011-08-08 19:01:25 -0700541 if(auth_kernel_img)
542 {
543 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700544 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700545 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700546 }
547
Neeti Desai465491e2012-07-31 12:53:35 -0700548 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700549 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
550 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700551
Neeti Desai465491e2012-07-31 12:53:35 -0700552 #if DEVICE_TREE
553 if(hdr->dt_size) {
554 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700555 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700556
557 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
558
559 /* Restriction that the device tree entry table should be less than a page*/
560 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
561
562 /* Validate the device tree table header */
563 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
564 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
565 return -1;
566 }
567
568 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700569 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700570 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
571 return -1;
572 }
573
574 /* Read device device tree in the "tags_add */
575 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
576 }
577 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700578 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700579 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700580 {
581 write_device_info_mmc(&device);
582 #ifdef TZ_TAMPER_FUSE
583 set_tamper_fuse_cmd();
584 #endif
585 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530586 #if USE_PCOM_SECBOOT
587 set_tamper_flag(device.is_tampered);
588 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700589 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700590 else
591 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800592 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
593 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
594 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700595
Amol Jadib6be5c12012-11-14 13:39:51 -0800596 dprintf(INFO, "Loading boot image (%d): start\n",
597 kernel_actual + ramdisk_actual);
598
599 offset = page_size;
600
601 /* Load kernel */
602 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700603 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
604 return -1;
605 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800606 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700607
Amol Jadib6be5c12012-11-14 13:39:51 -0800608 /* Load ramdisk */
609 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700610 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800611 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700612 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
613 return -1;
614 }
615 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800616 offset += ramdisk_actual;
617
618 dprintf(INFO, "Loading boot image (%d): done\n",
619 kernel_actual + ramdisk_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700620
621 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800622 offset += second_actual;
623 /* Second image loading not implemented. */
624 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700625 }
626
627 #if DEVICE_TREE
628 if(hdr->dt_size != 0) {
629
630 /* Read the device tree table into buffer */
631 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
632 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
633 return -1;
634 }
635 table = (struct dt_table*) dt_buf;
636
637 /* Restriction that the device tree entry table should be less than a page*/
638 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
639
640 /* Validate the device tree table header */
641 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
642 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
643 return -1;
644 }
645
646 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700647 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700648 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
649 return -1;
650 }
651
652 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700653 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
654 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
655 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
656 return -1;
657 }
658 }
659 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700660 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700661
662unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700663
664 if(hdr->cmdline[0]) {
665 cmdline = (char*) hdr->cmdline;
666 } else {
667 cmdline = DEFAULT_CMDLINE;
668 }
669 dprintf(INFO, "cmdline = '%s'\n", cmdline);
670
Greg Griscod2471ef2011-07-14 13:00:42 -0700671 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700672 (const char *)cmdline, board_machtype(),
673 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
674
675 return 0;
676}
677
Dima Zavin214cc642009-01-26 11:16:21 -0800678int boot_linux_from_flash(void)
679{
680 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800681 struct ptentry *ptn;
682 struct ptable *ptable;
683 unsigned offset = 0;
684 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800685
Shashank Mittalcd98d472011-08-02 14:29:24 -0700686 unsigned char *image_addr = 0;
687 unsigned kernel_actual;
688 unsigned ramdisk_actual;
689 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800690 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700691
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700692#if DEVICE_TREE
693 struct dt_table *table;
694 struct dt_entry *dt_entry_ptr;
695#endif
696
697
David Ng183a7422009-12-07 14:55:21 -0800698 if (target_is_emmc_boot()) {
699 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
700 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
701 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
702 return -1;
703 }
704 goto continue_boot;
705 }
706
Dima Zavin214cc642009-01-26 11:16:21 -0800707 ptable = flash_get_ptable();
708 if (ptable == NULL) {
709 dprintf(CRITICAL, "ERROR: Partition table not found\n");
710 return -1;
711 }
712
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800713 if(!boot_into_recovery)
714 {
715 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700716
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800717 if (ptn == NULL) {
718 dprintf(CRITICAL, "ERROR: No boot partition found\n");
719 return -1;
720 }
721 }
722 else
723 {
724 ptn = ptable_find(ptable, "recovery");
725 if (ptn == NULL) {
726 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
727 return -1;
728 }
Dima Zavin214cc642009-01-26 11:16:21 -0800729 }
730
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800731 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800732 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
733 return -1;
734 }
Dima Zavin214cc642009-01-26 11:16:21 -0800735
736 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700737 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800738 return -1;
739 }
740
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800741 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700742 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 -0800743 return -1;
744 }
745
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700746 /* Get virtual addresses since the hdr saves physical addresses. */
747 hdr->kernel_addr = VA(hdr->kernel_addr);
748 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
749 hdr->tags_addr = VA(hdr->tags_addr);
750
Shashank Mittalcd98d472011-08-02 14:29:24 -0700751 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700752 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700753 {
754 image_addr = (unsigned char *)target_get_scratch_address();
755 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
756 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
757 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800758
Shashank Mittalcd98d472011-08-02 14:29:24 -0700759 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700760
761 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700762 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700763
Amol Jadib6be5c12012-11-14 13:39:51 -0800764 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
765
Shashank Mittalcd98d472011-08-02 14:29:24 -0700766 /* Read image without signature */
767 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
768 {
769 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
770 return -1;
771 }
Dima Zavin214cc642009-01-26 11:16:21 -0800772
Amol Jadib6be5c12012-11-14 13:39:51 -0800773 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
774
Shashank Mittalcd98d472011-08-02 14:29:24 -0700775 offset = imagesize_actual;
776 /* Read signature */
777 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
778 {
779 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
780 }
781 else
782 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800783 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700784
785 /* Verify signature */
786 auth_kernel_img = image_verify((unsigned char *)image_addr,
787 (unsigned char *)(image_addr + imagesize_actual),
788 imagesize_actual,
789 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700790
Amol Jadib6be5c12012-11-14 13:39:51 -0800791 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
792
Shashank Mittal162244e2011-08-08 19:01:25 -0700793 if(auth_kernel_img)
794 {
795 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700796 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700797 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700798 }
799
800 /* Move kernel and ramdisk to correct address */
801 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
802 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700803
804 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700805 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700806 {
807 write_device_info_flash(&device);
808 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530809#if USE_PCOM_SECBOOT
810 set_tamper_flag(device.is_tampered);
811#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700812 }
813 else
814 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700815 offset = page_size;
816
Amol Jadib6be5c12012-11-14 13:39:51 -0800817 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
818 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
819 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
820
821 dprintf(INFO, "Loading boot image (%d): start\n",
822 kernel_actual + ramdisk_actual);
823
824 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700825 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
826 return -1;
827 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800828 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700829
Amol Jadib6be5c12012-11-14 13:39:51 -0800830 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700831 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
832 return -1;
833 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800834 offset += ramdisk_actual;
835
836 dprintf(INFO, "Loading boot image (%d): done\n",
837 kernel_actual + ramdisk_actual);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700838
839 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800840 offset += second_actual;
841 /* Second image loading not implemented. */
842 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700843 }
844
845#if DEVICE_TREE
846 if(hdr->dt_size != 0) {
847
848 /* Read the device tree table into buffer */
849 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
850 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
851 return -1;
852 }
853
854 table = (struct dt_table*) dt_buf;
855
856 /* Restriction that the device tree entry table should be less than a page*/
857 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
858
859 /* Validate the device tree table header */
860 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
861 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
862 return -1;
863 }
864
865 /* Calculate the offset of device tree within device tree table */
866 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
867 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
868 return -1;
869 }
870
871 /* Read device device tree in the "tags_add */
872 if(flash_read(ptn, offset + dt_entry_ptr->offset,
873 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
874 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
875 return -1;
876 }
877 }
878#endif
879
Shashank Mittalcd98d472011-08-02 14:29:24 -0700880 }
David Ng183a7422009-12-07 14:55:21 -0800881continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800882
883 if(hdr->cmdline[0]) {
884 cmdline = (char*) hdr->cmdline;
885 } else {
886 cmdline = DEFAULT_CMDLINE;
887 }
888 dprintf(INFO, "cmdline = '%s'\n", cmdline);
889
890 /* TODO: create/pass atags to kernel */
891
Ajay Dudanie28a6072011-07-01 13:59:46 -0700892 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800893 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800894 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
895
896 return 0;
897}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800898
Shashank Mittal162244e2011-08-08 19:01:25 -0700899unsigned char info_buf[4096];
900void write_device_info_mmc(device_info *dev)
901{
902 struct device_info *info = (void*) info_buf;
903 unsigned long long ptn = 0;
904 unsigned long long size;
905 int index = INVALID_PTN;
906
907 index = partition_get_index("aboot");
908 ptn = partition_get_offset(index);
909 if(ptn == 0)
910 {
911 return;
912 }
913
914 size = partition_get_size(index);
915
916 memcpy(info, dev, sizeof(device_info));
917
918 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
919 {
920 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
921 return;
922 }
923}
924
925void read_device_info_mmc(device_info *dev)
926{
927 struct device_info *info = (void*) info_buf;
928 unsigned long long ptn = 0;
929 unsigned long long size;
930 int index = INVALID_PTN;
931
932 index = partition_get_index("aboot");
933 ptn = partition_get_offset(index);
934 if(ptn == 0)
935 {
936 return;
937 }
938
939 size = partition_get_size(index);
940
941 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
942 {
943 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
944 return;
945 }
946
947 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
948 {
949 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
950 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -0700951 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700952
953 write_device_info_mmc(info);
954 }
955 memcpy(dev, info, sizeof(device_info));
956}
957
958void write_device_info_flash(device_info *dev)
959{
960 struct device_info *info = (void *) info_buf;
961 struct ptentry *ptn;
962 struct ptable *ptable;
963
964 ptable = flash_get_ptable();
965 if (ptable == NULL)
966 {
967 dprintf(CRITICAL, "ERROR: Partition table not found\n");
968 return;
969 }
970
971 ptn = ptable_find(ptable, "devinfo");
972 if (ptn == NULL)
973 {
974 dprintf(CRITICAL, "ERROR: No boot partition found\n");
975 return;
976 }
977
978 memcpy(info, dev, sizeof(device_info));
979
980 if (flash_write(ptn, 0, (void *)info_buf, page_size))
981 {
982 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
983 return;
984 }
985}
986
987void read_device_info_flash(device_info *dev)
988{
989 struct device_info *info = (void*) info_buf;
990 struct ptentry *ptn;
991 struct ptable *ptable;
992
993 ptable = flash_get_ptable();
994 if (ptable == NULL)
995 {
996 dprintf(CRITICAL, "ERROR: Partition table not found\n");
997 return;
998 }
999
1000 ptn = ptable_find(ptable, "devinfo");
1001 if (ptn == NULL)
1002 {
1003 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1004 return;
1005 }
1006
1007 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1008 {
1009 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1010 return;
1011 }
1012
1013 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1014 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001015 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1016 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001017 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001018 write_device_info_flash(info);
1019 }
1020 memcpy(dev, info, sizeof(device_info));
1021}
1022
1023void write_device_info(device_info *dev)
1024{
1025 if(target_is_emmc_boot())
1026 {
1027 write_device_info_mmc(dev);
1028 }
1029 else
1030 {
1031 write_device_info_flash(dev);
1032 }
1033}
1034
1035void read_device_info(device_info *dev)
1036{
1037 if(target_is_emmc_boot())
1038 {
1039 read_device_info_mmc(dev);
1040 }
1041 else
1042 {
1043 read_device_info_flash(dev);
1044 }
1045}
1046
1047void reset_device_info()
1048{
1049 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001050 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001051 write_device_info(&device);
1052}
1053
1054void set_device_root()
1055{
1056 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001057 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001058 write_device_info(&device);
1059}
1060
Amol Jadicb524072012-08-09 16:40:18 -07001061#if DEVICE_TREE
1062int copy_dtb(uint8_t *boot_image_start)
1063{
1064 uint32 dt_image_offset = 0;
1065 uint32_t n;
1066 struct dt_table *table;
1067 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001068
1069 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1070
Amol Jadicb524072012-08-09 16:40:18 -07001071 if(hdr->dt_size != 0) {
1072
1073 /* add kernel offset */
1074 dt_image_offset += page_size;
1075 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1076 dt_image_offset += n;
1077
1078 /* add ramdisk offset */
1079 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1080 dt_image_offset += n;
1081
1082 /* add second offset */
1083 if(hdr->second_size != 0) {
1084 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1085 dt_image_offset += n;
1086 }
1087
1088 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001089 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001090
1091 /* Restriction that the device tree entry table should be less than a page*/
1092 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1093
1094 /* Validate the device tree table header */
1095 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1096 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1097 return -1;
1098 }
1099
1100 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001101 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001102 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1103 return -1;
1104 }
1105
1106 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001107 memmove((void*) hdr->tags_addr,
1108 boot_image_start + dt_image_offset + dt_entry_ptr->offset,
1109 dt_entry_ptr->size);
Amol Jadicb524072012-08-09 16:40:18 -07001110 }
1111
1112 /* Everything looks fine. Return success. */
1113 return 0;
1114}
1115#endif
1116
Brian Swetland9c4c0752009-01-25 16:23:50 -08001117void cmd_boot(const char *arg, void *data, unsigned sz)
1118{
1119 unsigned kernel_actual;
1120 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001121 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001122 char *ptr = ((char*) data);
1123
1124 if (sz < sizeof(hdr)) {
1125 fastboot_fail("invalid bootimage header");
1126 return;
1127 }
1128
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001129 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001130
1131 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001132 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001133
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001134 if(target_is_emmc_boot() && hdr->page_size) {
1135 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001136 page_mask = page_size - 1;
1137 }
1138
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001139 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1140 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1141
1142 /* Get virtual addresses since the hdr saves physical addresses. */
1143 hdr->kernel_addr = VA(hdr->kernel_addr);
1144 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1145 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001146
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001147 /* sz should have atleast raw boot image */
1148 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001149 fastboot_fail("incomplete bootimage");
1150 return;
1151 }
1152
Amol Jadicb524072012-08-09 16:40:18 -07001153#if DEVICE_TREE
1154 /* find correct dtb and copy it to right location */
1155 if(copy_dtb(data))
1156 {
1157 fastboot_fail("dtb not found");
1158 return;
1159 }
1160#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001161
1162 fastboot_okay("");
1163 udc_stop();
1164
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001165 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1166 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
Amol Jadicb524072012-08-09 16:40:18 -07001167
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001168 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
1169 (const char*) hdr->cmdline, board_machtype(),
1170 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001171}
1172
Dima Zavin214cc642009-01-26 11:16:21 -08001173void cmd_erase(const char *arg, void *data, unsigned sz)
1174{
1175 struct ptentry *ptn;
1176 struct ptable *ptable;
1177
1178 ptable = flash_get_ptable();
1179 if (ptable == NULL) {
1180 fastboot_fail("partition table doesn't exist");
1181 return;
1182 }
1183
1184 ptn = ptable_find(ptable, arg);
1185 if (ptn == NULL) {
1186 fastboot_fail("unknown partition name");
1187 return;
1188 }
1189
1190 if (flash_erase(ptn)) {
1191 fastboot_fail("failed to erase partition");
1192 return;
1193 }
1194 fastboot_okay("");
1195}
1196
Bikas Gurungd48bd242010-09-04 19:54:32 -07001197
1198void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1199{
1200 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -08001201 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -07001202 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001203
Kinson Chikf1a43512011-07-14 11:28:39 -07001204 index = partition_get_index(arg);
1205 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001206
Kinson Chikf1a43512011-07-14 11:28:39 -07001207 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001208 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001209 return;
1210 }
neetidb4b24d62012-01-20 12:13:09 -08001211 /* Simple inefficient version of erase. Just writing
1212 0 in first block */
1213 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1214 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001215 return;
1216 }
1217 fastboot_okay("");
1218}
1219
1220
Ajay Dudani5c761132011-04-07 20:19:04 -07001221void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001222{
1223 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001224 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001225 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001226
Greg Grisco6e754772011-06-23 12:19:39 -07001227 if (!strcmp(arg, "partition"))
1228 {
1229 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001230 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001231 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001232 return;
1233 }
1234 }
Greg Grisco6e754772011-06-23 12:19:39 -07001235 else
1236 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001237 index = partition_get_index(arg);
1238 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001239 if(ptn == 0) {
1240 fastboot_fail("partition table doesn't exist");
1241 return;
1242 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001243
Greg Grisco6e754772011-06-23 12:19:39 -07001244 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1245 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1246 fastboot_fail("image is not a boot image");
1247 return;
1248 }
1249 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001250
Kinson Chikf1a43512011-07-14 11:28:39 -07001251 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001252 if (ROUND_TO_PAGE(sz,511) > size) {
1253 fastboot_fail("size too large");
1254 return;
1255 }
1256 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1257 fastboot_fail("flash write failure");
1258 return;
1259 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001260 }
1261 fastboot_okay("");
1262 return;
1263}
1264
Ajay Dudani5c761132011-04-07 20:19:04 -07001265void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1266{
1267 unsigned int chunk;
1268 unsigned int chunk_data_sz;
1269 sparse_header_t *sparse_header;
1270 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001271 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001272 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301273 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001274 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001275
Kinson Chikf1a43512011-07-14 11:28:39 -07001276 index = partition_get_index(arg);
1277 ptn = partition_get_offset(index);
1278 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001279 fastboot_fail("partition table doesn't exist");
1280 return;
1281 }
1282
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301283 size = partition_get_size(index);
1284 if (ROUND_TO_PAGE(sz,511) > size) {
1285 fastboot_fail("size too large");
1286 return;
1287 }
1288
Ajay Dudani5c761132011-04-07 20:19:04 -07001289 /* Read and skip over sparse image header */
1290 sparse_header = (sparse_header_t *) data;
1291 data += sparse_header->file_hdr_sz;
1292 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1293 {
1294 /* Skip the remaining bytes in a header that is longer than
1295 * we expected.
1296 */
1297 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1298 }
1299
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001300 dprintf (SPEW, "=== Sparse Image Header ===\n");
1301 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1302 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1303 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1304 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1305 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1306 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1307 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1308 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001309
1310 /* Start processing chunks */
1311 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1312 {
1313 /* Read and skip over chunk header */
1314 chunk_header = (chunk_header_t *) data;
1315 data += sizeof(chunk_header_t);
1316
1317 dprintf (SPEW, "=== Chunk Header ===\n");
1318 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1319 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1320 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1321
1322 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1323 {
1324 /* Skip the remaining bytes in a header that is longer than
1325 * we expected.
1326 */
1327 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1328 }
1329
1330 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1331 switch (chunk_header->chunk_type)
1332 {
1333 case CHUNK_TYPE_RAW:
1334 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1335 chunk_data_sz))
1336 {
1337 fastboot_fail("Bogus chunk size for chunk type Raw");
1338 return;
1339 }
1340
Ajay Dudaniab18f022011-05-12 14:39:22 -07001341 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1342 chunk_data_sz,
1343 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001344 {
1345 fastboot_fail("flash write failure");
1346 return;
1347 }
1348 total_blocks += chunk_header->chunk_sz;
1349 data += chunk_data_sz;
1350 break;
1351
1352 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001353 total_blocks += chunk_header->chunk_sz;
1354 break;
1355
Ajay Dudani5c761132011-04-07 20:19:04 -07001356 case CHUNK_TYPE_CRC:
1357 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1358 {
1359 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1360 return;
1361 }
1362 total_blocks += chunk_header->chunk_sz;
1363 data += chunk_data_sz;
1364 break;
1365
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001366 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001367 fastboot_fail("Unknown chunk type");
1368 return;
1369 }
1370 }
1371
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001372 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1373 total_blocks, sparse_header->total_blks);
1374
1375 if(total_blocks != sparse_header->total_blks)
1376 {
1377 fastboot_fail("sparse image write failure");
1378 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001379
1380 fastboot_okay("");
1381 return;
1382}
1383
1384void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1385{
1386 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001387 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1388 unsigned int *magic_number = (unsigned int *) data;
1389 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001390
Neeti Desai127b9e02012-03-20 16:11:23 -07001391 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1392 magic_number[1] == DECRYPT_MAGIC_1)
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001393 {
1394#ifdef SSD_ENABLE
Neeti Desai127b9e02012-03-20 16:11:23 -07001395 ret = decrypt_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001396#endif
Greg Griscod6250552011-06-29 14:40:23 -07001397 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001398 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1399 return;
1400 }
1401 }
Neeti Desai127b9e02012-03-20 16:11:23 -07001402 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1403 magic_number[1] == ENCRYPT_MAGIC_1)
1404 {
1405#ifdef SSD_ENABLE
1406 ret = encrypt_scm((uint32 **) &data, &sz);
1407#endif
1408 if (ret != 0) {
1409 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1410 return;
1411 }
1412 }
1413
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001414 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001415 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1416 cmd_flash_mmc_img(arg, data, sz);
1417 else
1418 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001419 return;
1420}
1421
Dima Zavin214cc642009-01-26 11:16:21 -08001422void cmd_flash(const char *arg, void *data, unsigned sz)
1423{
1424 struct ptentry *ptn;
1425 struct ptable *ptable;
1426 unsigned extra = 0;
1427
1428 ptable = flash_get_ptable();
1429 if (ptable == NULL) {
1430 fastboot_fail("partition table doesn't exist");
1431 return;
1432 }
1433
1434 ptn = ptable_find(ptable, arg);
1435 if (ptn == NULL) {
1436 fastboot_fail("unknown partition name");
1437 return;
1438 }
1439
1440 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1441 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1442 fastboot_fail("image is not a boot image");
1443 return;
1444 }
1445 }
1446
Amol Jadi5c61a952012-05-04 17:05:35 -07001447 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001448 || !strcmp(ptn->name, "userdata")
1449 || !strcmp(ptn->name, "persist")
1450 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301451 if (flash_ecc_bch_enabled())
1452 /* Spare data bytes for 8 bit ECC increased by 4 */
1453 extra = ((page_size >> 9) * 20);
1454 else
1455 extra = ((page_size >> 9) * 16);
1456 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001457 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001458
1459 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1460 if (flash_write(ptn, extra, data, sz)) {
1461 fastboot_fail("flash write failure");
1462 return;
1463 }
1464 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1465 fastboot_okay("");
1466}
1467
1468void cmd_continue(const char *arg, void *data, unsigned sz)
1469{
1470 fastboot_okay("");
1471 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001472 if (target_is_emmc_boot())
1473 {
1474 boot_linux_from_mmc();
1475 }
1476 else
1477 {
1478 boot_linux_from_flash();
1479 }
Dima Zavin214cc642009-01-26 11:16:21 -08001480}
1481
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001482void cmd_reboot(const char *arg, void *data, unsigned sz)
1483{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001484 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001485 fastboot_okay("");
1486 reboot_device(0);
1487}
1488
1489void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1490{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001491 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001492 fastboot_okay("");
1493 reboot_device(FASTBOOT_MODE);
1494}
1495
Shashank Mittal162244e2011-08-08 19:01:25 -07001496void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1497{
1498 if(!device.is_unlocked)
1499 {
1500 device.is_unlocked = 1;
1501 write_device_info(&device);
1502 }
1503 fastboot_okay("");
1504}
1505
Shashank Mittala0032282011-08-26 14:50:11 -07001506void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1507{
1508 char response[64];
1509 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1510 fastboot_info(response);
1511 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1512 fastboot_info(response);
1513 fastboot_okay("");
1514}
1515
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001516void splash_screen ()
1517{
1518 struct ptentry *ptn;
1519 struct ptable *ptable;
1520 struct fbcon_config *fb_display = NULL;
1521
1522 if (!target_is_emmc_boot())
1523 {
1524 ptable = flash_get_ptable();
1525 if (ptable == NULL) {
1526 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001527 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001528 }
1529
1530 ptn = ptable_find(ptable, "splash");
1531 if (ptn == NULL) {
1532 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1533 } else {
1534 fb_display = fbcon_display();
1535 if (fb_display) {
1536 if (flash_read(ptn, 0, fb_display->base,
1537 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1538 fbcon_clear();
1539 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1540 }
1541 }
1542 }
1543 }
1544}
1545
Brian Swetland9c4c0752009-01-25 16:23:50 -08001546void aboot_init(const struct app_descriptor *app)
1547{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001548 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001549 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001550 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001551
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001552 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001553 if (target_is_emmc_boot())
1554 {
1555 page_size = 2048;
1556 page_mask = page_size - 1;
1557 }
1558 else
1559 {
1560 page_size = flash_page_size();
1561 page_mask = page_size - 1;
1562 }
1563
Shashank Mittal162244e2011-08-08 19:01:25 -07001564 if(target_use_signed_kernel())
1565 {
1566 read_device_info(&device);
1567
Shashank Mittal162244e2011-08-08 19:01:25 -07001568 }
1569
Greg Griscod6250552011-06-29 14:40:23 -07001570 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001571 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001572 surf_udc_device.serialno = sn_buf;
1573
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001574 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001575 if (keys_get_state(KEY_HOME) != 0)
1576 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001577 if (keys_get_state(KEY_VOLUMEUP) != 0)
1578 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001579 if(!boot_into_recovery)
1580 {
1581 if (keys_get_state(KEY_BACK) != 0)
1582 goto fastboot;
1583 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1584 goto fastboot;
1585 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001586
1587 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001588 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001589 goto fastboot;
1590 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001591
Ajay Dudani77421292010-10-27 19:34:06 -07001592 reboot_mode = check_reboot_mode();
1593 if (reboot_mode == RECOVERY_MODE) {
1594 boot_into_recovery = 1;
1595 } else if(reboot_mode == FASTBOOT_MODE) {
1596 goto fastboot;
1597 }
1598
Shashank Mittal23b8f422010-04-16 19:27:21 -07001599 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001600 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001601 if(emmc_recovery_init())
1602 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001603 if(target_use_signed_kernel())
1604 {
1605 if((device.is_unlocked) || (device.is_tampered))
1606 {
1607 #ifdef TZ_TAMPER_FUSE
1608 set_tamper_fuse_cmd();
1609 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301610 #if USE_PCOM_SECBOOT
1611 set_tamper_flag(device.is_tampered);
1612 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001613 }
1614 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001615 boot_linux_from_mmc();
1616 }
1617 else
1618 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001619 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301620#if USE_PCOM_SECBOOT
1621 if((device.is_unlocked) || (device.is_tampered))
1622 set_tamper_flag(device.is_tampered);
1623#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001624 boot_linux_from_flash();
1625 }
Dima Zavinb4283602009-01-26 16:36:57 -08001626 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1627 "to fastboot mode.\n");
1628
1629fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001630
Shashank Mittal162244e2011-08-08 19:01:25 -07001631 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001632
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001633 if(!usb_init)
1634 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001635
1636 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001637
Shashank Mittal23b8f422010-04-16 19:27:21 -07001638 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001639 {
1640 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001641 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001642 }
1643 else
1644 {
1645 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001646 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001647 }
1648
1649 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001650 fastboot_register("reboot", cmd_reboot);
1651 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001652 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001653 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001654 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001655 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001656 fastboot_publish("serialno", sn_buf);
Kinson Chikf1a43512011-07-14 11:28:39 -07001657 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001658 sz = target_get_max_flash_size();
1659 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001660 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001661}
1662
1663APP_START(aboot)
1664 .init = aboot_init,
1665APP_END