blob: e95b6e74da20256d6353c62e5f4b8892541c091f [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kuogee Hsieh55e92c52013-12-20 08:34:10 -08005 * Copyright (c) 2009-2014, 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>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.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>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070055#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070056#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070057#include <boot_verifier.h>
Dima Zavin214cc642009-01-26 11:16:21 -080058
Neeti Desai17379b82012-06-04 18:42:53 -070059#if DEVICE_TREE
60#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070061#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070062#endif
63
Shashank Mittalcd98d472011-08-02 14:29:24 -070064#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080065#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080066#include "bootimg.h"
67#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070068#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070069#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070070#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070071#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070072#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070073
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070076extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070077extern int get_target_boot_params(const char *cmdline, const char *part,
78 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070079
80void write_device_info_mmc(device_info *dev);
81void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070082static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070083
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070084#define EXPAND(NAME) #NAME
85#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070086
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080087#ifdef MEMBASE
88#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
89#else
David Ng183a7422009-12-07 14:55:21 -080090#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080091#endif
92
Deepa Dinamani0e163a42013-05-24 17:08:15 -070093#ifndef MEMSIZE
94#define MEMSIZE 1024*1024
95#endif
96
97#define MAX_TAGS_SIZE 1024
98
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080099#define RECOVERY_MODE 0x77665502
100#define FASTBOOT_MODE 0x77665500
101
Kun Liang2f1601a2013-08-12 16:29:54 +0800102/* make 4096 as default size to ensure EFS,EXT4's erasing */
103#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800104#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800105
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800106#define UBI_MAGIC "UBI#"
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700107#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800108#define UBI_MAGIC_SIZE 0x04
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700109#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800110
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800111#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
112
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700113#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
114
Smita Ghoshb4555da2014-09-18 14:14:56 -0700115#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700116static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700117#else
David Ng183a7422009-12-07 14:55:21 -0800118static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700119#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800120static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300121static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800122static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800123static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700124static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300125static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800126
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800127static const char *baseband_apq = " androidboot.baseband=apq";
128static const char *baseband_msm = " androidboot.baseband=msm";
129static const char *baseband_csfb = " androidboot.baseband=csfb";
130static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700131static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800132static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700133static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800134static const char *baseband_dsda = " androidboot.baseband=dsda";
135static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800136static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800137static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800138
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700139static unsigned page_size = 0;
140static unsigned page_mask = 0;
141static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
142static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700143static char target_boot_params[64];
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700144
Shashank Mittalcd98d472011-08-02 14:29:24 -0700145/* Assuming unauthorized kernel image by default */
146static int auth_kernel_img = 0;
147
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700148static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700149
Dima Zavin42168f22009-01-30 11:52:22 -0800150struct atag_ptbl_entry
151{
152 char name[16];
153 unsigned offset;
154 unsigned size;
155 unsigned flags;
156};
157
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700158/*
159 * Partition info, required to be published
160 * for fastboot
161 */
162struct getvar_partition_info {
163 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
164 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
165 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
166 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
167 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
168};
169
170/*
171 * Right now, we are publishing the info for only
172 * three partitions
173 */
174struct getvar_partition_info part_info[] =
175{
176 { "system" , "partition-size:", "partition-type:", "", "ext4" },
177 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
178 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
179};
180
181char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700182char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800183char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700184char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530185char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700186
Greg Griscod2471ef2011-07-14 13:00:42 -0700187extern int emmc_recovery_init(void);
188
Kinson Chik0b1c8162011-08-31 16:31:57 -0700189#if NO_KEYPAD_DRIVER
190extern int fastboot_trigger(void);
191#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700192
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800193static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700194{
195 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700196#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800197 if (is_arm64)
198 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
199 else
200 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700201 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
202 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700203#endif
204}
205
Dima Zavin42168f22009-01-30 11:52:22 -0800206static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
207{
208 struct atag_ptbl_entry atag_ptn;
209
210 memcpy(atag_ptn.name, ptn->name, 16);
211 atag_ptn.name[15] = '\0';
212 atag_ptn.offset = ptn->start;
213 atag_ptn.size = ptn->length;
214 atag_ptn.flags = ptn->flags;
215 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
216 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
217}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800218
Neeti Desaie245d492012-06-01 12:52:13 -0700219unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800220{
David Ng183a7422009-12-07 14:55:21 -0800221 int cmdline_len = 0;
222 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800223 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700224 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800225 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300226 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700227 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700228 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800229
Brian Swetland9c4c0752009-01-25 16:23:50 -0800230 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800231 cmdline_len = strlen(cmdline);
232 have_cmdline = 1;
233 }
234 if (target_is_emmc_boot()) {
235 cmdline_len += strlen(emmc_cmdline);
Smita Ghoshb4555da2014-09-18 14:14:56 -0700236#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700237 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
238 ASSERT(boot_dev_buf);
239 platform_boot_dev_cmdline(boot_dev_buf);
240 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700241#endif
David Ng183a7422009-12-07 14:55:21 -0800242 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800243
244 cmdline_len += strlen(usb_sn_cmdline);
245 cmdline_len += strlen(sn_buf);
246
Pavel Nedev5614d222013-06-17 18:01:02 +0300247 if (boot_into_recovery && gpt_exists)
248 cmdline_len += strlen(secondary_gpt_enable);
249
Pavel Nedev328ac822013-04-05 15:25:11 +0300250 if (boot_into_ffbm) {
251 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700252 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800253 /* reduce kernel console messages to speed-up boot */
254 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700255 } else if (device.charger_screen_enabled &&
256 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700257 pause_at_bootup = 1;
258 cmdline_len += strlen(battchg_pause);
259 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800260
Shashank Mittalcd98d472011-08-02 14:29:24 -0700261 if(target_use_signed_kernel() && auth_kernel_img) {
262 cmdline_len += strlen(auth_kernel);
263 }
264
Joonwoo Park61112782013-10-02 19:50:39 -0700265 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
266 "system",
267 target_boot_params,
268 sizeof(target_boot_params)) == 0) {
269 have_target_boot_params = 1;
270 cmdline_len += strlen(target_boot_params);
271 }
272
Ajay Dudanid04110c2011-01-17 23:55:07 -0800273 /* Determine correct androidboot.baseband to use */
274 switch(target_baseband())
275 {
276 case BASEBAND_APQ:
277 cmdline_len += strlen(baseband_apq);
278 break;
279
280 case BASEBAND_MSM:
281 cmdline_len += strlen(baseband_msm);
282 break;
283
284 case BASEBAND_CSFB:
285 cmdline_len += strlen(baseband_csfb);
286 break;
287
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800288 case BASEBAND_SVLTE2A:
289 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800290 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700291
292 case BASEBAND_MDM:
293 cmdline_len += strlen(baseband_mdm);
294 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700295
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800296 case BASEBAND_MDM2:
297 cmdline_len += strlen(baseband_mdm2);
298 break;
299
Amol Jadi5c61a952012-05-04 17:05:35 -0700300 case BASEBAND_SGLTE:
301 cmdline_len += strlen(baseband_sglte);
302 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530303
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800304 case BASEBAND_SGLTE2:
305 cmdline_len += strlen(baseband_sglte2);
306 break;
307
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530308 case BASEBAND_DSDA:
309 cmdline_len += strlen(baseband_dsda);
310 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800311
312 case BASEBAND_DSDA2:
313 cmdline_len += strlen(baseband_dsda2);
314 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800315 }
316
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800317 if (cmdline) {
318 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
319 target_display_panel_node(device.display_panel,
320 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
321 strlen(display_panel_buf)) {
322 cmdline_len += strlen(display_panel_buf);
323 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700324 }
325
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800326 if (target_warm_boot()) {
327 warm_boot = true;
328 cmdline_len += strlen(warmboot_cmdline);
329 }
330
David Ng183a7422009-12-07 14:55:21 -0800331 if (cmdline_len > 0) {
332 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800333 unsigned char *dst;
334
335 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
336 ASSERT(cmdline_final != NULL);
337 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700338
Amol Jadi168b7712012-03-06 16:15:00 -0800339 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800340 if (have_cmdline) {
341 src = cmdline;
342 while ((*dst++ = *src++));
343 }
344 if (target_is_emmc_boot()) {
345 src = emmc_cmdline;
346 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700347 have_cmdline = 1;
348 while ((*dst++ = *src++));
Smita Ghoshb4555da2014-09-18 14:14:56 -0700349#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700350 src = boot_dev_buf;
351 if (have_cmdline) --dst;
352 while ((*dst++ = *src++));
353#endif
David Ngf773dde2010-07-26 19:55:08 -0700354 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800355
356 src = usb_sn_cmdline;
357 if (have_cmdline) --dst;
358 have_cmdline = 1;
359 while ((*dst++ = *src++));
360 src = sn_buf;
361 if (have_cmdline) --dst;
362 have_cmdline = 1;
363 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800364 if (warm_boot) {
365 if (have_cmdline) --dst;
366 src = warmboot_cmdline;
367 while ((*dst++ = *src++));
368 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800369
Pavel Nedev5614d222013-06-17 18:01:02 +0300370 if (boot_into_recovery && gpt_exists) {
371 src = secondary_gpt_enable;
372 if (have_cmdline) --dst;
373 while ((*dst++ = *src++));
374 }
375
Pavel Nedev328ac822013-04-05 15:25:11 +0300376 if (boot_into_ffbm) {
377 src = androidboot_mode;
378 if (have_cmdline) --dst;
379 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700380 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300381 if (have_cmdline) --dst;
382 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800383 src = loglevel;
384 if (have_cmdline) --dst;
385 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300386 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700387 src = battchg_pause;
388 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800389 while ((*dst++ = *src++));
390 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800391
Shashank Mittalcd98d472011-08-02 14:29:24 -0700392 if(target_use_signed_kernel() && auth_kernel_img) {
393 src = auth_kernel;
394 if (have_cmdline) --dst;
395 while ((*dst++ = *src++));
396 }
397
Ajay Dudanid04110c2011-01-17 23:55:07 -0800398 switch(target_baseband())
399 {
400 case BASEBAND_APQ:
401 src = baseband_apq;
402 if (have_cmdline) --dst;
403 while ((*dst++ = *src++));
404 break;
405
406 case BASEBAND_MSM:
407 src = baseband_msm;
408 if (have_cmdline) --dst;
409 while ((*dst++ = *src++));
410 break;
411
412 case BASEBAND_CSFB:
413 src = baseband_csfb;
414 if (have_cmdline) --dst;
415 while ((*dst++ = *src++));
416 break;
417
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800418 case BASEBAND_SVLTE2A:
419 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800420 if (have_cmdline) --dst;
421 while ((*dst++ = *src++));
422 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700423
424 case BASEBAND_MDM:
425 src = baseband_mdm;
426 if (have_cmdline) --dst;
427 while ((*dst++ = *src++));
428 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700429
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800430 case BASEBAND_MDM2:
431 src = baseband_mdm2;
432 if (have_cmdline) --dst;
433 while ((*dst++ = *src++));
434 break;
435
Amol Jadi5c61a952012-05-04 17:05:35 -0700436 case BASEBAND_SGLTE:
437 src = baseband_sglte;
438 if (have_cmdline) --dst;
439 while ((*dst++ = *src++));
440 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530441
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800442 case BASEBAND_SGLTE2:
443 src = baseband_sglte2;
444 if (have_cmdline) --dst;
445 while ((*dst++ = *src++));
446 break;
447
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530448 case BASEBAND_DSDA:
449 src = baseband_dsda;
450 if (have_cmdline) --dst;
451 while ((*dst++ = *src++));
452 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800453
454 case BASEBAND_DSDA2:
455 src = baseband_dsda2;
456 if (have_cmdline) --dst;
457 while ((*dst++ = *src++));
458 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800459 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700460
461 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700462 src = display_panel_buf;
463 if (have_cmdline) --dst;
464 while ((*dst++ = *src++));
465 }
Joonwoo Park61112782013-10-02 19:50:39 -0700466
467 if (have_target_boot_params) {
468 if (have_cmdline) --dst;
469 src = target_boot_params;
470 while ((*dst++ = *src++));
471 }
Neeti Desaie245d492012-06-01 12:52:13 -0700472 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700473
474
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700475 if (boot_dev_buf)
476 free(boot_dev_buf);
477
Maria Yu9ca0c402014-07-07 14:49:34 +0800478 dprintf(INFO, "cmdline: %s\n", cmdline_final ? cmdline_final : "");
Neeti Desaie245d492012-06-01 12:52:13 -0700479 return cmdline_final;
480}
481
482unsigned *atag_core(unsigned *ptr)
483{
484 /* CORE */
485 *ptr++ = 2;
486 *ptr++ = 0x54410001;
487
488 return ptr;
489
490}
491
492unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
493 unsigned ramdisk_size)
494{
495 if (ramdisk_size) {
496 *ptr++ = 4;
497 *ptr++ = 0x54420005;
498 *ptr++ = (unsigned)ramdisk;
499 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800500 }
501
Neeti Desaie245d492012-06-01 12:52:13 -0700502 return ptr;
503}
504
505unsigned *atag_ptable(unsigned **ptr_addr)
506{
507 int i;
508 struct ptable *ptable;
509
510 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700511 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
512 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700513 *(*ptr_addr)++ = 0x4d534d70;
514 for (i = 0; i < ptable->count; ++i)
515 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
516 }
517
518 return (*ptr_addr);
519}
520
521unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
522{
523 int cmdline_length = 0;
524 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700525 char *dest;
526
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800527 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700528 n = (cmdline_length + 4) & (~3);
529
530 *ptr++ = (n / 4) + 2;
531 *ptr++ = 0x54410009;
532 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800533 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700534 ptr += (n / 4);
535
536 return ptr;
537}
538
539unsigned *atag_end(unsigned *ptr)
540{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800541 /* END */
542 *ptr++ = 0;
543 *ptr++ = 0;
544
Neeti Desaie245d492012-06-01 12:52:13 -0700545 return ptr;
546}
547
548void generate_atags(unsigned *ptr, const char *cmdline,
549 void *ramdisk, unsigned ramdisk_size)
550{
551
552 ptr = atag_core(ptr);
553 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
554 ptr = target_atag_mem(ptr);
555
556 /* Skip NAND partition ATAGS for eMMC boot */
557 if (!target_is_emmc_boot()){
558 ptr = atag_ptable(&ptr);
559 }
560
561 ptr = atag_cmdline(ptr, cmdline);
562 ptr = atag_end(ptr);
563}
564
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700565typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700566void boot_linux(void *kernel, unsigned *tags,
567 const char *cmdline, unsigned machtype,
568 void *ramdisk, unsigned ramdisk_size)
569{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800570 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800571#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700572 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800573#endif
574
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700575 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800576 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800577 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800578
579 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700580
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800581 final_cmdline = update_cmdline((const char*)cmdline);
582
Neeti Desai17379b82012-06-04 18:42:53 -0700583#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800584 dprintf(INFO, "Updating device tree: start\n");
585
Neeti Desai17379b82012-06-04 18:42:53 -0700586 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800587 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700588 if(ret)
589 {
590 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
591 ASSERT(0);
592 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800593 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700594#else
Neeti Desaie245d492012-06-01 12:52:13 -0700595 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800596 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700597#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700598
Maria Yu52254c02014-07-04 16:14:54 +0800599 free(final_cmdline);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700600 /* Perform target specific cleanup */
601 target_uninit();
602
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800603 /* Turn off splash screen if enabled */
604#if DISPLAY_SPLASH_SCREEN
605 target_display_shutdown();
606#endif
607
608
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800609 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
610 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800611
612 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700613
Amol Jadi4421e652011-06-16 15:00:48 -0700614 /* do any platform specific cleanup before kernel entry */
615 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700616
Brian Swetland9c4c0752009-01-25 16:23:50 -0800617 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700618
Amol Jadi504f9fe2012-08-16 13:56:48 -0700619#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800620 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700621#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700622 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800623
624 if (IS_ARM64(kptr))
625 /* Jump to a 64bit kernel */
626 scm_elexec_call((paddr_t)kernel, tags_phys);
627 else
628 /* Jump to a 32bit kernel */
629 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800630}
631
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700632/* Function to check if the memory address range falls within the aboot
633 * boundaries.
634 * start: Start of the memory region
635 * size: Size of the memory region
636 */
637int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
638{
639 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800640 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700641 return -1;
642
643 /* Check for memory overlap. */
644 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
645 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700646 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700647 return 0;
648 else
649 return -1;
650}
651
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800652#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800653
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800654BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800655#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800656BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800657#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800658
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700659static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
660{
661 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800662#if IMAGE_VERIF_ALGO_SHA1
663 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
664#else
665 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
666#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700667
668 /* Assume device is rooted at this time. */
669 device.is_tampered = 1;
670
671 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
672
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700673#if VERIFIED_BOOT
674 if(boot_into_recovery)
675 {
676 ret = boot_verify_image((unsigned char *)bootimg_addr,
677 bootimg_size, "recovery");
678 }
679 else
680 {
681 ret = boot_verify_image((unsigned char *)bootimg_addr,
682 bootimg_size, "boot");
683 }
684 boot_verify_print_state();
685#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700686 ret = image_verify((unsigned char *)bootimg_addr,
687 (unsigned char *)(bootimg_addr + bootimg_size),
688 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800689 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700690#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700691 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
692
693 if (ret)
694 {
695 /* Authorized kernel */
696 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700697 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700698 }
699
700#if USE_PCOM_SECBOOT
701 set_tamper_flag(device.is_tampered);
702#endif
703
704 if(device.is_tampered)
705 {
706 write_device_info_mmc(&device);
707 #ifdef TZ_TAMPER_FUSE
708 set_tamper_fuse_cmd();
709 #endif
710 #ifdef ASSERT_ON_TAMPER
711 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
712 ASSERT(0);
713 #endif
714 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700715
716#if VERIFIED_BOOT
717 if(boot_verify_get_state() == RED)
718 {
719 if(!boot_into_recovery)
720 {
721 dprintf(CRITICAL,
722 "Device verification failed. Rebooting into recovery.\n");
723 reboot_device(RECOVERY_MODE);
724 }
725 else
726 {
727 dprintf(CRITICAL,
728 "Recovery image verification failed. Asserting..\n");
729 ASSERT(0);
730 }
731 }
732#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700733}
734
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530735static bool check_format_bit()
736{
737 bool ret = false;
738 int index;
739 uint64_t offset;
740 struct boot_selection_info *in = NULL;
741 char *buf = NULL;
742
743 index = partition_get_index("bootselect");
744 if (index == INVALID_PTN)
745 {
746 dprintf(INFO, "Unable to locate /bootselect partition\n");
747 return ret;
748 }
749 offset = partition_get_offset(index);
750 if(!offset)
751 {
752 dprintf(INFO, "partition /bootselect doesn't exist\n");
753 return ret;
754 }
755 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
756 ASSERT(buf);
757 if (mmc_read(offset, (unsigned int *)buf, page_size))
758 {
759 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
760 free(buf);
761 return ret;
762 }
763 in = (struct boot_selection_info *) buf;
764 if ((in->signature == BOOTSELECT_SIGNATURE) &&
765 (in->version == BOOTSELECT_VERSION)) {
766 if ((in->state_info & BOOTSELECT_FORMAT) &&
767 !(in->state_info & BOOTSELECT_FACTORY))
768 ret = true;
769 } else {
770 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
771 in->signature, in->version);
772 ASSERT(0);
773 }
774 free(buf);
775 return ret;
776}
777
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700778void boot_verifier_init()
779{
780
781 uint32_t boot_state;
782 /* Check if device unlock */
783 if(device.is_unlocked)
784 {
785 boot_verify_send_event(DEV_UNLOCK);
786 boot_verify_print_state();
787 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
788 return;
789 }
790 else
791 {
792 boot_verify_send_event(BOOT_INIT);
793 }
794
795 /* Initialize keystore */
796 boot_state = boot_verify_keystore_init();
797 if(boot_state == YELLOW)
798 {
799 boot_verify_print_state();
800 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
801 }
802}
803
Shashank Mittal23b8f422010-04-16 19:27:21 -0700804int boot_linux_from_mmc(void)
805{
806 struct boot_img_hdr *hdr = (void*) buf;
807 struct boot_img_hdr *uhdr;
808 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700809 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700810 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700811 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700812
Shashank Mittalcd98d472011-08-02 14:29:24 -0700813 unsigned char *image_addr = 0;
814 unsigned kernel_actual;
815 unsigned ramdisk_actual;
816 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700817 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700818
819#if DEVICE_TREE
820 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700821 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700822 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800823 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700824 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700825#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800826 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
827 struct kernel64_hdr *kptr = (void*) kbuf;
828
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530829 if (check_format_bit())
830 boot_into_recovery = 1;
831
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700832 if (!boot_into_recovery) {
833 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
834 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
835 if (rcode <= 0) {
836 boot_into_ffbm = false;
837 if (rcode < 0)
838 dprintf(CRITICAL,"failed to get ffbm cookie");
839 } else
840 boot_into_ffbm = true;
841 } else
842 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700843 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
844 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
845 dprintf(INFO, "Unified boot method!\n");
846 hdr = uhdr;
847 goto unified_boot;
848 }
Greg Griscod6250552011-06-29 14:40:23 -0700849 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700850 index = partition_get_index("boot");
851 ptn = partition_get_offset(index);
852 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700853 dprintf(CRITICAL, "ERROR: No boot partition found\n");
854 return -1;
855 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700856 }
857 else {
858 index = partition_get_index("recovery");
859 ptn = partition_get_offset(index);
860 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700861 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
862 return -1;
863 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700864 }
865
Greg Griscod6250552011-06-29 14:40:23 -0700866 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700867 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
868 return -1;
869 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700870
871 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700872 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700873 return -1;
874 }
875
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700876 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530877
878 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
879 dprintf(CRITICAL, "ERROR: Invalid page size\n");
880 return -1;
881 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700882 page_size = hdr->page_size;
883 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700884 }
885
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800886 /* Read the next page to get kernel Image header
887 * which lives in the second page for arm64 targets.
888 */
889
890 if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
891 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
892 return -1;
893 }
894
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700895 /*
896 * Update the kernel/ramdisk/tags address if the boot image header
897 * has default values, these default values come from mkbootimg when
898 * the boot image is flashed using fastboot flash:raw
899 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800900 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700901
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700902 /* Get virtual addresses since the hdr saves physical addresses. */
903 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
904 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
905 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700906
907 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
908 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
909
910 /* Check if the addresses in the header are valid. */
911 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
912 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
913 {
914 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
915 return -1;
916 }
917
918#ifndef DEVICE_TREE
919 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
920 {
921 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
922 return -1;
923 }
924#endif
925
Shashank Mittalcd98d472011-08-02 14:29:24 -0700926 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300927 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
928 (int) target_use_signed_kernel(),
929 device.is_unlocked,
930 device.is_tampered);
931
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700932#if VERIFIED_BOOT
933 boot_verifier_init();
934#endif
935
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700936 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800937 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700938 offset = 0;
939
Shashank Mittalcd98d472011-08-02 14:29:24 -0700940 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700941
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800942#if DEVICE_TREE
943 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
944 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700945
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700946 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700947 {
948 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
949 return -1;
950 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800951#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530952 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700953
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700954#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700955
Amol Jadib6be5c12012-11-14 13:39:51 -0800956 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700957 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800958
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700959 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
960 {
961 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
962 return -1;
963 }
964
Shashank Mittalcd98d472011-08-02 14:29:24 -0700965 /* Read image without signature */
966 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
967 {
968 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
969 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800970 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700971
Amol Jadib6be5c12012-11-14 13:39:51 -0800972 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700973 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800974
Shashank Mittalcd98d472011-08-02 14:29:24 -0700975 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700976
977 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
978 {
979 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
980 return -1;
981 }
982
Shashank Mittalcd98d472011-08-02 14:29:24 -0700983 /* Read signature */
984 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
985 {
986 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700987 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700988 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800989
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700990 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700991
Neeti Desai465491e2012-07-31 12:53:35 -0700992 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700993 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
994 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700995
Neeti Desai465491e2012-07-31 12:53:35 -0700996 #if DEVICE_TREE
997 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700998 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700999 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001000
Deepa Dinamani19648b42013-09-05 17:05:55 -07001001 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001002 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1003 return -1;
1004 }
Deepa Dinamani19648b42013-09-05 17:05:55 -07001005
Neeti Desai465491e2012-07-31 12:53:35 -07001006 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -07001007 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001008 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
1009 return -1;
1010 }
1011
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001012 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001013 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001014 {
1015 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1016 return -1;
1017 }
1018
Joel Kingaa335dc2013-06-03 16:11:08 -07001019 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001020 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -08001021 /*
1022 * If appended dev tree is found, update the atags with
1023 * memory address to the DTB appended location on RAM.
1024 * Else update with the atags address in the kernel header
1025 */
1026 void *dtb;
1027 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001028 hdr->kernel_size,
1029 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001030 if (!dtb) {
1031 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1032 return -1;
1033 }
Neeti Desai465491e2012-07-31 12:53:35 -07001034 }
1035 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001036 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001037 else
1038 {
Amol Jadib6be5c12012-11-14 13:39:51 -08001039 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001040
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001041 image_addr = (unsigned char *)target_get_scratch_address();
1042#if DEVICE_TREE
1043 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1044 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1045
1046 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
1047 {
1048 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1049 return -1;
1050 }
1051#else
1052 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1053
1054#endif
1055 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
1056 {
1057 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1058 return -1;
1059 }
1060
Amol Jadib6be5c12012-11-14 13:39:51 -08001061 dprintf(INFO, "Loading boot image (%d): start\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001062 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001063 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001064
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001065 offset = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001066
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001067 /* Load the entire boot image */
1068 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) {
1069 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
Shashank Mittalcd98d472011-08-02 14:29:24 -07001070 return -1;
1071 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001072
1073 dprintf(INFO, "Loading boot image (%d): done\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001074 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001075 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -07001076
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001077 #ifdef TZ_SAVE_KERNEL_HASH
1078 aboot_save_boot_hash_mmc(image_addr, imagesize_actual);
1079 #endif /* TZ_SAVE_KERNEL_HASH */
1080
1081 /* Move kernel, ramdisk and device tree to correct address */
1082 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1083 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Neeti Desai465491e2012-07-31 12:53:35 -07001084
1085 #if DEVICE_TREE
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001086 if(hdr->dt_size) {
1087 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1088 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001089
Deepa Dinamani19648b42013-09-05 17:05:55 -07001090 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001091 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1092 return -1;
1093 }
1094
Joel Kingaa335dc2013-06-03 16:11:08 -07001095 /* Find index of device tree within device tree table */
1096 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001097 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1098 return -1;
1099 }
1100
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001101 /* Validate and Read device device tree in the tags_addr */
Joel Kingaa335dc2013-06-03 16:11:08 -07001102 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001103 {
1104 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1105 return -1;
1106 }
1107
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001108 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001109 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301110 /* Validate the tags_addr */
1111 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1112 {
1113 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1114 return -1;
1115 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001116 /*
1117 * If appended dev tree is found, update the atags with
1118 * memory address to the DTB appended location on RAM.
1119 * Else update with the atags address in the kernel header
1120 */
1121 void *dtb;
1122 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001123 kernel_actual,
1124 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001125 if (!dtb) {
1126 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1127 return -1;
1128 }
Neeti Desai465491e2012-07-31 12:53:35 -07001129 }
1130 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001131 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001132
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001133 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1134 target_load_ssd_keystore();
1135
Shashank Mittal23b8f422010-04-16 19:27:21 -07001136unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001137
Dima Zavin77e41f32013-03-06 16:10:43 -08001138 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001139 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001140 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1141
1142 return 0;
1143}
1144
Dima Zavin214cc642009-01-26 11:16:21 -08001145int boot_linux_from_flash(void)
1146{
1147 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001148 struct ptentry *ptn;
1149 struct ptable *ptable;
1150 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001151
Shashank Mittalcd98d472011-08-02 14:29:24 -07001152 unsigned char *image_addr = 0;
1153 unsigned kernel_actual;
1154 unsigned ramdisk_actual;
1155 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001156 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001157
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001158#if DEVICE_TREE
1159 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001160 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001161 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001162 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001163#endif
1164
David Ng183a7422009-12-07 14:55:21 -08001165 if (target_is_emmc_boot()) {
1166 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1167 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1168 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1169 return -1;
1170 }
1171 goto continue_boot;
1172 }
1173
Dima Zavin214cc642009-01-26 11:16:21 -08001174 ptable = flash_get_ptable();
1175 if (ptable == NULL) {
1176 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1177 return -1;
1178 }
1179
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001180 if(!boot_into_recovery)
1181 {
1182 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001183
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001184 if (ptn == NULL) {
1185 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1186 return -1;
1187 }
1188 }
1189 else
1190 {
1191 ptn = ptable_find(ptable, "recovery");
1192 if (ptn == NULL) {
1193 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1194 return -1;
1195 }
Dima Zavin214cc642009-01-26 11:16:21 -08001196 }
1197
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001198 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001199 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1200 return -1;
1201 }
Dima Zavin214cc642009-01-26 11:16:21 -08001202
1203 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001204 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001205 return -1;
1206 }
1207
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001208 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001209 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 -08001210 return -1;
1211 }
1212
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001213 /*
1214 * Update the kernel/ramdisk/tags address if the boot image header
1215 * has default values, these default values come from mkbootimg when
1216 * the boot image is flashed using fastboot flash:raw
1217 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001218 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001219
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001220 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001221 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1222 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1223 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1224
1225 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1226 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1227
1228 /* Check if the addresses in the header are valid. */
1229 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1230 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1231 {
1232 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1233 return -1;
1234 }
1235
1236#ifndef DEVICE_TREE
1237 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1238 {
1239 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1240 return -1;
1241 }
1242#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001243
Shashank Mittalcd98d472011-08-02 14:29:24 -07001244 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001245 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001246 {
1247 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001248 offset = 0;
1249
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001250#if DEVICE_TREE
1251 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1252 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001253
1254 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1255 {
1256 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1257 return -1;
1258 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001259#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001260 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001261#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001262
Amol Jadib6be5c12012-11-14 13:39:51 -08001263 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001264 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001265
Shashank Mittalcd98d472011-08-02 14:29:24 -07001266 /* Read image without signature */
1267 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1268 {
1269 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1270 return -1;
1271 }
Dima Zavin214cc642009-01-26 11:16:21 -08001272
Amol Jadib6be5c12012-11-14 13:39:51 -08001273 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001274 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001275
Shashank Mittalcd98d472011-08-02 14:29:24 -07001276 offset = imagesize_actual;
1277 /* Read signature */
1278 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1279 {
1280 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001281 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001282 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001283
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001284 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001285
1286 /* Move kernel and ramdisk to correct address */
1287 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1288 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001289#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001290 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001291 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001292 {
1293 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1294 return -1;
1295 }
1296
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001297 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1298#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001299
1300 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001301 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001302 {
1303 write_device_info_flash(&device);
1304 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301305#if USE_PCOM_SECBOOT
1306 set_tamper_flag(device.is_tampered);
1307#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001308 }
1309 else
1310 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001311 offset = page_size;
1312
Amol Jadib6be5c12012-11-14 13:39:51 -08001313 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1314 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1315 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1316
1317 dprintf(INFO, "Loading boot image (%d): start\n",
1318 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001319 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001320
1321 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001322 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1323 return -1;
1324 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001325 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001326
Amol Jadib6be5c12012-11-14 13:39:51 -08001327 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001328 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1329 return -1;
1330 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001331 offset += ramdisk_actual;
1332
1333 dprintf(INFO, "Loading boot image (%d): done\n",
1334 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001335 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001336
1337 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001338 offset += second_actual;
1339 /* Second image loading not implemented. */
1340 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001341 }
1342
1343#if DEVICE_TREE
1344 if(hdr->dt_size != 0) {
1345
1346 /* Read the device tree table into buffer */
1347 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1348 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1349 return -1;
1350 }
1351
1352 table = (struct dt_table*) dt_buf;
1353
Deepa Dinamani19648b42013-09-05 17:05:55 -07001354 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001355 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1356 return -1;
1357 }
1358
Deepa Dinamani19648b42013-09-05 17:05:55 -07001359 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1360 if (!table)
1361 return -1;
1362
1363 /* Read the entire device tree table into buffer */
1364 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1365 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1366 return -1;
1367 }
1368
1369
Joel Kingaa335dc2013-06-03 16:11:08 -07001370 /* Find index of device tree within device tree table */
1371 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001372 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1373 return -1;
1374 }
1375
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001376 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001377 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001378 {
1379 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1380 return -1;
1381 }
1382
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001383 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001384 if(flash_read(ptn, offset + dt_entry.offset,
1385 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001386 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1387 return -1;
1388 }
1389 }
1390#endif
1391
Shashank Mittalcd98d472011-08-02 14:29:24 -07001392 }
David Ng183a7422009-12-07 14:55:21 -08001393continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001394
Dima Zavin214cc642009-01-26 11:16:21 -08001395 /* TODO: create/pass atags to kernel */
1396
Ajay Dudanie28a6072011-07-01 13:59:46 -07001397 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001398 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001399 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1400
1401 return 0;
1402}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001403
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001404BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001405void write_device_info_mmc(device_info *dev)
1406{
1407 struct device_info *info = (void*) info_buf;
1408 unsigned long long ptn = 0;
1409 unsigned long long size;
1410 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001411 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001412 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001413
1414 index = partition_get_index("aboot");
1415 ptn = partition_get_offset(index);
1416 if(ptn == 0)
1417 {
1418 return;
1419 }
1420
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001421 lun = partition_get_lun(index);
1422 mmc_set_lun(lun);
1423
Shashank Mittal162244e2011-08-08 19:01:25 -07001424 size = partition_get_size(index);
1425
1426 memcpy(info, dev, sizeof(device_info));
1427
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001428 blocksize = mmc_get_device_blocksize();
1429
1430 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001431 {
1432 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1433 return;
1434 }
1435}
1436
1437void read_device_info_mmc(device_info *dev)
1438{
1439 struct device_info *info = (void*) info_buf;
1440 unsigned long long ptn = 0;
1441 unsigned long long size;
1442 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001443 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001444
1445 index = partition_get_index("aboot");
1446 ptn = partition_get_offset(index);
1447 if(ptn == 0)
1448 {
1449 return;
1450 }
1451
1452 size = partition_get_size(index);
1453
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001454 blocksize = mmc_get_device_blocksize();
1455
1456 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001457 {
1458 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1459 return;
1460 }
1461
1462 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1463 {
1464 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Shashank Mittal7e033062014-09-25 19:58:07 -07001465#if DEFAULT_UNLOCK
1466 info->is_unlocked = 1;
1467#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001468 info->is_unlocked = 0;
Shashank Mittal7e033062014-09-25 19:58:07 -07001469#endif
1470 info->is_verified = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001471 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001472 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001473
1474 write_device_info_mmc(info);
1475 }
1476 memcpy(dev, info, sizeof(device_info));
1477}
1478
1479void write_device_info_flash(device_info *dev)
1480{
1481 struct device_info *info = (void *) info_buf;
1482 struct ptentry *ptn;
1483 struct ptable *ptable;
1484
1485 ptable = flash_get_ptable();
1486 if (ptable == NULL)
1487 {
1488 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1489 return;
1490 }
1491
1492 ptn = ptable_find(ptable, "devinfo");
1493 if (ptn == NULL)
1494 {
1495 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1496 return;
1497 }
1498
1499 memcpy(info, dev, sizeof(device_info));
1500
1501 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1502 {
1503 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1504 return;
1505 }
1506}
1507
1508void read_device_info_flash(device_info *dev)
1509{
1510 struct device_info *info = (void*) info_buf;
1511 struct ptentry *ptn;
1512 struct ptable *ptable;
1513
1514 ptable = flash_get_ptable();
1515 if (ptable == NULL)
1516 {
1517 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1518 return;
1519 }
1520
1521 ptn = ptable_find(ptable, "devinfo");
1522 if (ptn == NULL)
1523 {
1524 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1525 return;
1526 }
1527
1528 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1529 {
1530 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1531 return;
1532 }
1533
1534 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1535 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001536 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1537 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001538 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001539 write_device_info_flash(info);
1540 }
1541 memcpy(dev, info, sizeof(device_info));
1542}
1543
1544void write_device_info(device_info *dev)
1545{
1546 if(target_is_emmc_boot())
1547 {
1548 write_device_info_mmc(dev);
1549 }
1550 else
1551 {
1552 write_device_info_flash(dev);
1553 }
1554}
1555
1556void read_device_info(device_info *dev)
1557{
1558 if(target_is_emmc_boot())
1559 {
1560 read_device_info_mmc(dev);
1561 }
1562 else
1563 {
1564 read_device_info_flash(dev);
1565 }
1566}
1567
1568void reset_device_info()
1569{
1570 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001571 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001572 write_device_info(&device);
1573}
1574
1575void set_device_root()
1576{
1577 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001578 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001579 write_device_info(&device);
1580}
1581
Amol Jadicb524072012-08-09 16:40:18 -07001582#if DEVICE_TREE
1583int copy_dtb(uint8_t *boot_image_start)
1584{
1585 uint32 dt_image_offset = 0;
1586 uint32_t n;
1587 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001588 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001589 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001590
1591 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1592
Amol Jadicb524072012-08-09 16:40:18 -07001593 if(hdr->dt_size != 0) {
1594
1595 /* add kernel offset */
1596 dt_image_offset += page_size;
1597 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1598 dt_image_offset += n;
1599
1600 /* add ramdisk offset */
1601 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1602 dt_image_offset += n;
1603
1604 /* add second offset */
1605 if(hdr->second_size != 0) {
1606 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1607 dt_image_offset += n;
1608 }
1609
1610 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001611 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001612
Deepa Dinamani19648b42013-09-05 17:05:55 -07001613 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001614 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1615 return -1;
1616 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001617 /* Find index of device tree within device tree table */
1618 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001619 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1620 return -1;
1621 }
1622
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001623 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001624 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001625 {
1626 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1627 return -1;
1628 }
1629
Amol Jadicb524072012-08-09 16:40:18 -07001630 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001631 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001632 boot_image_start + dt_image_offset + dt_entry.offset,
1633 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001634 } else
1635 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001636
1637 /* Everything looks fine. Return success. */
1638 return 0;
1639}
1640#endif
1641
Brian Swetland9c4c0752009-01-25 16:23:50 -08001642void cmd_boot(const char *arg, void *data, unsigned sz)
1643{
1644 unsigned kernel_actual;
1645 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001646 uint32_t image_actual;
1647 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001648 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001649 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001650 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001651 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001652 int ret = 0;
1653 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001654
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001655#if VERIFIED_BOOT
1656 if(!device.is_unlocked)
1657 {
1658 fastboot_fail("unlock device to use this command");
1659 return;
1660 }
1661#endif
1662
Brian Swetland9c4c0752009-01-25 16:23:50 -08001663 if (sz < sizeof(hdr)) {
1664 fastboot_fail("invalid bootimage header");
1665 return;
1666 }
1667
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001668 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001669
1670 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001671 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001672
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001673 if(target_is_emmc_boot() && hdr->page_size) {
1674 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001675 page_mask = page_size - 1;
1676 }
1677
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001678 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1679 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001680#if DEVICE_TREE
1681 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1682#endif
1683
1684 image_actual = ADD_OF(page_size, kernel_actual);
1685 image_actual = ADD_OF(image_actual, ramdisk_actual);
1686 image_actual = ADD_OF(image_actual, dt_actual);
1687
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001688 if (target_use_signed_kernel() && (!device.is_unlocked))
1689 image_actual = ADD_OF(image_actual, sig_actual);
1690
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001691 /* sz should have atleast raw boot image */
1692 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001693 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001694 return;
1695 }
1696
1697 /* Verify the boot image
1698 * device & page_size are initialized in aboot_init
1699 */
1700 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001701 /* Pass size excluding signature size, otherwise we would try to
1702 * access signature beyond its length
1703 */
1704 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001705
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001706 /*
1707 * Update the kernel/ramdisk/tags address if the boot image header
1708 * has default values, these default values come from mkbootimg when
1709 * the boot image is flashed using fastboot flash:raw
1710 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001711 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1712 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001713
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001714 /* Get virtual addresses since the hdr saves physical addresses. */
1715 hdr->kernel_addr = VA(hdr->kernel_addr);
1716 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1717 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001718
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001719 /* Check if the addresses in the header are valid. */
1720 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1721 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1722 {
1723 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001724 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001725 }
1726
Amol Jadicb524072012-08-09 16:40:18 -07001727#if DEVICE_TREE
1728 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001729 ret = copy_dtb(data);
1730
1731 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001732#else
1733 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1734 {
1735 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001736 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001737 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001738#endif
1739
1740 /* Load ramdisk & kernel */
1741 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1742 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1743
1744#if DEVICE_TREE
1745 /*
1746 * If dtb is not found look for appended DTB in the kernel.
1747 * If appended dev tree is found, update the atags with
1748 * memory address to the DTB appended location on RAM.
1749 * Else update with the atags address in the kernel header
1750 */
1751 if (!dtb_copied) {
1752 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001753 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1754 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001755 if (!dtb) {
1756 fastboot_fail("dtb not found");
1757 return;
1758 }
Amol Jadicb524072012-08-09 16:40:18 -07001759 }
1760#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001761
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001762#ifndef DEVICE_TREE
1763 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1764 {
1765 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001766 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001767 }
1768#endif
1769
Brian Swetland9c4c0752009-01-25 16:23:50 -08001770 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001771 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001772
Dima Zavin77e41f32013-03-06 16:10:43 -08001773 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001774 (const char*) hdr->cmdline, board_machtype(),
1775 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001776}
1777
Dima Zavin214cc642009-01-26 11:16:21 -08001778void cmd_erase(const char *arg, void *data, unsigned sz)
1779{
1780 struct ptentry *ptn;
1781 struct ptable *ptable;
1782
1783 ptable = flash_get_ptable();
1784 if (ptable == NULL) {
1785 fastboot_fail("partition table doesn't exist");
1786 return;
1787 }
1788
1789 ptn = ptable_find(ptable, arg);
1790 if (ptn == NULL) {
1791 fastboot_fail("unknown partition name");
1792 return;
1793 }
1794
1795 if (flash_erase(ptn)) {
1796 fastboot_fail("failed to erase partition");
1797 return;
1798 }
1799 fastboot_okay("");
1800}
1801
Bikas Gurungd48bd242010-09-04 19:54:32 -07001802
1803void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1804{
Kun Liang2f1601a2013-08-12 16:29:54 +08001805 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001806 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001807 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001808 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001809 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001810
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001811#if VERIFIED_BOOT
1812 if(!strcmp(arg, KEYSTORE_PTN_NAME))
1813 {
1814 if(!device.is_unlocked)
1815 {
1816 fastboot_fail("unlock device to erase keystore");
1817 return;
1818 }
1819 }
1820#endif
1821
Kinson Chikf1a43512011-07-14 11:28:39 -07001822 index = partition_get_index(arg);
1823 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001824 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001825
Kinson Chikf1a43512011-07-14 11:28:39 -07001826 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001827 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001828 return;
1829 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001830
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001831 lun = partition_get_lun(index);
1832 mmc_set_lun(lun);
1833
Oliver Wangcee448d2013-10-22 18:40:13 +08001834#if MMC_SDHCI_SUPPORT
1835 if (mmc_erase_card(ptn, size)) {
1836 fastboot_fail("failed to erase partition\n");
1837 return;
1838 }
1839#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001840 size = partition_get_size(index);
1841 if (size > DEFAULT_ERASE_SIZE)
1842 size = DEFAULT_ERASE_SIZE;
1843
neetidb4b24d62012-01-20 12:13:09 -08001844 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001845 0 in first several blocks */
1846 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001847 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001848 return;
1849 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001850#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001851 fastboot_okay("");
1852}
1853
1854
Ajay Dudani5c761132011-04-07 20:19:04 -07001855void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001856{
1857 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001858 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001859 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001860 char *token = NULL;
1861 char *pname = NULL;
1862 uint8_t lun = 0;
1863 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001864
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001865 token = strtok(arg, ":");
1866 pname = token;
1867 token = strtok(NULL, ":");
1868 if(token)
1869 {
1870 lun = atoi(token);
1871 mmc_set_lun(lun);
1872 lun_set = true;
1873 }
1874
Mao Jinlong226f33a2014-07-04 17:24:10 +08001875 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07001876 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001877 if (!strcmp(pname, "partition"))
1878 {
1879 dprintf(INFO, "Attempt to write partition image.\n");
1880 if (write_partition(sz, (unsigned char *) data)) {
1881 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07001882 return;
1883 }
1884 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08001885 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001886 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001887#if VERIFIED_BOOT
1888 if(!strcmp(pname, KEYSTORE_PTN_NAME))
1889 {
1890 if(!device.is_unlocked)
1891 {
1892 fastboot_fail("unlock device to flash keystore");
1893 return;
1894 }
1895 if(!boot_verify_validate_keystore((unsigned char *)data))
1896 {
1897 fastboot_fail("image is not a keystore file");
1898 return;
1899 }
1900 }
1901#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08001902 index = partition_get_index(pname);
1903 ptn = partition_get_offset(index);
1904 if(ptn == 0) {
1905 fastboot_fail("partition table doesn't exist");
1906 return;
1907 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001908
Mao Jinlong226f33a2014-07-04 17:24:10 +08001909 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
1910 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1911 fastboot_fail("image is not a boot image");
1912 return;
1913 }
1914 }
1915
1916 if(!lun_set)
1917 {
1918 lun = partition_get_lun(index);
1919 mmc_set_lun(lun);
1920 }
1921
1922 size = partition_get_size(index);
1923 if (ROUND_TO_PAGE(sz,511) > size) {
1924 fastboot_fail("size too large");
1925 return;
1926 }
1927 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1928 fastboot_fail("flash write failure");
1929 return;
1930 }
Greg Grisco6e754772011-06-23 12:19:39 -07001931 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001932 }
1933 fastboot_okay("");
1934 return;
1935}
1936
Ajay Dudani5c761132011-04-07 20:19:04 -07001937void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1938{
1939 unsigned int chunk;
1940 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001941 uint32_t *fill_buf = NULL;
1942 uint32_t fill_val;
1943 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001944 sparse_header_t *sparse_header;
1945 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001946 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001947 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301948 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001949 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001950 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001951 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001952
Kinson Chikf1a43512011-07-14 11:28:39 -07001953 index = partition_get_index(arg);
1954 ptn = partition_get_offset(index);
1955 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001956 fastboot_fail("partition table doesn't exist");
1957 return;
1958 }
1959
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301960 size = partition_get_size(index);
1961 if (ROUND_TO_PAGE(sz,511) > size) {
1962 fastboot_fail("size too large");
1963 return;
1964 }
1965
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001966 lun = partition_get_lun(index);
1967 mmc_set_lun(lun);
1968
Ajay Dudani5c761132011-04-07 20:19:04 -07001969 /* Read and skip over sparse image header */
1970 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001971 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1972 fastboot_fail("size too large");
1973 return;
1974 }
1975
Ajay Dudani5c761132011-04-07 20:19:04 -07001976 data += sparse_header->file_hdr_sz;
1977 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1978 {
1979 /* Skip the remaining bytes in a header that is longer than
1980 * we expected.
1981 */
1982 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1983 }
1984
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001985 dprintf (SPEW, "=== Sparse Image Header ===\n");
1986 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1987 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1988 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1989 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1990 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1991 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1992 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1993 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001994
1995 /* Start processing chunks */
1996 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1997 {
1998 /* Read and skip over chunk header */
1999 chunk_header = (chunk_header_t *) data;
2000 data += sizeof(chunk_header_t);
2001
2002 dprintf (SPEW, "=== Chunk Header ===\n");
2003 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2004 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2005 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2006
2007 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2008 {
2009 /* Skip the remaining bytes in a header that is longer than
2010 * we expected.
2011 */
2012 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2013 }
2014
2015 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
2016 switch (chunk_header->chunk_type)
2017 {
2018 case CHUNK_TYPE_RAW:
2019 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2020 chunk_data_sz))
2021 {
2022 fastboot_fail("Bogus chunk size for chunk type Raw");
2023 return;
2024 }
2025
Ajay Dudaniab18f022011-05-12 14:39:22 -07002026 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2027 chunk_data_sz,
2028 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002029 {
2030 fastboot_fail("flash write failure");
2031 return;
2032 }
2033 total_blocks += chunk_header->chunk_sz;
2034 data += chunk_data_sz;
2035 break;
2036
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002037 case CHUNK_TYPE_FILL:
2038 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2039 sizeof(uint32_t)))
2040 {
2041 fastboot_fail("Bogus chunk size for chunk type FILL");
2042 return;
2043 }
2044
2045 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2046 if (!fill_buf)
2047 {
2048 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2049 return;
2050 }
2051
2052 fill_val = *(uint32_t *)data;
2053 data = (char *) data + sizeof(uint32_t);
2054 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2055
2056 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2057 {
2058 fill_buf[i] = fill_val;
2059 }
2060
2061 for (i = 0; i < chunk_blk_cnt; i++)
2062 {
2063 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2064 sparse_header->blk_sz,
2065 fill_buf))
2066 {
2067 fastboot_fail("flash write failure");
2068 free(fill_buf);
2069 return;
2070 }
2071
2072 total_blocks++;
2073 }
2074
2075 free(fill_buf);
2076 break;
2077
Ajay Dudani5c761132011-04-07 20:19:04 -07002078 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002079 total_blocks += chunk_header->chunk_sz;
2080 break;
2081
Ajay Dudani5c761132011-04-07 20:19:04 -07002082 case CHUNK_TYPE_CRC:
2083 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2084 {
2085 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2086 return;
2087 }
2088 total_blocks += chunk_header->chunk_sz;
2089 data += chunk_data_sz;
2090 break;
2091
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002092 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002093 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002094 fastboot_fail("Unknown chunk type");
2095 return;
2096 }
2097 }
2098
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002099 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2100 total_blocks, sparse_header->total_blks);
2101
2102 if(total_blocks != sparse_header->total_blks)
2103 {
2104 fastboot_fail("sparse image write failure");
2105 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002106
2107 fastboot_okay("");
2108 return;
2109}
2110
2111void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2112{
2113 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002114 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2115 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002116
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002117#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002118 int ret=0;
2119 uint32 major_version=0;
2120 uint32 minor_version=0;
2121
2122 ret = scm_svc_version(&major_version,&minor_version);
2123 if(!ret)
2124 {
2125 if(major_version >= 2)
2126 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002127 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002128 {
2129 ret = encrypt_scm((uint32 **) &data, &sz);
2130 if (ret != 0) {
2131 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2132 return;
2133 }
2134
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002135 /* Protect only for SSD */
2136 if (!strcmp(arg, "ssd")) {
2137 ret = scm_protect_keystore((uint32 *) data, sz);
2138 if (ret != 0) {
2139 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2140 return;
2141 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002142 }
2143 }
2144 else
2145 {
2146 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2147 if(ret != 0)
2148 {
2149 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2150 return;
2151 }
2152 }
2153 }
2154 else
2155 {
2156 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2157 magic_number[1] == DECRYPT_MAGIC_1)
2158 {
2159 ret = decrypt_scm((uint32 **) &data, &sz);
2160 if (ret != 0) {
2161 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2162 return;
2163 }
2164 }
2165 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2166 magic_number[1] == ENCRYPT_MAGIC_1)
2167 {
2168 ret = encrypt_scm((uint32 **) &data, &sz);
2169 if (ret != 0) {
2170 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2171 return;
2172 }
2173 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002174 }
2175 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002176 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002177 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002178 dprintf(CRITICAL,"INVALID SVC Version\n");
2179 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002180 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002181#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002182
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002183#if VERIFIED_BOOT
2184 if(!device.is_unlocked && !device.is_verified)
2185 {
2186 fastboot_fail("device is locked. Cannot flash images");
2187 return;
2188 }
2189 if(!device.is_unlocked && device.is_verified)
2190 {
2191 if(!boot_verify_flash_allowed(arg))
2192 {
2193 fastboot_fail("cannot flash this partition in verified state");
2194 return;
2195 }
2196 }
2197#endif
2198
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002199 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002200 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2201 cmd_flash_mmc_img(arg, data, sz);
2202 else
2203 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002204 return;
2205}
2206
Dima Zavin214cc642009-01-26 11:16:21 -08002207void cmd_flash(const char *arg, void *data, unsigned sz)
2208{
2209 struct ptentry *ptn;
2210 struct ptable *ptable;
2211 unsigned extra = 0;
2212
2213 ptable = flash_get_ptable();
2214 if (ptable == NULL) {
2215 fastboot_fail("partition table doesn't exist");
2216 return;
2217 }
2218
2219 ptn = ptable_find(ptable, arg);
2220 if (ptn == NULL) {
2221 fastboot_fail("unknown partition name");
2222 return;
2223 }
2224
2225 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2226 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2227 fastboot_fail("image is not a boot image");
2228 return;
2229 }
2230 }
2231
Amol Jadi5c61a952012-05-04 17:05:35 -07002232 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002233 || !strcmp(ptn->name, "userdata")
2234 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002235 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002236 || !strcmp(ptn->name, "modem"))
2237 {
2238 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002239 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002240 else
2241 extra = 0;
2242 }
2243 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002244 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002245
2246 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2247 if (flash_write(ptn, extra, data, sz)) {
2248 fastboot_fail("flash write failure");
2249 return;
2250 }
2251 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2252 fastboot_okay("");
2253}
2254
2255void cmd_continue(const char *arg, void *data, unsigned sz)
2256{
2257 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002258 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002259
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002260 if (target_is_emmc_boot())
2261 {
2262 boot_linux_from_mmc();
2263 }
2264 else
2265 {
2266 boot_linux_from_flash();
2267 }
Dima Zavin214cc642009-01-26 11:16:21 -08002268}
2269
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002270void cmd_reboot(const char *arg, void *data, unsigned sz)
2271{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002272 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002273 fastboot_okay("");
2274 reboot_device(0);
2275}
2276
2277void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2278{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002279 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002280 fastboot_okay("");
2281 reboot_device(FASTBOOT_MODE);
2282}
2283
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002284void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2285{
2286 dprintf(INFO, "Enabling charger screen check\n");
2287 device.charger_screen_enabled = 1;
2288 write_device_info(&device);
2289 fastboot_okay("");
2290}
2291
2292void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2293{
2294 dprintf(INFO, "Disabling charger screen check\n");
2295 device.charger_screen_enabled = 0;
2296 write_device_info(&device);
2297 fastboot_okay("");
2298}
2299
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302300void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2301{
2302 dprintf(INFO, "Selecting display panel %s\n", arg);
2303 if (arg)
2304 strlcpy(device.display_panel, arg,
2305 sizeof(device.display_panel));
2306 write_device_info(&device);
2307 fastboot_okay("");
2308}
2309
Shashank Mittal162244e2011-08-08 19:01:25 -07002310void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2311{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002312 /* TODO: Wipe user data */
2313 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002314 {
2315 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002316 device.is_verified = 0;
2317 write_device_info(&device);
2318 }
2319 fastboot_okay("");
2320}
2321
2322void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2323{
2324 /* TODO: Wipe user data */
2325 if(device.is_unlocked || device.is_verified)
2326 {
2327 device.is_unlocked = 0;
2328 device.is_verified = 0;
2329 write_device_info(&device);
2330 }
2331 fastboot_okay("");
2332}
2333
2334void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2335{
2336 /* TODO: Wipe user data */
2337 if(device.is_unlocked || !device.is_verified)
2338 {
2339 device.is_unlocked = 0;
2340 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002341 write_device_info(&device);
2342 }
2343 fastboot_okay("");
2344}
2345
Shashank Mittala0032282011-08-26 14:50:11 -07002346void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2347{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302348 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002349 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002350 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002351 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2352 fastboot_info(response);
2353 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002354 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302355 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2356 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002357 fastboot_okay("");
2358}
2359
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002360void cmd_preflash(const char *arg, void *data, unsigned sz)
2361{
2362 fastboot_okay("");
2363}
2364
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302365static struct fbimage logo_header = {0};
2366struct fbimage* splash_screen_flash();
2367
2368int splash_screen_check_header(struct fbimage *logo)
2369{
2370 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2371 return -1;
2372 if (logo->header.width == 0 || logo->header.height == 0)
2373 return -1;
2374 return 0;
2375}
2376
2377struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002378{
2379 struct ptentry *ptn;
2380 struct ptable *ptable;
2381 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302382 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002383
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302384
2385 ptable = flash_get_ptable();
2386 if (ptable == NULL) {
2387 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2388 return NULL;
2389 }
2390 ptn = ptable_find(ptable, "splash");
2391 if (ptn == NULL) {
2392 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2393 return NULL;
2394 }
2395
2396 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2397 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2398 return NULL;
2399 }
2400
2401 if (splash_screen_check_header(logo)) {
2402 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2403 return NULL;
2404 }
2405
2406 fb_display = fbcon_display();
2407 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302408 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2409 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back to default logo\n");
2410 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002411 }
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302412 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302413 if (flash_read(ptn + sizeof(logo->header), 0,
2414 base,
2415 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2416 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302417 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302418 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002419 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302420 logo->image = base;
2421 }
2422
2423 return logo;
2424}
2425
2426struct fbimage* splash_screen_mmc()
2427{
2428 int index = INVALID_PTN;
2429 unsigned long long ptn = 0;
2430 struct fbcon_config *fb_display = NULL;
2431 struct fbimage *logo = &logo_header;
2432
2433 index = partition_get_index("splash");
2434 if (index == 0) {
2435 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2436 return NULL;
2437 }
2438
2439 ptn = partition_get_offset(index);
2440 if (ptn == 0) {
2441 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2442 return NULL;
2443 }
2444
2445 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2446 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2447 return NULL;
2448 }
2449
2450 if (splash_screen_check_header(logo)) {
2451 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2452 return NULL;
2453 }
2454
2455 fb_display = fbcon_display();
2456 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302457 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2458 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
2459 return NULL;
2460 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302461 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302462 if (mmc_read(ptn + sizeof(logo->header),
2463 base,
2464 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2465 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302466 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302467 return NULL;
2468 }
2469
2470 logo->image = base;
2471 }
2472
2473 return logo;
2474}
2475
2476
2477struct fbimage* fetch_image_from_partition()
2478{
2479 if (target_is_emmc_boot()) {
2480 return splash_screen_mmc();
2481 } else {
2482 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002483 }
2484}
2485
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002486/* Get the size from partiton name */
2487static void get_partition_size(const char *arg, char *response)
2488{
2489 uint64_t ptn = 0;
2490 uint64_t size;
2491 int index = INVALID_PTN;
2492
2493 index = partition_get_index(arg);
2494
2495 if (index == INVALID_PTN)
2496 {
2497 dprintf(CRITICAL, "Invalid partition index\n");
2498 return;
2499 }
2500
2501 ptn = partition_get_offset(index);
2502
2503 if(!ptn)
2504 {
2505 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2506 return;
2507 }
2508
2509 size = partition_get_size(index);
2510
2511 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2512 return;
2513}
2514
2515/*
2516 * Publish the partition type & size info
2517 * fastboot getvar will publish the required information.
2518 * fastboot getvar partition_size:<partition_name>: partition size in hex
2519 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2520 */
2521static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2522{
2523 uint8_t i;
2524
2525 for (i = 0; i < num_parts; i++) {
2526 get_partition_size(info[i].part_name, info[i].size_response);
2527
2528 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2529 {
2530 dprintf(CRITICAL, "partition size name truncated\n");
2531 return;
2532 }
2533 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2534 {
2535 dprintf(CRITICAL, "partition type name truncated\n");
2536 return;
2537 }
2538
2539 /* publish partition size & type info */
2540 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2541 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2542 }
2543}
2544
Amol Jadi5edf3552013-07-23 14:15:34 -07002545/* register commands and variables for fastboot */
2546void aboot_fastboot_register_commands(void)
2547{
2548 if (target_is_emmc_boot())
2549 {
2550 fastboot_register("flash:", cmd_flash_mmc);
2551 fastboot_register("erase:", cmd_erase_mmc);
2552 }
2553 else
2554 {
2555 fastboot_register("flash:", cmd_flash);
2556 fastboot_register("erase:", cmd_erase);
2557 }
2558
2559 fastboot_register("boot", cmd_boot);
2560 fastboot_register("continue", cmd_continue);
2561 fastboot_register("reboot", cmd_reboot);
2562 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2563 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002564 fastboot_register("oem lock", cmd_oem_lock);
2565 fastboot_register("oem verified", cmd_oem_verified);
Amol Jadi5edf3552013-07-23 14:15:34 -07002566 fastboot_register("oem device-info", cmd_oem_devinfo);
2567 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002568 fastboot_register("oem enable-charger-screen",
2569 cmd_oem_enable_charger_screen);
2570 fastboot_register("oem disable-charger-screen",
2571 cmd_oem_disable_charger_screen);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302572 fastboot_register("oem select-display-panel",
2573 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002574 /* publish variables and their values */
2575 fastboot_publish("product", TARGET(BOARD));
2576 fastboot_publish("kernel", "lk");
2577 fastboot_publish("serialno", sn_buf);
2578
2579 /*
2580 * partition info is supported only for emmc partitions
2581 * Calling this for NAND prints some error messages which
2582 * is harmless but misleading. Avoid calling this for NAND
2583 * devices.
2584 */
2585 if (target_is_emmc_boot())
2586 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2587
2588 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002589 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2590 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002591 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002592 /* Is the charger screen check enabled */
2593 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2594 device.charger_screen_enabled);
2595 fastboot_publish("charger-screen-enabled",
2596 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302597 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2598 device.display_panel);
2599 fastboot_publish("display-panel",
2600 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002601}
2602
Brian Swetland9c4c0752009-01-25 16:23:50 -08002603void aboot_init(const struct app_descriptor *app)
2604{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002605 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002606 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002607
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002608 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002609 if (target_is_emmc_boot())
2610 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002611 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002612 page_mask = page_size - 1;
2613 }
2614 else
2615 {
2616 page_size = flash_page_size();
2617 page_mask = page_size - 1;
2618 }
2619
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002620 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2621
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002622 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002623
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002624 /* Display splash screen if enabled */
2625#if DISPLAY_SPLASH_SCREEN
2626 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302627 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002628 dprintf(SPEW, "Display Init: Done\n");
2629#endif
2630
2631
Greg Griscod6250552011-06-29 14:40:23 -07002632 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002633 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002634
Dhaval Patel223ec952013-07-18 14:49:44 -07002635 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2636
Matthew Qindefd5562014-07-11 18:02:40 +08002637 /*
2638 * Check power off reason if user force reset,
2639 * if yes phone will do normal boot.
2640 */
2641 if (is_user_force_reset())
2642 goto normal_boot;
2643
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002644 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002645 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002646 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002647 dprintf(ALWAYS,"dload mode key sequence detected\n");
2648 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002649 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002650 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002651 }
2652 else
2653 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302654 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002655 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002656 }
2657 boot_into_fastboot = true;
2658 }
2659 if (!boot_into_fastboot)
2660 {
2661 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2662 boot_into_recovery = 1;
2663 if (!boot_into_recovery &&
2664 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002665 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002666 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002667 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002668 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002669 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002670 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002671
Ajay Dudani77421292010-10-27 19:34:06 -07002672 reboot_mode = check_reboot_mode();
2673 if (reboot_mode == RECOVERY_MODE) {
2674 boot_into_recovery = 1;
2675 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002676 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002677 }
2678
Matthew Qindefd5562014-07-11 18:02:40 +08002679normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03002680 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002681 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002682 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002683 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002684 if(emmc_recovery_init())
2685 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2686 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002687 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002688 if((device.is_unlocked) || (device.is_tampered))
2689 {
2690 #ifdef TZ_TAMPER_FUSE
2691 set_tamper_fuse_cmd();
2692 #endif
2693 #if USE_PCOM_SECBOOT
2694 set_tamper_flag(device.is_tampered);
2695 #endif
2696 }
Shashank Mittala0032282011-08-26 14:50:11 -07002697 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002698 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002699 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002700 else
2701 {
2702 recovery_init();
2703 #if USE_PCOM_SECBOOT
2704 if((device.is_unlocked) || (device.is_tampered))
2705 set_tamper_flag(device.is_tampered);
2706 #endif
2707 boot_linux_from_flash();
2708 }
2709 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2710 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002711 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002712
Amol Jadi5edf3552013-07-23 14:15:34 -07002713 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002714
Amol Jadi5edf3552013-07-23 14:15:34 -07002715 /* register aboot specific fastboot commands */
2716 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002717
Amol Jadi5edf3552013-07-23 14:15:34 -07002718 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002719 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002720
2721 /* initialize and start fastboot */
2722 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002723}
2724
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002725uint32_t get_page_size()
2726{
2727 return page_size;
2728}
2729
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002730/*
2731 * Calculated and save hash (SHA256) for non-signed boot image.
2732 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002733 * @param image_addr - Boot image address
2734 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002735 *
2736 * @return int - 0 on success, negative value on failure.
2737 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002738static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002739{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002740 unsigned int digest[8];
2741#if IMAGE_VERIF_ALGO_SHA1
2742 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
2743#else
2744 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
2745#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002746
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002747 target_crypto_init_params();
2748 hash_find(image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002749
2750 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002751 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002752
2753 return 0;
2754}
2755
Brian Swetland9c4c0752009-01-25 16:23:50 -08002756APP_START(aboot)
2757 .init = aboot_init,
2758APP_END