blob: 9c010915d0791d6eae53d0fcf519a371dd772831 [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=";
Pavel Nedev328ac822013-04-05 15:25:11 +030093static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -080094static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -080095static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -070096static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -080097
Ajay Dudani6cff85e2011-02-04 16:02:16 -080098static const char *baseband_apq = " androidboot.baseband=apq";
99static const char *baseband_msm = " androidboot.baseband=msm";
100static const char *baseband_csfb = " androidboot.baseband=csfb";
101static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700102static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700103static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800104static const char *baseband_dsda = " androidboot.baseband=dsda";
105static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800106static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800107
Shashank Mittalcd98d472011-08-02 14:29:24 -0700108/* Assuming unauthorized kernel image by default */
109static int auth_kernel_img = 0;
110
Shashank Mittal162244e2011-08-08 19:01:25 -0700111static device_info device = {DEVICE_MAGIC, 0, 0};
112
Brian Swetland9c4c0752009-01-25 16:23:50 -0800113static struct udc_device surf_udc_device = {
114 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800115 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800116 .version_id = 0x0100,
117 .manufacturer = "Google",
118 .product = "Android",
119};
120
Dima Zavin42168f22009-01-30 11:52:22 -0800121struct atag_ptbl_entry
122{
123 char name[16];
124 unsigned offset;
125 unsigned size;
126 unsigned flags;
127};
128
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700129/*
130 * Partition info, required to be published
131 * for fastboot
132 */
133struct getvar_partition_info {
134 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
135 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
136 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
137 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
138 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
139};
140
141/*
142 * Right now, we are publishing the info for only
143 * three partitions
144 */
145struct getvar_partition_info part_info[] =
146{
147 { "system" , "partition-size:", "partition-type:", "", "ext4" },
148 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
149 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
150};
151
152char max_download_size[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800153char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700154
Greg Griscod2471ef2011-07-14 13:00:42 -0700155extern int emmc_recovery_init(void);
156
Kinson Chik0b1c8162011-08-31 16:31:57 -0700157#if NO_KEYPAD_DRIVER
158extern int fastboot_trigger(void);
159#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700160
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700161static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
162{
163 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700164#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
165 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
166 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
167 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700168#endif
169}
170
Dima Zavin42168f22009-01-30 11:52:22 -0800171static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
172{
173 struct atag_ptbl_entry atag_ptn;
174
175 memcpy(atag_ptn.name, ptn->name, 16);
176 atag_ptn.name[15] = '\0';
177 atag_ptn.offset = ptn->start;
178 atag_ptn.size = ptn->length;
179 atag_ptn.flags = ptn->flags;
180 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
181 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
182}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800183
Neeti Desaie245d492012-06-01 12:52:13 -0700184unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800185{
David Ng183a7422009-12-07 14:55:21 -0800186 int cmdline_len = 0;
187 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800188 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700189 int pause_at_bootup = 0;
Pavel Nedev328ac822013-04-05 15:25:11 +0300190 char ffbm[10];
191 bool boot_into_ffbm = get_ffbm(ffbm, sizeof(ffbm));
Dima Zavin42168f22009-01-30 11:52:22 -0800192
Brian Swetland9c4c0752009-01-25 16:23:50 -0800193 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800194 cmdline_len = strlen(cmdline);
195 have_cmdline = 1;
196 }
197 if (target_is_emmc_boot()) {
198 cmdline_len += strlen(emmc_cmdline);
199 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800200
201 cmdline_len += strlen(usb_sn_cmdline);
202 cmdline_len += strlen(sn_buf);
203
Pavel Nedev328ac822013-04-05 15:25:11 +0300204 if (boot_into_ffbm) {
205 cmdline_len += strlen(androidboot_mode);
206 cmdline_len += strlen(ffbm);
Pavel Nedev898298c2013-02-27 12:36:09 -0800207 /* reduce kernel console messages to speed-up boot */
208 cmdline_len += strlen(loglevel);
Pavel Nedev328ac822013-04-05 15:25:11 +0300209 } else if (target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700210 pause_at_bootup = 1;
211 cmdline_len += strlen(battchg_pause);
212 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800213
Shashank Mittalcd98d472011-08-02 14:29:24 -0700214 if(target_use_signed_kernel() && auth_kernel_img) {
215 cmdline_len += strlen(auth_kernel);
216 }
217
Ajay Dudanid04110c2011-01-17 23:55:07 -0800218 /* Determine correct androidboot.baseband to use */
219 switch(target_baseband())
220 {
221 case BASEBAND_APQ:
222 cmdline_len += strlen(baseband_apq);
223 break;
224
225 case BASEBAND_MSM:
226 cmdline_len += strlen(baseband_msm);
227 break;
228
229 case BASEBAND_CSFB:
230 cmdline_len += strlen(baseband_csfb);
231 break;
232
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800233 case BASEBAND_SVLTE2A:
234 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800235 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700236
237 case BASEBAND_MDM:
238 cmdline_len += strlen(baseband_mdm);
239 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700240
241 case BASEBAND_SGLTE:
242 cmdline_len += strlen(baseband_sglte);
243 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530244
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800245 case BASEBAND_SGLTE2:
246 cmdline_len += strlen(baseband_sglte2);
247 break;
248
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530249 case BASEBAND_DSDA:
250 cmdline_len += strlen(baseband_dsda);
251 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800252
253 case BASEBAND_DSDA2:
254 cmdline_len += strlen(baseband_dsda2);
255 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800256 }
257
David Ng183a7422009-12-07 14:55:21 -0800258 if (cmdline_len > 0) {
259 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700260 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
261 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700262
Amol Jadi168b7712012-03-06 16:15:00 -0800263 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700264 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800265 if (have_cmdline) {
266 src = cmdline;
267 while ((*dst++ = *src++));
268 }
269 if (target_is_emmc_boot()) {
270 src = emmc_cmdline;
271 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700272 have_cmdline = 1;
273 while ((*dst++ = *src++));
274 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800275
276 src = usb_sn_cmdline;
277 if (have_cmdline) --dst;
278 have_cmdline = 1;
279 while ((*dst++ = *src++));
280 src = sn_buf;
281 if (have_cmdline) --dst;
282 have_cmdline = 1;
283 while ((*dst++ = *src++));
284
Pavel Nedev328ac822013-04-05 15:25:11 +0300285 if (boot_into_ffbm) {
286 src = androidboot_mode;
287 if (have_cmdline) --dst;
288 while ((*dst++ = *src++));
289 src = ffbm;
290 if (have_cmdline) --dst;
291 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800292 src = loglevel;
293 if (have_cmdline) --dst;
294 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300295 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700296 src = battchg_pause;
297 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800298 while ((*dst++ = *src++));
299 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800300
Shashank Mittalcd98d472011-08-02 14:29:24 -0700301 if(target_use_signed_kernel() && auth_kernel_img) {
302 src = auth_kernel;
303 if (have_cmdline) --dst;
304 while ((*dst++ = *src++));
305 }
306
Ajay Dudanid04110c2011-01-17 23:55:07 -0800307 switch(target_baseband())
308 {
309 case BASEBAND_APQ:
310 src = baseband_apq;
311 if (have_cmdline) --dst;
312 while ((*dst++ = *src++));
313 break;
314
315 case BASEBAND_MSM:
316 src = baseband_msm;
317 if (have_cmdline) --dst;
318 while ((*dst++ = *src++));
319 break;
320
321 case BASEBAND_CSFB:
322 src = baseband_csfb;
323 if (have_cmdline) --dst;
324 while ((*dst++ = *src++));
325 break;
326
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800327 case BASEBAND_SVLTE2A:
328 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800329 if (have_cmdline) --dst;
330 while ((*dst++ = *src++));
331 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700332
333 case BASEBAND_MDM:
334 src = baseband_mdm;
335 if (have_cmdline) --dst;
336 while ((*dst++ = *src++));
337 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700338
339 case BASEBAND_SGLTE:
340 src = baseband_sglte;
341 if (have_cmdline) --dst;
342 while ((*dst++ = *src++));
343 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530344
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800345 case BASEBAND_SGLTE2:
346 src = baseband_sglte2;
347 if (have_cmdline) --dst;
348 while ((*dst++ = *src++));
349 break;
350
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530351 case BASEBAND_DSDA:
352 src = baseband_dsda;
353 if (have_cmdline) --dst;
354 while ((*dst++ = *src++));
355 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800356
357 case BASEBAND_DSDA2:
358 src = baseband_dsda2;
359 if (have_cmdline) --dst;
360 while ((*dst++ = *src++));
361 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800362 }
Neeti Desaie245d492012-06-01 12:52:13 -0700363 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800364
365 dprintf(INFO, "cmdline: %s\n", cmdline_final);
366
Neeti Desaie245d492012-06-01 12:52:13 -0700367 return cmdline_final;
368}
369
370unsigned *atag_core(unsigned *ptr)
371{
372 /* CORE */
373 *ptr++ = 2;
374 *ptr++ = 0x54410001;
375
376 return ptr;
377
378}
379
380unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
381 unsigned ramdisk_size)
382{
383 if (ramdisk_size) {
384 *ptr++ = 4;
385 *ptr++ = 0x54420005;
386 *ptr++ = (unsigned)ramdisk;
387 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800388 }
389
Neeti Desaie245d492012-06-01 12:52:13 -0700390 return ptr;
391}
392
393unsigned *atag_ptable(unsigned **ptr_addr)
394{
395 int i;
396 struct ptable *ptable;
397
398 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700399 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
400 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700401 *(*ptr_addr)++ = 0x4d534d70;
402 for (i = 0; i < ptable->count; ++i)
403 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
404 }
405
406 return (*ptr_addr);
407}
408
409unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
410{
411 int cmdline_length = 0;
412 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700413 char *dest;
414
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800415 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700416 n = (cmdline_length + 4) & (~3);
417
418 *ptr++ = (n / 4) + 2;
419 *ptr++ = 0x54410009;
420 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800421 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700422 ptr += (n / 4);
423
424 return ptr;
425}
426
427unsigned *atag_end(unsigned *ptr)
428{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800429 /* END */
430 *ptr++ = 0;
431 *ptr++ = 0;
432
Neeti Desaie245d492012-06-01 12:52:13 -0700433 return ptr;
434}
435
436void generate_atags(unsigned *ptr, const char *cmdline,
437 void *ramdisk, unsigned ramdisk_size)
438{
439
440 ptr = atag_core(ptr);
441 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
442 ptr = target_atag_mem(ptr);
443
444 /* Skip NAND partition ATAGS for eMMC boot */
445 if (!target_is_emmc_boot()){
446 ptr = atag_ptable(&ptr);
447 }
448
449 ptr = atag_cmdline(ptr, cmdline);
450 ptr = atag_end(ptr);
451}
452
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700453typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700454void boot_linux(void *kernel, unsigned *tags,
455 const char *cmdline, unsigned machtype,
456 void *ramdisk, unsigned ramdisk_size)
457{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800458 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800459#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700460 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800461#endif
462
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700463 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800464 uint32_t tags_phys = PA((addr_t)tags);
465
466 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700467
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800468 final_cmdline = update_cmdline((const char*)cmdline);
469
Neeti Desai17379b82012-06-04 18:42:53 -0700470#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800471 dprintf(INFO, "Updating device tree: start\n");
472
Neeti Desai17379b82012-06-04 18:42:53 -0700473 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800474 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700475 if(ret)
476 {
477 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
478 ASSERT(0);
479 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800480 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700481#else
Neeti Desaie245d492012-06-01 12:52:13 -0700482 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800483 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700484#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700485
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800486 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
487 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800488
489 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700490
Amol Jadi4421e652011-06-16 15:00:48 -0700491 /* do any platform specific cleanup before kernel entry */
492 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700493
Brian Swetland9c4c0752009-01-25 16:23:50 -0800494 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700495
Amol Jadi504f9fe2012-08-16 13:56:48 -0700496#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800497 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700498#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700499 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700500 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800501}
502
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800503unsigned page_size = 0;
504unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800505
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800506#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800507
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700508BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800509#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700510BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800511#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800512
Shashank Mittal23b8f422010-04-16 19:27:21 -0700513int boot_linux_from_mmc(void)
514{
515 struct boot_img_hdr *hdr = (void*) buf;
516 struct boot_img_hdr *uhdr;
517 unsigned offset = 0;
518 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700519 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700520
Shashank Mittalcd98d472011-08-02 14:29:24 -0700521 unsigned char *image_addr = 0;
522 unsigned kernel_actual;
523 unsigned ramdisk_actual;
524 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700525 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700526
527#if DEVICE_TREE
528 struct dt_table *table;
529 struct dt_entry *dt_entry_ptr;
530 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800531 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700532#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700533
Shashank Mittal23b8f422010-04-16 19:27:21 -0700534 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
535 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
536 dprintf(INFO, "Unified boot method!\n");
537 hdr = uhdr;
538 goto unified_boot;
539 }
Greg Griscod6250552011-06-29 14:40:23 -0700540 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700541 index = partition_get_index("boot");
542 ptn = partition_get_offset(index);
543 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700544 dprintf(CRITICAL, "ERROR: No boot partition found\n");
545 return -1;
546 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700547 }
548 else {
549 index = partition_get_index("recovery");
550 ptn = partition_get_offset(index);
551 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700552 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
553 return -1;
554 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700555 }
556
Greg Griscod6250552011-06-29 14:40:23 -0700557 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700558 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
559 return -1;
560 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700561
562 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700563 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700564 return -1;
565 }
566
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700567 if (hdr->page_size && (hdr->page_size != page_size)) {
568 page_size = hdr->page_size;
569 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700570 }
571
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700572 /*
573 * Update the kernel/ramdisk/tags address if the boot image header
574 * has default values, these default values come from mkbootimg when
575 * the boot image is flashed using fastboot flash:raw
576 */
577 update_ker_tags_rdisk_addr(hdr);
578
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700579 /* Get virtual addresses since the hdr saves physical addresses. */
580 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
581 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
582 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
583
Shashank Mittalcd98d472011-08-02 14:29:24 -0700584 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700585 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800586 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700587 image_addr = (unsigned char *)target_get_scratch_address();
588 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
589 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800590#if DEVICE_TREE
591 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
592 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
593#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530594 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800595#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700596 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700597
598 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700599 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700600
Amol Jadib6be5c12012-11-14 13:39:51 -0800601 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700602 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800603
Shashank Mittalcd98d472011-08-02 14:29:24 -0700604 /* Read image without signature */
605 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
606 {
607 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
608 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800609 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700610
Amol Jadib6be5c12012-11-14 13:39:51 -0800611 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700612 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800613
Shashank Mittalcd98d472011-08-02 14:29:24 -0700614 offset = imagesize_actual;
615 /* Read signature */
616 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
617 {
618 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
619 }
620 else
621 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800622 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
623
Shashank Mittalcd98d472011-08-02 14:29:24 -0700624 auth_kernel_img = image_verify((unsigned char *)image_addr,
625 (unsigned char *)(image_addr + imagesize_actual),
626 imagesize_actual,
627 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700628
Amol Jadib6be5c12012-11-14 13:39:51 -0800629 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
630
Shashank Mittal162244e2011-08-08 19:01:25 -0700631 if(auth_kernel_img)
632 {
633 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700634 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700635 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700636 }
637
Neeti Desai465491e2012-07-31 12:53:35 -0700638 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700639 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
640 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800641#if DEVICE_TREE
642 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
643#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700644
Neeti Desai465491e2012-07-31 12:53:35 -0700645 #if DEVICE_TREE
646 if(hdr->dt_size) {
647 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700648 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700649
650 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
651
652 /* Restriction that the device tree entry table should be less than a page*/
653 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
654
655 /* Validate the device tree table header */
656 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
657 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
658 return -1;
659 }
660
661 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700662 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700663 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
664 return -1;
665 }
666
667 /* Read device device tree in the "tags_add */
668 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800669 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800670 /*
671 * If appended dev tree is found, update the atags with
672 * memory address to the DTB appended location on RAM.
673 * Else update with the atags address in the kernel header
674 */
675 void *dtb;
676 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700677 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800678 if (!dtb) {
679 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
680 return -1;
681 }
Neeti Desai465491e2012-07-31 12:53:35 -0700682 }
683 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700684 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700685 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700686 {
687 write_device_info_mmc(&device);
688 #ifdef TZ_TAMPER_FUSE
689 set_tamper_fuse_cmd();
690 #endif
691 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530692 #if USE_PCOM_SECBOOT
693 set_tamper_flag(device.is_tampered);
694 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700695 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700696 else
697 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800698 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
699 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
700 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700701
Amol Jadib6be5c12012-11-14 13:39:51 -0800702 dprintf(INFO, "Loading boot image (%d): start\n",
703 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700704 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800705
706 offset = page_size;
707
708 /* Load kernel */
709 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700710 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
711 return -1;
712 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800713 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700714
Amol Jadib6be5c12012-11-14 13:39:51 -0800715 /* Load ramdisk */
716 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700717 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800718 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700719 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
720 return -1;
721 }
722 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800723 offset += ramdisk_actual;
724
725 dprintf(INFO, "Loading boot image (%d): done\n",
726 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700727 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700728
729 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800730 offset += second_actual;
731 /* Second image loading not implemented. */
732 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700733 }
734
735 #if DEVICE_TREE
736 if(hdr->dt_size != 0) {
737
738 /* Read the device tree table into buffer */
739 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
740 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
741 return -1;
742 }
743 table = (struct dt_table*) dt_buf;
744
745 /* Restriction that the device tree entry table should be less than a page*/
746 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
747
748 /* Validate the device tree table header */
749 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
750 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
751 return -1;
752 }
753
754 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700755 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700756 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
757 return -1;
758 }
759
760 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700761 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
762 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
763 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
764 return -1;
765 }
Channagoud Kadabi35095622013-03-01 13:53:05 -0800766 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800767 /*
768 * If appended dev tree is found, update the atags with
769 * memory address to the DTB appended location on RAM.
770 * Else update with the atags address in the kernel header
771 */
772 void *dtb;
773 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700774 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800775 if (!dtb) {
776 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
777 return -1;
778 }
Neeti Desai465491e2012-07-31 12:53:35 -0700779 }
780 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700781 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700782
783unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700784
Dima Zavin77e41f32013-03-06 16:10:43 -0800785 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700786 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700787 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
788
789 return 0;
790}
791
Dima Zavin214cc642009-01-26 11:16:21 -0800792int boot_linux_from_flash(void)
793{
794 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800795 struct ptentry *ptn;
796 struct ptable *ptable;
797 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800798
Shashank Mittalcd98d472011-08-02 14:29:24 -0700799 unsigned char *image_addr = 0;
800 unsigned kernel_actual;
801 unsigned ramdisk_actual;
802 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800803 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700804
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700805#if DEVICE_TREE
806 struct dt_table *table;
807 struct dt_entry *dt_entry_ptr;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800808 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700809#endif
810
David Ng183a7422009-12-07 14:55:21 -0800811 if (target_is_emmc_boot()) {
812 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
813 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
814 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
815 return -1;
816 }
817 goto continue_boot;
818 }
819
Dima Zavin214cc642009-01-26 11:16:21 -0800820 ptable = flash_get_ptable();
821 if (ptable == NULL) {
822 dprintf(CRITICAL, "ERROR: Partition table not found\n");
823 return -1;
824 }
825
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800826 if(!boot_into_recovery)
827 {
828 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700829
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800830 if (ptn == NULL) {
831 dprintf(CRITICAL, "ERROR: No boot partition found\n");
832 return -1;
833 }
834 }
835 else
836 {
837 ptn = ptable_find(ptable, "recovery");
838 if (ptn == NULL) {
839 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
840 return -1;
841 }
Dima Zavin214cc642009-01-26 11:16:21 -0800842 }
843
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800844 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800845 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
846 return -1;
847 }
Dima Zavin214cc642009-01-26 11:16:21 -0800848
849 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700850 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800851 return -1;
852 }
853
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800854 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700855 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 -0800856 return -1;
857 }
858
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700859 /*
860 * Update the kernel/ramdisk/tags address if the boot image header
861 * has default values, these default values come from mkbootimg when
862 * the boot image is flashed using fastboot flash:raw
863 */
864 update_ker_tags_rdisk_addr(hdr);
865
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700866 /* Get virtual addresses since the hdr saves physical addresses. */
867 hdr->kernel_addr = VA(hdr->kernel_addr);
868 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
869 hdr->tags_addr = VA(hdr->tags_addr);
870
Shashank Mittalcd98d472011-08-02 14:29:24 -0700871 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700872 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700873 {
874 image_addr = (unsigned char *)target_get_scratch_address();
875 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
876 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800877#if DEVICE_TREE
878 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
879 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
880#else
Shashank Mittalcd98d472011-08-02 14:29:24 -0700881 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800882#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700883 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700884
885 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700886 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700887
Amol Jadib6be5c12012-11-14 13:39:51 -0800888 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700889 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800890
Shashank Mittalcd98d472011-08-02 14:29:24 -0700891 /* Read image without signature */
892 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
893 {
894 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
895 return -1;
896 }
Dima Zavin214cc642009-01-26 11:16:21 -0800897
Amol Jadib6be5c12012-11-14 13:39:51 -0800898 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700899 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800900
Shashank Mittalcd98d472011-08-02 14:29:24 -0700901 offset = imagesize_actual;
902 /* Read signature */
903 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
904 {
905 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
906 }
907 else
908 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800909 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700910
911 /* Verify signature */
912 auth_kernel_img = image_verify((unsigned char *)image_addr,
913 (unsigned char *)(image_addr + imagesize_actual),
914 imagesize_actual,
915 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700916
Amol Jadib6be5c12012-11-14 13:39:51 -0800917 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
918
Shashank Mittal162244e2011-08-08 19:01:25 -0700919 if(auth_kernel_img)
920 {
921 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700922 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700923 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700924 }
925
926 /* Move kernel and ramdisk to correct address */
927 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
928 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800929#if DEVICE_TREE
930 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
931#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700932
933 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700934 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700935 {
936 write_device_info_flash(&device);
937 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530938#if USE_PCOM_SECBOOT
939 set_tamper_flag(device.is_tampered);
940#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700941 }
942 else
943 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700944 offset = page_size;
945
Amol Jadib6be5c12012-11-14 13:39:51 -0800946 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
947 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
948 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
949
950 dprintf(INFO, "Loading boot image (%d): start\n",
951 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700952 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800953
954 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700955 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
956 return -1;
957 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800958 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700959
Amol Jadib6be5c12012-11-14 13:39:51 -0800960 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700961 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
962 return -1;
963 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800964 offset += ramdisk_actual;
965
966 dprintf(INFO, "Loading boot image (%d): done\n",
967 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700968 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700969
970 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800971 offset += second_actual;
972 /* Second image loading not implemented. */
973 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700974 }
975
976#if DEVICE_TREE
977 if(hdr->dt_size != 0) {
978
979 /* Read the device tree table into buffer */
980 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
981 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
982 return -1;
983 }
984
985 table = (struct dt_table*) dt_buf;
986
987 /* Restriction that the device tree entry table should be less than a page*/
988 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
989
990 /* Validate the device tree table header */
991 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
992 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
993 return -1;
994 }
995
996 /* Calculate the offset of device tree within device tree table */
997 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
998 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
999 return -1;
1000 }
1001
1002 /* Read device device tree in the "tags_add */
1003 if(flash_read(ptn, offset + dt_entry_ptr->offset,
1004 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
1005 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1006 return -1;
1007 }
1008 }
1009#endif
1010
Shashank Mittalcd98d472011-08-02 14:29:24 -07001011 }
David Ng183a7422009-12-07 14:55:21 -08001012continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001013
Dima Zavin214cc642009-01-26 11:16:21 -08001014 /* TODO: create/pass atags to kernel */
1015
Ajay Dudanie28a6072011-07-01 13:59:46 -07001016 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001017 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001018 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1019
1020 return 0;
1021}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001022
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001023BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001024void write_device_info_mmc(device_info *dev)
1025{
1026 struct device_info *info = (void*) info_buf;
1027 unsigned long long ptn = 0;
1028 unsigned long long size;
1029 int index = INVALID_PTN;
1030
1031 index = partition_get_index("aboot");
1032 ptn = partition_get_offset(index);
1033 if(ptn == 0)
1034 {
1035 return;
1036 }
1037
1038 size = partition_get_size(index);
1039
1040 memcpy(info, dev, sizeof(device_info));
1041
1042 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1043 {
1044 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1045 return;
1046 }
1047}
1048
1049void read_device_info_mmc(device_info *dev)
1050{
1051 struct device_info *info = (void*) info_buf;
1052 unsigned long long ptn = 0;
1053 unsigned long long size;
1054 int index = INVALID_PTN;
1055
1056 index = partition_get_index("aboot");
1057 ptn = partition_get_offset(index);
1058 if(ptn == 0)
1059 {
1060 return;
1061 }
1062
1063 size = partition_get_size(index);
1064
1065 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1066 {
1067 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1068 return;
1069 }
1070
1071 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1072 {
1073 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1074 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001075 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001076
1077 write_device_info_mmc(info);
1078 }
1079 memcpy(dev, info, sizeof(device_info));
1080}
1081
1082void write_device_info_flash(device_info *dev)
1083{
1084 struct device_info *info = (void *) info_buf;
1085 struct ptentry *ptn;
1086 struct ptable *ptable;
1087
1088 ptable = flash_get_ptable();
1089 if (ptable == NULL)
1090 {
1091 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1092 return;
1093 }
1094
1095 ptn = ptable_find(ptable, "devinfo");
1096 if (ptn == NULL)
1097 {
1098 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1099 return;
1100 }
1101
1102 memcpy(info, dev, sizeof(device_info));
1103
1104 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1105 {
1106 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1107 return;
1108 }
1109}
1110
1111void read_device_info_flash(device_info *dev)
1112{
1113 struct device_info *info = (void*) info_buf;
1114 struct ptentry *ptn;
1115 struct ptable *ptable;
1116
1117 ptable = flash_get_ptable();
1118 if (ptable == NULL)
1119 {
1120 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1121 return;
1122 }
1123
1124 ptn = ptable_find(ptable, "devinfo");
1125 if (ptn == NULL)
1126 {
1127 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1128 return;
1129 }
1130
1131 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1132 {
1133 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1134 return;
1135 }
1136
1137 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1138 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001139 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1140 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001141 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001142 write_device_info_flash(info);
1143 }
1144 memcpy(dev, info, sizeof(device_info));
1145}
1146
1147void write_device_info(device_info *dev)
1148{
1149 if(target_is_emmc_boot())
1150 {
1151 write_device_info_mmc(dev);
1152 }
1153 else
1154 {
1155 write_device_info_flash(dev);
1156 }
1157}
1158
1159void read_device_info(device_info *dev)
1160{
1161 if(target_is_emmc_boot())
1162 {
1163 read_device_info_mmc(dev);
1164 }
1165 else
1166 {
1167 read_device_info_flash(dev);
1168 }
1169}
1170
1171void reset_device_info()
1172{
1173 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001174 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001175 write_device_info(&device);
1176}
1177
1178void set_device_root()
1179{
1180 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001181 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001182 write_device_info(&device);
1183}
1184
Amol Jadicb524072012-08-09 16:40:18 -07001185#if DEVICE_TREE
1186int copy_dtb(uint8_t *boot_image_start)
1187{
1188 uint32 dt_image_offset = 0;
1189 uint32_t n;
1190 struct dt_table *table;
1191 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001192
1193 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1194
Amol Jadicb524072012-08-09 16:40:18 -07001195 if(hdr->dt_size != 0) {
1196
1197 /* add kernel offset */
1198 dt_image_offset += page_size;
1199 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1200 dt_image_offset += n;
1201
1202 /* add ramdisk offset */
1203 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1204 dt_image_offset += n;
1205
1206 /* add second offset */
1207 if(hdr->second_size != 0) {
1208 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1209 dt_image_offset += n;
1210 }
1211
1212 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001213 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001214
1215 /* Restriction that the device tree entry table should be less than a page*/
1216 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1217
1218 /* Validate the device tree table header */
1219 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1220 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1221 return -1;
1222 }
1223
1224 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001225 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001226 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1227 return -1;
1228 }
1229
1230 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001231 memmove((void*) hdr->tags_addr,
1232 boot_image_start + dt_image_offset + dt_entry_ptr->offset,
1233 dt_entry_ptr->size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001234 } else
1235 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001236
1237 /* Everything looks fine. Return success. */
1238 return 0;
1239}
1240#endif
1241
Brian Swetland9c4c0752009-01-25 16:23:50 -08001242void cmd_boot(const char *arg, void *data, unsigned sz)
1243{
1244 unsigned kernel_actual;
1245 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001246 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001247 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001248 int ret = 0;
1249 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001250
1251 if (sz < sizeof(hdr)) {
1252 fastboot_fail("invalid bootimage header");
1253 return;
1254 }
1255
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001256 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001257
1258 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001259 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001260
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001261 if(target_is_emmc_boot() && hdr->page_size) {
1262 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001263 page_mask = page_size - 1;
1264 }
1265
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001266 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1267 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1268
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001269 /*
1270 * Update the kernel/ramdisk/tags address if the boot image header
1271 * has default values, these default values come from mkbootimg when
1272 * the boot image is flashed using fastboot flash:raw
1273 */
1274 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001275
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001276 /* Get virtual addresses since the hdr saves physical addresses. */
1277 hdr->kernel_addr = VA(hdr->kernel_addr);
1278 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1279 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001280
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001281 /* sz should have atleast raw boot image */
1282 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001283 fastboot_fail("incomplete bootimage");
1284 return;
1285 }
1286
Amol Jadicb524072012-08-09 16:40:18 -07001287#if DEVICE_TREE
1288 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001289 ret = copy_dtb(data);
1290
1291 dtb_copied = !ret ? 1 : 0;
1292#endif
1293
1294 /* Load ramdisk & kernel */
1295 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1296 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1297
1298#if DEVICE_TREE
1299 /*
1300 * If dtb is not found look for appended DTB in the kernel.
1301 * If appended dev tree is found, update the atags with
1302 * memory address to the DTB appended location on RAM.
1303 * Else update with the atags address in the kernel header
1304 */
1305 if (!dtb_copied) {
1306 void *dtb;
Channagoud Kadabia83a6972013-04-25 15:26:04 -07001307 dtb = dev_tree_appended((void *)hdr->kernel_addr, (void *)hdr->tags_addr, hdr->kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001308 if (!dtb) {
1309 fastboot_fail("dtb not found");
1310 return;
1311 }
Amol Jadicb524072012-08-09 16:40:18 -07001312 }
1313#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001314
1315 fastboot_okay("");
1316 udc_stop();
1317
Dima Zavin77e41f32013-03-06 16:10:43 -08001318 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001319 (const char*) hdr->cmdline, board_machtype(),
1320 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001321}
1322
Dima Zavin214cc642009-01-26 11:16:21 -08001323void cmd_erase(const char *arg, void *data, unsigned sz)
1324{
1325 struct ptentry *ptn;
1326 struct ptable *ptable;
1327
1328 ptable = flash_get_ptable();
1329 if (ptable == NULL) {
1330 fastboot_fail("partition table doesn't exist");
1331 return;
1332 }
1333
1334 ptn = ptable_find(ptable, arg);
1335 if (ptn == NULL) {
1336 fastboot_fail("unknown partition name");
1337 return;
1338 }
1339
1340 if (flash_erase(ptn)) {
1341 fastboot_fail("failed to erase partition");
1342 return;
1343 }
1344 fastboot_okay("");
1345}
1346
Bikas Gurungd48bd242010-09-04 19:54:32 -07001347
1348void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1349{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001350 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001351 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001352 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001353
Kinson Chikf1a43512011-07-14 11:28:39 -07001354 index = partition_get_index(arg);
1355 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001356
Kinson Chikf1a43512011-07-14 11:28:39 -07001357 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001358 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001359 return;
1360 }
neetidb4b24d62012-01-20 12:13:09 -08001361 /* Simple inefficient version of erase. Just writing
1362 0 in first block */
1363 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1364 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001365 return;
1366 }
1367 fastboot_okay("");
1368}
1369
1370
Ajay Dudani5c761132011-04-07 20:19:04 -07001371void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001372{
1373 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001374 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001375 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001376
Greg Grisco6e754772011-06-23 12:19:39 -07001377 if (!strcmp(arg, "partition"))
1378 {
1379 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001380 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001381 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001382 return;
1383 }
1384 }
Greg Grisco6e754772011-06-23 12:19:39 -07001385 else
1386 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001387 index = partition_get_index(arg);
1388 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001389 if(ptn == 0) {
1390 fastboot_fail("partition table doesn't exist");
1391 return;
1392 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001393
Greg Grisco6e754772011-06-23 12:19:39 -07001394 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1395 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1396 fastboot_fail("image is not a boot image");
1397 return;
1398 }
1399 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001400
Kinson Chikf1a43512011-07-14 11:28:39 -07001401 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001402 if (ROUND_TO_PAGE(sz,511) > size) {
1403 fastboot_fail("size too large");
1404 return;
1405 }
1406 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1407 fastboot_fail("flash write failure");
1408 return;
1409 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001410 }
1411 fastboot_okay("");
1412 return;
1413}
1414
Ajay Dudani5c761132011-04-07 20:19:04 -07001415void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1416{
1417 unsigned int chunk;
1418 unsigned int chunk_data_sz;
1419 sparse_header_t *sparse_header;
1420 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001421 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001422 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301423 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001424 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001425
Kinson Chikf1a43512011-07-14 11:28:39 -07001426 index = partition_get_index(arg);
1427 ptn = partition_get_offset(index);
1428 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001429 fastboot_fail("partition table doesn't exist");
1430 return;
1431 }
1432
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301433 size = partition_get_size(index);
1434 if (ROUND_TO_PAGE(sz,511) > size) {
1435 fastboot_fail("size too large");
1436 return;
1437 }
1438
Ajay Dudani5c761132011-04-07 20:19:04 -07001439 /* Read and skip over sparse image header */
1440 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001441 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1442 fastboot_fail("size too large");
1443 return;
1444 }
1445
Ajay Dudani5c761132011-04-07 20:19:04 -07001446 data += sparse_header->file_hdr_sz;
1447 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1448 {
1449 /* Skip the remaining bytes in a header that is longer than
1450 * we expected.
1451 */
1452 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1453 }
1454
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001455 dprintf (SPEW, "=== Sparse Image Header ===\n");
1456 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1457 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1458 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1459 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1460 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1461 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1462 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1463 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001464
1465 /* Start processing chunks */
1466 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1467 {
1468 /* Read and skip over chunk header */
1469 chunk_header = (chunk_header_t *) data;
1470 data += sizeof(chunk_header_t);
1471
1472 dprintf (SPEW, "=== Chunk Header ===\n");
1473 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1474 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1475 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1476
1477 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1478 {
1479 /* Skip the remaining bytes in a header that is longer than
1480 * we expected.
1481 */
1482 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1483 }
1484
1485 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1486 switch (chunk_header->chunk_type)
1487 {
1488 case CHUNK_TYPE_RAW:
1489 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1490 chunk_data_sz))
1491 {
1492 fastboot_fail("Bogus chunk size for chunk type Raw");
1493 return;
1494 }
1495
Ajay Dudaniab18f022011-05-12 14:39:22 -07001496 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1497 chunk_data_sz,
1498 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001499 {
1500 fastboot_fail("flash write failure");
1501 return;
1502 }
1503 total_blocks += chunk_header->chunk_sz;
1504 data += chunk_data_sz;
1505 break;
1506
1507 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001508 total_blocks += chunk_header->chunk_sz;
1509 break;
1510
Ajay Dudani5c761132011-04-07 20:19:04 -07001511 case CHUNK_TYPE_CRC:
1512 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1513 {
1514 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1515 return;
1516 }
1517 total_blocks += chunk_header->chunk_sz;
1518 data += chunk_data_sz;
1519 break;
1520
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001521 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001522 fastboot_fail("Unknown chunk type");
1523 return;
1524 }
1525 }
1526
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001527 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1528 total_blocks, sparse_header->total_blks);
1529
1530 if(total_blocks != sparse_header->total_blks)
1531 {
1532 fastboot_fail("sparse image write failure");
1533 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001534
1535 fastboot_okay("");
1536 return;
1537}
1538
1539void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1540{
1541 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001542 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1543 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001544
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001545#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001546 int ret=0;
1547 uint32 major_version=0;
1548 uint32 minor_version=0;
1549
1550 ret = scm_svc_version(&major_version,&minor_version);
1551 if(!ret)
1552 {
1553 if(major_version >= 2)
1554 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001555 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001556 {
1557 ret = encrypt_scm((uint32 **) &data, &sz);
1558 if (ret != 0) {
1559 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1560 return;
1561 }
1562
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001563 /* Protect only for SSD */
1564 if (!strcmp(arg, "ssd")) {
1565 ret = scm_protect_keystore((uint32 *) data, sz);
1566 if (ret != 0) {
1567 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1568 return;
1569 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001570 }
1571 }
1572 else
1573 {
1574 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1575 if(ret != 0)
1576 {
1577 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1578 return;
1579 }
1580 }
1581 }
1582 else
1583 {
1584 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1585 magic_number[1] == DECRYPT_MAGIC_1)
1586 {
1587 ret = decrypt_scm((uint32 **) &data, &sz);
1588 if (ret != 0) {
1589 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1590 return;
1591 }
1592 }
1593 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1594 magic_number[1] == ENCRYPT_MAGIC_1)
1595 {
1596 ret = encrypt_scm((uint32 **) &data, &sz);
1597 if (ret != 0) {
1598 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1599 return;
1600 }
1601 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001602 }
1603 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001604 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001605 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001606 dprintf(CRITICAL,"INVALID SVC Version\n");
1607 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001608 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001609#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001610
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001611 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001612 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1613 cmd_flash_mmc_img(arg, data, sz);
1614 else
1615 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001616 return;
1617}
1618
Dima Zavin214cc642009-01-26 11:16:21 -08001619void cmd_flash(const char *arg, void *data, unsigned sz)
1620{
1621 struct ptentry *ptn;
1622 struct ptable *ptable;
1623 unsigned extra = 0;
1624
1625 ptable = flash_get_ptable();
1626 if (ptable == NULL) {
1627 fastboot_fail("partition table doesn't exist");
1628 return;
1629 }
1630
1631 ptn = ptable_find(ptable, arg);
1632 if (ptn == NULL) {
1633 fastboot_fail("unknown partition name");
1634 return;
1635 }
1636
1637 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1638 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1639 fastboot_fail("image is not a boot image");
1640 return;
1641 }
1642 }
1643
Amol Jadi5c61a952012-05-04 17:05:35 -07001644 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001645 || !strcmp(ptn->name, "userdata")
1646 || !strcmp(ptn->name, "persist")
1647 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301648 if (flash_ecc_bch_enabled())
1649 /* Spare data bytes for 8 bit ECC increased by 4 */
1650 extra = ((page_size >> 9) * 20);
1651 else
1652 extra = ((page_size >> 9) * 16);
1653 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001654 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001655
1656 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1657 if (flash_write(ptn, extra, data, sz)) {
1658 fastboot_fail("flash write failure");
1659 return;
1660 }
1661 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1662 fastboot_okay("");
1663}
1664
1665void cmd_continue(const char *arg, void *data, unsigned sz)
1666{
1667 fastboot_okay("");
1668 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001669 if (target_is_emmc_boot())
1670 {
1671 boot_linux_from_mmc();
1672 }
1673 else
1674 {
1675 boot_linux_from_flash();
1676 }
Dima Zavin214cc642009-01-26 11:16:21 -08001677}
1678
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001679void cmd_reboot(const char *arg, void *data, unsigned sz)
1680{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001681 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001682 fastboot_okay("");
1683 reboot_device(0);
1684}
1685
1686void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1687{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001688 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001689 fastboot_okay("");
1690 reboot_device(FASTBOOT_MODE);
1691}
1692
Shashank Mittal162244e2011-08-08 19:01:25 -07001693void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1694{
1695 if(!device.is_unlocked)
1696 {
1697 device.is_unlocked = 1;
1698 write_device_info(&device);
1699 }
1700 fastboot_okay("");
1701}
1702
Shashank Mittala0032282011-08-26 14:50:11 -07001703void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1704{
1705 char response[64];
1706 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1707 fastboot_info(response);
1708 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1709 fastboot_info(response);
1710 fastboot_okay("");
1711}
1712
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001713void cmd_preflash(const char *arg, void *data, unsigned sz)
1714{
1715 fastboot_okay("");
1716}
1717
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001718void splash_screen ()
1719{
1720 struct ptentry *ptn;
1721 struct ptable *ptable;
1722 struct fbcon_config *fb_display = NULL;
1723
1724 if (!target_is_emmc_boot())
1725 {
1726 ptable = flash_get_ptable();
1727 if (ptable == NULL) {
1728 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001729 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001730 }
1731
1732 ptn = ptable_find(ptable, "splash");
1733 if (ptn == NULL) {
1734 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1735 } else {
1736 fb_display = fbcon_display();
1737 if (fb_display) {
1738 if (flash_read(ptn, 0, fb_display->base,
1739 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1740 fbcon_clear();
1741 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1742 }
1743 }
1744 }
1745 }
1746}
1747
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001748/* Get the size from partiton name */
1749static void get_partition_size(const char *arg, char *response)
1750{
1751 uint64_t ptn = 0;
1752 uint64_t size;
1753 int index = INVALID_PTN;
1754
1755 index = partition_get_index(arg);
1756
1757 if (index == INVALID_PTN)
1758 {
1759 dprintf(CRITICAL, "Invalid partition index\n");
1760 return;
1761 }
1762
1763 ptn = partition_get_offset(index);
1764
1765 if(!ptn)
1766 {
1767 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1768 return;
1769 }
1770
1771 size = partition_get_size(index);
1772
1773 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1774 return;
1775}
1776
1777/*
1778 * Publish the partition type & size info
1779 * fastboot getvar will publish the required information.
1780 * fastboot getvar partition_size:<partition_name>: partition size in hex
1781 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1782 */
1783static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1784{
1785 uint8_t i;
1786
1787 for (i = 0; i < num_parts; i++) {
1788 get_partition_size(info[i].part_name, info[i].size_response);
1789
1790 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1791 {
1792 dprintf(CRITICAL, "partition size name truncated\n");
1793 return;
1794 }
1795 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1796 {
1797 dprintf(CRITICAL, "partition type name truncated\n");
1798 return;
1799 }
1800
1801 /* publish partition size & type info */
1802 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1803 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1804 }
1805}
1806
Brian Swetland9c4c0752009-01-25 16:23:50 -08001807void aboot_init(const struct app_descriptor *app)
1808{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001809 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001810 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001811 unsigned sz = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03001812 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001813
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001814 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001815 if (target_is_emmc_boot())
1816 {
1817 page_size = 2048;
1818 page_mask = page_size - 1;
1819 }
1820 else
1821 {
1822 page_size = flash_page_size();
1823 page_mask = page_size - 1;
1824 }
1825
Shashank Mittal162244e2011-08-08 19:01:25 -07001826 if(target_use_signed_kernel())
1827 {
1828 read_device_info(&device);
1829
Shashank Mittal162244e2011-08-08 19:01:25 -07001830 }
1831
Greg Griscod6250552011-06-29 14:40:23 -07001832 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001833 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001834 surf_udc_device.serialno = sn_buf;
1835
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001836 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001837 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001838 {
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001839 dprintf(ALWAYS,"dload mode key sequence detected");
1840 if (set_download_mode())
1841 {
1842 dprintf(CRITICAL,"dload mode not supported by target");
1843 }
1844 else
1845 {
1846 reboot_device(0);
1847 dprintf(CRITICAL,"Failed to reboot into dload mode");
1848 }
1849 boot_into_fastboot = true;
1850 }
1851 if (!boot_into_fastboot)
1852 {
1853 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
1854 boot_into_recovery = 1;
1855 if (!boot_into_recovery &&
1856 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03001857 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001858 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001859 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001860 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03001861 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001862 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001863
Ajay Dudani77421292010-10-27 19:34:06 -07001864 reboot_mode = check_reboot_mode();
1865 if (reboot_mode == RECOVERY_MODE) {
1866 boot_into_recovery = 1;
1867 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03001868 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07001869 }
1870
Pavel Nedev5d91d412013-04-29 11:34:24 +03001871 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001872 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03001873 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07001874 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03001875 if(emmc_recovery_init())
1876 dprintf(ALWAYS,"error in emmc_recovery_init\n");
1877 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07001878 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03001879 if((device.is_unlocked) || (device.is_tampered))
1880 {
1881 #ifdef TZ_TAMPER_FUSE
1882 set_tamper_fuse_cmd();
1883 #endif
1884 #if USE_PCOM_SECBOOT
1885 set_tamper_flag(device.is_tampered);
1886 #endif
1887 }
Shashank Mittala0032282011-08-26 14:50:11 -07001888 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03001889 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07001890 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03001891 else
1892 {
1893 recovery_init();
1894 #if USE_PCOM_SECBOOT
1895 if((device.is_unlocked) || (device.is_tampered))
1896 set_tamper_flag(device.is_tampered);
1897 #endif
1898 boot_linux_from_flash();
1899 }
1900 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1901 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001902 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001903
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001904 sz = target_get_max_flash_size();
1905
Shashank Mittal162244e2011-08-08 19:01:25 -07001906 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001907
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001908 if(!usb_init)
1909 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001910
1911 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001912
Shashank Mittal23b8f422010-04-16 19:27:21 -07001913 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001914 {
1915 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001916 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001917 }
1918 else
1919 {
1920 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001921 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001922 }
1923
1924 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001925 fastboot_register("reboot", cmd_reboot);
1926 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001927 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001928 fastboot_register("oem device-info", cmd_oem_devinfo);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001929 fastboot_register("preflash", cmd_preflash);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001930 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001931 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001932 fastboot_publish("serialno", sn_buf);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001933 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
1934 /* Max download size supported */
1935 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", sz);
1936 fastboot_publish("max-download-size", (const char *) max_download_size);
Kinson Chikf1a43512011-07-14 11:28:39 -07001937 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001938 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001939 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001940}
1941
1942APP_START(aboot)
1943 .init = aboot_init,
1944APP_END