blob: 55b11813a7089dcf6fd17af5242b8dbbec88fb8e [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
lijuang9a7d3b92015-11-30 14:41:24 +08005 * Copyright (c) 2009-2016, 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>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053061#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080062#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080063#if USE_RPMB_FOR_DEVINFO
64#include <rpmb.h>
65#endif
Dima Zavin214cc642009-01-26 11:16:21 -080066
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070067#if ENABLE_WBC
68#include <pm_app_smbchg.h>
69#endif
70
Neeti Desai17379b82012-06-04 18:42:53 -070071#if DEVICE_TREE
72#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070074#endif
75
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053076#if WDOG_SUPPORT
77#include <wdog.h>
78#endif
79
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070080#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070081#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080082#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080083#include "bootimg.h"
84#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070085#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080086#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070087#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070088#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070089#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020091#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070092#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080093#include <menu_keys_detect.h>
94#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070095#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070096
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070097extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070099extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700100extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530101 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700102
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700103void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104void write_device_info_mmc(device_info *dev);
105void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700106static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700107static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700108
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700109/* fastboot command function pointer */
110typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
111
112struct fastboot_cmd_desc {
113 char * name;
114 fastboot_cmd_fn cb;
115};
116
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700117#define EXPAND(NAME) #NAME
118#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700119
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700120#define DISPLAY_PANEL_HDMI "hdmi"
121
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800122#ifdef MEMBASE
123#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
124#else
David Ng183a7422009-12-07 14:55:21 -0800125#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800126#endif
127
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700128#ifndef MEMSIZE
129#define MEMSIZE 1024*1024
130#endif
131
132#define MAX_TAGS_SIZE 1024
133
Kun Liang2f1601a2013-08-12 16:29:54 +0800134/* make 4096 as default size to ensure EFS,EXT4's erasing */
135#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700136#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530137#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800138
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700139#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700140#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800141
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800142#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
143
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700144#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
145
Ameya Thakur10a33452016-06-13 14:24:26 -0700146//Size of the header that is used in case the boot image has
147//a uncompressed kernel + appended dtb
148#define PATCHED_KERNEL_HEADER_SIZE 20
149
150//String used to determine if the boot image has
151//a uncompressed kernel + appended dtb
152#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
153
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800154#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700155static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700156#else
David Ng183a7422009-12-07 14:55:21 -0800157static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700158#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800159static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300160static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800161static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800162static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800163static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700164static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300165static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200166static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800167
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800168static const char *baseband_apq = " androidboot.baseband=apq";
169static const char *baseband_msm = " androidboot.baseband=msm";
170static const char *baseband_csfb = " androidboot.baseband=csfb";
171static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700172static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800173static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700174static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800175static const char *baseband_dsda = " androidboot.baseband=dsda";
176static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800177static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800178static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530179static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800180
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700181#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530182#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700183static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700184static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530185static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700186//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700187
188struct verified_boot_verity_mode vbvm[] =
189{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700190#if ENABLE_VB_ATTEST
191 {false, "eio"},
192#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700193 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700194#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700195 {true, "enforcing"},
196};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700197struct verified_boot_state_name vbsn[] =
198{
199 {GREEN, "green"},
200 {ORANGE, "orange"},
201 {YELLOW,"yellow"},
202 {RED,"red" },
203};
204#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530205#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700206/*As per spec delay wait time before shutdown in Red state*/
207#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700208static unsigned page_size = 0;
209static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530210static unsigned mmc_blocksize = 0;
211static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700212static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
213static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530214static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800215static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700216static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700217bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530218static uint32_t dt_size = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700219
Shashank Mittalcd98d472011-08-02 14:29:24 -0700220/* Assuming unauthorized kernel image by default */
221static int auth_kernel_img = 0;
Parth Dixit6f5822f2015-10-18 01:20:53 +0530222#if VBOOT_MOTA
223static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
224#else
lijuang511a2b52015-08-14 20:50:51 +0800225static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
Parth Dixit6f5822f2015-10-18 01:20:53 +0530226#endif
vijay kumarc65876c2015-04-24 13:29:16 +0530227static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700228
vijay kumarca2e6812015-07-08 20:28:25 +0530229static char frp_ptns[2][8] = {"config","frp"};
230
lijuang511a2b52015-08-14 20:50:51 +0800231static const char *critical_flash_allowed_ptn[] = {
232 "aboot",
233 "rpm",
234 "tz",
235 "sbl",
236 "sdi",
237 "sbl1",
238 "xbl",
239 "hyp",
240 "pmic",
241 "bootloader",
242 "devinfo",
243 "partition"};
244
Dima Zavin42168f22009-01-30 11:52:22 -0800245struct atag_ptbl_entry
246{
247 char name[16];
248 unsigned offset;
249 unsigned size;
250 unsigned flags;
251};
252
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700253/*
254 * Partition info, required to be published
255 * for fastboot
256 */
257struct getvar_partition_info {
258 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
259 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
260 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
261 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
262 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
263};
264
265/*
266 * Right now, we are publishing the info for only
267 * three partitions
268 */
269struct getvar_partition_info part_info[] =
270{
271 { "system" , "partition-size:", "partition-type:", "", "ext4" },
272 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
273 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
274};
275
276char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700277char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800278char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700279char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530280char panel_display_mode[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800281
282#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800283char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800284char battery_soc_ok [MAX_RSP_SIZE];
285#endif
286
lijuangf16461c2015-08-03 17:09:34 +0800287char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700288
Greg Griscod2471ef2011-07-14 13:00:42 -0700289extern int emmc_recovery_init(void);
290
Kinson Chik0b1c8162011-08-31 16:31:57 -0700291#if NO_KEYPAD_DRIVER
292extern int fastboot_trigger(void);
293#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700294
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800295static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700296{
297 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700298#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800299 if (is_arm64)
300 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
301 else
302 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700303 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
304 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700305#endif
306}
307
Dima Zavin42168f22009-01-30 11:52:22 -0800308static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
309{
310 struct atag_ptbl_entry atag_ptn;
311
312 memcpy(atag_ptn.name, ptn->name, 16);
313 atag_ptn.name[15] = '\0';
314 atag_ptn.offset = ptn->start;
315 atag_ptn.size = ptn->length;
316 atag_ptn.flags = ptn->flags;
317 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
318 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
319}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800320
lijuang102dfa92015-10-09 18:31:03 +0800321#if CHECK_BAT_VOLTAGE
322void update_battery_status(void)
323{
324 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
325 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
326}
327#endif
328
Neeti Desaie245d492012-06-01 12:52:13 -0700329unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800330{
David Ng183a7422009-12-07 14:55:21 -0800331 int cmdline_len = 0;
332 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800333 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700334 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800335 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300336 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700337 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700338 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200339 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700340#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530341#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700342 uint32_t boot_state = boot_verify_get_state();
343#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530344#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700345
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200346#ifdef MDTP_SUPPORT
347 mdtp_activated(&is_mdtp_activated);
348#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800349
Brian Swetland9c4c0752009-01-25 16:23:50 -0800350 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800351 cmdline_len = strlen(cmdline);
352 have_cmdline = 1;
353 }
354 if (target_is_emmc_boot()) {
355 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800356#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700357 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
358 ASSERT(boot_dev_buf);
359 platform_boot_dev_cmdline(boot_dev_buf);
360 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700361#endif
David Ng183a7422009-12-07 14:55:21 -0800362 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800363
364 cmdline_len += strlen(usb_sn_cmdline);
365 cmdline_len += strlen(sn_buf);
366
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700367#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530368#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700369 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700370 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
371 {
372 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
373 ASSERT(0);
374 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700375 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Parth Dixita5715a02015-10-29 12:25:10 +0530376 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700377#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530378#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700379
Pavel Nedev5614d222013-06-17 18:01:02 +0300380 if (boot_into_recovery && gpt_exists)
381 cmdline_len += strlen(secondary_gpt_enable);
382
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200383 if(is_mdtp_activated)
384 cmdline_len += strlen(mdtp_activated_flag);
385
Pavel Nedev328ac822013-04-05 15:25:11 +0300386 if (boot_into_ffbm) {
387 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700388 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800389 /* reduce kernel console messages to speed-up boot */
390 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800391 } else if (boot_reason_alarm) {
392 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800393 } else if ((target_build_variant_user() || device.charger_screen_enabled)
394 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700395 pause_at_bootup = 1;
396 cmdline_len += strlen(battchg_pause);
397 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800398
Shashank Mittalcd98d472011-08-02 14:29:24 -0700399 if(target_use_signed_kernel() && auth_kernel_img) {
400 cmdline_len += strlen(auth_kernel);
401 }
402
Joonwoo Park61112782013-10-02 19:50:39 -0700403 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
404 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530405 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700406 have_target_boot_params = 1;
407 cmdline_len += strlen(target_boot_params);
408 }
409
Ajay Dudanid04110c2011-01-17 23:55:07 -0800410 /* Determine correct androidboot.baseband to use */
411 switch(target_baseband())
412 {
413 case BASEBAND_APQ:
414 cmdline_len += strlen(baseband_apq);
415 break;
416
417 case BASEBAND_MSM:
418 cmdline_len += strlen(baseband_msm);
419 break;
420
421 case BASEBAND_CSFB:
422 cmdline_len += strlen(baseband_csfb);
423 break;
424
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800425 case BASEBAND_SVLTE2A:
426 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800427 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700428
429 case BASEBAND_MDM:
430 cmdline_len += strlen(baseband_mdm);
431 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700432
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800433 case BASEBAND_MDM2:
434 cmdline_len += strlen(baseband_mdm2);
435 break;
436
Amol Jadi5c61a952012-05-04 17:05:35 -0700437 case BASEBAND_SGLTE:
438 cmdline_len += strlen(baseband_sglte);
439 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530440
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800441 case BASEBAND_SGLTE2:
442 cmdline_len += strlen(baseband_sglte2);
443 break;
444
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530445 case BASEBAND_DSDA:
446 cmdline_len += strlen(baseband_dsda);
447 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800448
449 case BASEBAND_DSDA2:
450 cmdline_len += strlen(baseband_dsda2);
451 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530452 case BASEBAND_APQ_NOWGR:
453 cmdline_len += strlen(baseband_apq_nowgr);
454 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800455 }
456
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800457 if (cmdline) {
458 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530459 target_display_panel_node(display_panel_buf,
460 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800461 strlen(display_panel_buf)) {
462 cmdline_len += strlen(display_panel_buf);
463 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700464 }
465
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800466 if (target_warm_boot()) {
467 warm_boot = true;
468 cmdline_len += strlen(warmboot_cmdline);
469 }
470
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800471#if TARGET_CMDLINE_SUPPORT
472 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
473 int target_cmd_line_len;
474 ASSERT(target_cmdline_buf);
475 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
476 cmdline_len += target_cmd_line_len;
477#endif
478
David Ng183a7422009-12-07 14:55:21 -0800479 if (cmdline_len > 0) {
480 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800481 unsigned char *dst;
482
483 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
484 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530485 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800486 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700487
Amol Jadi168b7712012-03-06 16:15:00 -0800488 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800489 if (have_cmdline) {
490 src = cmdline;
491 while ((*dst++ = *src++));
492 }
493 if (target_is_emmc_boot()) {
494 src = emmc_cmdline;
495 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700496 have_cmdline = 1;
497 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800498#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700499 src = boot_dev_buf;
500 if (have_cmdline) --dst;
501 while ((*dst++ = *src++));
502#endif
David Ngf773dde2010-07-26 19:55:08 -0700503 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800504
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700505#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530506#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700507 src = verified_state;
508 if(have_cmdline) --dst;
509 have_cmdline = 1;
510 while ((*dst++ = *src++));
511 src = vbsn[boot_state].name;
512 if(have_cmdline) --dst;
513 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700514
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700515 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
516 {
517 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
518 ASSERT(0);
519 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700520 src = verity_mode;
521 if(have_cmdline) --dst;
522 while ((*dst++ = *src++));
523 src = vbvm[device.verity_mode].name;
524 if(have_cmdline) -- dst;
525 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530526 src = keymaster_v1;
527 if(have_cmdline) --dst;
528 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700529#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530530#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800531 src = usb_sn_cmdline;
532 if (have_cmdline) --dst;
533 have_cmdline = 1;
534 while ((*dst++ = *src++));
535 src = sn_buf;
536 if (have_cmdline) --dst;
537 have_cmdline = 1;
538 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800539 if (warm_boot) {
540 if (have_cmdline) --dst;
541 src = warmboot_cmdline;
542 while ((*dst++ = *src++));
543 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800544
Pavel Nedev5614d222013-06-17 18:01:02 +0300545 if (boot_into_recovery && gpt_exists) {
546 src = secondary_gpt_enable;
547 if (have_cmdline) --dst;
548 while ((*dst++ = *src++));
549 }
550
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200551 if (is_mdtp_activated) {
552 src = mdtp_activated_flag;
553 if (have_cmdline) --dst;
554 while ((*dst++ = *src++));
555 }
556
Pavel Nedev328ac822013-04-05 15:25:11 +0300557 if (boot_into_ffbm) {
558 src = androidboot_mode;
559 if (have_cmdline) --dst;
560 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700561 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300562 if (have_cmdline) --dst;
563 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800564 src = loglevel;
565 if (have_cmdline) --dst;
566 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800567 } else if (boot_reason_alarm) {
568 src = alarmboot_cmdline;
569 if (have_cmdline) --dst;
570 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300571 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700572 src = battchg_pause;
573 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800574 while ((*dst++ = *src++));
575 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800576
Shashank Mittalcd98d472011-08-02 14:29:24 -0700577 if(target_use_signed_kernel() && auth_kernel_img) {
578 src = auth_kernel;
579 if (have_cmdline) --dst;
580 while ((*dst++ = *src++));
581 }
582
Ajay Dudanid04110c2011-01-17 23:55:07 -0800583 switch(target_baseband())
584 {
585 case BASEBAND_APQ:
586 src = baseband_apq;
587 if (have_cmdline) --dst;
588 while ((*dst++ = *src++));
589 break;
590
591 case BASEBAND_MSM:
592 src = baseband_msm;
593 if (have_cmdline) --dst;
594 while ((*dst++ = *src++));
595 break;
596
597 case BASEBAND_CSFB:
598 src = baseband_csfb;
599 if (have_cmdline) --dst;
600 while ((*dst++ = *src++));
601 break;
602
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800603 case BASEBAND_SVLTE2A:
604 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800605 if (have_cmdline) --dst;
606 while ((*dst++ = *src++));
607 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700608
609 case BASEBAND_MDM:
610 src = baseband_mdm;
611 if (have_cmdline) --dst;
612 while ((*dst++ = *src++));
613 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700614
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800615 case BASEBAND_MDM2:
616 src = baseband_mdm2;
617 if (have_cmdline) --dst;
618 while ((*dst++ = *src++));
619 break;
620
Amol Jadi5c61a952012-05-04 17:05:35 -0700621 case BASEBAND_SGLTE:
622 src = baseband_sglte;
623 if (have_cmdline) --dst;
624 while ((*dst++ = *src++));
625 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530626
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800627 case BASEBAND_SGLTE2:
628 src = baseband_sglte2;
629 if (have_cmdline) --dst;
630 while ((*dst++ = *src++));
631 break;
632
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530633 case BASEBAND_DSDA:
634 src = baseband_dsda;
635 if (have_cmdline) --dst;
636 while ((*dst++ = *src++));
637 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800638
639 case BASEBAND_DSDA2:
640 src = baseband_dsda2;
641 if (have_cmdline) --dst;
642 while ((*dst++ = *src++));
643 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530644 case BASEBAND_APQ_NOWGR:
645 src = baseband_apq_nowgr;
646 if (have_cmdline) --dst;
647 while ((*dst++ = *src++));
648 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800649 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700650
651 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700652 src = display_panel_buf;
653 if (have_cmdline) --dst;
654 while ((*dst++ = *src++));
655 }
Joonwoo Park61112782013-10-02 19:50:39 -0700656
657 if (have_target_boot_params) {
658 if (have_cmdline) --dst;
659 src = target_boot_params;
660 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530661 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700662 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800663
664#if TARGET_CMDLINE_SUPPORT
665 if (target_cmdline_buf && target_cmd_line_len)
666 {
667 if (have_cmdline) --dst;
668 src = target_cmdline_buf;
669 while((*dst++ = *src++));
670 free(target_cmdline_buf);
671 }
672#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700673 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700674
675
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700676 if (boot_dev_buf)
677 free(boot_dev_buf);
678
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800679 if (cmdline_final)
680 dprintf(INFO, "cmdline: %s\n", cmdline_final);
681 else
682 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700683 return cmdline_final;
684}
685
686unsigned *atag_core(unsigned *ptr)
687{
688 /* CORE */
689 *ptr++ = 2;
690 *ptr++ = 0x54410001;
691
692 return ptr;
693
694}
695
696unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
697 unsigned ramdisk_size)
698{
699 if (ramdisk_size) {
700 *ptr++ = 4;
701 *ptr++ = 0x54420005;
702 *ptr++ = (unsigned)ramdisk;
703 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800704 }
705
Neeti Desaie245d492012-06-01 12:52:13 -0700706 return ptr;
707}
708
709unsigned *atag_ptable(unsigned **ptr_addr)
710{
711 int i;
712 struct ptable *ptable;
713
714 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700715 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
716 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700717 *(*ptr_addr)++ = 0x4d534d70;
718 for (i = 0; i < ptable->count; ++i)
719 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
720 }
721
722 return (*ptr_addr);
723}
724
725unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
726{
727 int cmdline_length = 0;
728 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700729 char *dest;
730
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800731 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700732 n = (cmdline_length + 4) & (~3);
733
734 *ptr++ = (n / 4) + 2;
735 *ptr++ = 0x54410009;
736 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800737 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700738 ptr += (n / 4);
739
740 return ptr;
741}
742
743unsigned *atag_end(unsigned *ptr)
744{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800745 /* END */
746 *ptr++ = 0;
747 *ptr++ = 0;
748
Neeti Desaie245d492012-06-01 12:52:13 -0700749 return ptr;
750}
751
752void generate_atags(unsigned *ptr, const char *cmdline,
753 void *ramdisk, unsigned ramdisk_size)
754{
vijay kumar21a37452015-12-29 16:23:21 +0530755 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700756 ptr = atag_core(ptr);
757 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
758 ptr = target_atag_mem(ptr);
759
760 /* Skip NAND partition ATAGS for eMMC boot */
761 if (!target_is_emmc_boot()){
762 ptr = atag_ptable(&ptr);
763 }
764
vijay kumar21a37452015-12-29 16:23:21 +0530765 /*
766 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
767 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
768 */
769 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
770 ptr = atag_cmdline(ptr, cmdline);
771 ptr = atag_end(ptr);
772 }
773 else {
774 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
775 ASSERT(0);
776 }
Neeti Desaie245d492012-06-01 12:52:13 -0700777}
778
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700779typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700780void boot_linux(void *kernel, unsigned *tags,
781 const char *cmdline, unsigned machtype,
782 void *ramdisk, unsigned ramdisk_size)
783{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800784 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800785#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700786 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800787#endif
788
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700789 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800790 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530791 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800792
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530793 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700794
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800795 final_cmdline = update_cmdline((const char*)cmdline);
796
Neeti Desai17379b82012-06-04 18:42:53 -0700797#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800798 dprintf(INFO, "Updating device tree: start\n");
799
Neeti Desai17379b82012-06-04 18:42:53 -0700800 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530801 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700802 if(ret)
803 {
804 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
805 ASSERT(0);
806 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800807 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700808#else
Neeti Desaie245d492012-06-01 12:52:13 -0700809 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800810 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700811#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700812
Maria Yu52254c02014-07-04 16:14:54 +0800813 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700814
815#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800816#if !VBOOT_MOTA
817 if (device.verity_mode == 0) {
818#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700819#if ENABLE_VB_ATTEST
820 display_bootverify_menu(DISPLAY_MENU_EIO);
821#else
lijuangbdd9bb42016-03-01 18:22:17 +0800822 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700823#endif
lijuangbdd9bb42016-03-01 18:22:17 +0800824 wait_for_users_action();
825#else
826 dprintf(CRITICAL,
827 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
828 mdelay(5000);
829#endif
830 }
831
832#endif
833#endif
834
835#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700836 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700837 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700838 {
839 dprintf(INFO, "Failed to write protect dev info\n");
840 ASSERT(0);
841 }
842#endif
843
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800844 /* Turn off splash screen if enabled */
845#if DISPLAY_SPLASH_SCREEN
846 target_display_shutdown();
847#endif
848
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700849 /* Perform target specific cleanup */
850 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800851
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800852 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530853 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800854
855 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700856
Amol Jadi4421e652011-06-16 15:00:48 -0700857 /* do any platform specific cleanup before kernel entry */
858 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700859
Brian Swetland9c4c0752009-01-25 16:23:50 -0800860 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700861
Amol Jadi504f9fe2012-08-16 13:56:48 -0700862#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800863 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700864#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700865 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800866
867 if (IS_ARM64(kptr))
868 /* Jump to a 64bit kernel */
869 scm_elexec_call((paddr_t)kernel, tags_phys);
870 else
871 /* Jump to a 32bit kernel */
872 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800873}
874
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700875/* Function to check if the memory address range falls within the aboot
876 * boundaries.
877 * start: Start of the memory region
878 * size: Size of the memory region
879 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530880int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700881{
882 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800883 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700884 return -1;
885
886 /* Check for memory overlap. */
887 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
888 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700889 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700890 return 0;
891 else
892 return -1;
893}
894
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800895#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800896
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800897BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800898#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800899BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800900#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800901
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700902static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
903{
904 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800905
906#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800907#if IMAGE_VERIF_ALGO_SHA1
908 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
909#else
910 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
911#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800912#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700913
914 /* Assume device is rooted at this time. */
915 device.is_tampered = 1;
916
917 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
918
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700919#if VERIFIED_BOOT
920 if(boot_into_recovery)
921 {
922 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530923 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700924 }
925 else
926 {
927 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530928 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700929 }
930 boot_verify_print_state();
931#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700932 ret = image_verify((unsigned char *)bootimg_addr,
933 (unsigned char *)(bootimg_addr + bootimg_size),
934 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800935 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700936#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700937 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
938
939 if (ret)
940 {
941 /* Authorized kernel */
942 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700943 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700944 }
945
Amit Blay4aa292f2015-04-28 21:55:59 +0300946#ifdef MDTP_SUPPORT
947 {
948 /* Verify MDTP lock.
949 * For boot & recovery partitions, use aboot's verification result.
950 */
951 mdtp_ext_partition_verification_t ext_partition;
952 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
953 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
954 ext_partition.page_size = 0; /* Not needed since already validated */
955 ext_partition.image_addr = 0; /* Not needed since already validated */
956 ext_partition.image_size = 0; /* Not needed since already validated */
957 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
958 mdtp_fwlock_verify_lock(&ext_partition);
959 }
960#endif /* MDTP_SUPPORT */
961
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700962#if USE_PCOM_SECBOOT
963 set_tamper_flag(device.is_tampered);
964#endif
965
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700966#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700967 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700968 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700969 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800970#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800971 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700972#if ENABLE_VB_ATTEST
973 mdelay(DELAY_WAIT);
974 shutdown_device();
975#else
lijuanga40d6302015-07-20 20:10:13 +0800976 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700977#endif
lijuanga40d6302015-07-20 20:10:13 +0800978#else
979 dprintf(CRITICAL,
980 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
981 mdelay(5000);
982#endif
983
984 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700985 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800986#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800987 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800988 wait_for_users_action();
989#else
990 dprintf(CRITICAL,
991 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
992 mdelay(5000);
993#endif
994 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700995 default:
lijuanga40d6302015-07-20 20:10:13 +0800996 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700997 }
998#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700999#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301000 if(device.is_tampered)
1001 {
1002 write_device_info_mmc(&device);
1003 #ifdef TZ_TAMPER_FUSE
1004 set_tamper_fuse_cmd();
1005 #endif
1006 #ifdef ASSERT_ON_TAMPER
1007 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1008 ASSERT(0);
1009 #endif
1010 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001011#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001012}
1013
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301014static bool check_format_bit()
1015{
1016 bool ret = false;
1017 int index;
1018 uint64_t offset;
1019 struct boot_selection_info *in = NULL;
1020 char *buf = NULL;
1021
1022 index = partition_get_index("bootselect");
1023 if (index == INVALID_PTN)
1024 {
1025 dprintf(INFO, "Unable to locate /bootselect partition\n");
1026 return ret;
1027 }
1028 offset = partition_get_offset(index);
1029 if(!offset)
1030 {
1031 dprintf(INFO, "partition /bootselect doesn't exist\n");
1032 return ret;
1033 }
1034 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301035 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301036 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301037 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301038 {
1039 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1040 free(buf);
1041 return ret;
1042 }
1043 in = (struct boot_selection_info *) buf;
1044 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1045 (in->version == BOOTSELECT_VERSION)) {
1046 if ((in->state_info & BOOTSELECT_FORMAT) &&
1047 !(in->state_info & BOOTSELECT_FACTORY))
1048 ret = true;
1049 } else {
1050 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1051 in->signature, in->version);
1052 ASSERT(0);
1053 }
1054 free(buf);
1055 return ret;
1056}
1057
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001058void boot_verifier_init()
1059{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001060 uint32_t boot_state;
1061 /* Check if device unlock */
1062 if(device.is_unlocked)
1063 {
1064 boot_verify_send_event(DEV_UNLOCK);
1065 boot_verify_print_state();
1066 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1067 return;
1068 }
1069 else
1070 {
1071 boot_verify_send_event(BOOT_INIT);
1072 }
1073
1074 /* Initialize keystore */
1075 boot_state = boot_verify_keystore_init();
1076 if(boot_state == YELLOW)
1077 {
1078 boot_verify_print_state();
1079 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1080 }
1081}
1082
Shashank Mittal23b8f422010-04-16 19:27:21 -07001083int boot_linux_from_mmc(void)
1084{
1085 struct boot_img_hdr *hdr = (void*) buf;
1086 struct boot_img_hdr *uhdr;
1087 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001088 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001089 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001090 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001091
Shashank Mittalcd98d472011-08-02 14:29:24 -07001092 unsigned char *image_addr = 0;
1093 unsigned kernel_actual;
1094 unsigned ramdisk_actual;
1095 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001096 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001097
Matthew Qin271927e2015-03-31 22:07:07 -04001098 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001099 unsigned int out_len = 0;
1100 unsigned int out_avai_len = 0;
1101 unsigned char *out_addr = NULL;
1102 uint32_t dtb_offset = 0;
1103 unsigned char *kernel_start_addr = NULL;
1104 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001105 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001106 int rc;
Neeti Desai465491e2012-07-31 12:53:35 -07001107#if DEVICE_TREE
1108 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001109 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001110 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001111 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001112 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001113 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001114#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001115 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001116
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301117 if (check_format_bit())
1118 boot_into_recovery = 1;
1119
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001120 if (!boot_into_recovery) {
1121 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1122 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1123 if (rcode <= 0) {
1124 boot_into_ffbm = false;
1125 if (rcode < 0)
1126 dprintf(CRITICAL,"failed to get ffbm cookie");
1127 } else
1128 boot_into_ffbm = true;
1129 } else
1130 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001131 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1132 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1133 dprintf(INFO, "Unified boot method!\n");
1134 hdr = uhdr;
1135 goto unified_boot;
1136 }
Greg Griscod6250552011-06-29 14:40:23 -07001137 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001138 index = partition_get_index("boot");
1139 ptn = partition_get_offset(index);
1140 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001141 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1142 return -1;
1143 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001144 }
1145 else {
1146 index = partition_get_index("recovery");
1147 ptn = partition_get_offset(index);
1148 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001149 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1150 return -1;
1151 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001152 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001153 /* Set Lun for boot & recovery partitions */
1154 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001155
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301156 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001157 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1158 return -1;
1159 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001160
1161 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001162 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001163 return -1;
1164 }
1165
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001166 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301167
1168 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1169 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1170 return -1;
1171 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001172 page_size = hdr->page_size;
1173 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001174 }
1175
vijay kumar287bb542015-09-29 13:01:52 +05301176 /* ensure commandline is terminated */
1177 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1178
Matthew Qin49e51fa2015-02-09 10:40:45 +08001179 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1180 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001181
Matthew Qin49e51fa2015-02-09 10:40:45 +08001182 image_addr = (unsigned char *)target_get_scratch_address();
1183
1184#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301185#ifndef OSVERSION_IN_BOOTIMAGE
1186 dt_size = hdr->dt_size;
1187#endif
1188 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301189 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1190 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1191 return -1;
1192 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001193 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1194#else
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301195 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual + page_size)) {
1196 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1197 return -1;
1198 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001199 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1200#endif
1201
1202#if VERIFIED_BOOT
1203 boot_verifier_init();
1204#endif
1205
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301206 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001207 {
1208 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1209 return -1;
1210 }
1211
Matthew Qinbb7923d2015-02-09 10:56:09 +08001212 /*
1213 * Update loading flow of bootimage to support compressed/uncompressed
1214 * bootimage on both 64bit and 32bit platform.
1215 * 1. Load bootimage from emmc partition onto DDR.
1216 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1217 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1218 * platform accordingly.
1219 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1220 */
1221
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001222 dprintf(INFO, "Loading (%s) image (%d): start\n",
1223 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001224 bs_set_timestamp(BS_KERNEL_LOAD_START);
1225
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301226 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1227 {
1228 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1229 return -1;
1230 }
1231
Matthew Qinbb7923d2015-02-09 10:56:09 +08001232 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001233 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1234 {
1235 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1236 return -1;
1237 }
1238
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001239 dprintf(INFO, "Loading (%s) image (%d): done\n",
1240 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1241
Matthew Qin49e51fa2015-02-09 10:40:45 +08001242 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1243
1244 /* Authenticate Kernel */
1245 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1246 (int) target_use_signed_kernel(),
1247 device.is_unlocked,
1248 device.is_tampered);
1249
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001250 /* Change the condition a little bit to include the test framework support.
1251 * We would never reach this point if device is in fastboot mode, even if we did
1252 * that means we are in test mode, so execute kernel authentication part for the
1253 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301254 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001255 {
1256 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301257 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001258 {
1259 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1260 return -1;
1261 }
1262
1263 /* Read signature */
1264 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1265 {
1266 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1267 return -1;
1268 }
1269
1270 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001271 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301272 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001273 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001274 } else {
1275 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1276 #ifdef TZ_SAVE_KERNEL_HASH
1277 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1278 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001279
1280#ifdef MDTP_SUPPORT
1281 {
1282 /* Verify MDTP lock.
1283 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1284 * since verification was skipped in aboot. The signature is not part of the loaded image.
1285 */
1286 mdtp_ext_partition_verification_t ext_partition;
1287 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1288 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1289 ext_partition.page_size = page_size;
1290 ext_partition.image_addr = (uint32)image_addr;
1291 ext_partition.image_size = imagesize_actual;
1292 ext_partition.sig_avail = FALSE;
1293 mdtp_fwlock_verify_lock(&ext_partition);
1294 }
1295#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001296 }
1297
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001298#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001299 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001300 {
1301#if FBCON_DISPLAY_MSG
1302 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1303 wait_for_users_action();
1304#else
1305 dprintf(CRITICAL,
1306 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1307 mdelay(5000);
1308#endif
1309 }
1310#endif
1311
1312#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301313#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001314 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001315 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001316 ASSERT(0);
1317#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301318#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001319 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001320 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1321 * If not, continue booting.
1322 */
1323 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1324 {
1325 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1326 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001327 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001328 rc = decompress((unsigned char *)(image_addr + page_size),
1329 hdr->kernel_size, out_addr, out_avai_len,
1330 &dtb_offset, &out_len);
1331 if (rc)
1332 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001333 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001334 ASSERT(0);
1335 }
1336
Matthew Qin0b15b322015-05-19 05:20:54 -04001337 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001338 kptr = (struct kernel64_hdr *)out_addr;
1339 kernel_start_addr = out_addr;
1340 kernel_size = out_len;
1341 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001342 dprintf(INFO, "Uncpmpressed kernel in use\n");
1343 if (!strncmp((char*)(image_addr + page_size),
1344 PATCHED_KERNEL_MAGIC,
1345 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1346 dprintf(INFO, "Patched kernel detected\n");
1347 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1348 PATCHED_KERNEL_HEADER_SIZE);
1349 //The size of the kernel is stored at start of kernel image + 16
1350 //The dtb would start just after the kernel
1351 dtb_offset = *((uint32_t*)((unsigned char*)
1352 (image_addr + page_size +
1353 sizeof(PATCHED_KERNEL_MAGIC) -
1354 1)));
1355 //The actual kernel starts after the 20 byte header.
1356 kernel_start_addr = (unsigned char*)(image_addr +
1357 page_size + PATCHED_KERNEL_HEADER_SIZE);
1358 kernel_size = hdr->kernel_size;
1359 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1360 } else {
1361 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1362 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1363 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1364 kernel_size = hdr->kernel_size;
1365 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001366 }
1367
1368 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001369 * Update the kernel/ramdisk/tags address if the boot image header
1370 * has default values, these default values come from mkbootimg when
1371 * the boot image is flashed using fastboot flash:raw
1372 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001373 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001374
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001375 /* Get virtual addresses since the hdr saves physical addresses. */
1376 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1377 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1378 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001379
Matthew Qinbb7923d2015-02-09 10:56:09 +08001380 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001381 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001382 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001383 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1384 {
1385 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1386 return -1;
1387 }
1388
1389#ifndef DEVICE_TREE
1390 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1391 {
1392 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1393 return -1;
1394 }
1395#endif
1396
Matthew Qin49e51fa2015-02-09 10:40:45 +08001397 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001398 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001399 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001400
Matthew Qin49e51fa2015-02-09 10:40:45 +08001401 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301402 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001403 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1404 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001405
Matthew Qin49e51fa2015-02-09 10:40:45 +08001406 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1407 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1408 return -1;
1409 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001410
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301411 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1412 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301413 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301414 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1415 return -1;
1416 }
1417
Matthew Qin49e51fa2015-02-09 10:40:45 +08001418 /* Find index of device tree within device tree table */
1419 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1420 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1421 return -1;
1422 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001423
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301424 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1425 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1426 return -1;
1427 }
1428
1429 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301430 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301431 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1432 return -1;
1433 }
1434
Matthew Qin271927e2015-03-31 22:07:07 -04001435 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1436 {
1437 unsigned int compressed_size = 0;
1438 out_addr += out_len;
1439 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001440 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001441 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1442 dt_entry.size, out_addr, out_avai_len,
1443 &compressed_size, &dtb_size);
1444 if (rc)
1445 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001446 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001447 ASSERT(0);
1448 }
1449
Matthew Qin0b15b322015-05-19 05:20:54 -04001450 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001451 best_match_dt_addr = out_addr;
1452 } else {
1453 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1454 dtb_size = dt_entry.size;
1455 }
1456
Matthew Qin49e51fa2015-02-09 10:40:45 +08001457 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001458 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001459 {
1460 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1461 return -1;
1462 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001463
Matthew Qin271927e2015-03-31 22:07:07 -04001464 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001465 } else {
1466 /* Validate the tags_addr */
1467 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001468 {
1469 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1470 return -1;
1471 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001472 /*
1473 * If appended dev tree is found, update the atags with
1474 * memory address to the DTB appended location on RAM.
1475 * Else update with the atags address in the kernel header
1476 */
1477 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001478 dtb = dev_tree_appended(
1479 (void*)(image_addr + page_size +
1480 patched_kernel_hdr_size),
1481 hdr->kernel_size, dtb_offset,
1482 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001483 if (!dtb) {
1484 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001485 return -1;
1486 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001487 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001488 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001489
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001490 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1491 target_load_ssd_keystore();
1492
Shashank Mittal23b8f422010-04-16 19:27:21 -07001493unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001494
Dima Zavin77e41f32013-03-06 16:10:43 -08001495 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001496 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001497 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1498
1499 return 0;
1500}
1501
Dima Zavin214cc642009-01-26 11:16:21 -08001502int boot_linux_from_flash(void)
1503{
1504 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001505 struct ptentry *ptn;
1506 struct ptable *ptable;
1507 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001508
Shashank Mittalcd98d472011-08-02 14:29:24 -07001509 unsigned char *image_addr = 0;
1510 unsigned kernel_actual;
1511 unsigned ramdisk_actual;
1512 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301513 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001514
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001515#if DEVICE_TREE
1516 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001517 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301518 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001519 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001520 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301521 unsigned int dtb_size = 0;
1522 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001523#endif
1524
David Ng183a7422009-12-07 14:55:21 -08001525 if (target_is_emmc_boot()) {
1526 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1527 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1528 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1529 return -1;
1530 }
1531 goto continue_boot;
1532 }
1533
Dima Zavin214cc642009-01-26 11:16:21 -08001534 ptable = flash_get_ptable();
1535 if (ptable == NULL) {
1536 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1537 return -1;
1538 }
1539
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001540 if(!boot_into_recovery)
1541 {
1542 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001543
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001544 if (ptn == NULL) {
1545 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1546 return -1;
1547 }
1548 }
1549 else
1550 {
1551 ptn = ptable_find(ptable, "recovery");
1552 if (ptn == NULL) {
1553 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1554 return -1;
1555 }
Dima Zavin214cc642009-01-26 11:16:21 -08001556 }
1557
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001558 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001559 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1560 return -1;
1561 }
Dima Zavin214cc642009-01-26 11:16:21 -08001562
1563 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001564 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001565 return -1;
1566 }
1567
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001568 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001569 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 -08001570 return -1;
1571 }
1572
vijay kumar287bb542015-09-29 13:01:52 +05301573 /* ensure commandline is terminated */
1574 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1575
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001576 /*
1577 * Update the kernel/ramdisk/tags address if the boot image header
1578 * has default values, these default values come from mkbootimg when
1579 * the boot image is flashed using fastboot flash:raw
1580 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001581 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001582
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001583 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001584 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1585 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1586 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1587
1588 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1589 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1590
1591 /* Check if the addresses in the header are valid. */
1592 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1593 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1594 {
1595 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1596 return -1;
1597 }
1598
1599#ifndef DEVICE_TREE
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301600 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1601 {
1602 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1603 return -1;
1604 }
1605#else
1606
1607#ifndef OSVERSION_IN_BOOTIMAGE
1608 dt_size = hdr->dt_size;
1609#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001610#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001611
Shashank Mittalcd98d472011-08-02 14:29:24 -07001612 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001613 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001614 {
1615 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001616 offset = 0;
1617
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001618#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301619 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301620 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1621 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1622 return -1;
1623 }
1624
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001625 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001626
Parth Dixit4097b622016-03-15 14:42:27 +05301627 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001628 {
1629 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1630 return -1;
1631 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001632#else
vijay kumar966a9822015-12-09 18:36:09 +05301633 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1634 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1635 return -1;
1636 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001637 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001638#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001639
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001640 dprintf(INFO, "Loading (%s) image (%d): start\n",
1641 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001642 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001643
Vijay Kumar Pendotib3458362016-07-05 13:45:54 +05301644 if (UINT_MAX - page_size < imagesize_actual)
1645 {
1646 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1647 return -1;
1648 }
1649
1650 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1651 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1652 {
1653 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1654 return -1;
1655 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001656 /* Read image without signature */
1657 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1658 {
1659 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1660 return -1;
1661 }
Dima Zavin214cc642009-01-26 11:16:21 -08001662
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001663 dprintf(INFO, "Loading (%s) image (%d): done\n",
1664 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001665 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001666
Shashank Mittalcd98d472011-08-02 14:29:24 -07001667 offset = imagesize_actual;
1668 /* Read signature */
1669 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1670 {
1671 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001672 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001673 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001674
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301675 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001676
1677 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001678 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1679 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001680#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301681 if(dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001682
vijay kumar8f53e362015-11-24 13:38:11 +05301683 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1684
1685 table = (struct dt_table*) dt_table_offset;
1686
1687 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1688 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1689 return -1;
1690 }
1691
1692 /* Find index of device tree within device tree table */
1693 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1694 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1695 return -1;
1696 }
1697
1698 /* Validate and Read device device tree in the "tags_add */
1699 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1700 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1701 return -1;
1702 }
1703
1704 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1705 dtb_size = dt_entry.size;
1706 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1707 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001708#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001709
1710 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001711 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001712 {
1713 write_device_info_flash(&device);
1714 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301715#if USE_PCOM_SECBOOT
1716 set_tamper_flag(device.is_tampered);
1717#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001718 }
1719 else
1720 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001721 offset = page_size;
1722
Amol Jadib6be5c12012-11-14 13:39:51 -08001723 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1724 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1725 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1726
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001727 dprintf(INFO, "Loading (%s) image (%d): start\n",
1728 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1729
Amol Jadi492d5a52013-03-15 16:12:34 -07001730 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001731
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301732 if (UINT_MAX - offset < kernel_actual)
1733 {
1734 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1735 return -1;
1736 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001737 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001738 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1739 return -1;
1740 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001741 offset += kernel_actual;
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301742 if (UINT_MAX - offset < ramdisk_actual)
1743 {
1744 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1745 return -1;
1746 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001747 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001748 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1749 return -1;
1750 }
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301751
Amol Jadib6be5c12012-11-14 13:39:51 -08001752 offset += ramdisk_actual;
1753
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001754 dprintf(INFO, "Loading (%s) image (%d): done\n",
1755 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1756
Amol Jadi492d5a52013-03-15 16:12:34 -07001757 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001758
1759 if(hdr->second_size != 0) {
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301760 if (UINT_MAX - offset < second_actual)
1761 {
1762 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1763 return -1;
1764 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001765 offset += second_actual;
1766 /* Second image loading not implemented. */
1767 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001768 }
1769
1770#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301771 if(dt_size != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001772
1773 /* Read the device tree table into buffer */
1774 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1775 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1776 return -1;
1777 }
1778
1779 table = (struct dt_table*) dt_buf;
1780
Deepa Dinamani19648b42013-09-05 17:05:55 -07001781 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001782 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1783 return -1;
1784 }
1785
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301786 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1787 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301788 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301789 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1790 return -1;
1791 }
1792
Deepa Dinamani19648b42013-09-05 17:05:55 -07001793 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1794 if (!table)
1795 return -1;
1796
1797 /* Read the entire device tree table into buffer */
1798 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1799 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1800 return -1;
1801 }
1802
1803
Joel Kingaa335dc2013-06-03 16:11:08 -07001804 /* Find index of device tree within device tree table */
1805 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001806 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1807 return -1;
1808 }
1809
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001810 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001811 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001812 {
1813 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1814 return -1;
1815 }
1816
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001817 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001818 if(flash_read(ptn, offset + dt_entry.offset,
1819 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001820 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1821 return -1;
1822 }
1823 }
1824#endif
1825
Shashank Mittalcd98d472011-08-02 14:29:24 -07001826 }
David Ng183a7422009-12-07 14:55:21 -08001827continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001828
Dima Zavin214cc642009-01-26 11:16:21 -08001829 /* TODO: create/pass atags to kernel */
1830
Ajay Dudanie28a6072011-07-01 13:59:46 -07001831 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001832 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001833 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1834
1835 return 0;
1836}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001837
Shashank Mittal162244e2011-08-08 19:01:25 -07001838void write_device_info_mmc(device_info *dev)
1839{
Shashank Mittal162244e2011-08-08 19:01:25 -07001840 unsigned long long ptn = 0;
1841 unsigned long long size;
1842 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001843 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001844 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001845 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001846
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001847 if (devinfo_present)
1848 index = partition_get_index("devinfo");
1849 else
1850 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001851
Shashank Mittal162244e2011-08-08 19:01:25 -07001852 ptn = partition_get_offset(index);
1853 if(ptn == 0)
1854 {
1855 return;
1856 }
1857
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001858 lun = partition_get_lun(index);
1859 mmc_set_lun(lun);
1860
Shashank Mittal162244e2011-08-08 19:01:25 -07001861 size = partition_get_size(index);
1862
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001863 blocksize = mmc_get_device_blocksize();
1864
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001865 if (devinfo_present)
1866 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1867 else
1868 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1869 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001870 {
1871 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001872 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001873 }
1874}
1875
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001876void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001877{
Shashank Mittal162244e2011-08-08 19:01:25 -07001878 unsigned long long ptn = 0;
1879 unsigned long long size;
1880 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001881 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001882 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001883
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001884 if ((index = partition_get_index("devinfo")) < 0)
1885 {
1886 devinfo_present = false;
1887 index = partition_get_index("aboot");
1888 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001889
Shashank Mittal162244e2011-08-08 19:01:25 -07001890 ptn = partition_get_offset(index);
1891 if(ptn == 0)
1892 {
1893 return;
1894 }
1895
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001896 mmc_set_lun(partition_get_lun(index));
1897
Shashank Mittal162244e2011-08-08 19:01:25 -07001898 size = partition_get_size(index);
1899
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001900 blocksize = mmc_get_device_blocksize();
1901
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001902 if (devinfo_present)
1903 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1904 else
1905 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1906 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001907 {
1908 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001909 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001910 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001911}
1912
1913void write_device_info_flash(device_info *dev)
1914{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001915 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001916 struct ptentry *ptn;
1917 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001918 if(info == NULL)
1919 {
1920 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1921 ASSERT(0);
1922 }
1923 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001924 ptable = flash_get_ptable();
1925 if (ptable == NULL)
1926 {
1927 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1928 return;
1929 }
1930
1931 ptn = ptable_find(ptable, "devinfo");
1932 if (ptn == NULL)
1933 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001934 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001935 return;
1936 }
1937
1938 memcpy(info, dev, sizeof(device_info));
1939
1940 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1941 {
1942 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1943 return;
1944 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001945 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001946}
1947
vijay kumarc65876c2015-04-24 13:29:16 +05301948static int read_allow_oem_unlock(device_info *dev)
1949{
vijay kumarc65876c2015-04-24 13:29:16 +05301950 unsigned offset;
1951 int index;
1952 unsigned long long ptn;
1953 unsigned long long ptn_size;
1954 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001955 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301956
vijay kumarca2e6812015-07-08 20:28:25 +05301957 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301958 if (index == INVALID_PTN)
1959 {
vijay kumarca2e6812015-07-08 20:28:25 +05301960 index = partition_get_index(frp_ptns[1]);
1961 if (index == INVALID_PTN)
1962 {
1963 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1964 return -1;
1965 }
vijay kumarc65876c2015-04-24 13:29:16 +05301966 }
1967
1968 ptn = partition_get_offset(index);
1969 ptn_size = partition_get_size(index);
1970 offset = ptn_size - blocksize;
1971
Mayank Grover48860402016-11-29 12:34:53 +05301972 /* Set Lun for partition */
1973 mmc_set_lun(partition_get_lun(index));
1974
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001975 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301976 {
1977 dprintf(CRITICAL, "Reading MMC failed\n");
1978 return -1;
1979 }
1980
1981 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1982 is_allow_unlock = buf[blocksize-1] & 0x01;
1983 return 0;
1984}
1985
1986static int write_allow_oem_unlock(bool allow_unlock)
1987{
vijay kumarc65876c2015-04-24 13:29:16 +05301988 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301989 int index;
1990 unsigned long long ptn;
1991 unsigned long long ptn_size;
1992 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001993 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301994
vijay kumarca2e6812015-07-08 20:28:25 +05301995 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301996 if (index == INVALID_PTN)
1997 {
vijay kumarca2e6812015-07-08 20:28:25 +05301998 index = partition_get_index(frp_ptns[1]);
1999 if (index == INVALID_PTN)
2000 {
2001 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2002 return -1;
2003 }
vijay kumarc65876c2015-04-24 13:29:16 +05302004 }
2005
2006 ptn = partition_get_offset(index);
2007 ptn_size = partition_get_size(index);
2008 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302009 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302010
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002011 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302012 {
2013 dprintf(CRITICAL, "Reading MMC failed\n");
2014 return -1;
2015 }
2016
2017 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2018 buf[blocksize-1] = allow_unlock;
2019 if (mmc_write(ptn + offset, blocksize, buf))
2020 {
2021 dprintf(CRITICAL, "Writing MMC failed\n");
2022 return -1;
2023 }
2024
2025 return 0;
2026}
2027
Shashank Mittal162244e2011-08-08 19:01:25 -07002028void read_device_info_flash(device_info *dev)
2029{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002030 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002031 struct ptentry *ptn;
2032 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002033 if(info == NULL)
2034 {
2035 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2036 ASSERT(0);
2037 }
2038 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002039 ptable = flash_get_ptable();
2040 if (ptable == NULL)
2041 {
2042 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2043 return;
2044 }
2045
2046 ptn = ptable_find(ptable, "devinfo");
2047 if (ptn == NULL)
2048 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002049 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002050 return;
2051 }
2052
2053 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2054 {
2055 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2056 return;
2057 }
2058
2059 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2060 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002061 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2062 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002063 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002064 write_device_info_flash(info);
2065 }
2066 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002067 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002068}
2069
2070void write_device_info(device_info *dev)
2071{
2072 if(target_is_emmc_boot())
2073 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002074 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2075 if(info == NULL)
2076 {
2077 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2078 ASSERT(0);
2079 }
2080 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002081 memcpy(info, dev, sizeof(struct device_info));
2082
2083#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002084 if (is_secure_boot_enable()) {
2085 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2086 ASSERT(0);
2087 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002088 else
2089 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002090#else
2091 write_device_info_mmc(info);
2092#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002093 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002094 }
2095 else
2096 {
2097 write_device_info_flash(dev);
2098 }
2099}
2100
2101void read_device_info(device_info *dev)
2102{
2103 if(target_is_emmc_boot())
2104 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002105 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2106 if(info == NULL)
2107 {
2108 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2109 ASSERT(0);
2110 }
2111 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002112
2113#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002114 if (is_secure_boot_enable()) {
2115 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2116 ASSERT(0);
2117 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002118 else
2119 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002120#else
2121 read_device_info_mmc(info);
2122#endif
2123
2124 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2125 {
2126 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002127 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002128 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302129#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002130 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302131#endif
lijuang511a2b52015-08-14 20:50:51 +08002132 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002133 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302134#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002135 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302136#endif
lijuang511a2b52015-08-14 20:50:51 +08002137 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002138 info->is_tampered = 0;
2139 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302140#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002141 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302142#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002143 write_device_info(info);
2144 }
2145 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002146 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002147 }
2148 else
2149 {
2150 read_device_info_flash(dev);
2151 }
2152}
2153
2154void reset_device_info()
2155{
2156 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002157 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002158 write_device_info(&device);
2159}
2160
2161void set_device_root()
2162{
2163 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002164 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002165 write_device_info(&device);
2166}
2167
lijuang4ece1e72015-08-14 21:02:36 +08002168/* set device unlock value
2169 * Must check FRP before call this function
2170 * Need to wipe data when unlock status changed
2171 * type 0: oem unlock
2172 * type 1: unlock critical
2173 * status 0: unlock as false
2174 * status 1: lock as true
2175 */
2176void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002177{
lijuang4ece1e72015-08-14 21:02:36 +08002178 if (type == UNLOCK)
2179 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302180#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002181 else if (type == UNLOCK_CRITICAL)
2182 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302183#endif
lijuang4ece1e72015-08-14 21:02:36 +08002184 write_device_info(&device);
2185}
2186
2187static void set_device_unlock(int type, bool status)
2188{
2189 int is_unlocked = -1;
2190 char response[MAX_RSP_SIZE];
2191
2192 /* check device unlock status if it is as expected */
2193 if (type == UNLOCK)
2194 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302195#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002196 else if (type == UNLOCK_CRITICAL)
2197 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302198#endif
lijuang4ece1e72015-08-14 21:02:36 +08002199 if (is_unlocked == status) {
2200 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2201 fastboot_info(response);
2202 fastboot_okay("");
2203 return;
2204 }
2205
2206 /* status is true, it means to unlock device */
2207 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002208 if(!is_allow_unlock) {
2209 fastboot_fail("oem unlock is not allowed");
2210 return;
2211 }
2212
lijuang4ece1e72015-08-14 21:02:36 +08002213#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002214 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002215 fastboot_okay("");
2216 return;
2217#else
2218 if (type == UNLOCK) {
2219 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2220 return;
2221 }
2222#endif
lijuang21f12f52015-08-22 16:22:19 +08002223 }
lijuang4ece1e72015-08-14 21:02:36 +08002224
2225 set_device_unlock_value(type, status);
2226
2227 /* wipe data */
2228 struct recovery_message msg;
2229
2230 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2231 write_misc(0, &msg, sizeof(msg));
2232
2233 fastboot_okay("");
2234 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002235}
2236
lijuang511a2b52015-08-14 20:50:51 +08002237static bool critical_flash_allowed(const char * entry)
2238{
2239 uint32_t i = 0;
2240 if (entry == NULL)
2241 return false;
2242
2243 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2244 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2245 return true;
2246 }
2247 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002248}
2249
2250#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002251int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2252{
2253 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002254 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002255 struct dt_table *table;
2256 struct dt_entry dt_entry;
2257 uint32_t dt_hdr_size;
2258 unsigned int compressed_size = 0;
2259 unsigned int dtb_size = 0;
2260 unsigned int out_avai_len = 0;
2261 unsigned char *out_addr = NULL;
2262 unsigned char *best_match_dt_addr = NULL;
2263 int rc;
2264
2265 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2266
Parth Dixit4097b622016-03-15 14:42:27 +05302267#ifndef OSVERSION_IN_BOOTIMAGE
2268 dt_size = hdr->dt_size;
2269#endif
2270
2271 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002272 /* add kernel offset */
2273 dt_image_offset += page_size;
2274 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2275 dt_image_offset += n;
2276
2277 /* add ramdisk offset */
2278 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2279 dt_image_offset += n;
2280
2281 /* add second offset */
2282 if(hdr->second_size != 0) {
2283 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2284 dt_image_offset += n;
2285 }
2286
2287 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002288 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002289
Deepa Dinamani19648b42013-09-05 17:05:55 -07002290 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002291 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2292 return -1;
2293 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302294
2295 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2296 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302297 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302298 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2299 return -1;
2300 }
2301
Joel Kingaa335dc2013-06-03 16:11:08 -07002302 /* Find index of device tree within device tree table */
2303 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002304 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2305 return -1;
2306 }
2307
Matthew Qin271927e2015-03-31 22:07:07 -04002308 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2309 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2310 {
2311 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2312 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002313 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002314 rc = decompress(best_match_dt_addr,
2315 dt_entry.size, out_addr, out_avai_len,
2316 &compressed_size, &dtb_size);
2317 if (rc)
2318 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002319 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002320 ASSERT(0);
2321 }
2322
Matthew Qin0b15b322015-05-19 05:20:54 -04002323 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002324 best_match_dt_addr = out_addr;
2325 } else {
2326 dtb_size = dt_entry.size;
2327 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002328 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002329 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002330 {
2331 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2332 return -1;
2333 }
2334
Amol Jadicb524072012-08-09 16:40:18 -07002335 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002336 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002337 } else
2338 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002339
2340 /* Everything looks fine. Return success. */
2341 return 0;
2342}
2343#endif
2344
Brian Swetland9c4c0752009-01-25 16:23:50 -08002345void cmd_boot(const char *arg, void *data, unsigned sz)
2346{
Amit Blay8a510302015-08-17 09:20:01 +03002347#ifdef MDTP_SUPPORT
2348 static bool is_mdtp_activated = 0;
2349#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002350 unsigned kernel_actual;
2351 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002352 uint32_t image_actual;
2353 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302354 uint32_t sig_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002355 struct boot_img_hdr *hdr = NULL;
2356 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002357 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002358 int ret = 0;
2359 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002360 unsigned int out_len = 0;
2361 unsigned int out_avai_len = 0;
2362 unsigned char *out_addr = NULL;
2363 uint32_t dtb_offset = 0;
2364 unsigned char *kernel_start_addr = NULL;
2365 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002366 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002367
lijuang2008ff22016-03-07 17:56:27 +08002368#if FBCON_DISPLAY_MSG
2369 /* Exit keys' detection thread firstly */
2370 exit_menu_keys_detection();
2371#endif
2372
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002373#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002374 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002375 {
2376 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002377 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002378 }
2379#endif
2380
Brian Swetland9c4c0752009-01-25 16:23:50 -08002381 if (sz < sizeof(hdr)) {
2382 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002383 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002384 }
2385
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002386 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002387
2388 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002389 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002390
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002391 if(target_is_emmc_boot() && hdr->page_size) {
2392 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002393 page_mask = page_size - 1;
2394 }
2395
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002396 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2397 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002398#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302399#ifndef OSVERSION_IN_BOOTIMAGE
2400 dt_size = hdr->dt_size;
2401#endif
2402 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002403#endif
2404
2405 image_actual = ADD_OF(page_size, kernel_actual);
2406 image_actual = ADD_OF(image_actual, ramdisk_actual);
2407 image_actual = ADD_OF(image_actual, dt_actual);
2408
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302409 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2410 /* Calculate the signature length from boot image */
2411 sig_actual = read_der_message_length(
2412 (unsigned char*)(data + image_actual),sz);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002413 image_actual = ADD_OF(image_actual, sig_actual);
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302414 }
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002415
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002416 /* sz should have atleast raw boot image */
2417 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002418 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002419 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002420 }
2421
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002422 // Initialize boot state before trying to verify boot.img
2423#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002424 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002425 /* Handle overflow if the input image size is greater than
2426 * boot image buffer can hold
2427 */
2428 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2429 {
2430 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002431 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002432 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002433#endif
2434
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002435 /* Verify the boot image
2436 * device & page_size are initialized in aboot_init
2437 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002438 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002439 /* Pass size excluding signature size, otherwise we would try to
2440 * access signature beyond its length
2441 */
2442 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002443 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002444#ifdef MDTP_SUPPORT
2445 else
2446 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002447 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002448 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002449
2450 if (!is_mdtp_activated) {
2451 ext_partition.partition = MDTP_PARTITION_NONE;
2452 mdtp_fwlock_verify_lock(&ext_partition);
2453 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002454 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002455
Amir Kotzer7c768c02016-04-13 09:08:05 +03002456 /* If mdtp state cannot be validate, block fastboot boot*/
2457 if(mdtp_activated(&is_mdtp_activated)){
2458 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2459 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2460 goto boot_failed;
2461 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002462 if(is_mdtp_activated){
2463 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002464 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002465 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002466#endif /* MDTP_SUPPORT */
2467
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002468#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302469#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002470 // send root of trust
2471 if(!send_rot_command((uint32_t)device.is_unlocked))
2472 ASSERT(0);
2473#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302474#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002475 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002476 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2477 * If not, continue booting.
2478 */
2479 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2480 {
2481 out_addr = (unsigned char *)target_get_scratch_address();
2482 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2483 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002484 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002485 ret = decompress((unsigned char *)(ptr + page_size),
2486 hdr->kernel_size, out_addr, out_avai_len,
2487 &dtb_offset, &out_len);
2488 if (ret)
2489 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002490 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002491 ASSERT(0);
2492 }
2493
Matthew Qin0b15b322015-05-19 05:20:54 -04002494 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002495 kptr = (struct kernel64_hdr *)out_addr;
2496 kernel_start_addr = out_addr;
2497 kernel_size = out_len;
2498 } else {
2499 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2500 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2501 kernel_size = hdr->kernel_size;
2502 }
2503
2504 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002505 * Update the kernel/ramdisk/tags address if the boot image header
2506 * has default values, these default values come from mkbootimg when
2507 * the boot image is flashed using fastboot flash:raw
2508 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002509 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002510
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002511 /* Get virtual addresses since the hdr saves physical addresses. */
2512 hdr->kernel_addr = VA(hdr->kernel_addr);
2513 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2514 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002515
Matthew Qinbb7923d2015-02-09 10:56:09 +08002516 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002517 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002518 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002519 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2520 {
2521 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002522 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002523 }
2524
Amol Jadicb524072012-08-09 16:40:18 -07002525#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002526 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002527 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002528 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002529
2530 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002531#else
2532 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2533 {
2534 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002535 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002536 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002537#endif
2538
2539 /* Load ramdisk & kernel */
2540 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002541 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002542
2543#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002544 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2545 {
2546 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002547 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002548 }
2549
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002550 /*
2551 * If dtb is not found look for appended DTB in the kernel.
2552 * If appended dev tree is found, update the atags with
2553 * memory address to the DTB appended location on RAM.
2554 * Else update with the atags address in the kernel header
2555 */
2556 if (!dtb_copied) {
2557 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002558 dtb = dev_tree_appended((void*)(ptr + page_size),
2559 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002560 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002561 if (!dtb) {
2562 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002563 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002564 }
Amol Jadicb524072012-08-09 16:40:18 -07002565 }
2566#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002567
2568 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002569 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002570
Dima Zavin77e41f32013-03-06 16:10:43 -08002571 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002572 (const char*) hdr->cmdline, board_machtype(),
2573 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002574
2575 /* fastboot already stop, it's no need to show fastboot menu */
2576 return;
2577boot_failed:
2578#if FBCON_DISPLAY_MSG
2579 /* revert to fastboot menu if boot failed */
2580 display_fastboot_menu();
2581#endif
2582 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002583}
2584
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002585void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002586{
2587 struct ptentry *ptn;
2588 struct ptable *ptable;
2589
2590 ptable = flash_get_ptable();
2591 if (ptable == NULL) {
2592 fastboot_fail("partition table doesn't exist");
2593 return;
2594 }
2595
2596 ptn = ptable_find(ptable, arg);
2597 if (ptn == NULL) {
2598 fastboot_fail("unknown partition name");
2599 return;
2600 }
2601
2602 if (flash_erase(ptn)) {
2603 fastboot_fail("failed to erase partition");
2604 return;
2605 }
2606 fastboot_okay("");
2607}
2608
Bikas Gurungd48bd242010-09-04 19:54:32 -07002609
2610void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2611{
2612 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002613 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002614 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002615 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302616 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002617
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002618#if VERIFIED_BOOT
2619 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2620 {
2621 if(!device.is_unlocked)
2622 {
2623 fastboot_fail("unlock device to erase keystore");
2624 return;
2625 }
2626 }
2627#endif
2628
Kinson Chikf1a43512011-07-14 11:28:39 -07002629 index = partition_get_index(arg);
2630 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002631 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002632
Kinson Chikf1a43512011-07-14 11:28:39 -07002633 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002634 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002635 return;
2636 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002637
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002638 lun = partition_get_lun(index);
2639 mmc_set_lun(lun);
2640
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002641 if (platform_boot_dev_isemmc())
2642 {
2643 if (mmc_erase_card(ptn, size)) {
2644 fastboot_fail("failed to erase partition\n");
2645 return;
2646 }
2647 } else {
2648 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2649 size = partition_get_size(index);
2650 if (size > DEFAULT_ERASE_SIZE)
2651 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002652
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002653 /* Simple inefficient version of erase. Just writing
2654 0 in first several blocks */
2655 if (mmc_write(ptn , size, (unsigned int *)out)) {
2656 fastboot_fail("failed to erase partition");
2657 return;
2658 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302659 /*Erase FDE metadata at the userdata footer*/
2660 if(!(strncmp(arg, "userdata", 8)))
2661 {
2662 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2663 memset((void *)footer, 0, FOOTER_SIZE);
2664
2665 size = partition_get_size(index);
2666
2667 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2668 fastboot_fail("failed to erase userdata footer");
2669 free(footer);
2670 return;
2671 }
2672 free(footer);
2673 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002674 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002675#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302676#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002677 if(!(strncmp(arg, "userdata", 8)))
2678 if(send_delete_keys_to_tz())
2679 ASSERT(0);
2680#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302681#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002682 fastboot_okay("");
2683}
2684
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002685void cmd_erase(const char *arg, void *data, unsigned sz)
2686{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002687#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002688 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002689 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002690 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002691 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002692 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002693 return;
2694 }
2695 }
2696#endif
2697
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002698 if(target_is_emmc_boot())
2699 cmd_erase_mmc(arg, data, sz);
2700 else
2701 cmd_erase_nand(arg, data, sz);
2702}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002703
Channagoud Kadabiad259832015-05-29 11:14:17 -07002704static uint32_t aboot_get_secret_key()
2705{
2706 /* 0 is invalid secret key, update this implementation to return
2707 * device specific unique secret key
2708 */
2709 return 0;
2710}
2711
Ajay Dudani5c761132011-04-07 20:19:04 -07002712void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002713{
2714 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002715 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002716 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002717 char *token = NULL;
2718 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002719 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002720 uint8_t lun = 0;
2721 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002722
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002723 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002724 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002725 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002726 if(token)
2727 {
2728 lun = atoi(token);
2729 mmc_set_lun(lun);
2730 lun_set = true;
2731 }
2732
Mao Jinlong226f33a2014-07-04 17:24:10 +08002733 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002734 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002735 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2736 {
2737 if (!aboot_frp_unlock(pname, data, sz))
2738 {
2739 fastboot_info("FRP unlock successful");
2740 fastboot_okay("");
2741 }
2742 else
2743 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2744
2745 return;
2746 }
2747
Mao Jinlong226f33a2014-07-04 17:24:10 +08002748 if (!strcmp(pname, "partition"))
2749 {
2750 dprintf(INFO, "Attempt to write partition image.\n");
2751 if (write_partition(sz, (unsigned char *) data)) {
2752 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002753 return;
2754 }
2755 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002756 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002757 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002758#if VERIFIED_BOOT
2759 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2760 {
2761 if(!device.is_unlocked)
2762 {
2763 fastboot_fail("unlock device to flash keystore");
2764 return;
2765 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302766 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002767 {
2768 fastboot_fail("image is not a keystore file");
2769 return;
2770 }
2771 }
2772#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002773 index = partition_get_index(pname);
2774 ptn = partition_get_offset(index);
2775 if(ptn == 0) {
2776 fastboot_fail("partition table doesn't exist");
2777 return;
2778 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002779
Mao Jinlong226f33a2014-07-04 17:24:10 +08002780 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2781 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2782 fastboot_fail("image is not a boot image");
2783 return;
2784 }
2785 }
2786
2787 if(!lun_set)
2788 {
2789 lun = partition_get_lun(index);
2790 mmc_set_lun(lun);
2791 }
2792
2793 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302794 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002795 fastboot_fail("size too large");
2796 return;
2797 }
2798 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2799 fastboot_fail("flash write failure");
2800 return;
2801 }
Greg Grisco6e754772011-06-23 12:19:39 -07002802 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002803 }
2804 fastboot_okay("");
2805 return;
2806}
2807
Ajay Dudanide984792015-03-02 09:57:41 -08002808void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2809{
2810 int i, images;
2811 meta_header_t *meta_header;
2812 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302813 /*End of the image address*/
2814 uintptr_t data_end;
2815
2816 if( (UINT_MAX - sz) > (uintptr_t)data )
2817 data_end = (uintptr_t)data + sz;
2818 else
2819 {
2820 fastboot_fail("Cannot flash: image header corrupt");
2821 return;
2822 }
2823
2824 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2825 {
2826 fastboot_fail("Cannot flash: image header corrupt");
2827 return;
2828 }
Ajay Dudanide984792015-03-02 09:57:41 -08002829
lijuang8ee21882016-04-19 16:57:11 +08002830 /* If device is locked:
2831 * Forbid to flash image to avoid the device to bypass the image
2832 * which with "any" name other than bootloader. Because it maybe
2833 * a meta package of all partitions.
2834 */
2835#if VERIFIED_BOOT
2836 if (target_build_variant_user()) {
2837 if (!device.is_unlocked) {
2838 fastboot_fail("Device is locked, meta image flashing is not allowed");
2839 return;
2840 }
2841#if !VBOOT_MOTA
2842 if(!device.is_unlock_critical) {
2843 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
2844 return;
2845 }
2846#endif
2847 }
2848#endif
2849
Ajay Dudanide984792015-03-02 09:57:41 -08002850 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302851 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
2852 {
2853 fastboot_fail("Cannot flash: image header corrupt");
2854 return;
2855 }
Ajay Dudanide984792015-03-02 09:57:41 -08002856 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2857
2858 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2859
2860 for (i=0; i<images; i++) {
2861
2862 if((img_header_entry[i].ptn_name == NULL) ||
2863 (img_header_entry[i].start_offset == 0) ||
2864 (img_header_entry[i].size == 0))
2865 break;
2866
Parth Dixit3e2d3032016-03-04 17:11:52 +05302867 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
2868 + img_header_entry[i].size) )
2869 {
2870 fastboot_fail("Cannot flash: image size mismatch");
2871 break;
2872 }
2873
Ajay Dudanide984792015-03-02 09:57:41 -08002874 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2875 (void *) data + img_header_entry[i].start_offset,
2876 img_header_entry[i].size);
2877 }
2878
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002879 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2880 {
2881 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2882 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2883 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2884 }
2885 else
2886 {
2887 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2888 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2889 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2890 }
2891
2892 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002893 fastboot_okay("");
2894 return;
2895}
2896
Ajay Dudani5c761132011-04-07 20:19:04 -07002897void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2898{
2899 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002900 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002901 uint32_t *fill_buf = NULL;
2902 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002903 sparse_header_t *sparse_header;
2904 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002905 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002906 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302907 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002908 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302909 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002910 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302911 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05302912 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002913
Kinson Chikf1a43512011-07-14 11:28:39 -07002914 index = partition_get_index(arg);
2915 ptn = partition_get_offset(index);
2916 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002917 fastboot_fail("partition table doesn't exist");
2918 return;
2919 }
2920
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302921 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302922
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002923 lun = partition_get_lun(index);
2924 mmc_set_lun(lun);
2925
vijay kumar800255e2015-04-24 20:26:19 +05302926 if (sz < sizeof(sparse_header_t)) {
2927 fastboot_fail("size too low");
2928 return;
2929 }
2930
Ajay Dudani5c761132011-04-07 20:19:04 -07002931 /* Read and skip over sparse image header */
2932 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302933
vijay kumar1321f342015-03-27 12:13:42 +05302934 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002935 fastboot_fail("size too large");
2936 return;
2937 }
2938
vijay kumarde4fcf62015-04-23 13:05:49 +05302939 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302940
Parth Dixit64b1a482016-03-07 16:31:26 +05302941 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302942 fastboot_fail("buffer overreads occured due to invalid sparse header");
2943 return;
2944 }
2945
vijay kumarde4fcf62015-04-23 13:05:49 +05302946 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002947 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302948 fastboot_fail("sparse header size mismatch");
2949 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002950 }
2951
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002952 dprintf (SPEW, "=== Sparse Image Header ===\n");
2953 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2954 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2955 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2956 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2957 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2958 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2959 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2960 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002961
2962 /* Start processing chunks */
2963 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2964 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302965 /* Make sure the total image size does not exceed the partition size */
2966 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2967 fastboot_fail("size too large");
2968 return;
2969 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002970 /* Read and skip over chunk header */
2971 chunk_header = (chunk_header_t *) data;
2972 data += sizeof(chunk_header_t);
2973
Parth Dixit64b1a482016-03-07 16:31:26 +05302974 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302975 fastboot_fail("buffer overreads occured due to invalid sparse header");
2976 return;
2977 }
2978
Ajay Dudani5c761132011-04-07 20:19:04 -07002979 dprintf (SPEW, "=== Chunk Header ===\n");
2980 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2981 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2982 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2983
vijay kumar800255e2015-04-24 20:26:19 +05302984 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002985 {
vijay kumar800255e2015-04-24 20:26:19 +05302986 fastboot_fail("chunk header size mismatch");
2987 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002988 }
2989
wufeng.jiang813dc352015-06-02 23:02:46 -04002990 if (!sparse_header->blk_sz ){
2991 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302992 return;
2993 }
2994
wufeng.jiang813dc352015-06-02 23:02:46 -04002995 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2996
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302997 /* Make sure that the chunk size calculated from sparse image does not
2998 * exceed partition size
2999 */
3000 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3001 {
3002 fastboot_fail("Chunk data size exceeds partition size");
3003 return;
3004 }
3005
Ajay Dudani5c761132011-04-07 20:19:04 -07003006 switch (chunk_header->chunk_type)
3007 {
3008 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003009 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003010 chunk_data_sz))
3011 {
3012 fastboot_fail("Bogus chunk size for chunk type Raw");
3013 return;
3014 }
3015
Parth Dixit64b1a482016-03-07 16:31:26 +05303016 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303017 fastboot_fail("buffer overreads occured due to invalid sparse header");
3018 return;
3019 }
3020
wufeng.jiang813dc352015-06-02 23:02:46 -04003021 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3022 otherwise it will return in the line above
3023 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003024 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003025 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003026 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003027 {
3028 fastboot_fail("flash write failure");
3029 return;
3030 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303031 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3032 fastboot_fail("Bogus size for RAW chunk type");
3033 return;
3034 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003035 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003036 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003037 break;
3038
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003039 case CHUNK_TYPE_FILL:
3040 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3041 sizeof(uint32_t)))
3042 {
3043 fastboot_fail("Bogus chunk size for chunk type FILL");
3044 return;
3045 }
3046
3047 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
3048 if (!fill_buf)
3049 {
3050 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3051 return;
3052 }
3053
Parth Dixit64b1a482016-03-07 16:31:26 +05303054 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303055 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303056 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303057 return;
3058 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003059 fill_val = *(uint32_t *)data;
3060 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003061
3062 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3063 {
3064 fill_buf[i] = fill_val;
3065 }
3066
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303067 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3068 {
3069 fastboot_fail("bogus size for chunk FILL type");
3070 free(fill_buf);
3071 return;
3072 }
3073
wufeng.jiang813dc352015-06-02 23:02:46 -04003074 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003075 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303076 /* Make sure that the data written to partition does not exceed partition size */
3077 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3078 {
3079 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303080 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303081 return;
3082 }
3083
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003084 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3085 sparse_header->blk_sz,
3086 fill_buf))
3087 {
3088 fastboot_fail("flash write failure");
3089 free(fill_buf);
3090 return;
3091 }
3092
3093 total_blocks++;
3094 }
3095
3096 free(fill_buf);
3097 break;
3098
Ajay Dudani5c761132011-04-07 20:19:04 -07003099 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303100 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3101 fastboot_fail("bogus size for chunk DONT CARE type");
3102 return;
3103 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003104 total_blocks += chunk_header->chunk_sz;
3105 break;
3106
Ajay Dudani5c761132011-04-07 20:19:04 -07003107 case CHUNK_TYPE_CRC:
3108 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3109 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003110 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003111 return;
3112 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303113 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3114 fastboot_fail("bogus size for chunk CRC type");
3115 return;
3116 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003117 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303118 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303119 fastboot_fail("integer overflow occured");
3120 return;
3121 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003122 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303123 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303124 fastboot_fail("buffer overreads occured due to invalid sparse header");
3125 return;
3126 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003127 break;
3128
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003129 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003130 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003131 fastboot_fail("Unknown chunk type");
3132 return;
3133 }
3134 }
3135
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003136 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3137 total_blocks, sparse_header->total_blks);
3138
3139 if(total_blocks != sparse_header->total_blks)
3140 {
3141 fastboot_fail("sparse image write failure");
3142 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003143
3144 fastboot_okay("");
3145 return;
3146}
3147
3148void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3149{
3150 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003151 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003152
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003153#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003154 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3155 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003156 int ret=0;
3157 uint32 major_version=0;
3158 uint32 minor_version=0;
3159
3160 ret = scm_svc_version(&major_version,&minor_version);
3161 if(!ret)
3162 {
3163 if(major_version >= 2)
3164 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003165 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003166 {
3167 ret = encrypt_scm((uint32 **) &data, &sz);
3168 if (ret != 0) {
3169 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3170 return;
3171 }
3172
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003173 /* Protect only for SSD */
3174 if (!strcmp(arg, "ssd")) {
3175 ret = scm_protect_keystore((uint32 *) data, sz);
3176 if (ret != 0) {
3177 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3178 return;
3179 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003180 }
3181 }
3182 else
3183 {
3184 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3185 if(ret != 0)
3186 {
3187 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3188 return;
3189 }
3190 }
3191 }
3192 else
3193 {
3194 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3195 magic_number[1] == DECRYPT_MAGIC_1)
3196 {
3197 ret = decrypt_scm((uint32 **) &data, &sz);
3198 if (ret != 0) {
3199 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3200 return;
3201 }
3202 }
3203 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3204 magic_number[1] == ENCRYPT_MAGIC_1)
3205 {
3206 ret = encrypt_scm((uint32 **) &data, &sz);
3207 if (ret != 0) {
3208 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3209 return;
3210 }
3211 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003212 }
3213 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003214 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003215 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003216 dprintf(CRITICAL,"INVALID SVC Version\n");
3217 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003218 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003219#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003220
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003221#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003222 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003223 {
lijuang511a2b52015-08-14 20:50:51 +08003224 /* if device is locked:
3225 * common partition will not allow to be flashed
3226 * critical partition will allow to flash image.
3227 */
3228 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3229 fastboot_fail("Partition flashing is not allowed");
3230 return;
3231 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303232#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003233 /* if device critical is locked:
3234 * common partition will allow to be flashed
3235 * critical partition will not allow to flash image.
3236 */
3237 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3238 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003239 return;
3240 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303241#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003242 }
3243#endif
3244
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003245 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003246 meta_header = (meta_header_t *) data;
3247 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003248 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003249 else if (meta_header->magic == META_HEADER_MAGIC)
3250 cmd_flash_meta_img(arg, data, sz);
3251 else
3252 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003253
3254#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303255#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003256 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3257 {
3258 // reset dm_verity mode to enforcing
3259 device.verity_mode = 1;
3260 write_device_info(&device);
3261 }
3262#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303263#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003264
Ajay Dudani5c761132011-04-07 20:19:04 -07003265 return;
3266}
3267
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003268void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3269{
3270 struct ptentry *sys_ptn;
3271 struct ptable *ptable;
3272
3273 ptable = flash_get_ptable();
3274 if (ptable == NULL) {
3275 fastboot_fail("partition table doesn't exist");
3276 return;
3277 }
3278
3279 sys_ptn = ptable_find(ptable, "system");
3280 if (sys_ptn == NULL) {
3281 fastboot_fail("system partition not found");
3282 return;
3283 }
3284
3285 sz = ROUND_TO_PAGE(sz, page_mask);
3286 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3287 fastboot_fail("update_ubi_vol failed");
3288 else
3289 fastboot_okay("");
3290}
3291
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003292void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003293{
3294 struct ptentry *ptn;
3295 struct ptable *ptable;
3296 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303297 uint64_t partition_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003298
3299 ptable = flash_get_ptable();
3300 if (ptable == NULL) {
3301 fastboot_fail("partition table doesn't exist");
3302 return;
3303 }
3304
3305 ptn = ptable_find(ptable, arg);
3306 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003307 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3308 arg);
3309 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003310 return;
3311 }
3312
3313 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3314 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3315 fastboot_fail("image is not a boot image");
3316 return;
3317 }
3318 }
3319
Amol Jadi5c61a952012-05-04 17:05:35 -07003320 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003321 || !strcmp(ptn->name, "userdata")
3322 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003323 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003324 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003325 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003326 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003327 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003328
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303329 partition_size = (uint64_t)ptn->length * (uint64_t)flash_num_pages_per_blk() * (uint64_t)flash_page_size();
3330 if (partition_size > UINT_MAX) {
3331 fastboot_fail("Invalid partition size");
3332 return;
3333 }
3334
3335 if (sz > partition_size) {
3336 fastboot_fail("Image size too large");
3337 return;
3338 }
3339
Dima Zavin214cc642009-01-26 11:16:21 -08003340 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003341 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3342 if (flash_ubi_img(ptn, data, sz)) {
3343 fastboot_fail("flash write failure");
3344 return;
3345 }
3346 } else {
3347 if (flash_write(ptn, extra, data, sz)) {
3348 fastboot_fail("flash write failure");
3349 return;
3350 }
Dima Zavin214cc642009-01-26 11:16:21 -08003351 }
3352 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3353 fastboot_okay("");
3354}
3355
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003356void cmd_flash(const char *arg, void *data, unsigned sz)
3357{
3358 if(target_is_emmc_boot())
3359 cmd_flash_mmc(arg, data, sz);
3360 else
3361 cmd_flash_nand(arg, data, sz);
3362}
3363
Dima Zavin214cc642009-01-26 11:16:21 -08003364void cmd_continue(const char *arg, void *data, unsigned sz)
3365{
3366 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003367 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003368
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003369 if (target_is_emmc_boot())
3370 {
lijuanga40d6302015-07-20 20:10:13 +08003371#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003372 /* Exit keys' detection thread firstly */
3373 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003374#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003375 boot_linux_from_mmc();
3376 }
3377 else
3378 {
3379 boot_linux_from_flash();
3380 }
Dima Zavin214cc642009-01-26 11:16:21 -08003381}
3382
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003383void cmd_reboot(const char *arg, void *data, unsigned sz)
3384{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003385 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003386 fastboot_okay("");
3387 reboot_device(0);
3388}
3389
3390void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3391{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003392 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003393 fastboot_okay("");
3394 reboot_device(FASTBOOT_MODE);
3395}
3396
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003397void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3398{
3399 dprintf(INFO, "Enabling charger screen check\n");
3400 device.charger_screen_enabled = 1;
3401 write_device_info(&device);
3402 fastboot_okay("");
3403}
3404
3405void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3406{
3407 dprintf(INFO, "Disabling charger screen check\n");
3408 device.charger_screen_enabled = 0;
3409 write_device_info(&device);
3410 fastboot_okay("");
3411}
3412
lijuanga25d8bb2015-09-16 13:11:52 +08003413void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3414{
3415 char *p = NULL;
3416 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303417 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003418
3419 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303420 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003421 if (p) {
3422 if (!strncmp(p, "0", 1)) {
3423 device.charger_screen_enabled = 0;
3424 } else if (!strncmp(p, "1", 1)) {
3425 device.charger_screen_enabled = 1;
3426 }
3427 }
3428 }
3429
3430 /* update charger_screen_enabled value for getvar
3431 * command
3432 */
3433 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3434 device.charger_screen_enabled);
3435
3436 write_device_info(&device);
3437 fastboot_okay("");
3438}
3439
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303440void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3441{
3442 dprintf(INFO, "Selecting display panel %s\n", arg);
3443 if (arg)
3444 strlcpy(device.display_panel, arg,
3445 sizeof(device.display_panel));
3446 write_device_info(&device);
3447 fastboot_okay("");
3448}
3449
Shashank Mittal162244e2011-08-08 19:01:25 -07003450void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3451{
lijuang4ece1e72015-08-14 21:02:36 +08003452 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303453}
3454
3455void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3456{
lijuang4ece1e72015-08-14 21:02:36 +08003457 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303458 if(!is_allow_unlock) {
3459 fastboot_fail("oem unlock is not allowed");
3460 return;
3461 }
3462
lijuang4ece1e72015-08-14 21:02:36 +08003463 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303464
lijuang4ece1e72015-08-14 21:02:36 +08003465 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303466 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003467
vijay kumarc65876c2015-04-24 13:29:16 +05303468 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3469 write_misc(0, &msg, sizeof(msg));
3470
3471 fastboot_okay("");
3472 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003473 }
3474 fastboot_okay("");
3475}
3476
Channagoud Kadabiad259832015-05-29 11:14:17 -07003477static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3478{
3479 int ret = 1;
3480 uint32_t secret_key;
3481 char seckey_buffer[MAX_RSP_SIZE];
3482
3483 secret_key = aboot_get_secret_key();
3484 if (secret_key)
3485 {
3486 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3487 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3488 {
3489 is_allow_unlock = true;
3490 write_allow_oem_unlock(is_allow_unlock);
3491 ret = 0;
3492 }
3493 }
3494 return ret;
3495}
3496
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003497void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3498{
lijuang4ece1e72015-08-14 21:02:36 +08003499 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003500}
3501
Shashank Mittala0032282011-08-26 14:50:11 -07003502void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3503{
lijuang511a2b52015-08-14 20:50:51 +08003504 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003505 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003506 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003507 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3508 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303509#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003510 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3511 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303512#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003513 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003514 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303515 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3516 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003517 fastboot_okay("");
3518}
3519
lijuang511a2b52015-08-14 20:50:51 +08003520void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3521{
3522 char response[MAX_RSP_SIZE];
3523 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3524 fastboot_info(response);
3525 fastboot_okay("");
3526}
3527
3528void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3529{
lijuang4ece1e72015-08-14 21:02:36 +08003530 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003531}
3532
3533void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3534{
lijuang4ece1e72015-08-14 21:02:36 +08003535 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003536}
3537
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003538void cmd_preflash(const char *arg, void *data, unsigned sz)
3539{
3540 fastboot_okay("");
3541}
3542
Mao Flynn7b379f32015-04-20 00:28:30 +08003543static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303544
Mao Flynn7b379f32015-04-20 00:28:30 +08003545int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303546{
Mao Flynn7b379f32015-04-20 00:28:30 +08003547 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303548 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003549 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303550 return -1;
3551 return 0;
3552}
3553
Mao Flynn7b379f32015-04-20 00:28:30 +08003554int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003555{
3556 struct ptentry *ptn;
3557 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003558 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003559 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003560
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303561 ptable = flash_get_ptable();
3562 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003563 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3564 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303565 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003566
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303567 ptn = ptable_find(ptable, "splash");
3568 if (ptn == NULL) {
3569 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003570 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303571 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003572 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303573 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003574 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303575 }
3576
Mao Flynn7b379f32015-04-20 00:28:30 +08003577 header = (struct logo_img_header *)logo_header;
3578 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303579 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003580 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303581 }
3582
3583 fb_display = fbcon_display();
3584 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003585 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3586 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3587
3588 /* if the logo is full-screen size, remove "fbcon_clear()" */
3589 if ((header->width != fb_display->width)
3590 || (header->height != fb_display->height))
3591 fbcon_clear();
3592
3593 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3594 (uint32_t *)base,
3595 (header->blocks * 512))) {
3596 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3597 return -1;
3598 }
3599 fbcon_extract_to_screen(header, base);
3600 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003601 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003602
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003603 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3604 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003605 return -1;
3606 }
3607
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303608 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003609 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3610 (uint32_t *)base,
3611 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303612 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303613 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003614 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003615 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303616 }
3617
Mao Flynn7b379f32015-04-20 00:28:30 +08003618 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303619}
3620
Mao Flynn7b379f32015-04-20 00:28:30 +08003621int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303622{
3623 int index = INVALID_PTN;
3624 unsigned long long ptn = 0;
3625 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003626 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003627 uint32_t blocksize, realsize, readsize;
3628 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303629
3630 index = partition_get_index("splash");
3631 if (index == 0) {
3632 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003633 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303634 }
3635
3636 ptn = partition_get_offset(index);
3637 if (ptn == 0) {
3638 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003639 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303640 }
3641
Mao Flynn1893a7f2015-06-03 12:03:36 +08003642 mmc_set_lun(partition_get_lun(index));
3643
3644 blocksize = mmc_get_device_blocksize();
3645 if (blocksize == 0) {
3646 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3647 return -1;
3648 }
3649
3650 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003651 if (!fb_display)
3652 {
3653 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3654 return -1;
3655 }
3656
Mao Flynn1893a7f2015-06-03 12:03:36 +08003657 base = (uint8_t *) fb_display->base;
3658
3659 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303660 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003661 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303662 }
3663
Mao Flynn1893a7f2015-06-03 12:03:36 +08003664 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003665 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303666 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003667 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303668 }
3669
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303670 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003671 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3672 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003673
Mao Flynn1893a7f2015-06-03 12:03:36 +08003674 realsize = header->blocks * 512;
3675 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3676
3677 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003678 if ((header->width != fb_display->width)
3679 || (header->height != fb_display->height))
3680 fbcon_clear();
3681
Mao Flynn1893a7f2015-06-03 12:03:36 +08003682 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003683 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3684 return -1;
3685 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003686
Mao Flynn1893a7f2015-06-03 12:03:36 +08003687 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3688 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303689
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003690 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3691 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003692 return -1;
3693 }
3694
3695 realsize = header->width * header->height * fb_display->bpp / 8;
3696 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3697
3698 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3699 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3700 fbcon_clear();
3701 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3702 return -1;
3703 }
3704 } else {
3705 if (mmc_read(ptn + blocksize ,
3706 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3707 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3708 return -1;
3709 }
3710 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3711 }
3712 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303713 }
3714
Mao Flynn7b379f32015-04-20 00:28:30 +08003715 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303716}
3717
Mao Flynn7b379f32015-04-20 00:28:30 +08003718int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303719{
3720 if (target_is_emmc_boot()) {
3721 return splash_screen_mmc();
3722 } else {
3723 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003724 }
3725}
3726
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003727/* Get the size from partiton name */
3728static void get_partition_size(const char *arg, char *response)
3729{
3730 uint64_t ptn = 0;
3731 uint64_t size;
3732 int index = INVALID_PTN;
3733
3734 index = partition_get_index(arg);
3735
3736 if (index == INVALID_PTN)
3737 {
3738 dprintf(CRITICAL, "Invalid partition index\n");
3739 return;
3740 }
3741
3742 ptn = partition_get_offset(index);
3743
3744 if(!ptn)
3745 {
3746 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3747 return;
3748 }
3749
3750 size = partition_get_size(index);
3751
3752 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3753 return;
3754}
3755
3756/*
3757 * Publish the partition type & size info
3758 * fastboot getvar will publish the required information.
3759 * fastboot getvar partition_size:<partition_name>: partition size in hex
3760 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3761 */
3762static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3763{
3764 uint8_t i;
3765
3766 for (i = 0; i < num_parts; i++) {
3767 get_partition_size(info[i].part_name, info[i].size_response);
3768
3769 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3770 {
3771 dprintf(CRITICAL, "partition size name truncated\n");
3772 return;
3773 }
3774 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3775 {
3776 dprintf(CRITICAL, "partition type name truncated\n");
3777 return;
3778 }
3779
3780 /* publish partition size & type info */
3781 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3782 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3783 }
3784}
3785
lijuang4ece1e72015-08-14 21:02:36 +08003786void get_product_name(unsigned char *buf)
3787{
3788 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3789 return;
3790}
3791
3792void get_bootloader_version(unsigned char *buf)
3793{
3794 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3795 return;
3796}
3797
3798void get_baseband_version(unsigned char *buf)
3799{
3800 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3801 return;
3802}
3803
3804bool is_device_locked()
3805{
3806 return device.is_unlocked ? false:true;
3807}
3808
Amol Jadi5edf3552013-07-23 14:15:34 -07003809/* register commands and variables for fastboot */
3810void aboot_fastboot_register_commands(void)
3811{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003812 int i;
lijuangf16461c2015-08-03 17:09:34 +08003813 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003814
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003815 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003816 /* By default the enabled list is empty. */
3817 {"", NULL},
3818 /* move commands enclosed within the below ifndef to here
3819 * if they need to be enabled in user build.
3820 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003821#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003822 /* Register the following commands only for non-user builds */
3823 {"flash:", cmd_flash},
3824 {"erase:", cmd_erase},
3825 {"boot", cmd_boot},
3826 {"continue", cmd_continue},
3827 {"reboot", cmd_reboot},
3828 {"reboot-bootloader", cmd_reboot_bootloader},
3829 {"oem unlock", cmd_oem_unlock},
3830 {"oem unlock-go", cmd_oem_unlock_go},
3831 {"oem lock", cmd_oem_lock},
3832 {"flashing unlock", cmd_oem_unlock},
3833 {"flashing lock", cmd_oem_lock},
3834 {"flashing lock_critical", cmd_flashing_lock_critical},
3835 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3836 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3837 {"oem device-info", cmd_oem_devinfo},
3838 {"preflash", cmd_preflash},
3839 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3840 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003841 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003842 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003843#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003844 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003845#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003846#endif
lijuang511a2b52015-08-14 20:50:51 +08003847 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003848
3849 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3850 for (i = 1; i < fastboot_cmds_count; i++)
3851 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3852
Amol Jadi5edf3552013-07-23 14:15:34 -07003853 /* publish variables and their values */
3854 fastboot_publish("product", TARGET(BOARD));
3855 fastboot_publish("kernel", "lk");
3856 fastboot_publish("serialno", sn_buf);
3857
3858 /*
3859 * partition info is supported only for emmc partitions
3860 * Calling this for NAND prints some error messages which
3861 * is harmless but misleading. Avoid calling this for NAND
3862 * devices.
3863 */
3864 if (target_is_emmc_boot())
3865 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3866
3867 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003868 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3869 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003870 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003871 /* Is the charger screen check enabled */
3872 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3873 device.charger_screen_enabled);
3874 fastboot_publish("charger-screen-enabled",
3875 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003876 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303877 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3878 device.display_panel);
3879 fastboot_publish("display-panel",
3880 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003881 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3882 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003883 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3884 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3885 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3886 target_is_emmc_boot()? "eMMC":"UFS");
3887 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003888#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003889 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003890 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003891 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003892#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003893}
3894
Brian Swetland9c4c0752009-01-25 16:23:50 -08003895void aboot_init(const struct app_descriptor *app)
3896{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003897 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003898
lijuang39831732016-01-08 17:49:02 +08003899 /* Initialise wdog to catch early lk crashes */
3900#if WDOG_SUPPORT
3901 msm_wdog_init();
3902#endif
3903
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003904 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003905 if (target_is_emmc_boot())
3906 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003907 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003908 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303909 mmc_blocksize = mmc_get_device_blocksize();
3910 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003911 }
3912 else
3913 {
3914 page_size = flash_page_size();
3915 page_mask = page_size - 1;
3916 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003917 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3918
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003919 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303920 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003921
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003922 /* Display splash screen if enabled */
3923#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003924#if NO_ALARM_DISPLAY
3925 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003926#endif
lijuang99c02d82015-02-13 19:04:34 +08003927 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07003928#if DISPLAY_HDMI_PRIMARY
3929 if (!strlen(device.display_panel))
3930 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
3931 sizeof(device.display_panel));
3932#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003933#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003934 /* Wait if the display shutdown is in progress */
3935 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003936 if (!pm_appsbl_display_init_done())
3937 target_display_init(device.display_panel);
3938 else
3939 display_image_on_screen();
3940#else
lijuang99c02d82015-02-13 19:04:34 +08003941 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003942#endif
lijuang99c02d82015-02-13 19:04:34 +08003943 dprintf(SPEW, "Display Init: Done\n");
3944#if NO_ALARM_DISPLAY
3945 }
3946#endif
3947#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003948
Greg Griscod6250552011-06-29 14:40:23 -07003949 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003950 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003951
Dhaval Patel223ec952013-07-18 14:49:44 -07003952 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3953
Matthew Qindefd5562014-07-11 18:02:40 +08003954 /*
3955 * Check power off reason if user force reset,
3956 * if yes phone will do normal boot.
3957 */
3958 if (is_user_force_reset())
3959 goto normal_boot;
3960
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003961 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003962 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003963 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003964 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003965 reboot_device(EMERGENCY_DLOAD);
3966 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3967
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003968 boot_into_fastboot = true;
3969 }
3970 if (!boot_into_fastboot)
3971 {
3972 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3973 boot_into_recovery = 1;
3974 if (!boot_into_recovery &&
3975 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003976 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003977 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003978 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003979 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003980 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003981 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003982
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003983#if USE_PON_REBOOT_REG
3984 reboot_mode = check_hard_reboot_mode();
3985#else
Ajay Dudani77421292010-10-27 19:34:06 -07003986 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003987#endif
3988 if (reboot_mode == RECOVERY_MODE)
3989 {
Ajay Dudani77421292010-10-27 19:34:06 -07003990 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003991 }
3992 else if(reboot_mode == FASTBOOT_MODE)
3993 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003994 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003995 }
3996 else if(reboot_mode == ALARM_BOOT)
3997 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003998 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003999 }
Parth Dixit207573a2015-10-27 17:26:21 +05304000#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05304001#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004002 else if (reboot_mode == DM_VERITY_ENFORCING)
4003 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004004 device.verity_mode = 1;
4005 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004006 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004007#if ENABLE_VB_ATTEST
4008 else if (reboot_mode == DM_VERITY_EIO)
4009#else
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004010 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004011#endif
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004012 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004013 device.verity_mode = 0;
4014 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004015 }
4016 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4017 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004018 if(send_delete_keys_to_tz())
4019 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07004020 }
Parth Dixit207573a2015-10-27 17:26:21 +05304021#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05304022#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004023
Matthew Qindefd5562014-07-11 18:02:40 +08004024normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004025 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004026 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004027 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004028 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004029 if(emmc_recovery_init())
4030 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4031 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004032 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004033 if((device.is_unlocked) || (device.is_tampered))
4034 {
4035 #ifdef TZ_TAMPER_FUSE
4036 set_tamper_fuse_cmd();
4037 #endif
4038 #if USE_PCOM_SECBOOT
4039 set_tamper_flag(device.is_tampered);
4040 #endif
4041 }
Shashank Mittala0032282011-08-26 14:50:11 -07004042 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004043
Pavel Nedev5d91d412013-04-29 11:34:24 +03004044 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07004045 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004046 else
4047 {
4048 recovery_init();
4049 #if USE_PCOM_SECBOOT
4050 if((device.is_unlocked) || (device.is_tampered))
4051 set_tamper_flag(device.is_tampered);
4052 #endif
4053 boot_linux_from_flash();
4054 }
4055 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4056 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004057 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004058
Amol Jadi5edf3552013-07-23 14:15:34 -07004059 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004060
Amol Jadi5edf3552013-07-23 14:15:34 -07004061 /* register aboot specific fastboot commands */
4062 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004063
Amol Jadi5edf3552013-07-23 14:15:34 -07004064 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004065 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004066
4067 /* initialize and start fastboot */
4068 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004069#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004070 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004071#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004072}
4073
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004074uint32_t get_page_size()
4075{
4076 return page_size;
4077}
4078
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004079/*
4080 * Calculated and save hash (SHA256) for non-signed boot image.
4081 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004082 * @param image_addr - Boot image address
4083 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004084 *
4085 * @return int - 0 on success, negative value on failure.
4086 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004087static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004088{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004089 unsigned int digest[8];
4090#if IMAGE_VERIF_ALGO_SHA1
4091 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4092#else
4093 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4094#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004095
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004096 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004097 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004098
4099 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004100 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004101
4102 return 0;
4103}
4104
Brian Swetland9c4c0752009-01-25 16:23:50 -08004105APP_START(aboot)
4106 .init = aboot_init,
4107APP_END