blob: 118ebaaea01ee79bb8e9e7a74e46b8cd15645dd2 [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>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030054#include <sha.h>
Dima Zavin214cc642009-01-26 11:16:21 -080055
Neeti Desai17379b82012-06-04 18:42:53 -070056#if DEVICE_TREE
57#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070058#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070059#endif
60
Shashank Mittalcd98d472011-08-02 14:29:24 -070061#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080062#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080063#include "bootimg.h"
64#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070065#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070066#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070067#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070068#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080069
Shashank Mittal162244e2011-08-08 19:01:25 -070070#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070071
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern bool target_use_signed_kernel(void);
73extern void dsb();
74extern void isb();
75extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070076extern void target_uninit(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070077
78void write_device_info_mmc(device_info *dev);
79void write_device_info_flash(device_info *dev);
80
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070081#define EXPAND(NAME) #NAME
82#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070083
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080084#ifdef MEMBASE
85#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
86#else
David Ng183a7422009-12-07 14:55:21 -080087#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080088#endif
89
Deepa Dinamani0e163a42013-05-24 17:08:15 -070090#ifndef MEMSIZE
91#define MEMSIZE 1024*1024
92#endif
93
94#define MAX_TAGS_SIZE 1024
95
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080096#define RECOVERY_MODE 0x77665502
97#define FASTBOOT_MODE 0x77665500
98
David Ng183a7422009-12-07 14:55:21 -080099static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800100static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300101static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800102static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800103static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700104static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300105static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800106
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800107static const char *baseband_apq = " androidboot.baseband=apq";
108static const char *baseband_msm = " androidboot.baseband=msm";
109static const char *baseband_csfb = " androidboot.baseband=csfb";
110static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700111static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700112static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800113static const char *baseband_dsda = " androidboot.baseband=dsda";
114static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800115static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800116
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700117static unsigned page_size = 0;
118static unsigned page_mask = 0;
119static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
120static bool boot_into_ffbm;
121
Shashank Mittalcd98d472011-08-02 14:29:24 -0700122/* Assuming unauthorized kernel image by default */
123static int auth_kernel_img = 0;
124
Shashank Mittal162244e2011-08-08 19:01:25 -0700125static device_info device = {DEVICE_MAGIC, 0, 0};
126
Brian Swetland9c4c0752009-01-25 16:23:50 -0800127static struct udc_device surf_udc_device = {
128 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800129 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800130 .version_id = 0x0100,
131 .manufacturer = "Google",
132 .product = "Android",
133};
134
Dima Zavin42168f22009-01-30 11:52:22 -0800135struct atag_ptbl_entry
136{
137 char name[16];
138 unsigned offset;
139 unsigned size;
140 unsigned flags;
141};
142
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700143/*
144 * Partition info, required to be published
145 * for fastboot
146 */
147struct getvar_partition_info {
148 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
149 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
150 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
151 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
152 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
153};
154
155/*
156 * Right now, we are publishing the info for only
157 * three partitions
158 */
159struct getvar_partition_info part_info[] =
160{
161 { "system" , "partition-size:", "partition-type:", "", "ext4" },
162 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
163 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
164};
165
166char max_download_size[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800167char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700168
Greg Griscod2471ef2011-07-14 13:00:42 -0700169extern int emmc_recovery_init(void);
170
Kinson Chik0b1c8162011-08-31 16:31:57 -0700171#if NO_KEYPAD_DRIVER
172extern int fastboot_trigger(void);
173#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700174
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700175static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
176{
177 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700178#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
179 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
180 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
181 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700182#endif
183}
184
Dima Zavin42168f22009-01-30 11:52:22 -0800185static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
186{
187 struct atag_ptbl_entry atag_ptn;
188
189 memcpy(atag_ptn.name, ptn->name, 16);
190 atag_ptn.name[15] = '\0';
191 atag_ptn.offset = ptn->start;
192 atag_ptn.size = ptn->length;
193 atag_ptn.flags = ptn->flags;
194 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
195 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
196}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800197
Neeti Desaie245d492012-06-01 12:52:13 -0700198unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800199{
David Ng183a7422009-12-07 14:55:21 -0800200 int cmdline_len = 0;
201 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800202 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700203 int pause_at_bootup = 0;
Pavel Nedev5614d222013-06-17 18:01:02 +0300204 bool gpt_exists = partition_gpt_exists();
Dima Zavin42168f22009-01-30 11:52:22 -0800205
Brian Swetland9c4c0752009-01-25 16:23:50 -0800206 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800207 cmdline_len = strlen(cmdline);
208 have_cmdline = 1;
209 }
210 if (target_is_emmc_boot()) {
211 cmdline_len += strlen(emmc_cmdline);
212 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800213
214 cmdline_len += strlen(usb_sn_cmdline);
215 cmdline_len += strlen(sn_buf);
216
Pavel Nedev5614d222013-06-17 18:01:02 +0300217 if (boot_into_recovery && gpt_exists)
218 cmdline_len += strlen(secondary_gpt_enable);
219
Pavel Nedev328ac822013-04-05 15:25:11 +0300220 if (boot_into_ffbm) {
221 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700222 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800223 /* reduce kernel console messages to speed-up boot */
224 cmdline_len += strlen(loglevel);
Pavel Nedev328ac822013-04-05 15:25:11 +0300225 } else if (target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700226 pause_at_bootup = 1;
227 cmdline_len += strlen(battchg_pause);
228 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800229
Shashank Mittalcd98d472011-08-02 14:29:24 -0700230 if(target_use_signed_kernel() && auth_kernel_img) {
231 cmdline_len += strlen(auth_kernel);
232 }
233
Ajay Dudanid04110c2011-01-17 23:55:07 -0800234 /* Determine correct androidboot.baseband to use */
235 switch(target_baseband())
236 {
237 case BASEBAND_APQ:
238 cmdline_len += strlen(baseband_apq);
239 break;
240
241 case BASEBAND_MSM:
242 cmdline_len += strlen(baseband_msm);
243 break;
244
245 case BASEBAND_CSFB:
246 cmdline_len += strlen(baseband_csfb);
247 break;
248
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800249 case BASEBAND_SVLTE2A:
250 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800251 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700252
253 case BASEBAND_MDM:
254 cmdline_len += strlen(baseband_mdm);
255 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700256
257 case BASEBAND_SGLTE:
258 cmdline_len += strlen(baseband_sglte);
259 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530260
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800261 case BASEBAND_SGLTE2:
262 cmdline_len += strlen(baseband_sglte2);
263 break;
264
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530265 case BASEBAND_DSDA:
266 cmdline_len += strlen(baseband_dsda);
267 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800268
269 case BASEBAND_DSDA2:
270 cmdline_len += strlen(baseband_dsda2);
271 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800272 }
273
David Ng183a7422009-12-07 14:55:21 -0800274 if (cmdline_len > 0) {
275 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700276 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
277 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700278
Amol Jadi168b7712012-03-06 16:15:00 -0800279 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700280 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800281 if (have_cmdline) {
282 src = cmdline;
283 while ((*dst++ = *src++));
284 }
285 if (target_is_emmc_boot()) {
286 src = emmc_cmdline;
287 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700288 have_cmdline = 1;
289 while ((*dst++ = *src++));
290 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800291
292 src = usb_sn_cmdline;
293 if (have_cmdline) --dst;
294 have_cmdline = 1;
295 while ((*dst++ = *src++));
296 src = sn_buf;
297 if (have_cmdline) --dst;
298 have_cmdline = 1;
299 while ((*dst++ = *src++));
300
Pavel Nedev5614d222013-06-17 18:01:02 +0300301 if (boot_into_recovery && gpt_exists) {
302 src = secondary_gpt_enable;
303 if (have_cmdline) --dst;
304 while ((*dst++ = *src++));
305 }
306
Pavel Nedev328ac822013-04-05 15:25:11 +0300307 if (boot_into_ffbm) {
308 src = androidboot_mode;
309 if (have_cmdline) --dst;
310 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700311 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300312 if (have_cmdline) --dst;
313 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800314 src = loglevel;
315 if (have_cmdline) --dst;
316 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300317 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700318 src = battchg_pause;
319 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800320 while ((*dst++ = *src++));
321 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800322
Shashank Mittalcd98d472011-08-02 14:29:24 -0700323 if(target_use_signed_kernel() && auth_kernel_img) {
324 src = auth_kernel;
325 if (have_cmdline) --dst;
326 while ((*dst++ = *src++));
327 }
328
Ajay Dudanid04110c2011-01-17 23:55:07 -0800329 switch(target_baseband())
330 {
331 case BASEBAND_APQ:
332 src = baseband_apq;
333 if (have_cmdline) --dst;
334 while ((*dst++ = *src++));
335 break;
336
337 case BASEBAND_MSM:
338 src = baseband_msm;
339 if (have_cmdline) --dst;
340 while ((*dst++ = *src++));
341 break;
342
343 case BASEBAND_CSFB:
344 src = baseband_csfb;
345 if (have_cmdline) --dst;
346 while ((*dst++ = *src++));
347 break;
348
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800349 case BASEBAND_SVLTE2A:
350 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800351 if (have_cmdline) --dst;
352 while ((*dst++ = *src++));
353 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700354
355 case BASEBAND_MDM:
356 src = baseband_mdm;
357 if (have_cmdline) --dst;
358 while ((*dst++ = *src++));
359 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700360
361 case BASEBAND_SGLTE:
362 src = baseband_sglte;
363 if (have_cmdline) --dst;
364 while ((*dst++ = *src++));
365 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530366
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800367 case BASEBAND_SGLTE2:
368 src = baseband_sglte2;
369 if (have_cmdline) --dst;
370 while ((*dst++ = *src++));
371 break;
372
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530373 case BASEBAND_DSDA:
374 src = baseband_dsda;
375 if (have_cmdline) --dst;
376 while ((*dst++ = *src++));
377 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800378
379 case BASEBAND_DSDA2:
380 src = baseband_dsda2;
381 if (have_cmdline) --dst;
382 while ((*dst++ = *src++));
383 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800384 }
Neeti Desaie245d492012-06-01 12:52:13 -0700385 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800386 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700387 return cmdline_final;
388}
389
390unsigned *atag_core(unsigned *ptr)
391{
392 /* CORE */
393 *ptr++ = 2;
394 *ptr++ = 0x54410001;
395
396 return ptr;
397
398}
399
400unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
401 unsigned ramdisk_size)
402{
403 if (ramdisk_size) {
404 *ptr++ = 4;
405 *ptr++ = 0x54420005;
406 *ptr++ = (unsigned)ramdisk;
407 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800408 }
409
Neeti Desaie245d492012-06-01 12:52:13 -0700410 return ptr;
411}
412
413unsigned *atag_ptable(unsigned **ptr_addr)
414{
415 int i;
416 struct ptable *ptable;
417
418 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700419 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
420 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700421 *(*ptr_addr)++ = 0x4d534d70;
422 for (i = 0; i < ptable->count; ++i)
423 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
424 }
425
426 return (*ptr_addr);
427}
428
429unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
430{
431 int cmdline_length = 0;
432 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700433 char *dest;
434
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800435 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700436 n = (cmdline_length + 4) & (~3);
437
438 *ptr++ = (n / 4) + 2;
439 *ptr++ = 0x54410009;
440 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800441 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700442 ptr += (n / 4);
443
444 return ptr;
445}
446
447unsigned *atag_end(unsigned *ptr)
448{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800449 /* END */
450 *ptr++ = 0;
451 *ptr++ = 0;
452
Neeti Desaie245d492012-06-01 12:52:13 -0700453 return ptr;
454}
455
456void generate_atags(unsigned *ptr, const char *cmdline,
457 void *ramdisk, unsigned ramdisk_size)
458{
459
460 ptr = atag_core(ptr);
461 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
462 ptr = target_atag_mem(ptr);
463
464 /* Skip NAND partition ATAGS for eMMC boot */
465 if (!target_is_emmc_boot()){
466 ptr = atag_ptable(&ptr);
467 }
468
469 ptr = atag_cmdline(ptr, cmdline);
470 ptr = atag_end(ptr);
471}
472
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700473typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700474void boot_linux(void *kernel, unsigned *tags,
475 const char *cmdline, unsigned machtype,
476 void *ramdisk, unsigned ramdisk_size)
477{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800478 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800479#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700480 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800481#endif
482
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700483 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800484 uint32_t tags_phys = PA((addr_t)tags);
485
486 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700487
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800488 final_cmdline = update_cmdline((const char*)cmdline);
489
Neeti Desai17379b82012-06-04 18:42:53 -0700490#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800491 dprintf(INFO, "Updating device tree: start\n");
492
Neeti Desai17379b82012-06-04 18:42:53 -0700493 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800494 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700495 if(ret)
496 {
497 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
498 ASSERT(0);
499 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800500 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700501#else
Neeti Desaie245d492012-06-01 12:52:13 -0700502 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800503 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700504#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700505
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700506 /* Perform target specific cleanup */
507 target_uninit();
508
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800509 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
510 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800511
512 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700513
Amol Jadi4421e652011-06-16 15:00:48 -0700514 /* do any platform specific cleanup before kernel entry */
515 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700516
Brian Swetland9c4c0752009-01-25 16:23:50 -0800517 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700518
Amol Jadi504f9fe2012-08-16 13:56:48 -0700519#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800520 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700521#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700522 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700523 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800524}
525
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700526/* Function to check if the memory address range falls within the aboot
527 * boundaries.
528 * start: Start of the memory region
529 * size: Size of the memory region
530 */
531int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
532{
533 /* Check for boundary conditions. */
534 if ((start + size) < start)
535 return -1;
536
537 /* Check for memory overlap. */
538 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
539 return 0;
540 else if (start > (MEMBASE + MEMSIZE))
541 return 0;
542 else
543 return -1;
544}
545
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800546#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800547
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700548BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800549#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700550BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800551#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800552
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700553static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
554{
555 int ret;
556
557 /* Assume device is rooted at this time. */
558 device.is_tampered = 1;
559
560 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
561
562 ret = image_verify((unsigned char *)bootimg_addr,
563 (unsigned char *)(bootimg_addr + bootimg_size),
564 bootimg_size,
565 CRYPTO_AUTH_ALG_SHA256);
566
567 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
568
569 if (ret)
570 {
571 /* Authorized kernel */
572 device.is_tampered = 0;
573 }
574
575#if USE_PCOM_SECBOOT
576 set_tamper_flag(device.is_tampered);
577#endif
578
579 if(device.is_tampered)
580 {
581 write_device_info_mmc(&device);
582 #ifdef TZ_TAMPER_FUSE
583 set_tamper_fuse_cmd();
584 #endif
585 #ifdef ASSERT_ON_TAMPER
586 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
587 ASSERT(0);
588 #endif
589 }
590}
591
Shashank Mittal23b8f422010-04-16 19:27:21 -0700592int boot_linux_from_mmc(void)
593{
594 struct boot_img_hdr *hdr = (void*) buf;
595 struct boot_img_hdr *uhdr;
596 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700597 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700598 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700599 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700600
Shashank Mittalcd98d472011-08-02 14:29:24 -0700601 unsigned char *image_addr = 0;
602 unsigned kernel_actual;
603 unsigned ramdisk_actual;
604 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700605 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700606
607#if DEVICE_TREE
608 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700609 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700610 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800611 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700612#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700613 if (!boot_into_recovery) {
614 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
615 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
616 if (rcode <= 0) {
617 boot_into_ffbm = false;
618 if (rcode < 0)
619 dprintf(CRITICAL,"failed to get ffbm cookie");
620 } else
621 boot_into_ffbm = true;
622 } else
623 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700624 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
625 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
626 dprintf(INFO, "Unified boot method!\n");
627 hdr = uhdr;
628 goto unified_boot;
629 }
Greg Griscod6250552011-06-29 14:40:23 -0700630 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700631 index = partition_get_index("boot");
632 ptn = partition_get_offset(index);
633 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700634 dprintf(CRITICAL, "ERROR: No boot partition found\n");
635 return -1;
636 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700637 }
638 else {
639 index = partition_get_index("recovery");
640 ptn = partition_get_offset(index);
641 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700642 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
643 return -1;
644 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700645 }
646
Greg Griscod6250552011-06-29 14:40:23 -0700647 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700648 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
649 return -1;
650 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700651
652 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700653 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700654 return -1;
655 }
656
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700657 if (hdr->page_size && (hdr->page_size != page_size)) {
658 page_size = hdr->page_size;
659 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700660 }
661
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700662 /*
663 * Update the kernel/ramdisk/tags address if the boot image header
664 * has default values, these default values come from mkbootimg when
665 * the boot image is flashed using fastboot flash:raw
666 */
667 update_ker_tags_rdisk_addr(hdr);
668
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700669 /* Get virtual addresses since the hdr saves physical addresses. */
670 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
671 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
672 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700673
674 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
675 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
676
677 /* Check if the addresses in the header are valid. */
678 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
679 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
680 {
681 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
682 return -1;
683 }
684
685#ifndef DEVICE_TREE
686 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
687 {
688 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
689 return -1;
690 }
691#endif
692
Shashank Mittalcd98d472011-08-02 14:29:24 -0700693 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300694 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
695 (int) target_use_signed_kernel(),
696 device.is_unlocked,
697 device.is_tampered);
698
Shashank Mittala0032282011-08-26 14:50:11 -0700699 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800700 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700701 offset = 0;
702
Shashank Mittalcd98d472011-08-02 14:29:24 -0700703 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700704
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800705#if DEVICE_TREE
706 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
707 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700708
709 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
710 {
711 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
712 return -1;
713 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800714#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530715 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700716
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700717#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700718
Amol Jadib6be5c12012-11-14 13:39:51 -0800719 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700720 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800721
Shashank Mittalcd98d472011-08-02 14:29:24 -0700722 /* Read image without signature */
723 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
724 {
725 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
726 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800727 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700728
Amol Jadib6be5c12012-11-14 13:39:51 -0800729 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700730 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800731
Shashank Mittalcd98d472011-08-02 14:29:24 -0700732 offset = imagesize_actual;
733 /* Read signature */
734 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
735 {
736 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700737 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700738 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800739
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700740 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700741
Neeti Desai465491e2012-07-31 12:53:35 -0700742 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700743 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
744 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700745
Neeti Desai465491e2012-07-31 12:53:35 -0700746 #if DEVICE_TREE
747 if(hdr->dt_size) {
748 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700749 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700750
751 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
752
Joel Kingaa335dc2013-06-03 16:11:08 -0700753 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700754 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
755 return -1;
756 }
Neeti Desai465491e2012-07-31 12:53:35 -0700757 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700758 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700759 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
760 return -1;
761 }
762
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700763 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700764 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700765 {
766 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
767 return -1;
768 }
769
Joel Kingaa335dc2013-06-03 16:11:08 -0700770 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800771 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800772 /*
773 * If appended dev tree is found, update the atags with
774 * memory address to the DTB appended location on RAM.
775 * Else update with the atags address in the kernel header
776 */
777 void *dtb;
778 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700779 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800780 if (!dtb) {
781 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
782 return -1;
783 }
Neeti Desai465491e2012-07-31 12:53:35 -0700784 }
785 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700786 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700787 else
788 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800789 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700790
Amol Jadib6be5c12012-11-14 13:39:51 -0800791 dprintf(INFO, "Loading boot image (%d): start\n",
792 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700793 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800794
795 offset = page_size;
796
797 /* Load kernel */
798 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700799 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
800 return -1;
801 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800802 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700803
Amol Jadib6be5c12012-11-14 13:39:51 -0800804 /* Load ramdisk */
805 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700806 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800807 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700808 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
809 return -1;
810 }
811 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800812 offset += ramdisk_actual;
813
814 dprintf(INFO, "Loading boot image (%d): done\n",
815 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700816 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700817
818 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800819 offset += second_actual;
820 /* Second image loading not implemented. */
821 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700822 }
823
824 #if DEVICE_TREE
825 if(hdr->dt_size != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700826 /* Read the device tree table into buffer */
827 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
828 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
829 return -1;
830 }
831 table = (struct dt_table*) dt_buf;
832
Joel Kingaa335dc2013-06-03 16:11:08 -0700833 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700834 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
835 return -1;
836 }
837
Joel Kingaa335dc2013-06-03 16:11:08 -0700838 /* Find index of device tree within device tree table */
839 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700840 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
841 return -1;
842 }
843
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700844 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700845 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700846 {
847 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
848 return -1;
849 }
850
Joel Kingaa335dc2013-06-03 16:11:08 -0700851 if(mmc_read(ptn + offset + dt_entry.offset,
852 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700853 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
854 return -1;
855 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300856 #ifdef TZ_SAVE_KERNEL_HASH
857 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
858 hdr->ramdisk_addr, ramdisk_actual,
859 ptn, offset, hdr->dt_size);
860 #endif /* TZ_SAVE_KERNEL_HASH */
861
Channagoud Kadabi35095622013-03-01 13:53:05 -0800862 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800863 /*
864 * If appended dev tree is found, update the atags with
865 * memory address to the DTB appended location on RAM.
866 * Else update with the atags address in the kernel header
867 */
868 void *dtb;
869 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700870 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800871 if (!dtb) {
872 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
873 return -1;
874 }
Neeti Desai465491e2012-07-31 12:53:35 -0700875 }
876 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700877 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700878
Stanimir Varbanov69ec5462013-07-18 18:17:42 +0300879 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
880 target_load_ssd_keystore();
881
Shashank Mittal23b8f422010-04-16 19:27:21 -0700882unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700883
Dima Zavin77e41f32013-03-06 16:10:43 -0800884 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700885 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700886 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
887
888 return 0;
889}
890
Dima Zavin214cc642009-01-26 11:16:21 -0800891int boot_linux_from_flash(void)
892{
893 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800894 struct ptentry *ptn;
895 struct ptable *ptable;
896 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800897
Shashank Mittalcd98d472011-08-02 14:29:24 -0700898 unsigned char *image_addr = 0;
899 unsigned kernel_actual;
900 unsigned ramdisk_actual;
901 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800902 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700903
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700904#if DEVICE_TREE
905 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700906 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800907 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700908#endif
909
David Ng183a7422009-12-07 14:55:21 -0800910 if (target_is_emmc_boot()) {
911 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
912 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
913 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
914 return -1;
915 }
916 goto continue_boot;
917 }
918
Dima Zavin214cc642009-01-26 11:16:21 -0800919 ptable = flash_get_ptable();
920 if (ptable == NULL) {
921 dprintf(CRITICAL, "ERROR: Partition table not found\n");
922 return -1;
923 }
924
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800925 if(!boot_into_recovery)
926 {
927 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700928
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800929 if (ptn == NULL) {
930 dprintf(CRITICAL, "ERROR: No boot partition found\n");
931 return -1;
932 }
933 }
934 else
935 {
936 ptn = ptable_find(ptable, "recovery");
937 if (ptn == NULL) {
938 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
939 return -1;
940 }
Dima Zavin214cc642009-01-26 11:16:21 -0800941 }
942
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800943 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800944 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
945 return -1;
946 }
Dima Zavin214cc642009-01-26 11:16:21 -0800947
948 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700949 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800950 return -1;
951 }
952
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800953 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700954 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 -0800955 return -1;
956 }
957
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700958 /*
959 * Update the kernel/ramdisk/tags address if the boot image header
960 * has default values, these default values come from mkbootimg when
961 * the boot image is flashed using fastboot flash:raw
962 */
963 update_ker_tags_rdisk_addr(hdr);
964
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700965 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700966 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
967 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
968 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
969
970 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
971 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
972
973 /* Check if the addresses in the header are valid. */
974 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
975 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
976 {
977 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
978 return -1;
979 }
980
981#ifndef DEVICE_TREE
982 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
983 {
984 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
985 return -1;
986 }
987#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700988
Shashank Mittalcd98d472011-08-02 14:29:24 -0700989 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700990 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700991 {
992 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700993 offset = 0;
994
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800995#if DEVICE_TREE
996 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
997 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700998
999 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1000 {
1001 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1002 return -1;
1003 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001004#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001005 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001006#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001007
Amol Jadib6be5c12012-11-14 13:39:51 -08001008 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001009 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001010
Shashank Mittalcd98d472011-08-02 14:29:24 -07001011 /* Read image without signature */
1012 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1013 {
1014 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1015 return -1;
1016 }
Dima Zavin214cc642009-01-26 11:16:21 -08001017
Amol Jadib6be5c12012-11-14 13:39:51 -08001018 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001019 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001020
Shashank Mittalcd98d472011-08-02 14:29:24 -07001021 offset = imagesize_actual;
1022 /* Read signature */
1023 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1024 {
1025 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001026 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001027 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001028
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001029 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001030
1031 /* Move kernel and ramdisk to correct address */
1032 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1033 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001034#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001035 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001036 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001037 {
1038 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1039 return -1;
1040 }
1041
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001042 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1043#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001044
1045 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001046 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001047 {
1048 write_device_info_flash(&device);
1049 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301050#if USE_PCOM_SECBOOT
1051 set_tamper_flag(device.is_tampered);
1052#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001053 }
1054 else
1055 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001056 offset = page_size;
1057
Amol Jadib6be5c12012-11-14 13:39:51 -08001058 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1059 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1060 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1061
1062 dprintf(INFO, "Loading boot image (%d): start\n",
1063 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001064 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001065
1066 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001067 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1068 return -1;
1069 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001070 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001071
Amol Jadib6be5c12012-11-14 13:39:51 -08001072 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001073 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1074 return -1;
1075 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001076 offset += ramdisk_actual;
1077
1078 dprintf(INFO, "Loading boot image (%d): done\n",
1079 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001080 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001081
1082 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001083 offset += second_actual;
1084 /* Second image loading not implemented. */
1085 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001086 }
1087
1088#if DEVICE_TREE
1089 if(hdr->dt_size != 0) {
1090
1091 /* Read the device tree table into buffer */
1092 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1093 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1094 return -1;
1095 }
1096
1097 table = (struct dt_table*) dt_buf;
1098
Joel Kingaa335dc2013-06-03 16:11:08 -07001099 if (dev_tree_validate(table, hdr->page_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001100 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1101 return -1;
1102 }
1103
Joel Kingaa335dc2013-06-03 16:11:08 -07001104 /* Find index of device tree within device tree table */
1105 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001106 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1107 return -1;
1108 }
1109
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001110 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001111 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001112 {
1113 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1114 return -1;
1115 }
1116
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001117 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001118 if(flash_read(ptn, offset + dt_entry.offset,
1119 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001120 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1121 return -1;
1122 }
1123 }
1124#endif
1125
Shashank Mittalcd98d472011-08-02 14:29:24 -07001126 }
David Ng183a7422009-12-07 14:55:21 -08001127continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001128
Dima Zavin214cc642009-01-26 11:16:21 -08001129 /* TODO: create/pass atags to kernel */
1130
Ajay Dudanie28a6072011-07-01 13:59:46 -07001131 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001132 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001133 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1134
1135 return 0;
1136}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001137
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001138BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001139void write_device_info_mmc(device_info *dev)
1140{
1141 struct device_info *info = (void*) info_buf;
1142 unsigned long long ptn = 0;
1143 unsigned long long size;
1144 int index = INVALID_PTN;
1145
1146 index = partition_get_index("aboot");
1147 ptn = partition_get_offset(index);
1148 if(ptn == 0)
1149 {
1150 return;
1151 }
1152
1153 size = partition_get_size(index);
1154
1155 memcpy(info, dev, sizeof(device_info));
1156
1157 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1158 {
1159 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1160 return;
1161 }
1162}
1163
1164void read_device_info_mmc(device_info *dev)
1165{
1166 struct device_info *info = (void*) info_buf;
1167 unsigned long long ptn = 0;
1168 unsigned long long size;
1169 int index = INVALID_PTN;
1170
1171 index = partition_get_index("aboot");
1172 ptn = partition_get_offset(index);
1173 if(ptn == 0)
1174 {
1175 return;
1176 }
1177
1178 size = partition_get_size(index);
1179
1180 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1181 {
1182 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1183 return;
1184 }
1185
1186 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1187 {
1188 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1189 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001190 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001191
1192 write_device_info_mmc(info);
1193 }
1194 memcpy(dev, info, sizeof(device_info));
1195}
1196
1197void write_device_info_flash(device_info *dev)
1198{
1199 struct device_info *info = (void *) info_buf;
1200 struct ptentry *ptn;
1201 struct ptable *ptable;
1202
1203 ptable = flash_get_ptable();
1204 if (ptable == NULL)
1205 {
1206 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1207 return;
1208 }
1209
1210 ptn = ptable_find(ptable, "devinfo");
1211 if (ptn == NULL)
1212 {
1213 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1214 return;
1215 }
1216
1217 memcpy(info, dev, sizeof(device_info));
1218
1219 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1220 {
1221 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1222 return;
1223 }
1224}
1225
1226void read_device_info_flash(device_info *dev)
1227{
1228 struct device_info *info = (void*) info_buf;
1229 struct ptentry *ptn;
1230 struct ptable *ptable;
1231
1232 ptable = flash_get_ptable();
1233 if (ptable == NULL)
1234 {
1235 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1236 return;
1237 }
1238
1239 ptn = ptable_find(ptable, "devinfo");
1240 if (ptn == NULL)
1241 {
1242 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1243 return;
1244 }
1245
1246 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1247 {
1248 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1249 return;
1250 }
1251
1252 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1253 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001254 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1255 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001256 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001257 write_device_info_flash(info);
1258 }
1259 memcpy(dev, info, sizeof(device_info));
1260}
1261
1262void write_device_info(device_info *dev)
1263{
1264 if(target_is_emmc_boot())
1265 {
1266 write_device_info_mmc(dev);
1267 }
1268 else
1269 {
1270 write_device_info_flash(dev);
1271 }
1272}
1273
1274void read_device_info(device_info *dev)
1275{
1276 if(target_is_emmc_boot())
1277 {
1278 read_device_info_mmc(dev);
1279 }
1280 else
1281 {
1282 read_device_info_flash(dev);
1283 }
1284}
1285
1286void reset_device_info()
1287{
1288 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001289 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001290 write_device_info(&device);
1291}
1292
1293void set_device_root()
1294{
1295 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001296 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001297 write_device_info(&device);
1298}
1299
Amol Jadicb524072012-08-09 16:40:18 -07001300#if DEVICE_TREE
1301int copy_dtb(uint8_t *boot_image_start)
1302{
1303 uint32 dt_image_offset = 0;
1304 uint32_t n;
1305 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001306 struct dt_entry dt_entry;
Amol Jadicb524072012-08-09 16:40:18 -07001307
1308 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1309
Amol Jadicb524072012-08-09 16:40:18 -07001310 if(hdr->dt_size != 0) {
1311
1312 /* add kernel offset */
1313 dt_image_offset += page_size;
1314 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1315 dt_image_offset += n;
1316
1317 /* add ramdisk offset */
1318 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1319 dt_image_offset += n;
1320
1321 /* add second offset */
1322 if(hdr->second_size != 0) {
1323 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1324 dt_image_offset += n;
1325 }
1326
1327 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001328 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001329
Joel Kingaa335dc2013-06-03 16:11:08 -07001330 if (dev_tree_validate(table, hdr->page_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001331 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1332 return -1;
1333 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001334 /* Find index of device tree within device tree table */
1335 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001336 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1337 return -1;
1338 }
1339
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001340 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001341 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001342 {
1343 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1344 return -1;
1345 }
1346
Amol Jadicb524072012-08-09 16:40:18 -07001347 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001348 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001349 boot_image_start + dt_image_offset + dt_entry.offset,
1350 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001351 } else
1352 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001353
1354 /* Everything looks fine. Return success. */
1355 return 0;
1356}
1357#endif
1358
Brian Swetland9c4c0752009-01-25 16:23:50 -08001359void cmd_boot(const char *arg, void *data, unsigned sz)
1360{
1361 unsigned kernel_actual;
1362 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001363 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001364 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001365 int ret = 0;
1366 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001367
1368 if (sz < sizeof(hdr)) {
1369 fastboot_fail("invalid bootimage header");
1370 return;
1371 }
1372
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001373 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001374
1375 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001376 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001377
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001378 if(target_is_emmc_boot() && hdr->page_size) {
1379 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001380 page_mask = page_size - 1;
1381 }
1382
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001383 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1384 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1385
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001386 /*
1387 * Update the kernel/ramdisk/tags address if the boot image header
1388 * has default values, these default values come from mkbootimg when
1389 * the boot image is flashed using fastboot flash:raw
1390 */
1391 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001392
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001393 /* Get virtual addresses since the hdr saves physical addresses. */
1394 hdr->kernel_addr = VA(hdr->kernel_addr);
1395 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1396 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001397
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001398 /* Check if the addresses in the header are valid. */
1399 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1400 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1401 {
1402 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1403 return -1;
1404 }
1405
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001406 /* sz should have atleast raw boot image */
1407 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001408 fastboot_fail("incomplete bootimage");
1409 return;
1410 }
1411
Amol Jadicb524072012-08-09 16:40:18 -07001412#if DEVICE_TREE
1413 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001414 ret = copy_dtb(data);
1415
1416 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001417#else
1418 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1419 {
1420 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1421 return -1;
1422 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001423#endif
1424
1425 /* Load ramdisk & kernel */
1426 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1427 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1428
1429#if DEVICE_TREE
1430 /*
1431 * If dtb is not found look for appended DTB in the kernel.
1432 * If appended dev tree is found, update the atags with
1433 * memory address to the DTB appended location on RAM.
1434 * Else update with the atags address in the kernel header
1435 */
1436 if (!dtb_copied) {
1437 void *dtb;
Channagoud Kadabia83a6972013-04-25 15:26:04 -07001438 dtb = dev_tree_appended((void *)hdr->kernel_addr, (void *)hdr->tags_addr, hdr->kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001439 if (!dtb) {
1440 fastboot_fail("dtb not found");
1441 return;
1442 }
Amol Jadicb524072012-08-09 16:40:18 -07001443 }
1444#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001445
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001446#ifndef DEVICE_TREE
1447 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1448 {
1449 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1450 return -1;
1451 }
1452#endif
1453
Brian Swetland9c4c0752009-01-25 16:23:50 -08001454 fastboot_okay("");
1455 udc_stop();
1456
Dima Zavin77e41f32013-03-06 16:10:43 -08001457 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001458 (const char*) hdr->cmdline, board_machtype(),
1459 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001460}
1461
Dima Zavin214cc642009-01-26 11:16:21 -08001462void cmd_erase(const char *arg, void *data, unsigned sz)
1463{
1464 struct ptentry *ptn;
1465 struct ptable *ptable;
1466
1467 ptable = flash_get_ptable();
1468 if (ptable == NULL) {
1469 fastboot_fail("partition table doesn't exist");
1470 return;
1471 }
1472
1473 ptn = ptable_find(ptable, arg);
1474 if (ptn == NULL) {
1475 fastboot_fail("unknown partition name");
1476 return;
1477 }
1478
1479 if (flash_erase(ptn)) {
1480 fastboot_fail("failed to erase partition");
1481 return;
1482 }
1483 fastboot_okay("");
1484}
1485
Bikas Gurungd48bd242010-09-04 19:54:32 -07001486
1487void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1488{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001489 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001490 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001491 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001492
Kinson Chikf1a43512011-07-14 11:28:39 -07001493 index = partition_get_index(arg);
1494 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001495
Kinson Chikf1a43512011-07-14 11:28:39 -07001496 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001497 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001498 return;
1499 }
neetidb4b24d62012-01-20 12:13:09 -08001500 /* Simple inefficient version of erase. Just writing
1501 0 in first block */
1502 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1503 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001504 return;
1505 }
1506 fastboot_okay("");
1507}
1508
1509
Ajay Dudani5c761132011-04-07 20:19:04 -07001510void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001511{
1512 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001513 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001514 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001515
Greg Grisco6e754772011-06-23 12:19:39 -07001516 if (!strcmp(arg, "partition"))
1517 {
1518 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001519 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001520 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001521 return;
1522 }
1523 }
Greg Grisco6e754772011-06-23 12:19:39 -07001524 else
1525 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001526 index = partition_get_index(arg);
1527 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001528 if(ptn == 0) {
1529 fastboot_fail("partition table doesn't exist");
1530 return;
1531 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001532
Greg Grisco6e754772011-06-23 12:19:39 -07001533 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1534 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1535 fastboot_fail("image is not a boot image");
1536 return;
1537 }
1538 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001539
Kinson Chikf1a43512011-07-14 11:28:39 -07001540 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001541 if (ROUND_TO_PAGE(sz,511) > size) {
1542 fastboot_fail("size too large");
1543 return;
1544 }
1545 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1546 fastboot_fail("flash write failure");
1547 return;
1548 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001549 }
1550 fastboot_okay("");
1551 return;
1552}
1553
Ajay Dudani5c761132011-04-07 20:19:04 -07001554void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1555{
1556 unsigned int chunk;
1557 unsigned int chunk_data_sz;
1558 sparse_header_t *sparse_header;
1559 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001560 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001561 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301562 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001563 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001564
Kinson Chikf1a43512011-07-14 11:28:39 -07001565 index = partition_get_index(arg);
1566 ptn = partition_get_offset(index);
1567 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001568 fastboot_fail("partition table doesn't exist");
1569 return;
1570 }
1571
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301572 size = partition_get_size(index);
1573 if (ROUND_TO_PAGE(sz,511) > size) {
1574 fastboot_fail("size too large");
1575 return;
1576 }
1577
Ajay Dudani5c761132011-04-07 20:19:04 -07001578 /* Read and skip over sparse image header */
1579 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001580 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1581 fastboot_fail("size too large");
1582 return;
1583 }
1584
Ajay Dudani5c761132011-04-07 20:19:04 -07001585 data += sparse_header->file_hdr_sz;
1586 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1587 {
1588 /* Skip the remaining bytes in a header that is longer than
1589 * we expected.
1590 */
1591 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1592 }
1593
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001594 dprintf (SPEW, "=== Sparse Image Header ===\n");
1595 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1596 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1597 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1598 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1599 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1600 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1601 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1602 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001603
1604 /* Start processing chunks */
1605 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1606 {
1607 /* Read and skip over chunk header */
1608 chunk_header = (chunk_header_t *) data;
1609 data += sizeof(chunk_header_t);
1610
1611 dprintf (SPEW, "=== Chunk Header ===\n");
1612 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1613 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1614 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1615
1616 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1617 {
1618 /* Skip the remaining bytes in a header that is longer than
1619 * we expected.
1620 */
1621 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1622 }
1623
1624 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1625 switch (chunk_header->chunk_type)
1626 {
1627 case CHUNK_TYPE_RAW:
1628 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1629 chunk_data_sz))
1630 {
1631 fastboot_fail("Bogus chunk size for chunk type Raw");
1632 return;
1633 }
1634
Ajay Dudaniab18f022011-05-12 14:39:22 -07001635 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1636 chunk_data_sz,
1637 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001638 {
1639 fastboot_fail("flash write failure");
1640 return;
1641 }
1642 total_blocks += chunk_header->chunk_sz;
1643 data += chunk_data_sz;
1644 break;
1645
1646 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001647 total_blocks += chunk_header->chunk_sz;
1648 break;
1649
Ajay Dudani5c761132011-04-07 20:19:04 -07001650 case CHUNK_TYPE_CRC:
1651 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1652 {
1653 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1654 return;
1655 }
1656 total_blocks += chunk_header->chunk_sz;
1657 data += chunk_data_sz;
1658 break;
1659
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001660 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001661 fastboot_fail("Unknown chunk type");
1662 return;
1663 }
1664 }
1665
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001666 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1667 total_blocks, sparse_header->total_blks);
1668
1669 if(total_blocks != sparse_header->total_blks)
1670 {
1671 fastboot_fail("sparse image write failure");
1672 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001673
1674 fastboot_okay("");
1675 return;
1676}
1677
1678void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1679{
1680 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001681 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1682 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001683
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001684#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001685 int ret=0;
1686 uint32 major_version=0;
1687 uint32 minor_version=0;
1688
1689 ret = scm_svc_version(&major_version,&minor_version);
1690 if(!ret)
1691 {
1692 if(major_version >= 2)
1693 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001694 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001695 {
1696 ret = encrypt_scm((uint32 **) &data, &sz);
1697 if (ret != 0) {
1698 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1699 return;
1700 }
1701
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001702 /* Protect only for SSD */
1703 if (!strcmp(arg, "ssd")) {
1704 ret = scm_protect_keystore((uint32 *) data, sz);
1705 if (ret != 0) {
1706 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1707 return;
1708 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001709 }
1710 }
1711 else
1712 {
1713 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1714 if(ret != 0)
1715 {
1716 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1717 return;
1718 }
1719 }
1720 }
1721 else
1722 {
1723 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1724 magic_number[1] == DECRYPT_MAGIC_1)
1725 {
1726 ret = decrypt_scm((uint32 **) &data, &sz);
1727 if (ret != 0) {
1728 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1729 return;
1730 }
1731 }
1732 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1733 magic_number[1] == ENCRYPT_MAGIC_1)
1734 {
1735 ret = encrypt_scm((uint32 **) &data, &sz);
1736 if (ret != 0) {
1737 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1738 return;
1739 }
1740 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001741 }
1742 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001743 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001744 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001745 dprintf(CRITICAL,"INVALID SVC Version\n");
1746 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001747 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001748#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001749
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001750 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001751 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1752 cmd_flash_mmc_img(arg, data, sz);
1753 else
1754 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001755 return;
1756}
1757
Dima Zavin214cc642009-01-26 11:16:21 -08001758void cmd_flash(const char *arg, void *data, unsigned sz)
1759{
1760 struct ptentry *ptn;
1761 struct ptable *ptable;
1762 unsigned extra = 0;
1763
1764 ptable = flash_get_ptable();
1765 if (ptable == NULL) {
1766 fastboot_fail("partition table doesn't exist");
1767 return;
1768 }
1769
1770 ptn = ptable_find(ptable, arg);
1771 if (ptn == NULL) {
1772 fastboot_fail("unknown partition name");
1773 return;
1774 }
1775
1776 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1777 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1778 fastboot_fail("image is not a boot image");
1779 return;
1780 }
1781 }
1782
Amol Jadi5c61a952012-05-04 17:05:35 -07001783 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001784 || !strcmp(ptn->name, "userdata")
1785 || !strcmp(ptn->name, "persist")
1786 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301787 if (flash_ecc_bch_enabled())
1788 /* Spare data bytes for 8 bit ECC increased by 4 */
1789 extra = ((page_size >> 9) * 20);
1790 else
1791 extra = ((page_size >> 9) * 16);
1792 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001793 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001794
1795 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1796 if (flash_write(ptn, extra, data, sz)) {
1797 fastboot_fail("flash write failure");
1798 return;
1799 }
1800 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1801 fastboot_okay("");
1802}
1803
1804void cmd_continue(const char *arg, void *data, unsigned sz)
1805{
1806 fastboot_okay("");
1807 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001808 if (target_is_emmc_boot())
1809 {
1810 boot_linux_from_mmc();
1811 }
1812 else
1813 {
1814 boot_linux_from_flash();
1815 }
Dima Zavin214cc642009-01-26 11:16:21 -08001816}
1817
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001818void cmd_reboot(const char *arg, void *data, unsigned sz)
1819{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001820 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001821 fastboot_okay("");
1822 reboot_device(0);
1823}
1824
1825void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1826{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001827 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001828 fastboot_okay("");
1829 reboot_device(FASTBOOT_MODE);
1830}
1831
Shashank Mittal162244e2011-08-08 19:01:25 -07001832void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1833{
1834 if(!device.is_unlocked)
1835 {
1836 device.is_unlocked = 1;
1837 write_device_info(&device);
1838 }
1839 fastboot_okay("");
1840}
1841
Shashank Mittala0032282011-08-26 14:50:11 -07001842void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1843{
1844 char response[64];
1845 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1846 fastboot_info(response);
1847 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1848 fastboot_info(response);
1849 fastboot_okay("");
1850}
1851
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001852void cmd_preflash(const char *arg, void *data, unsigned sz)
1853{
1854 fastboot_okay("");
1855}
1856
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001857void splash_screen ()
1858{
1859 struct ptentry *ptn;
1860 struct ptable *ptable;
1861 struct fbcon_config *fb_display = NULL;
1862
1863 if (!target_is_emmc_boot())
1864 {
1865 ptable = flash_get_ptable();
1866 if (ptable == NULL) {
1867 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001868 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001869 }
1870
1871 ptn = ptable_find(ptable, "splash");
1872 if (ptn == NULL) {
1873 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1874 } else {
1875 fb_display = fbcon_display();
1876 if (fb_display) {
1877 if (flash_read(ptn, 0, fb_display->base,
1878 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1879 fbcon_clear();
1880 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1881 }
1882 }
1883 }
1884 }
1885}
1886
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001887/* Get the size from partiton name */
1888static void get_partition_size(const char *arg, char *response)
1889{
1890 uint64_t ptn = 0;
1891 uint64_t size;
1892 int index = INVALID_PTN;
1893
1894 index = partition_get_index(arg);
1895
1896 if (index == INVALID_PTN)
1897 {
1898 dprintf(CRITICAL, "Invalid partition index\n");
1899 return;
1900 }
1901
1902 ptn = partition_get_offset(index);
1903
1904 if(!ptn)
1905 {
1906 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1907 return;
1908 }
1909
1910 size = partition_get_size(index);
1911
1912 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1913 return;
1914}
1915
1916/*
1917 * Publish the partition type & size info
1918 * fastboot getvar will publish the required information.
1919 * fastboot getvar partition_size:<partition_name>: partition size in hex
1920 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1921 */
1922static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1923{
1924 uint8_t i;
1925
1926 for (i = 0; i < num_parts; i++) {
1927 get_partition_size(info[i].part_name, info[i].size_response);
1928
1929 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1930 {
1931 dprintf(CRITICAL, "partition size name truncated\n");
1932 return;
1933 }
1934 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1935 {
1936 dprintf(CRITICAL, "partition type name truncated\n");
1937 return;
1938 }
1939
1940 /* publish partition size & type info */
1941 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1942 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1943 }
1944}
1945
Brian Swetland9c4c0752009-01-25 16:23:50 -08001946void aboot_init(const struct app_descriptor *app)
1947{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001948 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001949 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001950 unsigned sz = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03001951 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001952
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001953 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001954 if (target_is_emmc_boot())
1955 {
1956 page_size = 2048;
1957 page_mask = page_size - 1;
1958 }
1959 else
1960 {
1961 page_size = flash_page_size();
1962 page_mask = page_size - 1;
1963 }
1964
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001965 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
1966
Shashank Mittal162244e2011-08-08 19:01:25 -07001967 if(target_use_signed_kernel())
1968 {
1969 read_device_info(&device);
1970
Shashank Mittal162244e2011-08-08 19:01:25 -07001971 }
1972
Greg Griscod6250552011-06-29 14:40:23 -07001973 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001974 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001975 surf_udc_device.serialno = sn_buf;
1976
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001977 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001978 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001979 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001980 dprintf(ALWAYS,"dload mode key sequence detected\n");
1981 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001982 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001983 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001984 }
1985 else
1986 {
1987 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001988 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001989 }
1990 boot_into_fastboot = true;
1991 }
1992 if (!boot_into_fastboot)
1993 {
1994 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
1995 boot_into_recovery = 1;
1996 if (!boot_into_recovery &&
1997 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03001998 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001999 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002000 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002001 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002002 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002003 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002004
Ajay Dudani77421292010-10-27 19:34:06 -07002005 reboot_mode = check_reboot_mode();
2006 if (reboot_mode == RECOVERY_MODE) {
2007 boot_into_recovery = 1;
2008 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002009 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002010 }
2011
Pavel Nedev5d91d412013-04-29 11:34:24 +03002012 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002013 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002014 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002015 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002016 if(emmc_recovery_init())
2017 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2018 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002019 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002020 if((device.is_unlocked) || (device.is_tampered))
2021 {
2022 #ifdef TZ_TAMPER_FUSE
2023 set_tamper_fuse_cmd();
2024 #endif
2025 #if USE_PCOM_SECBOOT
2026 set_tamper_flag(device.is_tampered);
2027 #endif
2028 }
Shashank Mittala0032282011-08-26 14:50:11 -07002029 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002030 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002031 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002032 else
2033 {
2034 recovery_init();
2035 #if USE_PCOM_SECBOOT
2036 if((device.is_unlocked) || (device.is_tampered))
2037 set_tamper_flag(device.is_tampered);
2038 #endif
2039 boot_linux_from_flash();
2040 }
2041 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2042 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002043 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002044
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002045 sz = target_get_max_flash_size();
2046
Shashank Mittal162244e2011-08-08 19:01:25 -07002047 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002048
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002049 if(!usb_init)
2050 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002051
2052 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002053
Shashank Mittal23b8f422010-04-16 19:27:21 -07002054 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002055 {
2056 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002057 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002058 }
2059 else
2060 {
2061 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002062 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002063 }
2064
2065 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002066 fastboot_register("reboot", cmd_reboot);
2067 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07002068 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07002069 fastboot_register("oem device-info", cmd_oem_devinfo);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002070 fastboot_register("preflash", cmd_preflash);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07002071 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08002072 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08002073 fastboot_publish("serialno", sn_buf);
Channagoud Kadabi873d8ff2013-07-01 17:31:40 -07002074 /*
2075 * fastboot publish is supported only for emmc partitions
2076 * Calling this for NAND prints some error messages which
2077 * is harmless but misleading. Avoid calling this for NAND
2078 * devices.
2079 */
2080 if (target_is_emmc_boot())
2081 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002082 /* Max download size supported */
2083 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", sz);
2084 fastboot_publish("max-download-size", (const char *) max_download_size);
Kinson Chikf1a43512011-07-14 11:28:39 -07002085 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07002086 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002087 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002088}
2089
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002090uint32_t get_page_size()
2091{
2092 return page_size;
2093}
2094
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002095/*
2096 * Calculated and save hash (SHA256) for non-signed boot image.
2097 *
2098 * Hash the same data that is checked on the signed boot image.
2099 * Kernel and Ramdisk are already read to memory buffers.
2100 * Need to read the entire device-tree from mmc
2101 * since non-signed image only read the DT tags of the relevant platform.
2102 *
2103 * @param kernel_addr - kernel bufer
2104 * @param kernel_actual - kernel size in bytes
2105 * @param ramdisk_addr - ramdisk buffer
2106 * @param ramdisk_actual - ramdisk size
2107 * @param ptn - partition
2108 * @param dt_offset - device tree offset on mmc partition
2109 * @param dt_size
2110 *
2111 * @return int - 0 on success, negative value on failure.
2112 */
2113int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2114 void *ramdisk_addr, unsigned ramdisk_actual,
2115 unsigned long long ptn,
2116 unsigned dt_offset, unsigned dt_size)
2117{
2118 SHA256_CTX sha256_ctx;
2119 char digest[32]={0};
2120 char *buf = (char *)target_get_scratch_address();
2121 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2122 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2123
2124 SHA256_Init(&sha256_ctx);
2125
2126 /* Read Boot Header */
2127 if (mmc_read(ptn, buf, page_size))
2128 {
2129 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2130 return -1;
2131 }
2132 /* Read entire Device Tree */
2133 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2134 {
2135 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2136 return -1;
2137 }
2138 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2139 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2140 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2141 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2142
2143 SHA256_Final(digest, &sha256_ctx);
2144
2145 save_kernel_hash_cmd(digest);
2146 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2147
2148 return 0;
2149}
2150
Brian Swetland9c4c0752009-01-25 16:23:50 -08002151APP_START(aboot)
2152 .init = aboot_init,
2153APP_END