blob: 1fe24a8e23d5a0b385d43ff7eb976cfa993265ba [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
Deepa Dinamani0e163a42013-05-24 17:08:15 -070088#ifndef MEMSIZE
89#define MEMSIZE 1024*1024
90#endif
91
92#define MAX_TAGS_SIZE 1024
93
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080094#define RECOVERY_MODE 0x77665502
95#define FASTBOOT_MODE 0x77665500
96
David Ng183a7422009-12-07 14:55:21 -080097static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080098static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +030099static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800100static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800101static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700102static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -0800103
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800104static const char *baseband_apq = " androidboot.baseband=apq";
105static const char *baseband_msm = " androidboot.baseband=msm";
106static const char *baseband_csfb = " androidboot.baseband=csfb";
107static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700108static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700109static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800110static const char *baseband_dsda = " androidboot.baseband=dsda";
111static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800112static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800113
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700114static unsigned page_size = 0;
115static unsigned page_mask = 0;
116static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
117static bool boot_into_ffbm;
118
Shashank Mittalcd98d472011-08-02 14:29:24 -0700119/* Assuming unauthorized kernel image by default */
120static int auth_kernel_img = 0;
121
Shashank Mittal162244e2011-08-08 19:01:25 -0700122static device_info device = {DEVICE_MAGIC, 0, 0};
123
Brian Swetland9c4c0752009-01-25 16:23:50 -0800124static struct udc_device surf_udc_device = {
125 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800126 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800127 .version_id = 0x0100,
128 .manufacturer = "Google",
129 .product = "Android",
130};
131
Dima Zavin42168f22009-01-30 11:52:22 -0800132struct atag_ptbl_entry
133{
134 char name[16];
135 unsigned offset;
136 unsigned size;
137 unsigned flags;
138};
139
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700140/*
141 * Partition info, required to be published
142 * for fastboot
143 */
144struct getvar_partition_info {
145 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
146 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
147 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
148 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
149 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
150};
151
152/*
153 * Right now, we are publishing the info for only
154 * three partitions
155 */
156struct getvar_partition_info part_info[] =
157{
158 { "system" , "partition-size:", "partition-type:", "", "ext4" },
159 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
160 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
161};
162
163char max_download_size[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800164char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700165
Greg Griscod2471ef2011-07-14 13:00:42 -0700166extern int emmc_recovery_init(void);
167
Kinson Chik0b1c8162011-08-31 16:31:57 -0700168#if NO_KEYPAD_DRIVER
169extern int fastboot_trigger(void);
170#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700171
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700172static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
173{
174 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700175#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
176 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
177 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
178 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700179#endif
180}
181
Dima Zavin42168f22009-01-30 11:52:22 -0800182static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
183{
184 struct atag_ptbl_entry atag_ptn;
185
186 memcpy(atag_ptn.name, ptn->name, 16);
187 atag_ptn.name[15] = '\0';
188 atag_ptn.offset = ptn->start;
189 atag_ptn.size = ptn->length;
190 atag_ptn.flags = ptn->flags;
191 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
192 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
193}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800194
Neeti Desaie245d492012-06-01 12:52:13 -0700195unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800196{
David Ng183a7422009-12-07 14:55:21 -0800197 int cmdline_len = 0;
198 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800199 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700200 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800201
Brian Swetland9c4c0752009-01-25 16:23:50 -0800202 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800203 cmdline_len = strlen(cmdline);
204 have_cmdline = 1;
205 }
206 if (target_is_emmc_boot()) {
207 cmdline_len += strlen(emmc_cmdline);
208 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800209
210 cmdline_len += strlen(usb_sn_cmdline);
211 cmdline_len += strlen(sn_buf);
212
Pavel Nedev328ac822013-04-05 15:25:11 +0300213 if (boot_into_ffbm) {
214 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700215 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800216 /* reduce kernel console messages to speed-up boot */
217 cmdline_len += strlen(loglevel);
Pavel Nedev328ac822013-04-05 15:25:11 +0300218 } else if (target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700219 pause_at_bootup = 1;
220 cmdline_len += strlen(battchg_pause);
221 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800222
Shashank Mittalcd98d472011-08-02 14:29:24 -0700223 if(target_use_signed_kernel() && auth_kernel_img) {
224 cmdline_len += strlen(auth_kernel);
225 }
226
Ajay Dudanid04110c2011-01-17 23:55:07 -0800227 /* Determine correct androidboot.baseband to use */
228 switch(target_baseband())
229 {
230 case BASEBAND_APQ:
231 cmdline_len += strlen(baseband_apq);
232 break;
233
234 case BASEBAND_MSM:
235 cmdline_len += strlen(baseband_msm);
236 break;
237
238 case BASEBAND_CSFB:
239 cmdline_len += strlen(baseband_csfb);
240 break;
241
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800242 case BASEBAND_SVLTE2A:
243 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800244 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700245
246 case BASEBAND_MDM:
247 cmdline_len += strlen(baseband_mdm);
248 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700249
250 case BASEBAND_SGLTE:
251 cmdline_len += strlen(baseband_sglte);
252 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530253
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800254 case BASEBAND_SGLTE2:
255 cmdline_len += strlen(baseband_sglte2);
256 break;
257
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530258 case BASEBAND_DSDA:
259 cmdline_len += strlen(baseband_dsda);
260 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800261
262 case BASEBAND_DSDA2:
263 cmdline_len += strlen(baseband_dsda2);
264 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800265 }
266
David Ng183a7422009-12-07 14:55:21 -0800267 if (cmdline_len > 0) {
268 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700269 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
270 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700271
Amol Jadi168b7712012-03-06 16:15:00 -0800272 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700273 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800274 if (have_cmdline) {
275 src = cmdline;
276 while ((*dst++ = *src++));
277 }
278 if (target_is_emmc_boot()) {
279 src = emmc_cmdline;
280 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700281 have_cmdline = 1;
282 while ((*dst++ = *src++));
283 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800284
285 src = usb_sn_cmdline;
286 if (have_cmdline) --dst;
287 have_cmdline = 1;
288 while ((*dst++ = *src++));
289 src = sn_buf;
290 if (have_cmdline) --dst;
291 have_cmdline = 1;
292 while ((*dst++ = *src++));
293
Pavel Nedev328ac822013-04-05 15:25:11 +0300294 if (boot_into_ffbm) {
295 src = androidboot_mode;
296 if (have_cmdline) --dst;
297 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700298 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300299 if (have_cmdline) --dst;
300 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800301 src = loglevel;
302 if (have_cmdline) --dst;
303 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300304 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700305 src = battchg_pause;
306 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800307 while ((*dst++ = *src++));
308 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800309
Shashank Mittalcd98d472011-08-02 14:29:24 -0700310 if(target_use_signed_kernel() && auth_kernel_img) {
311 src = auth_kernel;
312 if (have_cmdline) --dst;
313 while ((*dst++ = *src++));
314 }
315
Ajay Dudanid04110c2011-01-17 23:55:07 -0800316 switch(target_baseband())
317 {
318 case BASEBAND_APQ:
319 src = baseband_apq;
320 if (have_cmdline) --dst;
321 while ((*dst++ = *src++));
322 break;
323
324 case BASEBAND_MSM:
325 src = baseband_msm;
326 if (have_cmdline) --dst;
327 while ((*dst++ = *src++));
328 break;
329
330 case BASEBAND_CSFB:
331 src = baseband_csfb;
332 if (have_cmdline) --dst;
333 while ((*dst++ = *src++));
334 break;
335
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800336 case BASEBAND_SVLTE2A:
337 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800338 if (have_cmdline) --dst;
339 while ((*dst++ = *src++));
340 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700341
342 case BASEBAND_MDM:
343 src = baseband_mdm;
344 if (have_cmdline) --dst;
345 while ((*dst++ = *src++));
346 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700347
348 case BASEBAND_SGLTE:
349 src = baseband_sglte;
350 if (have_cmdline) --dst;
351 while ((*dst++ = *src++));
352 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530353
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800354 case BASEBAND_SGLTE2:
355 src = baseband_sglte2;
356 if (have_cmdline) --dst;
357 while ((*dst++ = *src++));
358 break;
359
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530360 case BASEBAND_DSDA:
361 src = baseband_dsda;
362 if (have_cmdline) --dst;
363 while ((*dst++ = *src++));
364 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800365
366 case BASEBAND_DSDA2:
367 src = baseband_dsda2;
368 if (have_cmdline) --dst;
369 while ((*dst++ = *src++));
370 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800371 }
Neeti Desaie245d492012-06-01 12:52:13 -0700372 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800373 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700374 return cmdline_final;
375}
376
377unsigned *atag_core(unsigned *ptr)
378{
379 /* CORE */
380 *ptr++ = 2;
381 *ptr++ = 0x54410001;
382
383 return ptr;
384
385}
386
387unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
388 unsigned ramdisk_size)
389{
390 if (ramdisk_size) {
391 *ptr++ = 4;
392 *ptr++ = 0x54420005;
393 *ptr++ = (unsigned)ramdisk;
394 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800395 }
396
Neeti Desaie245d492012-06-01 12:52:13 -0700397 return ptr;
398}
399
400unsigned *atag_ptable(unsigned **ptr_addr)
401{
402 int i;
403 struct ptable *ptable;
404
405 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700406 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
407 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700408 *(*ptr_addr)++ = 0x4d534d70;
409 for (i = 0; i < ptable->count; ++i)
410 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
411 }
412
413 return (*ptr_addr);
414}
415
416unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
417{
418 int cmdline_length = 0;
419 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700420 char *dest;
421
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800422 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700423 n = (cmdline_length + 4) & (~3);
424
425 *ptr++ = (n / 4) + 2;
426 *ptr++ = 0x54410009;
427 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800428 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700429 ptr += (n / 4);
430
431 return ptr;
432}
433
434unsigned *atag_end(unsigned *ptr)
435{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800436 /* END */
437 *ptr++ = 0;
438 *ptr++ = 0;
439
Neeti Desaie245d492012-06-01 12:52:13 -0700440 return ptr;
441}
442
443void generate_atags(unsigned *ptr, const char *cmdline,
444 void *ramdisk, unsigned ramdisk_size)
445{
446
447 ptr = atag_core(ptr);
448 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
449 ptr = target_atag_mem(ptr);
450
451 /* Skip NAND partition ATAGS for eMMC boot */
452 if (!target_is_emmc_boot()){
453 ptr = atag_ptable(&ptr);
454 }
455
456 ptr = atag_cmdline(ptr, cmdline);
457 ptr = atag_end(ptr);
458}
459
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700460typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700461void boot_linux(void *kernel, unsigned *tags,
462 const char *cmdline, unsigned machtype,
463 void *ramdisk, unsigned ramdisk_size)
464{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800465 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800466#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700467 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800468#endif
469
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700470 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800471 uint32_t tags_phys = PA((addr_t)tags);
472
473 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700474
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800475 final_cmdline = update_cmdline((const char*)cmdline);
476
Neeti Desai17379b82012-06-04 18:42:53 -0700477#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800478 dprintf(INFO, "Updating device tree: start\n");
479
Neeti Desai17379b82012-06-04 18:42:53 -0700480 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800481 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700482 if(ret)
483 {
484 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
485 ASSERT(0);
486 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800487 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700488#else
Neeti Desaie245d492012-06-01 12:52:13 -0700489 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800490 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700491#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700492
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800493 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
494 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800495
496 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700497
Amol Jadi4421e652011-06-16 15:00:48 -0700498 /* do any platform specific cleanup before kernel entry */
499 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700500
Brian Swetland9c4c0752009-01-25 16:23:50 -0800501 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700502
Amol Jadi504f9fe2012-08-16 13:56:48 -0700503#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800504 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700505#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700506 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700507 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800508}
509
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700510/* Function to check if the memory address range falls within the aboot
511 * boundaries.
512 * start: Start of the memory region
513 * size: Size of the memory region
514 */
515int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
516{
517 /* Check for boundary conditions. */
518 if ((start + size) < start)
519 return -1;
520
521 /* Check for memory overlap. */
522 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
523 return 0;
524 else if (start > (MEMBASE + MEMSIZE))
525 return 0;
526 else
527 return -1;
528}
529
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800530#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800531
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700532BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800533#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700534BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800535#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800536
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700537static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
538{
539 int ret;
540
541 /* Assume device is rooted at this time. */
542 device.is_tampered = 1;
543
544 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
545
546 ret = image_verify((unsigned char *)bootimg_addr,
547 (unsigned char *)(bootimg_addr + bootimg_size),
548 bootimg_size,
549 CRYPTO_AUTH_ALG_SHA256);
550
551 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
552
553 if (ret)
554 {
555 /* Authorized kernel */
556 device.is_tampered = 0;
557 }
558
559#if USE_PCOM_SECBOOT
560 set_tamper_flag(device.is_tampered);
561#endif
562
563 if(device.is_tampered)
564 {
565 write_device_info_mmc(&device);
566 #ifdef TZ_TAMPER_FUSE
567 set_tamper_fuse_cmd();
568 #endif
569 #ifdef ASSERT_ON_TAMPER
570 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
571 ASSERT(0);
572 #endif
573 }
574}
575
Shashank Mittal23b8f422010-04-16 19:27:21 -0700576int boot_linux_from_mmc(void)
577{
578 struct boot_img_hdr *hdr = (void*) buf;
579 struct boot_img_hdr *uhdr;
580 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700581 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700582 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700583 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700584
Shashank Mittalcd98d472011-08-02 14:29:24 -0700585 unsigned char *image_addr = 0;
586 unsigned kernel_actual;
587 unsigned ramdisk_actual;
588 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700589 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700590
591#if DEVICE_TREE
592 struct dt_table *table;
593 struct dt_entry *dt_entry_ptr;
594 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800595 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700596#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700597 if (!boot_into_recovery) {
598 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
599 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
600 if (rcode <= 0) {
601 boot_into_ffbm = false;
602 if (rcode < 0)
603 dprintf(CRITICAL,"failed to get ffbm cookie");
604 } else
605 boot_into_ffbm = true;
606 } else
607 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700608 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
609 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
610 dprintf(INFO, "Unified boot method!\n");
611 hdr = uhdr;
612 goto unified_boot;
613 }
Greg Griscod6250552011-06-29 14:40:23 -0700614 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700615 index = partition_get_index("boot");
616 ptn = partition_get_offset(index);
617 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700618 dprintf(CRITICAL, "ERROR: No boot partition found\n");
619 return -1;
620 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700621 }
622 else {
623 index = partition_get_index("recovery");
624 ptn = partition_get_offset(index);
625 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700626 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
627 return -1;
628 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700629 }
630
Greg Griscod6250552011-06-29 14:40:23 -0700631 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700632 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
633 return -1;
634 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700635
636 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700637 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700638 return -1;
639 }
640
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700641 if (hdr->page_size && (hdr->page_size != page_size)) {
642 page_size = hdr->page_size;
643 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700644 }
645
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700646 /*
647 * Update the kernel/ramdisk/tags address if the boot image header
648 * has default values, these default values come from mkbootimg when
649 * the boot image is flashed using fastboot flash:raw
650 */
651 update_ker_tags_rdisk_addr(hdr);
652
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700653 /* Get virtual addresses since the hdr saves physical addresses. */
654 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
655 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
656 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700657
658 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
659 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
660
661 /* Check if the addresses in the header are valid. */
662 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
663 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
664 {
665 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
666 return -1;
667 }
668
669#ifndef DEVICE_TREE
670 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
671 {
672 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
673 return -1;
674 }
675#endif
676
Shashank Mittalcd98d472011-08-02 14:29:24 -0700677 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700678 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800679 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700680 offset = 0;
681
Shashank Mittalcd98d472011-08-02 14:29:24 -0700682 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700683
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800684#if DEVICE_TREE
685 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
686 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700687
688 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
689 {
690 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
691 return -1;
692 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800693#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530694 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700695
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700696#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700697
Amol Jadib6be5c12012-11-14 13:39:51 -0800698 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700699 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800700
Shashank Mittalcd98d472011-08-02 14:29:24 -0700701 /* Read image without signature */
702 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
703 {
704 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
705 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800706 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700707
Amol Jadib6be5c12012-11-14 13:39:51 -0800708 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700709 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800710
Shashank Mittalcd98d472011-08-02 14:29:24 -0700711 offset = imagesize_actual;
712 /* Read signature */
713 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
714 {
715 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700716 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700717 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800718
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700719 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700720
Neeti Desai465491e2012-07-31 12:53:35 -0700721 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700722 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
723 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700724
Neeti Desai465491e2012-07-31 12:53:35 -0700725 #if DEVICE_TREE
726 if(hdr->dt_size) {
727 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700728 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700729
730 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
731
732 /* Restriction that the device tree entry table should be less than a page*/
733 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
734
735 /* Validate the device tree table header */
736 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
737 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
738 return -1;
739 }
740
741 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700742 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700743 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
744 return -1;
745 }
746
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700747 /* Validate and Read device device tree in the "tags_add */
748 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
749 {
750 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
751 return -1;
752 }
753
Neeti Desai465491e2012-07-31 12:53:35 -0700754 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800755 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800756 /*
757 * If appended dev tree is found, update the atags with
758 * memory address to the DTB appended location on RAM.
759 * Else update with the atags address in the kernel header
760 */
761 void *dtb;
762 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700763 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800764 if (!dtb) {
765 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
766 return -1;
767 }
Neeti Desai465491e2012-07-31 12:53:35 -0700768 }
769 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700770 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700771 else
772 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800773 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700774
Amol Jadib6be5c12012-11-14 13:39:51 -0800775 dprintf(INFO, "Loading boot image (%d): start\n",
776 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700777 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800778
779 offset = page_size;
780
781 /* Load kernel */
782 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700783 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
784 return -1;
785 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800786 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700787
Amol Jadib6be5c12012-11-14 13:39:51 -0800788 /* Load ramdisk */
789 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700790 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800791 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700792 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
793 return -1;
794 }
795 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800796 offset += ramdisk_actual;
797
798 dprintf(INFO, "Loading boot image (%d): done\n",
799 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700800 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700801
802 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800803 offset += second_actual;
804 /* Second image loading not implemented. */
805 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700806 }
807
808 #if DEVICE_TREE
809 if(hdr->dt_size != 0) {
810
811 /* Read the device tree table into buffer */
812 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
813 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
814 return -1;
815 }
816 table = (struct dt_table*) dt_buf;
817
818 /* Restriction that the device tree entry table should be less than a page*/
819 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
820
821 /* Validate the device tree table header */
822 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
823 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
824 return -1;
825 }
826
827 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700828 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700829 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
830 return -1;
831 }
832
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700833 /* Validate and Read device device tree in the "tags_add */
834 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
835 {
836 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
837 return -1;
838 }
839
Neeti Desai465491e2012-07-31 12:53:35 -0700840 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
841 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
842 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
843 return -1;
844 }
Channagoud Kadabi35095622013-03-01 13:53:05 -0800845 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800846 /*
847 * If appended dev tree is found, update the atags with
848 * memory address to the DTB appended location on RAM.
849 * Else update with the atags address in the kernel header
850 */
851 void *dtb;
852 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700853 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800854 if (!dtb) {
855 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
856 return -1;
857 }
Neeti Desai465491e2012-07-31 12:53:35 -0700858 }
859 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700860 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700861
862unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700863
Dima Zavin77e41f32013-03-06 16:10:43 -0800864 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700865 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700866 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
867
868 return 0;
869}
870
Dima Zavin214cc642009-01-26 11:16:21 -0800871int boot_linux_from_flash(void)
872{
873 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800874 struct ptentry *ptn;
875 struct ptable *ptable;
876 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800877
Shashank Mittalcd98d472011-08-02 14:29:24 -0700878 unsigned char *image_addr = 0;
879 unsigned kernel_actual;
880 unsigned ramdisk_actual;
881 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800882 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700883
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700884#if DEVICE_TREE
885 struct dt_table *table;
886 struct dt_entry *dt_entry_ptr;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800887 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700888#endif
889
David Ng183a7422009-12-07 14:55:21 -0800890 if (target_is_emmc_boot()) {
891 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
892 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
893 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
894 return -1;
895 }
896 goto continue_boot;
897 }
898
Dima Zavin214cc642009-01-26 11:16:21 -0800899 ptable = flash_get_ptable();
900 if (ptable == NULL) {
901 dprintf(CRITICAL, "ERROR: Partition table not found\n");
902 return -1;
903 }
904
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800905 if(!boot_into_recovery)
906 {
907 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700908
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800909 if (ptn == NULL) {
910 dprintf(CRITICAL, "ERROR: No boot partition found\n");
911 return -1;
912 }
913 }
914 else
915 {
916 ptn = ptable_find(ptable, "recovery");
917 if (ptn == NULL) {
918 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
919 return -1;
920 }
Dima Zavin214cc642009-01-26 11:16:21 -0800921 }
922
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800923 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800924 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
925 return -1;
926 }
Dima Zavin214cc642009-01-26 11:16:21 -0800927
928 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700929 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800930 return -1;
931 }
932
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800933 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700934 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 -0800935 return -1;
936 }
937
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700938 /*
939 * Update the kernel/ramdisk/tags address if the boot image header
940 * has default values, these default values come from mkbootimg when
941 * the boot image is flashed using fastboot flash:raw
942 */
943 update_ker_tags_rdisk_addr(hdr);
944
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700945 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700946 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
947 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
948 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
949
950 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
951 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
952
953 /* Check if the addresses in the header are valid. */
954 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
955 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
956 {
957 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
958 return -1;
959 }
960
961#ifndef DEVICE_TREE
962 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
963 {
964 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
965 return -1;
966 }
967#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700968
Shashank Mittalcd98d472011-08-02 14:29:24 -0700969 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700970 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700971 {
972 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700973 offset = 0;
974
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800975#if DEVICE_TREE
976 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
977 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700978
979 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
980 {
981 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
982 return -1;
983 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800984#else
Shashank Mittalcd98d472011-08-02 14:29:24 -0700985 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800986#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700987
Amol Jadib6be5c12012-11-14 13:39:51 -0800988 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700989 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800990
Shashank Mittalcd98d472011-08-02 14:29:24 -0700991 /* Read image without signature */
992 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
993 {
994 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
995 return -1;
996 }
Dima Zavin214cc642009-01-26 11:16:21 -0800997
Amol Jadib6be5c12012-11-14 13:39:51 -0800998 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700999 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001000
Shashank Mittalcd98d472011-08-02 14:29:24 -07001001 offset = imagesize_actual;
1002 /* Read signature */
1003 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1004 {
1005 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001006 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001007 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001008
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001009 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001010
1011 /* Move kernel and ramdisk to correct address */
1012 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1013 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001014#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001015 /* Validate and Read device device tree in the "tags_add */
1016 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
1017 {
1018 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1019 return -1;
1020 }
1021
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001022 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1023#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001024
1025 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001026 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001027 {
1028 write_device_info_flash(&device);
1029 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301030#if USE_PCOM_SECBOOT
1031 set_tamper_flag(device.is_tampered);
1032#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001033 }
1034 else
1035 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001036 offset = page_size;
1037
Amol Jadib6be5c12012-11-14 13:39:51 -08001038 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1039 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1040 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1041
1042 dprintf(INFO, "Loading boot image (%d): start\n",
1043 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001044 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001045
1046 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001047 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1048 return -1;
1049 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001050 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001051
Amol Jadib6be5c12012-11-14 13:39:51 -08001052 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001053 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1054 return -1;
1055 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001056 offset += ramdisk_actual;
1057
1058 dprintf(INFO, "Loading boot image (%d): done\n",
1059 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001060 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001061
1062 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001063 offset += second_actual;
1064 /* Second image loading not implemented. */
1065 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001066 }
1067
1068#if DEVICE_TREE
1069 if(hdr->dt_size != 0) {
1070
1071 /* Read the device tree table into buffer */
1072 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1073 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1074 return -1;
1075 }
1076
1077 table = (struct dt_table*) dt_buf;
1078
1079 /* Restriction that the device tree entry table should be less than a page*/
1080 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1081
1082 /* Validate the device tree table header */
1083 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1084 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1085 return -1;
1086 }
1087
1088 /* Calculate the offset of device tree within device tree table */
1089 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
1090 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1091 return -1;
1092 }
1093
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001094 /* Validate and Read device device tree in the "tags_add */
1095 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
1096 {
1097 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1098 return -1;
1099 }
1100
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001101 /* Read device device tree in the "tags_add */
1102 if(flash_read(ptn, offset + dt_entry_ptr->offset,
1103 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
1104 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1105 return -1;
1106 }
1107 }
1108#endif
1109
Shashank Mittalcd98d472011-08-02 14:29:24 -07001110 }
David Ng183a7422009-12-07 14:55:21 -08001111continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001112
Dima Zavin214cc642009-01-26 11:16:21 -08001113 /* TODO: create/pass atags to kernel */
1114
Ajay Dudanie28a6072011-07-01 13:59:46 -07001115 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001116 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001117 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1118
1119 return 0;
1120}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001121
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001122BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001123void write_device_info_mmc(device_info *dev)
1124{
1125 struct device_info *info = (void*) info_buf;
1126 unsigned long long ptn = 0;
1127 unsigned long long size;
1128 int index = INVALID_PTN;
1129
1130 index = partition_get_index("aboot");
1131 ptn = partition_get_offset(index);
1132 if(ptn == 0)
1133 {
1134 return;
1135 }
1136
1137 size = partition_get_size(index);
1138
1139 memcpy(info, dev, sizeof(device_info));
1140
1141 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1142 {
1143 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1144 return;
1145 }
1146}
1147
1148void read_device_info_mmc(device_info *dev)
1149{
1150 struct device_info *info = (void*) info_buf;
1151 unsigned long long ptn = 0;
1152 unsigned long long size;
1153 int index = INVALID_PTN;
1154
1155 index = partition_get_index("aboot");
1156 ptn = partition_get_offset(index);
1157 if(ptn == 0)
1158 {
1159 return;
1160 }
1161
1162 size = partition_get_size(index);
1163
1164 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1165 {
1166 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1167 return;
1168 }
1169
1170 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1171 {
1172 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1173 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001174 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001175
1176 write_device_info_mmc(info);
1177 }
1178 memcpy(dev, info, sizeof(device_info));
1179}
1180
1181void write_device_info_flash(device_info *dev)
1182{
1183 struct device_info *info = (void *) info_buf;
1184 struct ptentry *ptn;
1185 struct ptable *ptable;
1186
1187 ptable = flash_get_ptable();
1188 if (ptable == NULL)
1189 {
1190 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1191 return;
1192 }
1193
1194 ptn = ptable_find(ptable, "devinfo");
1195 if (ptn == NULL)
1196 {
1197 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1198 return;
1199 }
1200
1201 memcpy(info, dev, sizeof(device_info));
1202
1203 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1204 {
1205 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1206 return;
1207 }
1208}
1209
1210void read_device_info_flash(device_info *dev)
1211{
1212 struct device_info *info = (void*) info_buf;
1213 struct ptentry *ptn;
1214 struct ptable *ptable;
1215
1216 ptable = flash_get_ptable();
1217 if (ptable == NULL)
1218 {
1219 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1220 return;
1221 }
1222
1223 ptn = ptable_find(ptable, "devinfo");
1224 if (ptn == NULL)
1225 {
1226 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1227 return;
1228 }
1229
1230 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1231 {
1232 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1233 return;
1234 }
1235
1236 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1237 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001238 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1239 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001240 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001241 write_device_info_flash(info);
1242 }
1243 memcpy(dev, info, sizeof(device_info));
1244}
1245
1246void write_device_info(device_info *dev)
1247{
1248 if(target_is_emmc_boot())
1249 {
1250 write_device_info_mmc(dev);
1251 }
1252 else
1253 {
1254 write_device_info_flash(dev);
1255 }
1256}
1257
1258void read_device_info(device_info *dev)
1259{
1260 if(target_is_emmc_boot())
1261 {
1262 read_device_info_mmc(dev);
1263 }
1264 else
1265 {
1266 read_device_info_flash(dev);
1267 }
1268}
1269
1270void reset_device_info()
1271{
1272 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001273 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001274 write_device_info(&device);
1275}
1276
1277void set_device_root()
1278{
1279 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001280 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001281 write_device_info(&device);
1282}
1283
Amol Jadicb524072012-08-09 16:40:18 -07001284#if DEVICE_TREE
1285int copy_dtb(uint8_t *boot_image_start)
1286{
1287 uint32 dt_image_offset = 0;
1288 uint32_t n;
1289 struct dt_table *table;
1290 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001291
1292 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1293
Amol Jadicb524072012-08-09 16:40:18 -07001294 if(hdr->dt_size != 0) {
1295
1296 /* add kernel offset */
1297 dt_image_offset += page_size;
1298 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1299 dt_image_offset += n;
1300
1301 /* add ramdisk offset */
1302 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1303 dt_image_offset += n;
1304
1305 /* add second offset */
1306 if(hdr->second_size != 0) {
1307 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1308 dt_image_offset += n;
1309 }
1310
1311 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001312 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001313
1314 /* Restriction that the device tree entry table should be less than a page*/
1315 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1316
1317 /* Validate the device tree table header */
1318 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1319 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1320 return -1;
1321 }
1322
1323 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001324 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001325 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1326 return -1;
1327 }
1328
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001329 /* Validate and Read device device tree in the "tags_add */
1330 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
1331 {
1332 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1333 return -1;
1334 }
1335
Amol Jadicb524072012-08-09 16:40:18 -07001336 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001337 memmove((void*) hdr->tags_addr,
1338 boot_image_start + dt_image_offset + dt_entry_ptr->offset,
1339 dt_entry_ptr->size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001340 } else
1341 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001342
1343 /* Everything looks fine. Return success. */
1344 return 0;
1345}
1346#endif
1347
Brian Swetland9c4c0752009-01-25 16:23:50 -08001348void cmd_boot(const char *arg, void *data, unsigned sz)
1349{
1350 unsigned kernel_actual;
1351 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001352 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001353 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001354 int ret = 0;
1355 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001356
1357 if (sz < sizeof(hdr)) {
1358 fastboot_fail("invalid bootimage header");
1359 return;
1360 }
1361
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001362 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001363
1364 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001365 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001366
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001367 if(target_is_emmc_boot() && hdr->page_size) {
1368 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001369 page_mask = page_size - 1;
1370 }
1371
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001372 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1373 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1374
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001375 /*
1376 * Update the kernel/ramdisk/tags address if the boot image header
1377 * has default values, these default values come from mkbootimg when
1378 * the boot image is flashed using fastboot flash:raw
1379 */
1380 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001381
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001382 /* Get virtual addresses since the hdr saves physical addresses. */
1383 hdr->kernel_addr = VA(hdr->kernel_addr);
1384 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1385 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001386
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001387 /* Check if the addresses in the header are valid. */
1388 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1389 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1390 {
1391 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1392 return -1;
1393 }
1394
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001395 /* sz should have atleast raw boot image */
1396 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001397 fastboot_fail("incomplete bootimage");
1398 return;
1399 }
1400
Amol Jadicb524072012-08-09 16:40:18 -07001401#if DEVICE_TREE
1402 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001403 ret = copy_dtb(data);
1404
1405 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001406#else
1407 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1408 {
1409 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1410 return -1;
1411 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001412#endif
1413
1414 /* Load ramdisk & kernel */
1415 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1416 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1417
1418#if DEVICE_TREE
1419 /*
1420 * If dtb is not found look for appended DTB in the kernel.
1421 * If appended dev tree is found, update the atags with
1422 * memory address to the DTB appended location on RAM.
1423 * Else update with the atags address in the kernel header
1424 */
1425 if (!dtb_copied) {
1426 void *dtb;
Channagoud Kadabia83a6972013-04-25 15:26:04 -07001427 dtb = dev_tree_appended((void *)hdr->kernel_addr, (void *)hdr->tags_addr, hdr->kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001428 if (!dtb) {
1429 fastboot_fail("dtb not found");
1430 return;
1431 }
Amol Jadicb524072012-08-09 16:40:18 -07001432 }
1433#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001434
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001435#ifndef DEVICE_TREE
1436 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1437 {
1438 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1439 return -1;
1440 }
1441#endif
1442
Brian Swetland9c4c0752009-01-25 16:23:50 -08001443 fastboot_okay("");
1444 udc_stop();
1445
Dima Zavin77e41f32013-03-06 16:10:43 -08001446 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001447 (const char*) hdr->cmdline, board_machtype(),
1448 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001449}
1450
Dima Zavin214cc642009-01-26 11:16:21 -08001451void cmd_erase(const char *arg, void *data, unsigned sz)
1452{
1453 struct ptentry *ptn;
1454 struct ptable *ptable;
1455
1456 ptable = flash_get_ptable();
1457 if (ptable == NULL) {
1458 fastboot_fail("partition table doesn't exist");
1459 return;
1460 }
1461
1462 ptn = ptable_find(ptable, arg);
1463 if (ptn == NULL) {
1464 fastboot_fail("unknown partition name");
1465 return;
1466 }
1467
1468 if (flash_erase(ptn)) {
1469 fastboot_fail("failed to erase partition");
1470 return;
1471 }
1472 fastboot_okay("");
1473}
1474
Bikas Gurungd48bd242010-09-04 19:54:32 -07001475
1476void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1477{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001478 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001479 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001480 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001481
Kinson Chikf1a43512011-07-14 11:28:39 -07001482 index = partition_get_index(arg);
1483 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001484
Kinson Chikf1a43512011-07-14 11:28:39 -07001485 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001486 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001487 return;
1488 }
neetidb4b24d62012-01-20 12:13:09 -08001489 /* Simple inefficient version of erase. Just writing
1490 0 in first block */
1491 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1492 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001493 return;
1494 }
1495 fastboot_okay("");
1496}
1497
1498
Ajay Dudani5c761132011-04-07 20:19:04 -07001499void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001500{
1501 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001502 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001503 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001504
Greg Grisco6e754772011-06-23 12:19:39 -07001505 if (!strcmp(arg, "partition"))
1506 {
1507 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001508 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001509 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001510 return;
1511 }
1512 }
Greg Grisco6e754772011-06-23 12:19:39 -07001513 else
1514 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001515 index = partition_get_index(arg);
1516 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001517 if(ptn == 0) {
1518 fastboot_fail("partition table doesn't exist");
1519 return;
1520 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001521
Greg Grisco6e754772011-06-23 12:19:39 -07001522 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1523 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1524 fastboot_fail("image is not a boot image");
1525 return;
1526 }
1527 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001528
Kinson Chikf1a43512011-07-14 11:28:39 -07001529 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001530 if (ROUND_TO_PAGE(sz,511) > size) {
1531 fastboot_fail("size too large");
1532 return;
1533 }
1534 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1535 fastboot_fail("flash write failure");
1536 return;
1537 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001538 }
1539 fastboot_okay("");
1540 return;
1541}
1542
Ajay Dudani5c761132011-04-07 20:19:04 -07001543void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1544{
1545 unsigned int chunk;
1546 unsigned int chunk_data_sz;
1547 sparse_header_t *sparse_header;
1548 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001549 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001550 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301551 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001552 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001553
Kinson Chikf1a43512011-07-14 11:28:39 -07001554 index = partition_get_index(arg);
1555 ptn = partition_get_offset(index);
1556 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001557 fastboot_fail("partition table doesn't exist");
1558 return;
1559 }
1560
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301561 size = partition_get_size(index);
1562 if (ROUND_TO_PAGE(sz,511) > size) {
1563 fastboot_fail("size too large");
1564 return;
1565 }
1566
Ajay Dudani5c761132011-04-07 20:19:04 -07001567 /* Read and skip over sparse image header */
1568 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001569 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1570 fastboot_fail("size too large");
1571 return;
1572 }
1573
Ajay Dudani5c761132011-04-07 20:19:04 -07001574 data += sparse_header->file_hdr_sz;
1575 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1576 {
1577 /* Skip the remaining bytes in a header that is longer than
1578 * we expected.
1579 */
1580 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1581 }
1582
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001583 dprintf (SPEW, "=== Sparse Image Header ===\n");
1584 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1585 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1586 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1587 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1588 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1589 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1590 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1591 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001592
1593 /* Start processing chunks */
1594 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1595 {
1596 /* Read and skip over chunk header */
1597 chunk_header = (chunk_header_t *) data;
1598 data += sizeof(chunk_header_t);
1599
1600 dprintf (SPEW, "=== Chunk Header ===\n");
1601 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1602 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1603 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1604
1605 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1606 {
1607 /* Skip the remaining bytes in a header that is longer than
1608 * we expected.
1609 */
1610 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1611 }
1612
1613 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1614 switch (chunk_header->chunk_type)
1615 {
1616 case CHUNK_TYPE_RAW:
1617 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1618 chunk_data_sz))
1619 {
1620 fastboot_fail("Bogus chunk size for chunk type Raw");
1621 return;
1622 }
1623
Ajay Dudaniab18f022011-05-12 14:39:22 -07001624 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1625 chunk_data_sz,
1626 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001627 {
1628 fastboot_fail("flash write failure");
1629 return;
1630 }
1631 total_blocks += chunk_header->chunk_sz;
1632 data += chunk_data_sz;
1633 break;
1634
1635 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001636 total_blocks += chunk_header->chunk_sz;
1637 break;
1638
Ajay Dudani5c761132011-04-07 20:19:04 -07001639 case CHUNK_TYPE_CRC:
1640 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1641 {
1642 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1643 return;
1644 }
1645 total_blocks += chunk_header->chunk_sz;
1646 data += chunk_data_sz;
1647 break;
1648
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001649 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001650 fastboot_fail("Unknown chunk type");
1651 return;
1652 }
1653 }
1654
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001655 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1656 total_blocks, sparse_header->total_blks);
1657
1658 if(total_blocks != sparse_header->total_blks)
1659 {
1660 fastboot_fail("sparse image write failure");
1661 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001662
1663 fastboot_okay("");
1664 return;
1665}
1666
1667void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1668{
1669 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001670 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1671 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001672
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001673#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001674 int ret=0;
1675 uint32 major_version=0;
1676 uint32 minor_version=0;
1677
1678 ret = scm_svc_version(&major_version,&minor_version);
1679 if(!ret)
1680 {
1681 if(major_version >= 2)
1682 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001683 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001684 {
1685 ret = encrypt_scm((uint32 **) &data, &sz);
1686 if (ret != 0) {
1687 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1688 return;
1689 }
1690
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001691 /* Protect only for SSD */
1692 if (!strcmp(arg, "ssd")) {
1693 ret = scm_protect_keystore((uint32 *) data, sz);
1694 if (ret != 0) {
1695 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1696 return;
1697 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001698 }
1699 }
1700 else
1701 {
1702 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1703 if(ret != 0)
1704 {
1705 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1706 return;
1707 }
1708 }
1709 }
1710 else
1711 {
1712 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1713 magic_number[1] == DECRYPT_MAGIC_1)
1714 {
1715 ret = decrypt_scm((uint32 **) &data, &sz);
1716 if (ret != 0) {
1717 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1718 return;
1719 }
1720 }
1721 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1722 magic_number[1] == ENCRYPT_MAGIC_1)
1723 {
1724 ret = encrypt_scm((uint32 **) &data, &sz);
1725 if (ret != 0) {
1726 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1727 return;
1728 }
1729 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001730 }
1731 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001732 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001733 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001734 dprintf(CRITICAL,"INVALID SVC Version\n");
1735 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001736 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001737#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001738
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001739 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001740 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1741 cmd_flash_mmc_img(arg, data, sz);
1742 else
1743 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001744 return;
1745}
1746
Dima Zavin214cc642009-01-26 11:16:21 -08001747void cmd_flash(const char *arg, void *data, unsigned sz)
1748{
1749 struct ptentry *ptn;
1750 struct ptable *ptable;
1751 unsigned extra = 0;
1752
1753 ptable = flash_get_ptable();
1754 if (ptable == NULL) {
1755 fastboot_fail("partition table doesn't exist");
1756 return;
1757 }
1758
1759 ptn = ptable_find(ptable, arg);
1760 if (ptn == NULL) {
1761 fastboot_fail("unknown partition name");
1762 return;
1763 }
1764
1765 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1766 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1767 fastboot_fail("image is not a boot image");
1768 return;
1769 }
1770 }
1771
Amol Jadi5c61a952012-05-04 17:05:35 -07001772 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001773 || !strcmp(ptn->name, "userdata")
1774 || !strcmp(ptn->name, "persist")
1775 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301776 if (flash_ecc_bch_enabled())
1777 /* Spare data bytes for 8 bit ECC increased by 4 */
1778 extra = ((page_size >> 9) * 20);
1779 else
1780 extra = ((page_size >> 9) * 16);
1781 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001782 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001783
1784 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1785 if (flash_write(ptn, extra, data, sz)) {
1786 fastboot_fail("flash write failure");
1787 return;
1788 }
1789 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1790 fastboot_okay("");
1791}
1792
1793void cmd_continue(const char *arg, void *data, unsigned sz)
1794{
1795 fastboot_okay("");
1796 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001797 if (target_is_emmc_boot())
1798 {
1799 boot_linux_from_mmc();
1800 }
1801 else
1802 {
1803 boot_linux_from_flash();
1804 }
Dima Zavin214cc642009-01-26 11:16:21 -08001805}
1806
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001807void cmd_reboot(const char *arg, void *data, unsigned sz)
1808{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001809 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001810 fastboot_okay("");
1811 reboot_device(0);
1812}
1813
1814void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1815{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001816 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001817 fastboot_okay("");
1818 reboot_device(FASTBOOT_MODE);
1819}
1820
Shashank Mittal162244e2011-08-08 19:01:25 -07001821void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1822{
1823 if(!device.is_unlocked)
1824 {
1825 device.is_unlocked = 1;
1826 write_device_info(&device);
1827 }
1828 fastboot_okay("");
1829}
1830
Shashank Mittala0032282011-08-26 14:50:11 -07001831void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1832{
1833 char response[64];
1834 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1835 fastboot_info(response);
1836 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1837 fastboot_info(response);
1838 fastboot_okay("");
1839}
1840
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001841void cmd_preflash(const char *arg, void *data, unsigned sz)
1842{
1843 fastboot_okay("");
1844}
1845
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001846void splash_screen ()
1847{
1848 struct ptentry *ptn;
1849 struct ptable *ptable;
1850 struct fbcon_config *fb_display = NULL;
1851
1852 if (!target_is_emmc_boot())
1853 {
1854 ptable = flash_get_ptable();
1855 if (ptable == NULL) {
1856 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001857 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001858 }
1859
1860 ptn = ptable_find(ptable, "splash");
1861 if (ptn == NULL) {
1862 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1863 } else {
1864 fb_display = fbcon_display();
1865 if (fb_display) {
1866 if (flash_read(ptn, 0, fb_display->base,
1867 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1868 fbcon_clear();
1869 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1870 }
1871 }
1872 }
1873 }
1874}
1875
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001876/* Get the size from partiton name */
1877static void get_partition_size(const char *arg, char *response)
1878{
1879 uint64_t ptn = 0;
1880 uint64_t size;
1881 int index = INVALID_PTN;
1882
1883 index = partition_get_index(arg);
1884
1885 if (index == INVALID_PTN)
1886 {
1887 dprintf(CRITICAL, "Invalid partition index\n");
1888 return;
1889 }
1890
1891 ptn = partition_get_offset(index);
1892
1893 if(!ptn)
1894 {
1895 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1896 return;
1897 }
1898
1899 size = partition_get_size(index);
1900
1901 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1902 return;
1903}
1904
1905/*
1906 * Publish the partition type & size info
1907 * fastboot getvar will publish the required information.
1908 * fastboot getvar partition_size:<partition_name>: partition size in hex
1909 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1910 */
1911static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1912{
1913 uint8_t i;
1914
1915 for (i = 0; i < num_parts; i++) {
1916 get_partition_size(info[i].part_name, info[i].size_response);
1917
1918 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1919 {
1920 dprintf(CRITICAL, "partition size name truncated\n");
1921 return;
1922 }
1923 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1924 {
1925 dprintf(CRITICAL, "partition type name truncated\n");
1926 return;
1927 }
1928
1929 /* publish partition size & type info */
1930 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1931 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1932 }
1933}
1934
Brian Swetland9c4c0752009-01-25 16:23:50 -08001935void aboot_init(const struct app_descriptor *app)
1936{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001937 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001938 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001939 unsigned sz = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03001940 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001941
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001942 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001943 if (target_is_emmc_boot())
1944 {
1945 page_size = 2048;
1946 page_mask = page_size - 1;
1947 }
1948 else
1949 {
1950 page_size = flash_page_size();
1951 page_mask = page_size - 1;
1952 }
1953
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001954 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
1955
Shashank Mittal162244e2011-08-08 19:01:25 -07001956 if(target_use_signed_kernel())
1957 {
1958 read_device_info(&device);
1959
Shashank Mittal162244e2011-08-08 19:01:25 -07001960 }
1961
Greg Griscod6250552011-06-29 14:40:23 -07001962 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001963 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001964 surf_udc_device.serialno = sn_buf;
1965
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001966 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001967 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001968 {
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001969 dprintf(ALWAYS,"dload mode key sequence detected");
1970 if (set_download_mode())
1971 {
1972 dprintf(CRITICAL,"dload mode not supported by target");
1973 }
1974 else
1975 {
1976 reboot_device(0);
1977 dprintf(CRITICAL,"Failed to reboot into dload mode");
1978 }
1979 boot_into_fastboot = true;
1980 }
1981 if (!boot_into_fastboot)
1982 {
1983 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
1984 boot_into_recovery = 1;
1985 if (!boot_into_recovery &&
1986 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03001987 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001988 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001989 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001990 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03001991 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001992 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001993
Ajay Dudani77421292010-10-27 19:34:06 -07001994 reboot_mode = check_reboot_mode();
1995 if (reboot_mode == RECOVERY_MODE) {
1996 boot_into_recovery = 1;
1997 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03001998 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07001999 }
2000
Pavel Nedev5d91d412013-04-29 11:34:24 +03002001 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002002 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002003 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002004 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002005 if(emmc_recovery_init())
2006 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2007 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002008 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002009 if((device.is_unlocked) || (device.is_tampered))
2010 {
2011 #ifdef TZ_TAMPER_FUSE
2012 set_tamper_fuse_cmd();
2013 #endif
2014 #if USE_PCOM_SECBOOT
2015 set_tamper_flag(device.is_tampered);
2016 #endif
2017 }
Shashank Mittala0032282011-08-26 14:50:11 -07002018 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002019 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002020 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002021 else
2022 {
2023 recovery_init();
2024 #if USE_PCOM_SECBOOT
2025 if((device.is_unlocked) || (device.is_tampered))
2026 set_tamper_flag(device.is_tampered);
2027 #endif
2028 boot_linux_from_flash();
2029 }
2030 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2031 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002032 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002033
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002034 sz = target_get_max_flash_size();
2035
Shashank Mittal162244e2011-08-08 19:01:25 -07002036 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002037
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002038 if(!usb_init)
2039 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002040
2041 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002042
Shashank Mittal23b8f422010-04-16 19:27:21 -07002043 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002044 {
2045 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002046 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002047 }
2048 else
2049 {
2050 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002051 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002052 }
2053
2054 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002055 fastboot_register("reboot", cmd_reboot);
2056 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07002057 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07002058 fastboot_register("oem device-info", cmd_oem_devinfo);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002059 fastboot_register("preflash", cmd_preflash);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07002060 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08002061 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08002062 fastboot_publish("serialno", sn_buf);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002063 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2064 /* Max download size supported */
2065 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", sz);
2066 fastboot_publish("max-download-size", (const char *) max_download_size);
Kinson Chikf1a43512011-07-14 11:28:39 -07002067 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07002068 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002069 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002070}
2071
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002072uint32_t get_page_size()
2073{
2074 return page_size;
2075}
2076
Brian Swetland9c4c0752009-01-25 16:23:50 -08002077APP_START(aboot)
2078 .init = aboot_init,
2079APP_END