blob: 6047ecb37be0bb115fd05aa0e3639b993cc1f9c0 [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>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070038#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
43#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080044#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080045#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080046#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070047#include <target.h>
48#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070049#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070050#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070051#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070052#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070053#include <boot_stats.h>
Dima Zavin214cc642009-01-26 11:16:21 -080054
Neeti Desai17379b82012-06-04 18:42:53 -070055#if DEVICE_TREE
56#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070057#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070058#endif
59
Shashank Mittalcd98d472011-08-02 14:29:24 -070060#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080061#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080062#include "bootimg.h"
63#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070064#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070065#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070066#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070067#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080068
Shashank Mittal162244e2011-08-08 19:01:25 -070069#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070070
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070071extern bool target_use_signed_kernel(void);
72extern void dsb();
73extern void isb();
74extern void platform_uninit(void);
75
76void write_device_info_mmc(device_info *dev);
77void write_device_info_flash(device_info *dev);
78
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070079#define EXPAND(NAME) #NAME
80#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070081
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080082#ifdef MEMBASE
83#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
84#else
David Ng183a7422009-12-07 14:55:21 -080085#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080086#endif
87
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080088#define RECOVERY_MODE 0x77665502
89#define FASTBOOT_MODE 0x77665500
90
David Ng183a7422009-12-07 14:55:21 -080091static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080092static const char *usb_sn_cmdline = " androidboot.serialno=";
Ajay Dudanica3a33c2011-11-18 08:31:40 -080093static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -070094static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -080095
Ajay Dudani6cff85e2011-02-04 16:02:16 -080096static const char *baseband_apq = " androidboot.baseband=apq";
97static const char *baseband_msm = " androidboot.baseband=msm";
98static const char *baseband_csfb = " androidboot.baseband=csfb";
99static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700100static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700101static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800102static const char *baseband_dsda = " androidboot.baseband=dsda";
103static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800104static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800105
Shashank Mittalcd98d472011-08-02 14:29:24 -0700106/* Assuming unauthorized kernel image by default */
107static int auth_kernel_img = 0;
108
Shashank Mittal162244e2011-08-08 19:01:25 -0700109static device_info device = {DEVICE_MAGIC, 0, 0};
110
Brian Swetland9c4c0752009-01-25 16:23:50 -0800111static struct udc_device surf_udc_device = {
112 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800113 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800114 .version_id = 0x0100,
115 .manufacturer = "Google",
116 .product = "Android",
117};
118
Dima Zavin42168f22009-01-30 11:52:22 -0800119struct atag_ptbl_entry
120{
121 char name[16];
122 unsigned offset;
123 unsigned size;
124 unsigned flags;
125};
126
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700127/*
128 * Partition info, required to be published
129 * for fastboot
130 */
131struct getvar_partition_info {
132 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
133 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
134 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
135 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
136 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
137};
138
139/*
140 * Right now, we are publishing the info for only
141 * three partitions
142 */
143struct getvar_partition_info part_info[] =
144{
145 { "system" , "partition-size:", "partition-type:", "", "ext4" },
146 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
147 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
148};
149
150char max_download_size[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800151char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700152
Greg Griscod2471ef2011-07-14 13:00:42 -0700153extern int emmc_recovery_init(void);
154
Kinson Chik0b1c8162011-08-31 16:31:57 -0700155#if NO_KEYPAD_DRIVER
156extern int fastboot_trigger(void);
157#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700158
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700159static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
160{
161 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700162#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
163 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
164 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
165 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700166#endif
167}
168
Dima Zavin42168f22009-01-30 11:52:22 -0800169static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
170{
171 struct atag_ptbl_entry atag_ptn;
172
173 memcpy(atag_ptn.name, ptn->name, 16);
174 atag_ptn.name[15] = '\0';
175 atag_ptn.offset = ptn->start;
176 atag_ptn.size = ptn->length;
177 atag_ptn.flags = ptn->flags;
178 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
179 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
180}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800181
Neeti Desaie245d492012-06-01 12:52:13 -0700182unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800183{
David Ng183a7422009-12-07 14:55:21 -0800184 int cmdline_len = 0;
185 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800186 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700187 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800188
Brian Swetland9c4c0752009-01-25 16:23:50 -0800189 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800190 cmdline_len = strlen(cmdline);
191 have_cmdline = 1;
192 }
193 if (target_is_emmc_boot()) {
194 cmdline_len += strlen(emmc_cmdline);
195 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800196
197 cmdline_len += strlen(usb_sn_cmdline);
198 cmdline_len += strlen(sn_buf);
199
David Ngf773dde2010-07-26 19:55:08 -0700200 if (target_pause_for_battery_charge()) {
201 pause_at_bootup = 1;
202 cmdline_len += strlen(battchg_pause);
203 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800204
Shashank Mittalcd98d472011-08-02 14:29:24 -0700205 if(target_use_signed_kernel() && auth_kernel_img) {
206 cmdline_len += strlen(auth_kernel);
207 }
208
Ajay Dudanid04110c2011-01-17 23:55:07 -0800209 /* Determine correct androidboot.baseband to use */
210 switch(target_baseband())
211 {
212 case BASEBAND_APQ:
213 cmdline_len += strlen(baseband_apq);
214 break;
215
216 case BASEBAND_MSM:
217 cmdline_len += strlen(baseband_msm);
218 break;
219
220 case BASEBAND_CSFB:
221 cmdline_len += strlen(baseband_csfb);
222 break;
223
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800224 case BASEBAND_SVLTE2A:
225 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800226 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700227
228 case BASEBAND_MDM:
229 cmdline_len += strlen(baseband_mdm);
230 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700231
232 case BASEBAND_SGLTE:
233 cmdline_len += strlen(baseband_sglte);
234 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530235
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800236 case BASEBAND_SGLTE2:
237 cmdline_len += strlen(baseband_sglte2);
238 break;
239
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530240 case BASEBAND_DSDA:
241 cmdline_len += strlen(baseband_dsda);
242 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800243
244 case BASEBAND_DSDA2:
245 cmdline_len += strlen(baseband_dsda2);
246 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800247 }
248
David Ng183a7422009-12-07 14:55:21 -0800249 if (cmdline_len > 0) {
250 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700251 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
252 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700253
Amol Jadi168b7712012-03-06 16:15:00 -0800254 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700255 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800256 if (have_cmdline) {
257 src = cmdline;
258 while ((*dst++ = *src++));
259 }
260 if (target_is_emmc_boot()) {
261 src = emmc_cmdline;
262 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700263 have_cmdline = 1;
264 while ((*dst++ = *src++));
265 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800266
267 src = usb_sn_cmdline;
268 if (have_cmdline) --dst;
269 have_cmdline = 1;
270 while ((*dst++ = *src++));
271 src = sn_buf;
272 if (have_cmdline) --dst;
273 have_cmdline = 1;
274 while ((*dst++ = *src++));
275
David Ngf773dde2010-07-26 19:55:08 -0700276 if (pause_at_bootup) {
277 src = battchg_pause;
278 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800279 while ((*dst++ = *src++));
280 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800281
Shashank Mittalcd98d472011-08-02 14:29:24 -0700282 if(target_use_signed_kernel() && auth_kernel_img) {
283 src = auth_kernel;
284 if (have_cmdline) --dst;
285 while ((*dst++ = *src++));
286 }
287
Ajay Dudanid04110c2011-01-17 23:55:07 -0800288 switch(target_baseband())
289 {
290 case BASEBAND_APQ:
291 src = baseband_apq;
292 if (have_cmdline) --dst;
293 while ((*dst++ = *src++));
294 break;
295
296 case BASEBAND_MSM:
297 src = baseband_msm;
298 if (have_cmdline) --dst;
299 while ((*dst++ = *src++));
300 break;
301
302 case BASEBAND_CSFB:
303 src = baseband_csfb;
304 if (have_cmdline) --dst;
305 while ((*dst++ = *src++));
306 break;
307
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800308 case BASEBAND_SVLTE2A:
309 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800310 if (have_cmdline) --dst;
311 while ((*dst++ = *src++));
312 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700313
314 case BASEBAND_MDM:
315 src = baseband_mdm;
316 if (have_cmdline) --dst;
317 while ((*dst++ = *src++));
318 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700319
320 case BASEBAND_SGLTE:
321 src = baseband_sglte;
322 if (have_cmdline) --dst;
323 while ((*dst++ = *src++));
324 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530325
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800326 case BASEBAND_SGLTE2:
327 src = baseband_sglte2;
328 if (have_cmdline) --dst;
329 while ((*dst++ = *src++));
330 break;
331
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530332 case BASEBAND_DSDA:
333 src = baseband_dsda;
334 if (have_cmdline) --dst;
335 while ((*dst++ = *src++));
336 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800337
338 case BASEBAND_DSDA2:
339 src = baseband_dsda2;
340 if (have_cmdline) --dst;
341 while ((*dst++ = *src++));
342 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800343 }
Neeti Desaie245d492012-06-01 12:52:13 -0700344 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800345
346 dprintf(INFO, "cmdline: %s\n", cmdline_final);
347
Neeti Desaie245d492012-06-01 12:52:13 -0700348 return cmdline_final;
349}
350
351unsigned *atag_core(unsigned *ptr)
352{
353 /* CORE */
354 *ptr++ = 2;
355 *ptr++ = 0x54410001;
356
357 return ptr;
358
359}
360
361unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
362 unsigned ramdisk_size)
363{
364 if (ramdisk_size) {
365 *ptr++ = 4;
366 *ptr++ = 0x54420005;
367 *ptr++ = (unsigned)ramdisk;
368 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800369 }
370
Neeti Desaie245d492012-06-01 12:52:13 -0700371 return ptr;
372}
373
374unsigned *atag_ptable(unsigned **ptr_addr)
375{
376 int i;
377 struct ptable *ptable;
378
379 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700380 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
381 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700382 *(*ptr_addr)++ = 0x4d534d70;
383 for (i = 0; i < ptable->count; ++i)
384 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
385 }
386
387 return (*ptr_addr);
388}
389
390unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
391{
392 int cmdline_length = 0;
393 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700394 char *dest;
395
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800396 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700397 n = (cmdline_length + 4) & (~3);
398
399 *ptr++ = (n / 4) + 2;
400 *ptr++ = 0x54410009;
401 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800402 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700403 ptr += (n / 4);
404
405 return ptr;
406}
407
408unsigned *atag_end(unsigned *ptr)
409{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800410 /* END */
411 *ptr++ = 0;
412 *ptr++ = 0;
413
Neeti Desaie245d492012-06-01 12:52:13 -0700414 return ptr;
415}
416
417void generate_atags(unsigned *ptr, const char *cmdline,
418 void *ramdisk, unsigned ramdisk_size)
419{
420
421 ptr = atag_core(ptr);
422 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
423 ptr = target_atag_mem(ptr);
424
425 /* Skip NAND partition ATAGS for eMMC boot */
426 if (!target_is_emmc_boot()){
427 ptr = atag_ptable(&ptr);
428 }
429
430 ptr = atag_cmdline(ptr, cmdline);
431 ptr = atag_end(ptr);
432}
433
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700434typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700435void boot_linux(void *kernel, unsigned *tags,
436 const char *cmdline, unsigned machtype,
437 void *ramdisk, unsigned ramdisk_size)
438{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800439 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800440#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700441 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800442#endif
443
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700444 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800445 uint32_t tags_phys = PA((addr_t)tags);
446
447 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700448
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800449 final_cmdline = update_cmdline((const char*)cmdline);
450
Neeti Desai17379b82012-06-04 18:42:53 -0700451#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800452 dprintf(INFO, "Updating device tree: start\n");
453
Neeti Desai17379b82012-06-04 18:42:53 -0700454 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800455 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700456 if(ret)
457 {
458 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
459 ASSERT(0);
460 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800461 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700462#else
Neeti Desaie245d492012-06-01 12:52:13 -0700463 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800464 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700465#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700466
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800467 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
468 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800469
470 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700471
Amol Jadi4421e652011-06-16 15:00:48 -0700472 /* do any platform specific cleanup before kernel entry */
473 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700474
Brian Swetland9c4c0752009-01-25 16:23:50 -0800475 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700476
Amol Jadi504f9fe2012-08-16 13:56:48 -0700477#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800478 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700479#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700480 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700481 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800482}
483
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800484unsigned page_size = 0;
485unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800486
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800487#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800488
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700489BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800490#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700491BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800492#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800493
Shashank Mittal23b8f422010-04-16 19:27:21 -0700494int boot_linux_from_mmc(void)
495{
496 struct boot_img_hdr *hdr = (void*) buf;
497 struct boot_img_hdr *uhdr;
498 unsigned offset = 0;
499 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700500 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700501
Shashank Mittalcd98d472011-08-02 14:29:24 -0700502 unsigned char *image_addr = 0;
503 unsigned kernel_actual;
504 unsigned ramdisk_actual;
505 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700506 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700507
508#if DEVICE_TREE
509 struct dt_table *table;
510 struct dt_entry *dt_entry_ptr;
511 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800512 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700513#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700514
Shashank Mittal23b8f422010-04-16 19:27:21 -0700515 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
516 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
517 dprintf(INFO, "Unified boot method!\n");
518 hdr = uhdr;
519 goto unified_boot;
520 }
Greg Griscod6250552011-06-29 14:40:23 -0700521 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700522 index = partition_get_index("boot");
523 ptn = partition_get_offset(index);
524 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700525 dprintf(CRITICAL, "ERROR: No boot partition found\n");
526 return -1;
527 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700528 }
529 else {
530 index = partition_get_index("recovery");
531 ptn = partition_get_offset(index);
532 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700533 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
534 return -1;
535 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700536 }
537
Greg Griscod6250552011-06-29 14:40:23 -0700538 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700539 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
540 return -1;
541 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700542
543 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700544 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700545 return -1;
546 }
547
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700548 if (hdr->page_size && (hdr->page_size != page_size)) {
549 page_size = hdr->page_size;
550 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700551 }
552
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700553 /*
554 * Update the kernel/ramdisk/tags address if the boot image header
555 * has default values, these default values come from mkbootimg when
556 * the boot image is flashed using fastboot flash:raw
557 */
558 update_ker_tags_rdisk_addr(hdr);
559
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700560 /* Get virtual addresses since the hdr saves physical addresses. */
561 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
562 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
563 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
564
Shashank Mittalcd98d472011-08-02 14:29:24 -0700565 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700566 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800567 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700568 image_addr = (unsigned char *)target_get_scratch_address();
569 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
570 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800571#if DEVICE_TREE
572 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
573 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
574#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530575 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800576#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700577 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700578
579 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700580 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700581
Amol Jadib6be5c12012-11-14 13:39:51 -0800582 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700583 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800584
Shashank Mittalcd98d472011-08-02 14:29:24 -0700585 /* Read image without signature */
586 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
587 {
588 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
589 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800590 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700591
Amol Jadib6be5c12012-11-14 13:39:51 -0800592 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700593 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800594
Shashank Mittalcd98d472011-08-02 14:29:24 -0700595 offset = imagesize_actual;
596 /* Read signature */
597 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
598 {
599 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
600 }
601 else
602 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800603 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
604
Shashank Mittalcd98d472011-08-02 14:29:24 -0700605 auth_kernel_img = image_verify((unsigned char *)image_addr,
606 (unsigned char *)(image_addr + imagesize_actual),
607 imagesize_actual,
608 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700609
Amol Jadib6be5c12012-11-14 13:39:51 -0800610 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
611
Shashank Mittal162244e2011-08-08 19:01:25 -0700612 if(auth_kernel_img)
613 {
614 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700615 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700616 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700617 }
618
Neeti Desai465491e2012-07-31 12:53:35 -0700619 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700620 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
621 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800622#if DEVICE_TREE
623 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
624#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700625
Neeti Desai465491e2012-07-31 12:53:35 -0700626 #if DEVICE_TREE
627 if(hdr->dt_size) {
628 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700629 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700630
631 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
632
633 /* Restriction that the device tree entry table should be less than a page*/
634 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
635
636 /* Validate the device tree table header */
637 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
638 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
639 return -1;
640 }
641
642 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700643 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700644 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
645 return -1;
646 }
647
648 /* Read device device tree in the "tags_add */
649 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800650 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800651 /*
652 * If appended dev tree is found, update the atags with
653 * memory address to the DTB appended location on RAM.
654 * Else update with the atags address in the kernel header
655 */
656 void *dtb;
657 dtb = dev_tree_appended((void*) hdr->kernel_addr,
658 (void *)hdr->tags_addr);
659 if (!dtb) {
660 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
661 return -1;
662 }
Neeti Desai465491e2012-07-31 12:53:35 -0700663 }
664 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700665 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700666 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700667 {
668 write_device_info_mmc(&device);
669 #ifdef TZ_TAMPER_FUSE
670 set_tamper_fuse_cmd();
671 #endif
672 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530673 #if USE_PCOM_SECBOOT
674 set_tamper_flag(device.is_tampered);
675 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700676 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700677 else
678 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800679 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
680 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
681 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700682
Amol Jadib6be5c12012-11-14 13:39:51 -0800683 dprintf(INFO, "Loading boot image (%d): start\n",
684 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700685 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800686
687 offset = page_size;
688
689 /* Load kernel */
690 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700691 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
692 return -1;
693 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800694 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700695
Amol Jadib6be5c12012-11-14 13:39:51 -0800696 /* Load ramdisk */
697 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700698 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800699 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700700 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
701 return -1;
702 }
703 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800704 offset += ramdisk_actual;
705
706 dprintf(INFO, "Loading boot image (%d): done\n",
707 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700708 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700709
710 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800711 offset += second_actual;
712 /* Second image loading not implemented. */
713 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700714 }
715
716 #if DEVICE_TREE
717 if(hdr->dt_size != 0) {
718
719 /* Read the device tree table into buffer */
720 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
721 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
722 return -1;
723 }
724 table = (struct dt_table*) dt_buf;
725
726 /* Restriction that the device tree entry table should be less than a page*/
727 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
728
729 /* Validate the device tree table header */
730 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
731 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
732 return -1;
733 }
734
735 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700736 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700737 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
738 return -1;
739 }
740
741 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700742 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
743 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
744 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
745 return -1;
746 }
Channagoud Kadabi35095622013-03-01 13:53:05 -0800747 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800748 /*
749 * If appended dev tree is found, update the atags with
750 * memory address to the DTB appended location on RAM.
751 * Else update with the atags address in the kernel header
752 */
753 void *dtb;
754 dtb = dev_tree_appended((void*) hdr->kernel_addr,
755 (void *)hdr->tags_addr);
756 if (!dtb) {
757 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
758 return -1;
759 }
Neeti Desai465491e2012-07-31 12:53:35 -0700760 }
761 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700762 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700763
764unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700765
Dima Zavin77e41f32013-03-06 16:10:43 -0800766 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700767 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700768 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
769
770 return 0;
771}
772
Dima Zavin214cc642009-01-26 11:16:21 -0800773int boot_linux_from_flash(void)
774{
775 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800776 struct ptentry *ptn;
777 struct ptable *ptable;
778 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800779
Shashank Mittalcd98d472011-08-02 14:29:24 -0700780 unsigned char *image_addr = 0;
781 unsigned kernel_actual;
782 unsigned ramdisk_actual;
783 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800784 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700785
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700786#if DEVICE_TREE
787 struct dt_table *table;
788 struct dt_entry *dt_entry_ptr;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800789 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700790#endif
791
David Ng183a7422009-12-07 14:55:21 -0800792 if (target_is_emmc_boot()) {
793 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
794 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
795 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
796 return -1;
797 }
798 goto continue_boot;
799 }
800
Dima Zavin214cc642009-01-26 11:16:21 -0800801 ptable = flash_get_ptable();
802 if (ptable == NULL) {
803 dprintf(CRITICAL, "ERROR: Partition table not found\n");
804 return -1;
805 }
806
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800807 if(!boot_into_recovery)
808 {
809 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700810
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800811 if (ptn == NULL) {
812 dprintf(CRITICAL, "ERROR: No boot partition found\n");
813 return -1;
814 }
815 }
816 else
817 {
818 ptn = ptable_find(ptable, "recovery");
819 if (ptn == NULL) {
820 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
821 return -1;
822 }
Dima Zavin214cc642009-01-26 11:16:21 -0800823 }
824
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800825 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800826 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
827 return -1;
828 }
Dima Zavin214cc642009-01-26 11:16:21 -0800829
830 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700831 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800832 return -1;
833 }
834
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800835 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700836 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 -0800837 return -1;
838 }
839
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700840 /*
841 * Update the kernel/ramdisk/tags address if the boot image header
842 * has default values, these default values come from mkbootimg when
843 * the boot image is flashed using fastboot flash:raw
844 */
845 update_ker_tags_rdisk_addr(hdr);
846
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700847 /* Get virtual addresses since the hdr saves physical addresses. */
848 hdr->kernel_addr = VA(hdr->kernel_addr);
849 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
850 hdr->tags_addr = VA(hdr->tags_addr);
851
Shashank Mittalcd98d472011-08-02 14:29:24 -0700852 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700853 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700854 {
855 image_addr = (unsigned char *)target_get_scratch_address();
856 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
857 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800858#if DEVICE_TREE
859 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
860 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
861#else
Shashank Mittalcd98d472011-08-02 14:29:24 -0700862 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800863#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700864 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700865
866 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700867 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700868
Amol Jadib6be5c12012-11-14 13:39:51 -0800869 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700870 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800871
Shashank Mittalcd98d472011-08-02 14:29:24 -0700872 /* Read image without signature */
873 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
874 {
875 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
876 return -1;
877 }
Dima Zavin214cc642009-01-26 11:16:21 -0800878
Amol Jadib6be5c12012-11-14 13:39:51 -0800879 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700880 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800881
Shashank Mittalcd98d472011-08-02 14:29:24 -0700882 offset = imagesize_actual;
883 /* Read signature */
884 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
885 {
886 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
887 }
888 else
889 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800890 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700891
892 /* Verify signature */
893 auth_kernel_img = image_verify((unsigned char *)image_addr,
894 (unsigned char *)(image_addr + imagesize_actual),
895 imagesize_actual,
896 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700897
Amol Jadib6be5c12012-11-14 13:39:51 -0800898 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
899
Shashank Mittal162244e2011-08-08 19:01:25 -0700900 if(auth_kernel_img)
901 {
902 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700903 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700904 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700905 }
906
907 /* Move kernel and ramdisk to correct address */
908 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
909 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800910#if DEVICE_TREE
911 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
912#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700913
914 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700915 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700916 {
917 write_device_info_flash(&device);
918 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530919#if USE_PCOM_SECBOOT
920 set_tamper_flag(device.is_tampered);
921#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700922 }
923 else
924 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700925 offset = page_size;
926
Amol Jadib6be5c12012-11-14 13:39:51 -0800927 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
928 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
929 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
930
931 dprintf(INFO, "Loading boot image (%d): start\n",
932 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700933 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800934
935 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700936 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
937 return -1;
938 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800939 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700940
Amol Jadib6be5c12012-11-14 13:39:51 -0800941 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700942 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
943 return -1;
944 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800945 offset += ramdisk_actual;
946
947 dprintf(INFO, "Loading boot image (%d): done\n",
948 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700949 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700950
951 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800952 offset += second_actual;
953 /* Second image loading not implemented. */
954 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700955 }
956
957#if DEVICE_TREE
958 if(hdr->dt_size != 0) {
959
960 /* Read the device tree table into buffer */
961 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
962 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
963 return -1;
964 }
965
966 table = (struct dt_table*) dt_buf;
967
968 /* Restriction that the device tree entry table should be less than a page*/
969 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
970
971 /* Validate the device tree table header */
972 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
973 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
974 return -1;
975 }
976
977 /* Calculate the offset of device tree within device tree table */
978 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
979 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
980 return -1;
981 }
982
983 /* Read device device tree in the "tags_add */
984 if(flash_read(ptn, offset + dt_entry_ptr->offset,
985 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
986 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
987 return -1;
988 }
989 }
990#endif
991
Shashank Mittalcd98d472011-08-02 14:29:24 -0700992 }
David Ng183a7422009-12-07 14:55:21 -0800993continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800994
Dima Zavin214cc642009-01-26 11:16:21 -0800995 /* TODO: create/pass atags to kernel */
996
Ajay Dudanie28a6072011-07-01 13:59:46 -0700997 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700998 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800999 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1000
1001 return 0;
1002}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001003
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001004BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001005void write_device_info_mmc(device_info *dev)
1006{
1007 struct device_info *info = (void*) info_buf;
1008 unsigned long long ptn = 0;
1009 unsigned long long size;
1010 int index = INVALID_PTN;
1011
1012 index = partition_get_index("aboot");
1013 ptn = partition_get_offset(index);
1014 if(ptn == 0)
1015 {
1016 return;
1017 }
1018
1019 size = partition_get_size(index);
1020
1021 memcpy(info, dev, sizeof(device_info));
1022
1023 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1024 {
1025 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1026 return;
1027 }
1028}
1029
1030void read_device_info_mmc(device_info *dev)
1031{
1032 struct device_info *info = (void*) info_buf;
1033 unsigned long long ptn = 0;
1034 unsigned long long size;
1035 int index = INVALID_PTN;
1036
1037 index = partition_get_index("aboot");
1038 ptn = partition_get_offset(index);
1039 if(ptn == 0)
1040 {
1041 return;
1042 }
1043
1044 size = partition_get_size(index);
1045
1046 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1047 {
1048 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1049 return;
1050 }
1051
1052 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1053 {
1054 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1055 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001056 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001057
1058 write_device_info_mmc(info);
1059 }
1060 memcpy(dev, info, sizeof(device_info));
1061}
1062
1063void write_device_info_flash(device_info *dev)
1064{
1065 struct device_info *info = (void *) info_buf;
1066 struct ptentry *ptn;
1067 struct ptable *ptable;
1068
1069 ptable = flash_get_ptable();
1070 if (ptable == NULL)
1071 {
1072 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1073 return;
1074 }
1075
1076 ptn = ptable_find(ptable, "devinfo");
1077 if (ptn == NULL)
1078 {
1079 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1080 return;
1081 }
1082
1083 memcpy(info, dev, sizeof(device_info));
1084
1085 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1086 {
1087 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1088 return;
1089 }
1090}
1091
1092void read_device_info_flash(device_info *dev)
1093{
1094 struct device_info *info = (void*) info_buf;
1095 struct ptentry *ptn;
1096 struct ptable *ptable;
1097
1098 ptable = flash_get_ptable();
1099 if (ptable == NULL)
1100 {
1101 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1102 return;
1103 }
1104
1105 ptn = ptable_find(ptable, "devinfo");
1106 if (ptn == NULL)
1107 {
1108 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1109 return;
1110 }
1111
1112 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1113 {
1114 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1115 return;
1116 }
1117
1118 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1119 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001120 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1121 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001122 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001123 write_device_info_flash(info);
1124 }
1125 memcpy(dev, info, sizeof(device_info));
1126}
1127
1128void write_device_info(device_info *dev)
1129{
1130 if(target_is_emmc_boot())
1131 {
1132 write_device_info_mmc(dev);
1133 }
1134 else
1135 {
1136 write_device_info_flash(dev);
1137 }
1138}
1139
1140void read_device_info(device_info *dev)
1141{
1142 if(target_is_emmc_boot())
1143 {
1144 read_device_info_mmc(dev);
1145 }
1146 else
1147 {
1148 read_device_info_flash(dev);
1149 }
1150}
1151
1152void reset_device_info()
1153{
1154 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001155 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001156 write_device_info(&device);
1157}
1158
1159void set_device_root()
1160{
1161 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001162 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001163 write_device_info(&device);
1164}
1165
Amol Jadicb524072012-08-09 16:40:18 -07001166#if DEVICE_TREE
1167int copy_dtb(uint8_t *boot_image_start)
1168{
1169 uint32 dt_image_offset = 0;
1170 uint32_t n;
1171 struct dt_table *table;
1172 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001173
1174 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1175
Amol Jadicb524072012-08-09 16:40:18 -07001176 if(hdr->dt_size != 0) {
1177
1178 /* add kernel offset */
1179 dt_image_offset += page_size;
1180 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1181 dt_image_offset += n;
1182
1183 /* add ramdisk offset */
1184 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1185 dt_image_offset += n;
1186
1187 /* add second offset */
1188 if(hdr->second_size != 0) {
1189 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1190 dt_image_offset += n;
1191 }
1192
1193 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001194 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001195
1196 /* Restriction that the device tree entry table should be less than a page*/
1197 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1198
1199 /* Validate the device tree table header */
1200 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1201 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1202 return -1;
1203 }
1204
1205 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001206 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001207 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1208 return -1;
1209 }
1210
1211 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001212 memmove((void*) hdr->tags_addr,
1213 boot_image_start + dt_image_offset + dt_entry_ptr->offset,
1214 dt_entry_ptr->size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001215 } else
1216 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001217
1218 /* Everything looks fine. Return success. */
1219 return 0;
1220}
1221#endif
1222
Brian Swetland9c4c0752009-01-25 16:23:50 -08001223void cmd_boot(const char *arg, void *data, unsigned sz)
1224{
1225 unsigned kernel_actual;
1226 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001227 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001228 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001229 int ret = 0;
1230 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001231
1232 if (sz < sizeof(hdr)) {
1233 fastboot_fail("invalid bootimage header");
1234 return;
1235 }
1236
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001237 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001238
1239 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001240 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001241
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001242 if(target_is_emmc_boot() && hdr->page_size) {
1243 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001244 page_mask = page_size - 1;
1245 }
1246
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001247 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1248 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1249
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001250 /*
1251 * Update the kernel/ramdisk/tags address if the boot image header
1252 * has default values, these default values come from mkbootimg when
1253 * the boot image is flashed using fastboot flash:raw
1254 */
1255 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001256
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001257 /* Get virtual addresses since the hdr saves physical addresses. */
1258 hdr->kernel_addr = VA(hdr->kernel_addr);
1259 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1260 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001261
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001262 /* sz should have atleast raw boot image */
1263 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001264 fastboot_fail("incomplete bootimage");
1265 return;
1266 }
1267
Amol Jadicb524072012-08-09 16:40:18 -07001268#if DEVICE_TREE
1269 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001270 ret = copy_dtb(data);
1271
1272 dtb_copied = !ret ? 1 : 0;
1273#endif
1274
1275 /* Load ramdisk & kernel */
1276 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1277 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1278
1279#if DEVICE_TREE
1280 /*
1281 * If dtb is not found look for appended DTB in the kernel.
1282 * If appended dev tree is found, update the atags with
1283 * memory address to the DTB appended location on RAM.
1284 * Else update with the atags address in the kernel header
1285 */
1286 if (!dtb_copied) {
1287 void *dtb;
1288 dtb = dev_tree_appended((void *)hdr->kernel_addr, (void *)hdr->tags_addr);
1289 if (!dtb) {
1290 fastboot_fail("dtb not found");
1291 return;
1292 }
Amol Jadicb524072012-08-09 16:40:18 -07001293 }
1294#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001295
1296 fastboot_okay("");
1297 udc_stop();
1298
Dima Zavin77e41f32013-03-06 16:10:43 -08001299 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001300 (const char*) hdr->cmdline, board_machtype(),
1301 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001302}
1303
Dima Zavin214cc642009-01-26 11:16:21 -08001304void cmd_erase(const char *arg, void *data, unsigned sz)
1305{
1306 struct ptentry *ptn;
1307 struct ptable *ptable;
1308
1309 ptable = flash_get_ptable();
1310 if (ptable == NULL) {
1311 fastboot_fail("partition table doesn't exist");
1312 return;
1313 }
1314
1315 ptn = ptable_find(ptable, arg);
1316 if (ptn == NULL) {
1317 fastboot_fail("unknown partition name");
1318 return;
1319 }
1320
1321 if (flash_erase(ptn)) {
1322 fastboot_fail("failed to erase partition");
1323 return;
1324 }
1325 fastboot_okay("");
1326}
1327
Bikas Gurungd48bd242010-09-04 19:54:32 -07001328
1329void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1330{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001331 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001332 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001333 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001334
Kinson Chikf1a43512011-07-14 11:28:39 -07001335 index = partition_get_index(arg);
1336 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001337
Kinson Chikf1a43512011-07-14 11:28:39 -07001338 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001339 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001340 return;
1341 }
neetidb4b24d62012-01-20 12:13:09 -08001342 /* Simple inefficient version of erase. Just writing
1343 0 in first block */
1344 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1345 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001346 return;
1347 }
1348 fastboot_okay("");
1349}
1350
1351
Ajay Dudani5c761132011-04-07 20:19:04 -07001352void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001353{
1354 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001355 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001356 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001357
Greg Grisco6e754772011-06-23 12:19:39 -07001358 if (!strcmp(arg, "partition"))
1359 {
1360 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001361 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001362 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001363 return;
1364 }
1365 }
Greg Grisco6e754772011-06-23 12:19:39 -07001366 else
1367 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001368 index = partition_get_index(arg);
1369 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001370 if(ptn == 0) {
1371 fastboot_fail("partition table doesn't exist");
1372 return;
1373 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001374
Greg Grisco6e754772011-06-23 12:19:39 -07001375 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1376 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1377 fastboot_fail("image is not a boot image");
1378 return;
1379 }
1380 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001381
Kinson Chikf1a43512011-07-14 11:28:39 -07001382 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001383 if (ROUND_TO_PAGE(sz,511) > size) {
1384 fastboot_fail("size too large");
1385 return;
1386 }
1387 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1388 fastboot_fail("flash write failure");
1389 return;
1390 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001391 }
1392 fastboot_okay("");
1393 return;
1394}
1395
Ajay Dudani5c761132011-04-07 20:19:04 -07001396void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1397{
1398 unsigned int chunk;
1399 unsigned int chunk_data_sz;
1400 sparse_header_t *sparse_header;
1401 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001402 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001403 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301404 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001405 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001406
Kinson Chikf1a43512011-07-14 11:28:39 -07001407 index = partition_get_index(arg);
1408 ptn = partition_get_offset(index);
1409 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001410 fastboot_fail("partition table doesn't exist");
1411 return;
1412 }
1413
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301414 size = partition_get_size(index);
1415 if (ROUND_TO_PAGE(sz,511) > size) {
1416 fastboot_fail("size too large");
1417 return;
1418 }
1419
Ajay Dudani5c761132011-04-07 20:19:04 -07001420 /* Read and skip over sparse image header */
1421 sparse_header = (sparse_header_t *) data;
1422 data += sparse_header->file_hdr_sz;
1423 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1424 {
1425 /* Skip the remaining bytes in a header that is longer than
1426 * we expected.
1427 */
1428 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1429 }
1430
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001431 dprintf (SPEW, "=== Sparse Image Header ===\n");
1432 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1433 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1434 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1435 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1436 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1437 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1438 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1439 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001440
1441 /* Start processing chunks */
1442 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1443 {
1444 /* Read and skip over chunk header */
1445 chunk_header = (chunk_header_t *) data;
1446 data += sizeof(chunk_header_t);
1447
1448 dprintf (SPEW, "=== Chunk Header ===\n");
1449 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1450 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1451 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1452
1453 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1454 {
1455 /* Skip the remaining bytes in a header that is longer than
1456 * we expected.
1457 */
1458 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1459 }
1460
1461 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1462 switch (chunk_header->chunk_type)
1463 {
1464 case CHUNK_TYPE_RAW:
1465 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1466 chunk_data_sz))
1467 {
1468 fastboot_fail("Bogus chunk size for chunk type Raw");
1469 return;
1470 }
1471
Ajay Dudaniab18f022011-05-12 14:39:22 -07001472 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1473 chunk_data_sz,
1474 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001475 {
1476 fastboot_fail("flash write failure");
1477 return;
1478 }
1479 total_blocks += chunk_header->chunk_sz;
1480 data += chunk_data_sz;
1481 break;
1482
1483 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001484 total_blocks += chunk_header->chunk_sz;
1485 break;
1486
Ajay Dudani5c761132011-04-07 20:19:04 -07001487 case CHUNK_TYPE_CRC:
1488 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1489 {
1490 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1491 return;
1492 }
1493 total_blocks += chunk_header->chunk_sz;
1494 data += chunk_data_sz;
1495 break;
1496
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001497 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001498 fastboot_fail("Unknown chunk type");
1499 return;
1500 }
1501 }
1502
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001503 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1504 total_blocks, sparse_header->total_blks);
1505
1506 if(total_blocks != sparse_header->total_blks)
1507 {
1508 fastboot_fail("sparse image write failure");
1509 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001510
1511 fastboot_okay("");
1512 return;
1513}
1514
1515void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1516{
1517 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001518 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1519 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001520
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001521#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001522 int ret=0;
1523 uint32 major_version=0;
1524 uint32 minor_version=0;
1525
1526 ret = scm_svc_version(&major_version,&minor_version);
1527 if(!ret)
1528 {
1529 if(major_version >= 2)
1530 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001531 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001532 {
1533 ret = encrypt_scm((uint32 **) &data, &sz);
1534 if (ret != 0) {
1535 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1536 return;
1537 }
1538
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001539 /* Protect only for SSD */
1540 if (!strcmp(arg, "ssd")) {
1541 ret = scm_protect_keystore((uint32 *) data, sz);
1542 if (ret != 0) {
1543 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1544 return;
1545 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001546 }
1547 }
1548 else
1549 {
1550 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1551 if(ret != 0)
1552 {
1553 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1554 return;
1555 }
1556 }
1557 }
1558 else
1559 {
1560 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1561 magic_number[1] == DECRYPT_MAGIC_1)
1562 {
1563 ret = decrypt_scm((uint32 **) &data, &sz);
1564 if (ret != 0) {
1565 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1566 return;
1567 }
1568 }
1569 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1570 magic_number[1] == ENCRYPT_MAGIC_1)
1571 {
1572 ret = encrypt_scm((uint32 **) &data, &sz);
1573 if (ret != 0) {
1574 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1575 return;
1576 }
1577 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001578 }
1579 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001580 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001581 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001582 dprintf(CRITICAL,"INVALID SVC Version\n");
1583 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001584 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001585#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001586
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001587 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001588 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1589 cmd_flash_mmc_img(arg, data, sz);
1590 else
1591 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001592 return;
1593}
1594
Dima Zavin214cc642009-01-26 11:16:21 -08001595void cmd_flash(const char *arg, void *data, unsigned sz)
1596{
1597 struct ptentry *ptn;
1598 struct ptable *ptable;
1599 unsigned extra = 0;
1600
1601 ptable = flash_get_ptable();
1602 if (ptable == NULL) {
1603 fastboot_fail("partition table doesn't exist");
1604 return;
1605 }
1606
1607 ptn = ptable_find(ptable, arg);
1608 if (ptn == NULL) {
1609 fastboot_fail("unknown partition name");
1610 return;
1611 }
1612
1613 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1614 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1615 fastboot_fail("image is not a boot image");
1616 return;
1617 }
1618 }
1619
Amol Jadi5c61a952012-05-04 17:05:35 -07001620 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001621 || !strcmp(ptn->name, "userdata")
1622 || !strcmp(ptn->name, "persist")
1623 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301624 if (flash_ecc_bch_enabled())
1625 /* Spare data bytes for 8 bit ECC increased by 4 */
1626 extra = ((page_size >> 9) * 20);
1627 else
1628 extra = ((page_size >> 9) * 16);
1629 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001630 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001631
1632 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1633 if (flash_write(ptn, extra, data, sz)) {
1634 fastboot_fail("flash write failure");
1635 return;
1636 }
1637 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1638 fastboot_okay("");
1639}
1640
1641void cmd_continue(const char *arg, void *data, unsigned sz)
1642{
1643 fastboot_okay("");
1644 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001645 if (target_is_emmc_boot())
1646 {
1647 boot_linux_from_mmc();
1648 }
1649 else
1650 {
1651 boot_linux_from_flash();
1652 }
Dima Zavin214cc642009-01-26 11:16:21 -08001653}
1654
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001655void cmd_reboot(const char *arg, void *data, unsigned sz)
1656{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001657 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001658 fastboot_okay("");
1659 reboot_device(0);
1660}
1661
1662void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1663{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001664 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001665 fastboot_okay("");
1666 reboot_device(FASTBOOT_MODE);
1667}
1668
Shashank Mittal162244e2011-08-08 19:01:25 -07001669void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1670{
1671 if(!device.is_unlocked)
1672 {
1673 device.is_unlocked = 1;
1674 write_device_info(&device);
1675 }
1676 fastboot_okay("");
1677}
1678
Shashank Mittala0032282011-08-26 14:50:11 -07001679void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1680{
1681 char response[64];
1682 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1683 fastboot_info(response);
1684 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1685 fastboot_info(response);
1686 fastboot_okay("");
1687}
1688
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001689void cmd_preflash(const char *arg, void *data, unsigned sz)
1690{
1691 fastboot_okay("");
1692}
1693
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001694void splash_screen ()
1695{
1696 struct ptentry *ptn;
1697 struct ptable *ptable;
1698 struct fbcon_config *fb_display = NULL;
1699
1700 if (!target_is_emmc_boot())
1701 {
1702 ptable = flash_get_ptable();
1703 if (ptable == NULL) {
1704 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001705 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001706 }
1707
1708 ptn = ptable_find(ptable, "splash");
1709 if (ptn == NULL) {
1710 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1711 } else {
1712 fb_display = fbcon_display();
1713 if (fb_display) {
1714 if (flash_read(ptn, 0, fb_display->base,
1715 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1716 fbcon_clear();
1717 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1718 }
1719 }
1720 }
1721 }
1722}
1723
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001724/* Get the size from partiton name */
1725static void get_partition_size(const char *arg, char *response)
1726{
1727 uint64_t ptn = 0;
1728 uint64_t size;
1729 int index = INVALID_PTN;
1730
1731 index = partition_get_index(arg);
1732
1733 if (index == INVALID_PTN)
1734 {
1735 dprintf(CRITICAL, "Invalid partition index\n");
1736 return;
1737 }
1738
1739 ptn = partition_get_offset(index);
1740
1741 if(!ptn)
1742 {
1743 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1744 return;
1745 }
1746
1747 size = partition_get_size(index);
1748
1749 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1750 return;
1751}
1752
1753/*
1754 * Publish the partition type & size info
1755 * fastboot getvar will publish the required information.
1756 * fastboot getvar partition_size:<partition_name>: partition size in hex
1757 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1758 */
1759static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1760{
1761 uint8_t i;
1762
1763 for (i = 0; i < num_parts; i++) {
1764 get_partition_size(info[i].part_name, info[i].size_response);
1765
1766 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1767 {
1768 dprintf(CRITICAL, "partition size name truncated\n");
1769 return;
1770 }
1771 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1772 {
1773 dprintf(CRITICAL, "partition type name truncated\n");
1774 return;
1775 }
1776
1777 /* publish partition size & type info */
1778 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1779 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1780 }
1781}
1782
Brian Swetland9c4c0752009-01-25 16:23:50 -08001783void aboot_init(const struct app_descriptor *app)
1784{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001785 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001786 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001787 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001788
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001789 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001790 if (target_is_emmc_boot())
1791 {
1792 page_size = 2048;
1793 page_mask = page_size - 1;
1794 }
1795 else
1796 {
1797 page_size = flash_page_size();
1798 page_mask = page_size - 1;
1799 }
1800
Shashank Mittal162244e2011-08-08 19:01:25 -07001801 if(target_use_signed_kernel())
1802 {
1803 read_device_info(&device);
1804
Shashank Mittal162244e2011-08-08 19:01:25 -07001805 }
1806
Greg Griscod6250552011-06-29 14:40:23 -07001807 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001808 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001809 surf_udc_device.serialno = sn_buf;
1810
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001811 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001812 if (keys_get_state(KEY_HOME) != 0)
1813 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001814 if (keys_get_state(KEY_VOLUMEUP) != 0)
1815 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001816 if(!boot_into_recovery)
1817 {
1818 if (keys_get_state(KEY_BACK) != 0)
1819 goto fastboot;
1820 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1821 goto fastboot;
1822 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001823
1824 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001825 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001826 goto fastboot;
1827 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001828
Ajay Dudani77421292010-10-27 19:34:06 -07001829 reboot_mode = check_reboot_mode();
1830 if (reboot_mode == RECOVERY_MODE) {
1831 boot_into_recovery = 1;
1832 } else if(reboot_mode == FASTBOOT_MODE) {
1833 goto fastboot;
1834 }
1835
Shashank Mittal23b8f422010-04-16 19:27:21 -07001836 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001837 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001838 if(emmc_recovery_init())
1839 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001840 if(target_use_signed_kernel())
1841 {
1842 if((device.is_unlocked) || (device.is_tampered))
1843 {
1844 #ifdef TZ_TAMPER_FUSE
1845 set_tamper_fuse_cmd();
1846 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301847 #if USE_PCOM_SECBOOT
1848 set_tamper_flag(device.is_tampered);
1849 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001850 }
1851 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001852 boot_linux_from_mmc();
1853 }
1854 else
1855 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001856 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301857#if USE_PCOM_SECBOOT
1858 if((device.is_unlocked) || (device.is_tampered))
1859 set_tamper_flag(device.is_tampered);
1860#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001861 boot_linux_from_flash();
1862 }
Dima Zavinb4283602009-01-26 16:36:57 -08001863 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1864 "to fastboot mode.\n");
1865
1866fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001867
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001868 sz = target_get_max_flash_size();
1869
Shashank Mittal162244e2011-08-08 19:01:25 -07001870 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001871
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001872 if(!usb_init)
1873 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001874
1875 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001876
Shashank Mittal23b8f422010-04-16 19:27:21 -07001877 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001878 {
1879 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001880 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001881 }
1882 else
1883 {
1884 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001885 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001886 }
1887
1888 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001889 fastboot_register("reboot", cmd_reboot);
1890 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001891 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001892 fastboot_register("oem device-info", cmd_oem_devinfo);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001893 fastboot_register("preflash", cmd_preflash);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001894 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001895 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001896 fastboot_publish("serialno", sn_buf);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001897 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
1898 /* Max download size supported */
1899 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", sz);
1900 fastboot_publish("max-download-size", (const char *) max_download_size);
Kinson Chikf1a43512011-07-14 11:28:39 -07001901 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001902 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001903 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001904}
1905
1906APP_START(aboot)
1907 .init = aboot_init,
1908APP_END