blob: d7ca01e0d480e1dd87d1754e20e8a2996670d426 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Amol Jadi2a15a272013-01-22 12:03:36 -08005 * Copyright (c) 2009-2013, 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>
Amol Jadi492d5a52013-03-15 16:12:34 -070052#include <boot_stats.h>
Dima Zavin214cc642009-01-26 11:16:21 -080053
Neeti Desai17379b82012-06-04 18:42:53 -070054#if DEVICE_TREE
55#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070056#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070057#endif
58
Shashank Mittalcd98d472011-08-02 14:29:24 -070059#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080060#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080061#include "bootimg.h"
62#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070063#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070064#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070065#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070066#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080067
Shashank Mittal162244e2011-08-08 19:01:25 -070068#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070069
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070070extern bool target_use_signed_kernel(void);
71extern void dsb();
72extern void isb();
73extern void platform_uninit(void);
74
75void write_device_info_mmc(device_info *dev);
76void write_device_info_flash(device_info *dev);
77
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070078#define EXPAND(NAME) #NAME
79#define TARGET(NAME) EXPAND(NAME)
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";
Amol Jadi2a15a272013-01-22 12:03:36 -0800101static const char *baseband_dsda = " androidboot.baseband=dsda";
102static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800103static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800104
Shashank Mittalcd98d472011-08-02 14:29:24 -0700105/* Assuming unauthorized kernel image by default */
106static int auth_kernel_img = 0;
107
Shashank Mittal162244e2011-08-08 19:01:25 -0700108static device_info device = {DEVICE_MAGIC, 0, 0};
109
Brian Swetland9c4c0752009-01-25 16:23:50 -0800110static struct udc_device surf_udc_device = {
111 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800112 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800113 .version_id = 0x0100,
114 .manufacturer = "Google",
115 .product = "Android",
116};
117
Dima Zavin42168f22009-01-30 11:52:22 -0800118struct atag_ptbl_entry
119{
120 char name[16];
121 unsigned offset;
122 unsigned size;
123 unsigned flags;
124};
125
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700126/*
127 * Partition info, required to be published
128 * for fastboot
129 */
130struct getvar_partition_info {
131 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
132 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
133 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
134 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
135 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
136};
137
138/*
139 * Right now, we are publishing the info for only
140 * three partitions
141 */
142struct getvar_partition_info part_info[] =
143{
144 { "system" , "partition-size:", "partition-type:", "", "ext4" },
145 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
146 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
147};
148
149char max_download_size[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800150char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700151
Greg Griscod2471ef2011-07-14 13:00:42 -0700152extern int emmc_recovery_init(void);
153
Kinson Chik0b1c8162011-08-31 16:31:57 -0700154#if NO_KEYPAD_DRIVER
155extern int fastboot_trigger(void);
156#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700157
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700158static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
159{
160 /* overwrite the destination of specified for the project */
161 /*
162 * Update the value to sane values only when the boot image
163 * header does not have sane values, this is added to make sure
164 * that we always use values from boot.img header and use the
165 * force values when boot image header has default values.
166 */
167#ifdef ABOOT_FORCE_KERNEL_ADDR
168 if (hdr->kernel_addr == ABOOT_DEFAULT_KERNEL_ADDR)
169 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
170#endif
171#ifdef ABOOT_FORCE_RAMDISK_ADDR
172 if (hdr->ramdisk_addr == ABOOT_DEFAULT_RAMDISK_ADDR)
173 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
174#endif
175#ifdef ABOOT_FORCE_TAGS_ADDR
176 if (hdr->tags_addr == ABOOT_DEFAULT_TAGS_ADDR)
177 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
178#endif
179}
180
Dima Zavin42168f22009-01-30 11:52:22 -0800181static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
182{
183 struct atag_ptbl_entry atag_ptn;
184
185 memcpy(atag_ptn.name, ptn->name, 16);
186 atag_ptn.name[15] = '\0';
187 atag_ptn.offset = ptn->start;
188 atag_ptn.size = ptn->length;
189 atag_ptn.flags = ptn->flags;
190 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
191 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
192}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800193
Neeti Desaie245d492012-06-01 12:52:13 -0700194unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800195{
David Ng183a7422009-12-07 14:55:21 -0800196 int cmdline_len = 0;
197 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800198 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700199 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800200
Brian Swetland9c4c0752009-01-25 16:23:50 -0800201 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800202 cmdline_len = strlen(cmdline);
203 have_cmdline = 1;
204 }
205 if (target_is_emmc_boot()) {
206 cmdline_len += strlen(emmc_cmdline);
207 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800208
209 cmdline_len += strlen(usb_sn_cmdline);
210 cmdline_len += strlen(sn_buf);
211
David Ngf773dde2010-07-26 19:55:08 -0700212 if (target_pause_for_battery_charge()) {
213 pause_at_bootup = 1;
214 cmdline_len += strlen(battchg_pause);
215 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800216
Shashank Mittalcd98d472011-08-02 14:29:24 -0700217 if(target_use_signed_kernel() && auth_kernel_img) {
218 cmdline_len += strlen(auth_kernel);
219 }
220
Ajay Dudanid04110c2011-01-17 23:55:07 -0800221 /* Determine correct androidboot.baseband to use */
222 switch(target_baseband())
223 {
224 case BASEBAND_APQ:
225 cmdline_len += strlen(baseband_apq);
226 break;
227
228 case BASEBAND_MSM:
229 cmdline_len += strlen(baseband_msm);
230 break;
231
232 case BASEBAND_CSFB:
233 cmdline_len += strlen(baseband_csfb);
234 break;
235
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800236 case BASEBAND_SVLTE2A:
237 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800238 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700239
240 case BASEBAND_MDM:
241 cmdline_len += strlen(baseband_mdm);
242 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700243
244 case BASEBAND_SGLTE:
245 cmdline_len += strlen(baseband_sglte);
246 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530247
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800248 case BASEBAND_SGLTE2:
249 cmdline_len += strlen(baseband_sglte2);
250 break;
251
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530252 case BASEBAND_DSDA:
253 cmdline_len += strlen(baseband_dsda);
254 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800255
256 case BASEBAND_DSDA2:
257 cmdline_len += strlen(baseband_dsda2);
258 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800259 }
260
David Ng183a7422009-12-07 14:55:21 -0800261 if (cmdline_len > 0) {
262 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700263 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
264 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700265
Amol Jadi168b7712012-03-06 16:15:00 -0800266 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700267 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800268 if (have_cmdline) {
269 src = cmdline;
270 while ((*dst++ = *src++));
271 }
272 if (target_is_emmc_boot()) {
273 src = emmc_cmdline;
274 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700275 have_cmdline = 1;
276 while ((*dst++ = *src++));
277 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800278
279 src = usb_sn_cmdline;
280 if (have_cmdline) --dst;
281 have_cmdline = 1;
282 while ((*dst++ = *src++));
283 src = sn_buf;
284 if (have_cmdline) --dst;
285 have_cmdline = 1;
286 while ((*dst++ = *src++));
287
David Ngf773dde2010-07-26 19:55:08 -0700288 if (pause_at_bootup) {
289 src = battchg_pause;
290 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800291 while ((*dst++ = *src++));
292 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800293
Shashank Mittalcd98d472011-08-02 14:29:24 -0700294 if(target_use_signed_kernel() && auth_kernel_img) {
295 src = auth_kernel;
296 if (have_cmdline) --dst;
297 while ((*dst++ = *src++));
298 }
299
Ajay Dudanid04110c2011-01-17 23:55:07 -0800300 switch(target_baseband())
301 {
302 case BASEBAND_APQ:
303 src = baseband_apq;
304 if (have_cmdline) --dst;
305 while ((*dst++ = *src++));
306 break;
307
308 case BASEBAND_MSM:
309 src = baseband_msm;
310 if (have_cmdline) --dst;
311 while ((*dst++ = *src++));
312 break;
313
314 case BASEBAND_CSFB:
315 src = baseband_csfb;
316 if (have_cmdline) --dst;
317 while ((*dst++ = *src++));
318 break;
319
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800320 case BASEBAND_SVLTE2A:
321 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800322 if (have_cmdline) --dst;
323 while ((*dst++ = *src++));
324 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700325
326 case BASEBAND_MDM:
327 src = baseband_mdm;
328 if (have_cmdline) --dst;
329 while ((*dst++ = *src++));
330 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700331
332 case BASEBAND_SGLTE:
333 src = baseband_sglte;
334 if (have_cmdline) --dst;
335 while ((*dst++ = *src++));
336 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530337
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800338 case BASEBAND_SGLTE2:
339 src = baseband_sglte2;
340 if (have_cmdline) --dst;
341 while ((*dst++ = *src++));
342 break;
343
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530344 case BASEBAND_DSDA:
345 src = baseband_dsda;
346 if (have_cmdline) --dst;
347 while ((*dst++ = *src++));
348 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800349
350 case BASEBAND_DSDA2:
351 src = baseband_dsda2;
352 if (have_cmdline) --dst;
353 while ((*dst++ = *src++));
354 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800355 }
Neeti Desaie245d492012-06-01 12:52:13 -0700356 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800357
358 dprintf(INFO, "cmdline: %s\n", cmdline_final);
359
Neeti Desaie245d492012-06-01 12:52:13 -0700360 return cmdline_final;
361}
362
363unsigned *atag_core(unsigned *ptr)
364{
365 /* CORE */
366 *ptr++ = 2;
367 *ptr++ = 0x54410001;
368
369 return ptr;
370
371}
372
373unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
374 unsigned ramdisk_size)
375{
376 if (ramdisk_size) {
377 *ptr++ = 4;
378 *ptr++ = 0x54420005;
379 *ptr++ = (unsigned)ramdisk;
380 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800381 }
382
Neeti Desaie245d492012-06-01 12:52:13 -0700383 return ptr;
384}
385
386unsigned *atag_ptable(unsigned **ptr_addr)
387{
388 int i;
389 struct ptable *ptable;
390
391 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700392 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
393 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700394 *(*ptr_addr)++ = 0x4d534d70;
395 for (i = 0; i < ptable->count; ++i)
396 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
397 }
398
399 return (*ptr_addr);
400}
401
402unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
403{
404 int cmdline_length = 0;
405 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700406 char *dest;
407
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800408 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700409 n = (cmdline_length + 4) & (~3);
410
411 *ptr++ = (n / 4) + 2;
412 *ptr++ = 0x54410009;
413 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800414 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700415 ptr += (n / 4);
416
417 return ptr;
418}
419
420unsigned *atag_end(unsigned *ptr)
421{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800422 /* END */
423 *ptr++ = 0;
424 *ptr++ = 0;
425
Neeti Desaie245d492012-06-01 12:52:13 -0700426 return ptr;
427}
428
429void generate_atags(unsigned *ptr, const char *cmdline,
430 void *ramdisk, unsigned ramdisk_size)
431{
432
433 ptr = atag_core(ptr);
434 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
435 ptr = target_atag_mem(ptr);
436
437 /* Skip NAND partition ATAGS for eMMC boot */
438 if (!target_is_emmc_boot()){
439 ptr = atag_ptable(&ptr);
440 }
441
442 ptr = atag_cmdline(ptr, cmdline);
443 ptr = atag_end(ptr);
444}
445
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700446typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700447void boot_linux(void *kernel, unsigned *tags,
448 const char *cmdline, unsigned machtype,
449 void *ramdisk, unsigned ramdisk_size)
450{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800451 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800452#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700453 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800454#endif
455
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700456 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800457 uint32_t tags_phys = PA((addr_t)tags);
458
459 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700460
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800461 final_cmdline = update_cmdline((const char*)cmdline);
462
Neeti Desai17379b82012-06-04 18:42:53 -0700463#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800464 dprintf(INFO, "Updating device tree: start\n");
465
Neeti Desai17379b82012-06-04 18:42:53 -0700466 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800467 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700468 if(ret)
469 {
470 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
471 ASSERT(0);
472 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800473 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700474#else
Neeti Desaie245d492012-06-01 12:52:13 -0700475 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800476 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700477#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700478
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800479 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
480 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800481
482 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700483
Amol Jadi4421e652011-06-16 15:00:48 -0700484 /* do any platform specific cleanup before kernel entry */
485 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700486
Brian Swetland9c4c0752009-01-25 16:23:50 -0800487 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700488
Amol Jadi504f9fe2012-08-16 13:56:48 -0700489#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800490 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700491#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700492 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700493 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800494}
495
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800496unsigned page_size = 0;
497unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800498
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800499#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800500
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800501static unsigned char buf[4096]; //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800502#if DEVICE_TREE
Neeti Desai465491e2012-07-31 12:53:35 -0700503static unsigned char dt_buf[4096];
Amol Jadib6be5c12012-11-14 13:39:51 -0800504#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800505
Shashank Mittal23b8f422010-04-16 19:27:21 -0700506int boot_linux_from_mmc(void)
507{
508 struct boot_img_hdr *hdr = (void*) buf;
509 struct boot_img_hdr *uhdr;
510 unsigned offset = 0;
511 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700512 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700513
Shashank Mittalcd98d472011-08-02 14:29:24 -0700514 unsigned char *image_addr = 0;
515 unsigned kernel_actual;
516 unsigned ramdisk_actual;
517 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700518 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700519
520#if DEVICE_TREE
521 struct dt_table *table;
522 struct dt_entry *dt_entry_ptr;
523 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800524 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700525#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700526
Shashank Mittal23b8f422010-04-16 19:27:21 -0700527 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
528 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
529 dprintf(INFO, "Unified boot method!\n");
530 hdr = uhdr;
531 goto unified_boot;
532 }
Greg Griscod6250552011-06-29 14:40:23 -0700533 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700534 index = partition_get_index("boot");
535 ptn = partition_get_offset(index);
536 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700537 dprintf(CRITICAL, "ERROR: No boot partition found\n");
538 return -1;
539 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700540 }
541 else {
542 index = partition_get_index("recovery");
543 ptn = partition_get_offset(index);
544 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700545 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
546 return -1;
547 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700548 }
549
Greg Griscod6250552011-06-29 14:40:23 -0700550 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700551 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
552 return -1;
553 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700554
555 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700556 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700557 return -1;
558 }
559
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700560 if (hdr->page_size && (hdr->page_size != page_size)) {
561 page_size = hdr->page_size;
562 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700563 }
564
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700565 /*
566 * Update the kernel/ramdisk/tags address if the boot image header
567 * has default values, these default values come from mkbootimg when
568 * the boot image is flashed using fastboot flash:raw
569 */
570 update_ker_tags_rdisk_addr(hdr);
571
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700572 /* Get virtual addresses since the hdr saves physical addresses. */
573 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
574 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
575 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
576
Shashank Mittalcd98d472011-08-02 14:29:24 -0700577 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700578 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800579 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700580 image_addr = (unsigned char *)target_get_scratch_address();
581 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
582 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800583#if DEVICE_TREE
584 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
585 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
586#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530587 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800588#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700589 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700590
591 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700592 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700593
Amol Jadib6be5c12012-11-14 13:39:51 -0800594 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700595 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800596
Shashank Mittalcd98d472011-08-02 14:29:24 -0700597 /* Read image without signature */
598 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
599 {
600 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
601 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800602 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700603
Amol Jadib6be5c12012-11-14 13:39:51 -0800604 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700605 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800606
Shashank Mittalcd98d472011-08-02 14:29:24 -0700607 offset = imagesize_actual;
608 /* Read signature */
609 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
610 {
611 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
612 }
613 else
614 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800615 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
616
Shashank Mittalcd98d472011-08-02 14:29:24 -0700617 auth_kernel_img = image_verify((unsigned char *)image_addr,
618 (unsigned char *)(image_addr + imagesize_actual),
619 imagesize_actual,
620 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700621
Amol Jadib6be5c12012-11-14 13:39:51 -0800622 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
623
Shashank Mittal162244e2011-08-08 19:01:25 -0700624 if(auth_kernel_img)
625 {
626 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700627 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700628 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700629 }
630
Neeti Desai465491e2012-07-31 12:53:35 -0700631 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700632 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
633 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800634#if DEVICE_TREE
635 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
636#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700637
Neeti Desai465491e2012-07-31 12:53:35 -0700638 #if DEVICE_TREE
639 if(hdr->dt_size) {
640 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700641 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700642
643 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
644
645 /* Restriction that the device tree entry table should be less than a page*/
646 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
647
648 /* Validate the device tree table header */
649 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
650 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
651 return -1;
652 }
653
654 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700655 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700656 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
657 return -1;
658 }
659
660 /* Read device device tree in the "tags_add */
661 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800662 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800663 /*
664 * If appended dev tree is found, update the atags with
665 * memory address to the DTB appended location on RAM.
666 * Else update with the atags address in the kernel header
667 */
668 void *dtb;
669 dtb = dev_tree_appended((void*) hdr->kernel_addr,
670 (void *)hdr->tags_addr);
671 if (!dtb) {
672 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
673 return -1;
674 }
Neeti Desai465491e2012-07-31 12:53:35 -0700675 }
676 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700677 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700678 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700679 {
680 write_device_info_mmc(&device);
681 #ifdef TZ_TAMPER_FUSE
682 set_tamper_fuse_cmd();
683 #endif
684 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530685 #if USE_PCOM_SECBOOT
686 set_tamper_flag(device.is_tampered);
687 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700688 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700689 else
690 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800691 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
692 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
693 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700694
Amol Jadib6be5c12012-11-14 13:39:51 -0800695 dprintf(INFO, "Loading boot image (%d): start\n",
696 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700697 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800698
699 offset = page_size;
700
701 /* Load kernel */
702 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700703 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
704 return -1;
705 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800706 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700707
Amol Jadib6be5c12012-11-14 13:39:51 -0800708 /* Load ramdisk */
709 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700710 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800711 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700712 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
713 return -1;
714 }
715 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800716 offset += ramdisk_actual;
717
718 dprintf(INFO, "Loading boot image (%d): done\n",
719 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700720 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700721
722 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800723 offset += second_actual;
724 /* Second image loading not implemented. */
725 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700726 }
727
728 #if DEVICE_TREE
729 if(hdr->dt_size != 0) {
730
731 /* Read the device tree table into buffer */
732 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
733 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
734 return -1;
735 }
736 table = (struct dt_table*) dt_buf;
737
738 /* Restriction that the device tree entry table should be less than a page*/
739 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
740
741 /* Validate the device tree table header */
742 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
743 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
744 return -1;
745 }
746
747 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700748 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700749 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
750 return -1;
751 }
752
753 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700754 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
755 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
756 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
757 return -1;
758 }
Channagoud Kadabi35095622013-03-01 13:53:05 -0800759 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800760 /*
761 * If appended dev tree is found, update the atags with
762 * memory address to the DTB appended location on RAM.
763 * Else update with the atags address in the kernel header
764 */
765 void *dtb;
766 dtb = dev_tree_appended((void*) hdr->kernel_addr,
767 (void *)hdr->tags_addr);
768 if (!dtb) {
769 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
770 return -1;
771 }
Neeti Desai465491e2012-07-31 12:53:35 -0700772 }
773 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700774 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700775
776unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700777
Dima Zavin77e41f32013-03-06 16:10:43 -0800778 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700779 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700780 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
781
782 return 0;
783}
784
Dima Zavin214cc642009-01-26 11:16:21 -0800785int boot_linux_from_flash(void)
786{
787 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800788 struct ptentry *ptn;
789 struct ptable *ptable;
790 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800791
Shashank Mittalcd98d472011-08-02 14:29:24 -0700792 unsigned char *image_addr = 0;
793 unsigned kernel_actual;
794 unsigned ramdisk_actual;
795 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800796 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700797
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700798#if DEVICE_TREE
799 struct dt_table *table;
800 struct dt_entry *dt_entry_ptr;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800801 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700802#endif
803
David Ng183a7422009-12-07 14:55:21 -0800804 if (target_is_emmc_boot()) {
805 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
806 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
807 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
808 return -1;
809 }
810 goto continue_boot;
811 }
812
Dima Zavin214cc642009-01-26 11:16:21 -0800813 ptable = flash_get_ptable();
814 if (ptable == NULL) {
815 dprintf(CRITICAL, "ERROR: Partition table not found\n");
816 return -1;
817 }
818
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800819 if(!boot_into_recovery)
820 {
821 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700822
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800823 if (ptn == NULL) {
824 dprintf(CRITICAL, "ERROR: No boot partition found\n");
825 return -1;
826 }
827 }
828 else
829 {
830 ptn = ptable_find(ptable, "recovery");
831 if (ptn == NULL) {
832 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
833 return -1;
834 }
Dima Zavin214cc642009-01-26 11:16:21 -0800835 }
836
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800837 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800838 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
839 return -1;
840 }
Dima Zavin214cc642009-01-26 11:16:21 -0800841
842 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700843 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800844 return -1;
845 }
846
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800847 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700848 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 -0800849 return -1;
850 }
851
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700852 /*
853 * Update the kernel/ramdisk/tags address if the boot image header
854 * has default values, these default values come from mkbootimg when
855 * the boot image is flashed using fastboot flash:raw
856 */
857 update_ker_tags_rdisk_addr(hdr);
858
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700859 /* Get virtual addresses since the hdr saves physical addresses. */
860 hdr->kernel_addr = VA(hdr->kernel_addr);
861 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
862 hdr->tags_addr = VA(hdr->tags_addr);
863
Shashank Mittalcd98d472011-08-02 14:29:24 -0700864 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700865 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700866 {
867 image_addr = (unsigned char *)target_get_scratch_address();
868 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
869 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800870#if DEVICE_TREE
871 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
872 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
873#else
Shashank Mittalcd98d472011-08-02 14:29:24 -0700874 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800875#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700876 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700877
878 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700879 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700880
Amol Jadib6be5c12012-11-14 13:39:51 -0800881 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700882 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800883
Shashank Mittalcd98d472011-08-02 14:29:24 -0700884 /* Read image without signature */
885 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
886 {
887 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
888 return -1;
889 }
Dima Zavin214cc642009-01-26 11:16:21 -0800890
Amol Jadib6be5c12012-11-14 13:39:51 -0800891 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700892 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800893
Shashank Mittalcd98d472011-08-02 14:29:24 -0700894 offset = imagesize_actual;
895 /* Read signature */
896 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
897 {
898 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
899 }
900 else
901 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800902 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700903
904 /* Verify signature */
905 auth_kernel_img = image_verify((unsigned char *)image_addr,
906 (unsigned char *)(image_addr + imagesize_actual),
907 imagesize_actual,
908 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700909
Amol Jadib6be5c12012-11-14 13:39:51 -0800910 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
911
Shashank Mittal162244e2011-08-08 19:01:25 -0700912 if(auth_kernel_img)
913 {
914 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700915 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700916 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700917 }
918
919 /* Move kernel and ramdisk to correct address */
920 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
921 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800922#if DEVICE_TREE
923 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
924#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700925
926 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700927 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700928 {
929 write_device_info_flash(&device);
930 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530931#if USE_PCOM_SECBOOT
932 set_tamper_flag(device.is_tampered);
933#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700934 }
935 else
936 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700937 offset = page_size;
938
Amol Jadib6be5c12012-11-14 13:39:51 -0800939 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
940 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
941 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
942
943 dprintf(INFO, "Loading boot image (%d): start\n",
944 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700945 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800946
947 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700948 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
949 return -1;
950 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800951 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700952
Amol Jadib6be5c12012-11-14 13:39:51 -0800953 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700954 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
955 return -1;
956 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800957 offset += ramdisk_actual;
958
959 dprintf(INFO, "Loading boot image (%d): done\n",
960 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700961 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700962
963 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800964 offset += second_actual;
965 /* Second image loading not implemented. */
966 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700967 }
968
969#if DEVICE_TREE
970 if(hdr->dt_size != 0) {
971
972 /* Read the device tree table into buffer */
973 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
974 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
975 return -1;
976 }
977
978 table = (struct dt_table*) dt_buf;
979
980 /* Restriction that the device tree entry table should be less than a page*/
981 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
982
983 /* Validate the device tree table header */
984 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
985 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
986 return -1;
987 }
988
989 /* Calculate the offset of device tree within device tree table */
990 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
991 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
992 return -1;
993 }
994
995 /* Read device device tree in the "tags_add */
996 if(flash_read(ptn, offset + dt_entry_ptr->offset,
997 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
998 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
999 return -1;
1000 }
1001 }
1002#endif
1003
Shashank Mittalcd98d472011-08-02 14:29:24 -07001004 }
David Ng183a7422009-12-07 14:55:21 -08001005continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001006
Dima Zavin214cc642009-01-26 11:16:21 -08001007 /* TODO: create/pass atags to kernel */
1008
Ajay Dudanie28a6072011-07-01 13:59:46 -07001009 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001010 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001011 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1012
1013 return 0;
1014}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001015
Shashank Mittal162244e2011-08-08 19:01:25 -07001016unsigned char info_buf[4096];
1017void write_device_info_mmc(device_info *dev)
1018{
1019 struct device_info *info = (void*) info_buf;
1020 unsigned long long ptn = 0;
1021 unsigned long long size;
1022 int index = INVALID_PTN;
1023
1024 index = partition_get_index("aboot");
1025 ptn = partition_get_offset(index);
1026 if(ptn == 0)
1027 {
1028 return;
1029 }
1030
1031 size = partition_get_size(index);
1032
1033 memcpy(info, dev, sizeof(device_info));
1034
1035 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1036 {
1037 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1038 return;
1039 }
1040}
1041
1042void read_device_info_mmc(device_info *dev)
1043{
1044 struct device_info *info = (void*) info_buf;
1045 unsigned long long ptn = 0;
1046 unsigned long long size;
1047 int index = INVALID_PTN;
1048
1049 index = partition_get_index("aboot");
1050 ptn = partition_get_offset(index);
1051 if(ptn == 0)
1052 {
1053 return;
1054 }
1055
1056 size = partition_get_size(index);
1057
1058 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1059 {
1060 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1061 return;
1062 }
1063
1064 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1065 {
1066 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1067 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001068 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001069
1070 write_device_info_mmc(info);
1071 }
1072 memcpy(dev, info, sizeof(device_info));
1073}
1074
1075void write_device_info_flash(device_info *dev)
1076{
1077 struct device_info *info = (void *) info_buf;
1078 struct ptentry *ptn;
1079 struct ptable *ptable;
1080
1081 ptable = flash_get_ptable();
1082 if (ptable == NULL)
1083 {
1084 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1085 return;
1086 }
1087
1088 ptn = ptable_find(ptable, "devinfo");
1089 if (ptn == NULL)
1090 {
1091 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1092 return;
1093 }
1094
1095 memcpy(info, dev, sizeof(device_info));
1096
1097 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1098 {
1099 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1100 return;
1101 }
1102}
1103
1104void read_device_info_flash(device_info *dev)
1105{
1106 struct device_info *info = (void*) info_buf;
1107 struct ptentry *ptn;
1108 struct ptable *ptable;
1109
1110 ptable = flash_get_ptable();
1111 if (ptable == NULL)
1112 {
1113 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1114 return;
1115 }
1116
1117 ptn = ptable_find(ptable, "devinfo");
1118 if (ptn == NULL)
1119 {
1120 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1121 return;
1122 }
1123
1124 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1125 {
1126 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1127 return;
1128 }
1129
1130 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1131 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001132 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1133 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001134 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001135 write_device_info_flash(info);
1136 }
1137 memcpy(dev, info, sizeof(device_info));
1138}
1139
1140void write_device_info(device_info *dev)
1141{
1142 if(target_is_emmc_boot())
1143 {
1144 write_device_info_mmc(dev);
1145 }
1146 else
1147 {
1148 write_device_info_flash(dev);
1149 }
1150}
1151
1152void read_device_info(device_info *dev)
1153{
1154 if(target_is_emmc_boot())
1155 {
1156 read_device_info_mmc(dev);
1157 }
1158 else
1159 {
1160 read_device_info_flash(dev);
1161 }
1162}
1163
1164void reset_device_info()
1165{
1166 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001167 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001168 write_device_info(&device);
1169}
1170
1171void set_device_root()
1172{
1173 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001174 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001175 write_device_info(&device);
1176}
1177
Amol Jadicb524072012-08-09 16:40:18 -07001178#if DEVICE_TREE
1179int copy_dtb(uint8_t *boot_image_start)
1180{
1181 uint32 dt_image_offset = 0;
1182 uint32_t n;
1183 struct dt_table *table;
1184 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001185
1186 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1187
Amol Jadicb524072012-08-09 16:40:18 -07001188 if(hdr->dt_size != 0) {
1189
1190 /* add kernel offset */
1191 dt_image_offset += page_size;
1192 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1193 dt_image_offset += n;
1194
1195 /* add ramdisk offset */
1196 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1197 dt_image_offset += n;
1198
1199 /* add second offset */
1200 if(hdr->second_size != 0) {
1201 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1202 dt_image_offset += n;
1203 }
1204
1205 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001206 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001207
1208 /* Restriction that the device tree entry table should be less than a page*/
1209 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1210
1211 /* Validate the device tree table header */
1212 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1213 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1214 return -1;
1215 }
1216
1217 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001218 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001219 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1220 return -1;
1221 }
1222
1223 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001224 memmove((void*) hdr->tags_addr,
1225 boot_image_start + dt_image_offset + dt_entry_ptr->offset,
1226 dt_entry_ptr->size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001227 } else {
1228 /*
Dima Zavin77e41f32013-03-06 16:10:43 -08001229 * If appended dev tree is found, update the atags with
1230 * memory address to the DTB appended location on RAM.
1231 * Else update with the atags address in the kernel header
Channagoud Kadabi35095622013-03-01 13:53:05 -08001232 */
Dima Zavin77e41f32013-03-06 16:10:43 -08001233 void *dtb;
1234 dtb = dev_tree_appended((void *)hdr->kernel_addr,
1235 (void *)hdr->tags_addr);
1236 if (!dtb) {
Channagoud Kadabi35095622013-03-01 13:53:05 -08001237 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1238 return -1;
1239 }
Amol Jadicb524072012-08-09 16:40:18 -07001240 }
1241
1242 /* Everything looks fine. Return success. */
1243 return 0;
1244}
1245#endif
1246
Brian Swetland9c4c0752009-01-25 16:23:50 -08001247void cmd_boot(const char *arg, void *data, unsigned sz)
1248{
1249 unsigned kernel_actual;
1250 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001251 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001252 char *ptr = ((char*) data);
1253
1254 if (sz < sizeof(hdr)) {
1255 fastboot_fail("invalid bootimage header");
1256 return;
1257 }
1258
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001259 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001260
1261 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001262 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001263
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001264 if(target_is_emmc_boot() && hdr->page_size) {
1265 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001266 page_mask = page_size - 1;
1267 }
1268
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001269 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1270 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1271
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001272 /*
1273 * Update the kernel/ramdisk/tags address if the boot image header
1274 * has default values, these default values come from mkbootimg when
1275 * the boot image is flashed using fastboot flash:raw
1276 */
1277 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001278
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001279 /* Get virtual addresses since the hdr saves physical addresses. */
1280 hdr->kernel_addr = VA(hdr->kernel_addr);
1281 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1282 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001283
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001284 /* sz should have atleast raw boot image */
1285 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001286 fastboot_fail("incomplete bootimage");
1287 return;
1288 }
1289
Dima Zavin77e41f32013-03-06 16:10:43 -08001290 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1291 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1292
Amol Jadicb524072012-08-09 16:40:18 -07001293#if DEVICE_TREE
1294 /* find correct dtb and copy it to right location */
1295 if(copy_dtb(data))
1296 {
1297 fastboot_fail("dtb not found");
1298 return;
1299 }
1300#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001301
1302 fastboot_okay("");
1303 udc_stop();
1304
Dima Zavin77e41f32013-03-06 16:10:43 -08001305 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001306 (const char*) hdr->cmdline, board_machtype(),
1307 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001308}
1309
Dima Zavin214cc642009-01-26 11:16:21 -08001310void cmd_erase(const char *arg, void *data, unsigned sz)
1311{
1312 struct ptentry *ptn;
1313 struct ptable *ptable;
1314
1315 ptable = flash_get_ptable();
1316 if (ptable == NULL) {
1317 fastboot_fail("partition table doesn't exist");
1318 return;
1319 }
1320
1321 ptn = ptable_find(ptable, arg);
1322 if (ptn == NULL) {
1323 fastboot_fail("unknown partition name");
1324 return;
1325 }
1326
1327 if (flash_erase(ptn)) {
1328 fastboot_fail("failed to erase partition");
1329 return;
1330 }
1331 fastboot_okay("");
1332}
1333
Bikas Gurungd48bd242010-09-04 19:54:32 -07001334
1335void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1336{
1337 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -08001338 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -07001339 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001340
Kinson Chikf1a43512011-07-14 11:28:39 -07001341 index = partition_get_index(arg);
1342 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001343
Kinson Chikf1a43512011-07-14 11:28:39 -07001344 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001345 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001346 return;
1347 }
neetidb4b24d62012-01-20 12:13:09 -08001348 /* Simple inefficient version of erase. Just writing
1349 0 in first block */
1350 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1351 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001352 return;
1353 }
1354 fastboot_okay("");
1355}
1356
1357
Ajay Dudani5c761132011-04-07 20:19:04 -07001358void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001359{
1360 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001361 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001362 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001363
Greg Grisco6e754772011-06-23 12:19:39 -07001364 if (!strcmp(arg, "partition"))
1365 {
1366 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001367 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001368 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001369 return;
1370 }
1371 }
Greg Grisco6e754772011-06-23 12:19:39 -07001372 else
1373 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001374 index = partition_get_index(arg);
1375 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001376 if(ptn == 0) {
1377 fastboot_fail("partition table doesn't exist");
1378 return;
1379 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001380
Greg Grisco6e754772011-06-23 12:19:39 -07001381 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1382 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1383 fastboot_fail("image is not a boot image");
1384 return;
1385 }
1386 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001387
Kinson Chikf1a43512011-07-14 11:28:39 -07001388 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001389 if (ROUND_TO_PAGE(sz,511) > size) {
1390 fastboot_fail("size too large");
1391 return;
1392 }
1393 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1394 fastboot_fail("flash write failure");
1395 return;
1396 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001397 }
1398 fastboot_okay("");
1399 return;
1400}
1401
Ajay Dudani5c761132011-04-07 20:19:04 -07001402void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1403{
1404 unsigned int chunk;
1405 unsigned int chunk_data_sz;
1406 sparse_header_t *sparse_header;
1407 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001408 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001409 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301410 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001411 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001412
Kinson Chikf1a43512011-07-14 11:28:39 -07001413 index = partition_get_index(arg);
1414 ptn = partition_get_offset(index);
1415 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001416 fastboot_fail("partition table doesn't exist");
1417 return;
1418 }
1419
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301420 size = partition_get_size(index);
1421 if (ROUND_TO_PAGE(sz,511) > size) {
1422 fastboot_fail("size too large");
1423 return;
1424 }
1425
Ajay Dudani5c761132011-04-07 20:19:04 -07001426 /* Read and skip over sparse image header */
1427 sparse_header = (sparse_header_t *) data;
1428 data += sparse_header->file_hdr_sz;
1429 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1430 {
1431 /* Skip the remaining bytes in a header that is longer than
1432 * we expected.
1433 */
1434 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1435 }
1436
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001437 dprintf (SPEW, "=== Sparse Image Header ===\n");
1438 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1439 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1440 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1441 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1442 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1443 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1444 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1445 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001446
1447 /* Start processing chunks */
1448 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1449 {
1450 /* Read and skip over chunk header */
1451 chunk_header = (chunk_header_t *) data;
1452 data += sizeof(chunk_header_t);
1453
1454 dprintf (SPEW, "=== Chunk Header ===\n");
1455 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1456 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1457 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1458
1459 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1460 {
1461 /* Skip the remaining bytes in a header that is longer than
1462 * we expected.
1463 */
1464 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1465 }
1466
1467 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1468 switch (chunk_header->chunk_type)
1469 {
1470 case CHUNK_TYPE_RAW:
1471 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1472 chunk_data_sz))
1473 {
1474 fastboot_fail("Bogus chunk size for chunk type Raw");
1475 return;
1476 }
1477
Ajay Dudaniab18f022011-05-12 14:39:22 -07001478 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1479 chunk_data_sz,
1480 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001481 {
1482 fastboot_fail("flash write failure");
1483 return;
1484 }
1485 total_blocks += chunk_header->chunk_sz;
1486 data += chunk_data_sz;
1487 break;
1488
1489 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001490 total_blocks += chunk_header->chunk_sz;
1491 break;
1492
Ajay Dudani5c761132011-04-07 20:19:04 -07001493 case CHUNK_TYPE_CRC:
1494 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1495 {
1496 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1497 return;
1498 }
1499 total_blocks += chunk_header->chunk_sz;
1500 data += chunk_data_sz;
1501 break;
1502
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001503 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001504 fastboot_fail("Unknown chunk type");
1505 return;
1506 }
1507 }
1508
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001509 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1510 total_blocks, sparse_header->total_blks);
1511
1512 if(total_blocks != sparse_header->total_blks)
1513 {
1514 fastboot_fail("sparse image write failure");
1515 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001516
1517 fastboot_okay("");
1518 return;
1519}
1520
1521void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1522{
1523 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001524 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1525 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001526
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001527#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001528 int ret=0;
1529 uint32 major_version=0;
1530 uint32 minor_version=0;
1531
1532 ret = scm_svc_version(&major_version,&minor_version);
1533 if(!ret)
1534 {
1535 if(major_version >= 2)
1536 {
1537 if( !strcmp(arg,"ssd") || !strcmp(arg,"tqs") )
1538 {
1539 ret = encrypt_scm((uint32 **) &data, &sz);
1540 if (ret != 0) {
1541 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1542 return;
1543 }
1544
1545 ret = scm_protect_keystore((uint32 *) data, sz);
1546 if (ret != 0) {
1547 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1548 return;
1549 }
1550 }
1551 else
1552 {
1553 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1554 if(ret != 0)
1555 {
1556 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1557 return;
1558 }
1559 }
1560 }
1561 else
1562 {
1563 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1564 magic_number[1] == DECRYPT_MAGIC_1)
1565 {
1566 ret = decrypt_scm((uint32 **) &data, &sz);
1567 if (ret != 0) {
1568 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1569 return;
1570 }
1571 }
1572 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1573 magic_number[1] == ENCRYPT_MAGIC_1)
1574 {
1575 ret = encrypt_scm((uint32 **) &data, &sz);
1576 if (ret != 0) {
1577 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1578 return;
1579 }
1580 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001581 }
1582 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001583 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001584 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001585 dprintf(CRITICAL,"INVALID SVC Version\n");
1586 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001587 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001588#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001589
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001590 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001591 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1592 cmd_flash_mmc_img(arg, data, sz);
1593 else
1594 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001595 return;
1596}
1597
Dima Zavin214cc642009-01-26 11:16:21 -08001598void cmd_flash(const char *arg, void *data, unsigned sz)
1599{
1600 struct ptentry *ptn;
1601 struct ptable *ptable;
1602 unsigned extra = 0;
1603
1604 ptable = flash_get_ptable();
1605 if (ptable == NULL) {
1606 fastboot_fail("partition table doesn't exist");
1607 return;
1608 }
1609
1610 ptn = ptable_find(ptable, arg);
1611 if (ptn == NULL) {
1612 fastboot_fail("unknown partition name");
1613 return;
1614 }
1615
1616 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1617 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1618 fastboot_fail("image is not a boot image");
1619 return;
1620 }
1621 }
1622
Amol Jadi5c61a952012-05-04 17:05:35 -07001623 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001624 || !strcmp(ptn->name, "userdata")
1625 || !strcmp(ptn->name, "persist")
1626 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301627 if (flash_ecc_bch_enabled())
1628 /* Spare data bytes for 8 bit ECC increased by 4 */
1629 extra = ((page_size >> 9) * 20);
1630 else
1631 extra = ((page_size >> 9) * 16);
1632 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001633 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001634
1635 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1636 if (flash_write(ptn, extra, data, sz)) {
1637 fastboot_fail("flash write failure");
1638 return;
1639 }
1640 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1641 fastboot_okay("");
1642}
1643
1644void cmd_continue(const char *arg, void *data, unsigned sz)
1645{
1646 fastboot_okay("");
1647 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001648 if (target_is_emmc_boot())
1649 {
1650 boot_linux_from_mmc();
1651 }
1652 else
1653 {
1654 boot_linux_from_flash();
1655 }
Dima Zavin214cc642009-01-26 11:16:21 -08001656}
1657
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001658void cmd_reboot(const char *arg, void *data, unsigned sz)
1659{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001660 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001661 fastboot_okay("");
1662 reboot_device(0);
1663}
1664
1665void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1666{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001667 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001668 fastboot_okay("");
1669 reboot_device(FASTBOOT_MODE);
1670}
1671
Shashank Mittal162244e2011-08-08 19:01:25 -07001672void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1673{
1674 if(!device.is_unlocked)
1675 {
1676 device.is_unlocked = 1;
1677 write_device_info(&device);
1678 }
1679 fastboot_okay("");
1680}
1681
Shashank Mittala0032282011-08-26 14:50:11 -07001682void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1683{
1684 char response[64];
1685 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1686 fastboot_info(response);
1687 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1688 fastboot_info(response);
1689 fastboot_okay("");
1690}
1691
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001692void cmd_preflash(const char *arg, void *data, unsigned sz)
1693{
1694 fastboot_okay("");
1695}
1696
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001697void splash_screen ()
1698{
1699 struct ptentry *ptn;
1700 struct ptable *ptable;
1701 struct fbcon_config *fb_display = NULL;
1702
1703 if (!target_is_emmc_boot())
1704 {
1705 ptable = flash_get_ptable();
1706 if (ptable == NULL) {
1707 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001708 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001709 }
1710
1711 ptn = ptable_find(ptable, "splash");
1712 if (ptn == NULL) {
1713 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1714 } else {
1715 fb_display = fbcon_display();
1716 if (fb_display) {
1717 if (flash_read(ptn, 0, fb_display->base,
1718 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1719 fbcon_clear();
1720 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1721 }
1722 }
1723 }
1724 }
1725}
1726
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001727/* Get the size from partiton name */
1728static void get_partition_size(const char *arg, char *response)
1729{
1730 uint64_t ptn = 0;
1731 uint64_t size;
1732 int index = INVALID_PTN;
1733
1734 index = partition_get_index(arg);
1735
1736 if (index == INVALID_PTN)
1737 {
1738 dprintf(CRITICAL, "Invalid partition index\n");
1739 return;
1740 }
1741
1742 ptn = partition_get_offset(index);
1743
1744 if(!ptn)
1745 {
1746 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1747 return;
1748 }
1749
1750 size = partition_get_size(index);
1751
1752 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1753 return;
1754}
1755
1756/*
1757 * Publish the partition type & size info
1758 * fastboot getvar will publish the required information.
1759 * fastboot getvar partition_size:<partition_name>: partition size in hex
1760 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1761 */
1762static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1763{
1764 uint8_t i;
1765
1766 for (i = 0; i < num_parts; i++) {
1767 get_partition_size(info[i].part_name, info[i].size_response);
1768
1769 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1770 {
1771 dprintf(CRITICAL, "partition size name truncated\n");
1772 return;
1773 }
1774 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1775 {
1776 dprintf(CRITICAL, "partition type name truncated\n");
1777 return;
1778 }
1779
1780 /* publish partition size & type info */
1781 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1782 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1783 }
1784}
1785
Brian Swetland9c4c0752009-01-25 16:23:50 -08001786void aboot_init(const struct app_descriptor *app)
1787{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001788 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001789 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001790 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001791
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001792 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001793 if (target_is_emmc_boot())
1794 {
1795 page_size = 2048;
1796 page_mask = page_size - 1;
1797 }
1798 else
1799 {
1800 page_size = flash_page_size();
1801 page_mask = page_size - 1;
1802 }
1803
Shashank Mittal162244e2011-08-08 19:01:25 -07001804 if(target_use_signed_kernel())
1805 {
1806 read_device_info(&device);
1807
Shashank Mittal162244e2011-08-08 19:01:25 -07001808 }
1809
Greg Griscod6250552011-06-29 14:40:23 -07001810 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001811 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001812 surf_udc_device.serialno = sn_buf;
1813
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001814 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001815 if (keys_get_state(KEY_HOME) != 0)
1816 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001817 if (keys_get_state(KEY_VOLUMEUP) != 0)
1818 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001819 if(!boot_into_recovery)
1820 {
1821 if (keys_get_state(KEY_BACK) != 0)
1822 goto fastboot;
1823 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1824 goto fastboot;
1825 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001826
1827 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001828 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001829 goto fastboot;
1830 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001831
Ajay Dudani77421292010-10-27 19:34:06 -07001832 reboot_mode = check_reboot_mode();
1833 if (reboot_mode == RECOVERY_MODE) {
1834 boot_into_recovery = 1;
1835 } else if(reboot_mode == FASTBOOT_MODE) {
1836 goto fastboot;
1837 }
1838
Shashank Mittal23b8f422010-04-16 19:27:21 -07001839 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001840 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001841 if(emmc_recovery_init())
1842 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001843 if(target_use_signed_kernel())
1844 {
1845 if((device.is_unlocked) || (device.is_tampered))
1846 {
1847 #ifdef TZ_TAMPER_FUSE
1848 set_tamper_fuse_cmd();
1849 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301850 #if USE_PCOM_SECBOOT
1851 set_tamper_flag(device.is_tampered);
1852 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001853 }
1854 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001855 boot_linux_from_mmc();
1856 }
1857 else
1858 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001859 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301860#if USE_PCOM_SECBOOT
1861 if((device.is_unlocked) || (device.is_tampered))
1862 set_tamper_flag(device.is_tampered);
1863#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001864 boot_linux_from_flash();
1865 }
Dima Zavinb4283602009-01-26 16:36:57 -08001866 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1867 "to fastboot mode.\n");
1868
1869fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001870
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001871 sz = target_get_max_flash_size();
1872
Shashank Mittal162244e2011-08-08 19:01:25 -07001873 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001874
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001875 if(!usb_init)
1876 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001877
1878 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001879
Shashank Mittal23b8f422010-04-16 19:27:21 -07001880 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001881 {
1882 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001883 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001884 }
1885 else
1886 {
1887 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001888 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001889 }
1890
1891 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001892 fastboot_register("reboot", cmd_reboot);
1893 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001894 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001895 fastboot_register("oem device-info", cmd_oem_devinfo);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001896 fastboot_register("preflash", cmd_preflash);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001897 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001898 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001899 fastboot_publish("serialno", sn_buf);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001900 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
1901 /* Max download size supported */
1902 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", sz);
1903 fastboot_publish("max-download-size", (const char *) max_download_size);
Kinson Chikf1a43512011-07-14 11:28:39 -07001904 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001905 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001906 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001907}
1908
1909APP_START(aboot)
1910 .init = aboot_init,
1911APP_END