blob: f84d9db7c2b681a5003214ce862bdba87d2fa50b [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Mayank Grover98c4c742019-04-25 17:21:37 +05305 * Copyright (c) 2009-2019, 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>
Mayank Grover351a75e2017-05-30 20:06:08 +053051#include <ab_partition_parser.h>
Monika Singh292b3e92018-03-17 22:40:23 +053052#include <verifiedboot.h>
Greg Griscod6250552011-06-29 14:40:23 -070053#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070054#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070055#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070056#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030057#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070058#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070059#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070060#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053061#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080062#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053063#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080064#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080065#if USE_RPMB_FOR_DEVINFO
66#include <rpmb.h>
67#endif
Dima Zavin214cc642009-01-26 11:16:21 -080068
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070069#if ENABLE_WBC
70#include <pm_app_smbchg.h>
71#endif
72
Neeti Desai17379b82012-06-04 18:42:53 -070073#if DEVICE_TREE
74#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070076#endif
77
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053078#if WDOG_SUPPORT
79#include <wdog.h>
80#endif
81
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070082#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070083#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080084#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080085#include "bootimg.h"
86#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070087#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080088#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070089#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070091#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070092#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020093#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070094#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080095#include <menu_keys_detect.h>
96#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070097#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070098
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700100extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700101extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700102extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530103 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700105void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700106void write_device_info_mmc(device_info *dev);
107void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700108static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700109static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530110static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530111bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530112static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530113static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700114/* fastboot command function pointer */
115typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
Monika Singh292b3e92018-03-17 22:40:23 +0530116bool get_perm_attr_status();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700117
118struct fastboot_cmd_desc {
119 char * name;
120 fastboot_cmd_fn cb;
121};
122
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700123#define EXPAND(NAME) #NAME
124#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700125
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700126#define DISPLAY_PANEL_HDMI "hdmi"
127
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800128#ifdef MEMBASE
129#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
130#else
David Ng183a7422009-12-07 14:55:21 -0800131#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800132#endif
133
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700134#ifndef MEMSIZE
135#define MEMSIZE 1024*1024
136#endif
137
138#define MAX_TAGS_SIZE 1024
Kishor PKee5c0a32018-03-06 16:49:46 +0530139#define PLL_CODES_OFFSET 4096
Kun Liang2f1601a2013-08-12 16:29:54 +0800140/* make 4096 as default size to ensure EFS,EXT4's erasing */
141#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700142#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530143#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800144
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700145#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700146#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800147
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800148#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
149
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700150#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
151
Ameya Thakur10a33452016-06-13 14:24:26 -0700152//Size of the header that is used in case the boot image has
153//a uncompressed kernel + appended dtb
154#define PATCHED_KERNEL_HEADER_SIZE 20
155
156//String used to determine if the boot image has
157//a uncompressed kernel + appended dtb
158#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
159
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800160#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700161static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700162#else
David Ng183a7422009-12-07 14:55:21 -0800163static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700164#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530165static const char *dynamic_bootdev_cmdline =
166 " androidboot.boot_devices=soc/";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800167static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300168static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530169
170static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800171static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800172static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800173static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700174static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300175static const char *secondary_gpt_enable = " gpt";
Monika Singh292b3e92018-03-17 22:40:23 +0530176#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200177static const char *mdtp_activated_flag = " mdtp";
Monika Singh292b3e92018-03-17 22:40:23 +0530178#endif
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800179static const char *baseband_apq = " androidboot.baseband=apq";
180static const char *baseband_msm = " androidboot.baseband=msm";
181static const char *baseband_csfb = " androidboot.baseband=csfb";
182static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700183static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800184static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700185static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800186static const char *baseband_dsda = " androidboot.baseband=dsda";
187static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800188static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800189static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530190static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530191static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
192static const char *skip_ramfs = " skip_initramfs";
Mayank Grover466d6562018-05-10 14:52:20 +0530193
194#if HIBERNATION_SUPPORT
195static const char *resume = " resume=/dev/mmcblk0p";
196#endif
197
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530198#ifdef INIT_BIN_LE
199static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE;
200#else
Mayank Grover3804be72017-06-22 11:59:23 +0530201static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530202#endif
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530203
204#if VERITY_LE
205static const char *verity_dev = " root=/dev/dm-0";
206static const char *verity_system_part = " dm=\"system";
207static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
208#else
Mayank Grover5384ed82018-05-09 12:09:24 +0530209static const char *sys_path = " root=/dev/mmcblk0p";
lijuang83ef4b22018-08-23 11:01:55 +0800210
211#define MAX_DTBO_IDX_STR 64
212static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx=";
Parth Dixit1375d9e2019-07-08 20:56:13 +0530213
214#define MAX_DTB_IDX_STR MAX_DTBO_IDX_STR
215static const char *android_boot_dtb_idx = " androidboot.dtb_idx=";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530216#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800217
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700218#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700219static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700220static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530221static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700222//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700223
224struct verified_boot_verity_mode vbvm[] =
225{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700226#if ENABLE_VB_ATTEST
227 {false, "eio"},
228#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700229 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700230#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700231 {true, "enforcing"},
232};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700233struct verified_boot_state_name vbsn[] =
234{
235 {GREEN, "green"},
236 {ORANGE, "orange"},
237 {YELLOW,"yellow"},
238 {RED,"red" },
239};
240#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700241/*As per spec delay wait time before shutdown in Red state*/
242#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700243static unsigned page_size = 0;
244static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530245static unsigned mmc_blocksize = 0;
246static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700247static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
248static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530249static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800250static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700251static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700252bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530253static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530254static char *vbcmdline;
255static bootinfo info = {0};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530256static void *recovery_dtbo_buf = NULL;
257static uint32_t recovery_dtbo_size = 0;
258
Shashank Mittalcd98d472011-08-02 14:29:24 -0700259/* Assuming unauthorized kernel image by default */
260static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530261static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
262
vijay kumarc65876c2015-04-24 13:29:16 +0530263static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700264
vijay kumarca2e6812015-07-08 20:28:25 +0530265static char frp_ptns[2][8] = {"config","frp"};
266
lijuang511a2b52015-08-14 20:50:51 +0800267static const char *critical_flash_allowed_ptn[] = {
268 "aboot",
269 "rpm",
270 "tz",
271 "sbl",
272 "sdi",
273 "sbl1",
274 "xbl",
275 "hyp",
276 "pmic",
277 "bootloader",
278 "devinfo",
279 "partition"};
280
Dima Zavin42168f22009-01-30 11:52:22 -0800281struct atag_ptbl_entry
282{
283 char name[16];
284 unsigned offset;
285 unsigned size;
286 unsigned flags;
287};
288
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700289/*
290 * Partition info, required to be published
291 * for fastboot
292 */
293struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530294 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700295 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
296 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
297 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
298 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
299};
300
301/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530302 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700303 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530304#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530305#define EXT_STR "ext4"
306#define F2FS_STR "f2fs"
307
308#define FS_SUPERBLOCK_OFFSET 0x400
309#define EXT_MAGIC 0xEF53
310#define EXT_MAGIC_OFFSET_SB 0x38
311#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
312#define F2FS_MAGIC_OFFSET_SB 0x0
313
314typedef enum fs_signature_type {
315 EXT_FS_SIGNATURE = 1,
316 EXT_F2FS_SIGNATURE = 2,
317 NO_FS = -1
318} fs_signature_type;
319
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530320struct getvar_partition_info part_info[NUM_PARTITIONS];
321struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700322{
Mayank Grover49920212018-02-09 18:15:55 +0530323 { "system" , "partition-size:", "partition-type:", "", "ext4" },
324 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
325 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
326 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700327};
328
329char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700330char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800331char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700332char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530333char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530334char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530335char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530336#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530337
338/* For IOT we are using custom version */
339#define PRODUCT_IOT_VERSION "IOT001"
340char bootloader_version_string[MAX_RSP_SIZE];
341#endif
lijuang102dfa92015-10-09 18:31:03 +0800342
343#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800344char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800345char battery_soc_ok [MAX_RSP_SIZE];
346#endif
347
lijuangf16461c2015-08-03 17:09:34 +0800348char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700349
Greg Griscod2471ef2011-07-14 13:00:42 -0700350extern int emmc_recovery_init(void);
351
Kinson Chik0b1c8162011-08-31 16:31:57 -0700352#if NO_KEYPAD_DRIVER
353extern int fastboot_trigger(void);
354#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700355
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530356static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700357{
358 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700359#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800360 if (is_arm64)
361 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
362 else
363 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700364 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
365 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700366#endif
367}
368
Dima Zavin42168f22009-01-30 11:52:22 -0800369static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
370{
371 struct atag_ptbl_entry atag_ptn;
372
373 memcpy(atag_ptn.name, ptn->name, 16);
374 atag_ptn.name[15] = '\0';
375 atag_ptn.offset = ptn->start;
376 atag_ptn.size = ptn->length;
377 atag_ptn.flags = ptn->flags;
378 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
379 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
380}
Mayank Grover9df84c02018-08-30 15:46:35 +0530381#ifdef VERIFIED_BOOT_2
382void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
383{
384 int index = 0;
385 char *vbm_img_buf = NULL;
386 unsigned long long ptn = 0;
387 unsigned long long ptn_size = 0;
388
389 /* Immediately return if dtbo is not supported */
390 index = partition_get_index("vbmeta");
391 ptn = partition_get_offset(index);
392 if(!ptn)
393 {
394 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
395 return;
396 }
397
398 ptn_size = partition_get_size(index);
399 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
400 {
401 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
402 return;
403 }
404
405 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
406 if (!vbm_img_buf)
407 {
408 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
409 return;
410 }
411
412 mmc_set_lun(partition_get_lun(index));
413 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
414 {
415 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
416 free(vbm_img_buf);
417 return;
418 }
419
420 *vbmeta_image_buf = vbm_img_buf;
421 *vbmeta_image_sz = (uint32_t)ptn_size;
422 return;
423}
424#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800425
lijuang102dfa92015-10-09 18:31:03 +0800426#if CHECK_BAT_VOLTAGE
427void update_battery_status(void)
428{
429 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
430 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
431}
432#endif
433
Neeti Desaie245d492012-06-01 12:52:13 -0700434unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800435{
David Ng183a7422009-12-07 14:55:21 -0800436 int cmdline_len = 0;
437 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800438 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700439 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800440 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300441 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700442 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700443 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530444#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530445 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530446#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530447 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530448 int system_ptn_index = -1;
449 unsigned int lun = 0;
450 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530451#if VERITY_LE
452 int syspath_buflen = strlen(verity_dev)
453 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
454 + strlen(verity_params) + sizeof(int) + 2;
455#else
lijuang83ef4b22018-08-23 11:01:55 +0800456 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
457 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
458 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530459 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
460 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530461#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530462 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530463#if HIBERNATION_SUPPORT
464 int resume_buflen = strlen(resume) + sizeof(int) + 2;
465 char resume_buf[resume_buflen];
466 int swap_ptn_index = INVALID_PTN;
467#endif
468
Mayank Grover889be1b2017-09-12 20:12:23 +0530469#if VERIFIED_BOOT
470 uint32_t boot_state = RED;
471#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530472
473#if USE_LE_SYSTEMD
474 is_systemd_present=true;
475#endif
476
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700477#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530478 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530479 {
480 boot_state = boot_verify_get_state();
481 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530482#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700483
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200484#ifdef MDTP_SUPPORT
485 mdtp_activated(&is_mdtp_activated);
486#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800487
Brian Swetland9c4c0752009-01-25 16:23:50 -0800488 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800489 cmdline_len = strlen(cmdline);
490 have_cmdline = 1;
491 }
492 if (target_is_emmc_boot()) {
493 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700494 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530495 if (!boot_dev_buf) {
496 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
497 } else {
498 platform_boot_dev_cmdline(boot_dev_buf);
499#if USE_BOOTDEV_CMDLINE
500 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700501#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530502 if (target_dynamic_partition_supported()) {
503 cmdline_len += strlen(dynamic_bootdev_cmdline);
504 cmdline_len += strlen(boot_dev_buf);
505 }
506 }
David Ng183a7422009-12-07 14:55:21 -0800507 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800508
509 cmdline_len += strlen(usb_sn_cmdline);
510 cmdline_len += strlen(sn_buf);
511
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700512#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530513 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700514 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530515 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
516 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
517 {
518 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
519 ASSERT(0);
520 }
521 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
522 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700523 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530524#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700525
Monika Singh292b3e92018-03-17 22:40:23 +0530526
527 if (vbcmdline != NULL) {
528 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
529 strlen(vbcmdline));
530 cmdline_len += strlen(vbcmdline);
531 }
532
Pavel Nedev5614d222013-06-17 18:01:02 +0300533 if (boot_into_recovery && gpt_exists)
534 cmdline_len += strlen(secondary_gpt_enable);
535
Monika Singh292b3e92018-03-17 22:40:23 +0530536#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200537 if(is_mdtp_activated)
538 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530539#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300540 if (boot_into_ffbm) {
541 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530542
543 if(is_systemd_present)
544 cmdline_len += strlen(systemd_ffbm_mode);
545
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700546 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800547 /* reduce kernel console messages to speed-up boot */
548 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800549 } else if (boot_reason_alarm) {
550 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800551 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800552 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700553 pause_at_bootup = 1;
554 cmdline_len += strlen(battchg_pause);
555 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800556
Shashank Mittalcd98d472011-08-02 14:29:24 -0700557 if(target_use_signed_kernel() && auth_kernel_img) {
558 cmdline_len += strlen(auth_kernel);
559 }
560
Joonwoo Park61112782013-10-02 19:50:39 -0700561 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
562 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530563 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700564 have_target_boot_params = 1;
565 cmdline_len += strlen(target_boot_params);
566 }
567
Ajay Dudanid04110c2011-01-17 23:55:07 -0800568 /* Determine correct androidboot.baseband to use */
569 switch(target_baseband())
570 {
571 case BASEBAND_APQ:
572 cmdline_len += strlen(baseband_apq);
573 break;
574
575 case BASEBAND_MSM:
576 cmdline_len += strlen(baseband_msm);
577 break;
578
579 case BASEBAND_CSFB:
580 cmdline_len += strlen(baseband_csfb);
581 break;
582
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800583 case BASEBAND_SVLTE2A:
584 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800585 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700586
587 case BASEBAND_MDM:
588 cmdline_len += strlen(baseband_mdm);
589 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700590
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800591 case BASEBAND_MDM2:
592 cmdline_len += strlen(baseband_mdm2);
593 break;
594
Amol Jadi5c61a952012-05-04 17:05:35 -0700595 case BASEBAND_SGLTE:
596 cmdline_len += strlen(baseband_sglte);
597 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530598
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800599 case BASEBAND_SGLTE2:
600 cmdline_len += strlen(baseband_sglte2);
601 break;
602
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530603 case BASEBAND_DSDA:
604 cmdline_len += strlen(baseband_dsda);
605 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800606
607 case BASEBAND_DSDA2:
608 cmdline_len += strlen(baseband_dsda2);
609 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530610 case BASEBAND_APQ_NOWGR:
611 cmdline_len += strlen(baseband_apq_nowgr);
612 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800613 }
614
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300615#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800616 if (cmdline) {
617 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530618 target_display_panel_node(display_panel_buf,
619 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800620 strlen(display_panel_buf)) {
621 cmdline_len += strlen(display_panel_buf);
622 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700623 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300624#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700625
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800626 if (target_warm_boot()) {
627 warm_boot = true;
628 cmdline_len += strlen(warmboot_cmdline);
629 }
630
Mayank Grover5384ed82018-05-09 12:09:24 +0530631 if (target_uses_system_as_root() ||
632 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530633 {
634 current_active_slot = partition_find_active_slot();
635 cmdline_len += (strlen(androidboot_slot_suffix)+
636 strlen(SUFFIX_SLOT(current_active_slot)));
637
Mayank Grover3804be72017-06-22 11:59:23 +0530638 system_ptn_index = partition_get_index("system");
639 if (platform_boot_dev_isemmc())
640 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530641#if VERITY_LE
642 /*
643 Condition 4: Verity and A/B both enabled
644 Eventual command line looks like:
645 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
646 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
647 */
648 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
649 verity_dev,
650 verity_system_part, suffix_slot[current_active_slot],
651 verity_params, system_ptn_index + 1);
652#else
653 /*
654 Condition 5: A/B enabled, but verity disabled
655 Eventual command line looks like:
656 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
657 ... root=/dev/mmcblk0p<NN> ...
658 */
659 snprintf(syspath_buf, syspath_buflen, " %s%d",
660 sys_path, system_ptn_index + 1);
661#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530662 }
663 else
664 {
665 lun = partition_get_lun(system_ptn_index);
666 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
667 lun_char_base + lun,
668 partition_get_index_in_lun("system", lun));
669 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530670
Monika Singh292b3e92018-03-17 22:40:23 +0530671#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530672 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530673#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530674 }
675
676 if (target_uses_system_as_root() ||
677 partition_multislot_is_supported())
678 {
679 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530680
681 /* For dynamic partition, support skip skip_initramfs */
682 if (!target_dynamic_partition_supported() &&
683 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530684 cmdline_len += strlen(skip_ramfs);
685 }
686
Mayank Grover466d6562018-05-10 14:52:20 +0530687#if HIBERNATION_SUPPORT
688 if (platform_boot_dev_isemmc())
689 {
690 swap_ptn_index = partition_get_index("swap");
691 if (swap_ptn_index != INVALID_PTN)
692 {
693 snprintf(resume_buf, resume_buflen,
694 " %s%d", resume,
695 (swap_ptn_index + 1));
696 cmdline_len += strlen(resume_buf);
697 }
698 else
699 {
700 dprintf(INFO, "WARNING: swap partition not found\n");
701 }
702 }
703#endif
704
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800705#if TARGET_CMDLINE_SUPPORT
706 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
707 int target_cmd_line_len;
708 ASSERT(target_cmdline_buf);
709 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
710 cmdline_len += target_cmd_line_len;
711#endif
712
lijuang83ef4b22018-08-23 11:01:55 +0800713#if !VERITY_LE
714 dtbo_idx = get_dtbo_idx ();
715 if (dtbo_idx != INVALID_PTN) {
716 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
717 android_boot_dtbo_idx, dtbo_idx);
718 cmdline_len += strlen (dtbo_idx_str);
719 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530720
721 dtb_idx = get_dtb_idx ();
722 if (dtb_idx != INVALID_PTN) {
723 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
724 android_boot_dtb_idx, dtb_idx);
725 cmdline_len += strlen (dtb_idx_str);
726 }
lijuang83ef4b22018-08-23 11:01:55 +0800727#endif
728
David Ng183a7422009-12-07 14:55:21 -0800729 if (cmdline_len > 0) {
730 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800731 unsigned char *dst;
732
733 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
734 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530735 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800736 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700737
Amol Jadi168b7712012-03-06 16:15:00 -0800738 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800739 if (have_cmdline) {
740 src = cmdline;
741 while ((*dst++ = *src++));
742 }
743 if (target_is_emmc_boot()) {
744 src = emmc_cmdline;
745 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700746 have_cmdline = 1;
747 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800748#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700749 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530750 if (have_cmdline &&
751 boot_dev_buf) {
752 --dst;
753 while ((*dst++ = *src++));
754 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700755#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530756 /* Dynamic partition append boot_devices */
757 if (target_dynamic_partition_supported() &&
758 boot_dev_buf) {
759 src = dynamic_bootdev_cmdline;
760 if (have_cmdline) --dst;
761 while ((*dst++ = *src++));
762 src = boot_dev_buf;
763 if (have_cmdline) --dst;
764 while ((*dst++ = *src++));
765 }
David Ngf773dde2010-07-26 19:55:08 -0700766 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800767
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700768#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530769 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700770 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530771 src = verified_state;
772 if(have_cmdline) --dst;
773 have_cmdline = 1;
774 while ((*dst++ = *src++));
775 src = vbsn[boot_state].name;
776 if(have_cmdline) --dst;
777 while ((*dst++ = *src++));
778
779 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
780 {
781 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
782 ASSERT(0);
783 }
784 src = verity_mode;
785 if(have_cmdline) --dst;
786 while ((*dst++ = *src++));
787 src = vbvm[device.verity_mode].name;
788 if(have_cmdline) -- dst;
789 while ((*dst++ = *src++));
790 src = keymaster_v1;
791 if(have_cmdline) --dst;
792 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700793 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530794#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530795
796 if (vbcmdline != NULL) {
797 src = vbcmdline;
798 if (have_cmdline) --dst;
799 while ((*dst++ = *src++));
800 }
801
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800802 src = usb_sn_cmdline;
803 if (have_cmdline) --dst;
804 have_cmdline = 1;
805 while ((*dst++ = *src++));
806 src = sn_buf;
807 if (have_cmdline) --dst;
808 have_cmdline = 1;
809 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800810 if (warm_boot) {
811 if (have_cmdline) --dst;
812 src = warmboot_cmdline;
813 while ((*dst++ = *src++));
814 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800815
Pavel Nedev5614d222013-06-17 18:01:02 +0300816 if (boot_into_recovery && gpt_exists) {
817 src = secondary_gpt_enable;
818 if (have_cmdline) --dst;
819 while ((*dst++ = *src++));
820 }
Monika Singh292b3e92018-03-17 22:40:23 +0530821#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200822 if (is_mdtp_activated) {
823 src = mdtp_activated_flag;
824 if (have_cmdline) --dst;
825 while ((*dst++ = *src++));
826 }
Monika Singh292b3e92018-03-17 22:40:23 +0530827#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300828 if (boot_into_ffbm) {
829 src = androidboot_mode;
830 if (have_cmdline) --dst;
831 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700832 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300833 if (have_cmdline) --dst;
834 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530835
836 if(is_systemd_present) {
837 src = systemd_ffbm_mode;
838 if (have_cmdline) --dst;
839 while ((*dst++ = *src++));
840 }
841
Pavel Nedev898298c2013-02-27 12:36:09 -0800842 src = loglevel;
843 if (have_cmdline) --dst;
844 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800845 } else if (boot_reason_alarm) {
846 src = alarmboot_cmdline;
847 if (have_cmdline) --dst;
848 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300849 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700850 src = battchg_pause;
851 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800852 while ((*dst++ = *src++));
853 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800854
Shashank Mittalcd98d472011-08-02 14:29:24 -0700855 if(target_use_signed_kernel() && auth_kernel_img) {
856 src = auth_kernel;
857 if (have_cmdline) --dst;
858 while ((*dst++ = *src++));
859 }
860
Ajay Dudanid04110c2011-01-17 23:55:07 -0800861 switch(target_baseband())
862 {
863 case BASEBAND_APQ:
864 src = baseband_apq;
865 if (have_cmdline) --dst;
866 while ((*dst++ = *src++));
867 break;
868
869 case BASEBAND_MSM:
870 src = baseband_msm;
871 if (have_cmdline) --dst;
872 while ((*dst++ = *src++));
873 break;
874
875 case BASEBAND_CSFB:
876 src = baseband_csfb;
877 if (have_cmdline) --dst;
878 while ((*dst++ = *src++));
879 break;
880
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800881 case BASEBAND_SVLTE2A:
882 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800883 if (have_cmdline) --dst;
884 while ((*dst++ = *src++));
885 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700886
887 case BASEBAND_MDM:
888 src = baseband_mdm;
889 if (have_cmdline) --dst;
890 while ((*dst++ = *src++));
891 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700892
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800893 case BASEBAND_MDM2:
894 src = baseband_mdm2;
895 if (have_cmdline) --dst;
896 while ((*dst++ = *src++));
897 break;
898
Amol Jadi5c61a952012-05-04 17:05:35 -0700899 case BASEBAND_SGLTE:
900 src = baseband_sglte;
901 if (have_cmdline) --dst;
902 while ((*dst++ = *src++));
903 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530904
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800905 case BASEBAND_SGLTE2:
906 src = baseband_sglte2;
907 if (have_cmdline) --dst;
908 while ((*dst++ = *src++));
909 break;
910
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530911 case BASEBAND_DSDA:
912 src = baseband_dsda;
913 if (have_cmdline) --dst;
914 while ((*dst++ = *src++));
915 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800916
917 case BASEBAND_DSDA2:
918 src = baseband_dsda2;
919 if (have_cmdline) --dst;
920 while ((*dst++ = *src++));
921 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530922 case BASEBAND_APQ_NOWGR:
923 src = baseband_apq_nowgr;
924 if (have_cmdline) --dst;
925 while ((*dst++ = *src++));
926 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800927 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700928
929 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700930 src = display_panel_buf;
931 if (have_cmdline) --dst;
932 while ((*dst++ = *src++));
933 }
Joonwoo Park61112782013-10-02 19:50:39 -0700934
935 if (have_target_boot_params) {
936 if (have_cmdline) --dst;
937 src = target_boot_params;
938 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530939 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700940 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800941
Mayank Grover351a75e2017-05-30 20:06:08 +0530942 if (partition_multislot_is_supported() && have_cmdline)
943 {
944 src = androidboot_slot_suffix;
945 --dst;
946 while ((*dst++ = *src++));
947 --dst;
948 src = SUFFIX_SLOT(current_active_slot);
949 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530950 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530951
Mayank Grover351a75e2017-05-30 20:06:08 +0530952
Mayank Grover5384ed82018-05-09 12:09:24 +0530953 /*
954 * System-As-Root behaviour, system.img should contain both
955 * system content and ramdisk content, and should be mounted at
956 * root(a/b).
957 * Apending skip_ramfs for non a/b builds which use, system as root.
958 */
959 if ((target_uses_system_as_root() ||
960 partition_multislot_is_supported()) &&
961 have_cmdline)
962 {
Mayank Groverb716edf2019-05-08 16:06:55 +0530963 if (!target_dynamic_partition_supported() &&
964 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +0530965 {
966 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +0530967 --dst;
968 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530969 }
970
971 src = sys_path_cmdline;
972 --dst;
973 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530974
Monika Singh292b3e92018-03-17 22:40:23 +0530975#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +0530976 src = syspath_buf;
977 --dst;
978 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +0530979#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530980 }
981
Mayank Grover466d6562018-05-10 14:52:20 +0530982#if HIBERNATION_SUPPORT
983 if (swap_ptn_index != INVALID_PTN)
984 {
985 src = resume_buf;
986 --dst;
987 while ((*dst++ = *src++));
988 }
989#endif
990
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800991#if TARGET_CMDLINE_SUPPORT
992 if (target_cmdline_buf && target_cmd_line_len)
993 {
994 if (have_cmdline) --dst;
995 src = target_cmdline_buf;
996 while((*dst++ = *src++));
997 free(target_cmdline_buf);
998 }
999#endif
lijuang83ef4b22018-08-23 11:01:55 +08001000
1001#if !VERITY_LE
1002 if (dtbo_idx != INVALID_PTN) {
1003 src = dtbo_idx_str;
1004 --dst;
1005 while ((*dst++ = *src++));
1006 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301007
1008 if (dtb_idx != INVALID_PTN) {
1009 src = dtb_idx_str;
1010 --dst;
1011 while ((*dst++ = *src++));
1012 }
lijuang83ef4b22018-08-23 11:01:55 +08001013#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001014 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001015
1016
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001017 if (boot_dev_buf)
1018 free(boot_dev_buf);
1019
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001020 if (cmdline_final)
1021 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1022 else
1023 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001024 return cmdline_final;
1025}
1026
1027unsigned *atag_core(unsigned *ptr)
1028{
1029 /* CORE */
1030 *ptr++ = 2;
1031 *ptr++ = 0x54410001;
1032
1033 return ptr;
1034
1035}
1036
1037unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1038 unsigned ramdisk_size)
1039{
1040 if (ramdisk_size) {
1041 *ptr++ = 4;
1042 *ptr++ = 0x54420005;
1043 *ptr++ = (unsigned)ramdisk;
1044 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001045 }
1046
Neeti Desaie245d492012-06-01 12:52:13 -07001047 return ptr;
1048}
1049
1050unsigned *atag_ptable(unsigned **ptr_addr)
1051{
1052 int i;
1053 struct ptable *ptable;
1054
1055 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001056 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1057 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001058 *(*ptr_addr)++ = 0x4d534d70;
1059 for (i = 0; i < ptable->count; ++i)
1060 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1061 }
1062
1063 return (*ptr_addr);
1064}
1065
1066unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1067{
1068 int cmdline_length = 0;
1069 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001070 char *dest;
1071
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001072 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001073 n = (cmdline_length + 4) & (~3);
1074
1075 *ptr++ = (n / 4) + 2;
1076 *ptr++ = 0x54410009;
1077 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001078 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001079 ptr += (n / 4);
1080
1081 return ptr;
1082}
1083
1084unsigned *atag_end(unsigned *ptr)
1085{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001086 /* END */
1087 *ptr++ = 0;
1088 *ptr++ = 0;
1089
Neeti Desaie245d492012-06-01 12:52:13 -07001090 return ptr;
1091}
1092
1093void generate_atags(unsigned *ptr, const char *cmdline,
1094 void *ramdisk, unsigned ramdisk_size)
1095{
vijay kumar21a37452015-12-29 16:23:21 +05301096 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001097 ptr = atag_core(ptr);
1098 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1099 ptr = target_atag_mem(ptr);
1100
1101 /* Skip NAND partition ATAGS for eMMC boot */
1102 if (!target_is_emmc_boot()){
1103 ptr = atag_ptable(&ptr);
1104 }
1105
vijay kumar21a37452015-12-29 16:23:21 +05301106 /*
1107 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1108 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1109 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301110 if (!cmdline)
1111 return;
1112
vijay kumar21a37452015-12-29 16:23:21 +05301113 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1114 ptr = atag_cmdline(ptr, cmdline);
1115 ptr = atag_end(ptr);
1116 }
1117 else {
1118 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1119 ASSERT(0);
1120 }
Neeti Desaie245d492012-06-01 12:52:13 -07001121}
1122
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001123typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001124void boot_linux(void *kernel, unsigned *tags,
1125 const char *cmdline, unsigned machtype,
1126 void *ramdisk, unsigned ramdisk_size)
1127{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001128 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001129#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001130 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001131#endif
1132
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001133 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001134 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301135 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001136
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301137 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001138
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001139 final_cmdline = update_cmdline((const char*)cmdline);
1140
Neeti Desai17379b82012-06-04 18:42:53 -07001141#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001142 dprintf(INFO, "Updating device tree: start\n");
1143
Neeti Desai17379b82012-06-04 18:42:53 -07001144 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301145 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001146 if(ret)
1147 {
1148 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1149 ASSERT(0);
1150 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001151 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001152#else
Neeti Desaie245d492012-06-01 12:52:13 -07001153 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001154 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001155#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001156
Monika Singh0ec6db82019-07-18 16:57:58 +05301157#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301158 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301159 {
1160 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001161#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001162#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301163 display_bootverify_menu(DISPLAY_MENU_EIO);
1164 wait_for_users_action();
1165 if(!pwr_key_is_pressed)
1166 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001167#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301168 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001169#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301170 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001171#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301172 dprintf(CRITICAL,
1173 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1174 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001175#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301176 }
lijuangbdd9bb42016-03-01 18:22:17 +08001177 }
lijuangbdd9bb42016-03-01 18:22:17 +08001178#endif
1179
1180#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001181 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001182 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001183 {
1184 dprintf(INFO, "Failed to write protect dev info\n");
1185 ASSERT(0);
1186 }
1187#endif
1188
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001189 /* Turn off splash screen if enabled */
1190#if DISPLAY_SPLASH_SCREEN
1191 target_display_shutdown();
1192#endif
1193
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001194 /* Perform target specific cleanup */
1195 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301196 free_verified_boot_resource(&info);
1197 if (final_cmdline)
1198 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001199
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001200 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301201 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001202
1203 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001204
Amol Jadi4421e652011-06-16 15:00:48 -07001205 /* do any platform specific cleanup before kernel entry */
1206 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001207
Brian Swetland9c4c0752009-01-25 16:23:50 -08001208 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001209
Amol Jadi504f9fe2012-08-16 13:56:48 -07001210#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001211 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001212#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001213 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001214
1215 if (IS_ARM64(kptr))
1216 /* Jump to a 64bit kernel */
1217 scm_elexec_call((paddr_t)kernel, tags_phys);
1218 else
1219 /* Jump to a 32bit kernel */
1220 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001221}
1222
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001223/* Function to check if the memory address range falls within the aboot
1224 * boundaries.
1225 * start: Start of the memory region
1226 * size: Size of the memory region
1227 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301228int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001229{
1230 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001231 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001232 return -1;
1233
1234 /* Check for memory overlap. */
1235 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1236 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001237 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001238 return 0;
1239 else
1240 return -1;
1241}
1242
Mayank Grovere559cec2017-10-17 15:12:03 +05301243/* Function to check if the memory address range falls beyond ddr region.
1244 * start: Start of the memory region
1245 * size: Size of the memory region
1246 */
1247int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1248{
1249 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1250 uint64_t ddr_size = smem_get_ddr_size();
1251 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1252 uintptr_t pa_start_addr = PA(start);
1253
1254 /* Check for boundary conditions. */
1255 if ((UINT_MAX - start) < size)
1256 return -1;
1257
1258 /* Check if memory range is beyond the ddr range. */
1259 if (pa_start_addr < ddr_pa_start_addr ||
1260 pa_start_addr >= (ddr_pa_end_addr) ||
1261 (pa_start_addr + size) > ddr_pa_end_addr)
1262 return -1;
1263 else
1264 return 0;
1265}
1266
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001267BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001268
Mayank Groverdd080e82018-09-04 20:12:13 +05301269int getimage(void **image_buffer, uint32_t *imgsize,
1270 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301271{
Mayank Groverdd080e82018-09-04 20:12:13 +05301272 uint32_t loadedindex;
1273 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301274 imgname == NULL) {
1275 dprintf(CRITICAL, "getimage: invalid parameters\n");
1276 return -1;
1277 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301278 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1279 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301280 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301281 *image_buffer = info.images[loadedindex].image_buffer;
1282 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301283 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1284 info.images[loadedindex].name,
1285 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301286 return 0;
1287 }
1288 }
1289 return -1;
1290}
1291
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001292static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1293{
1294 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001295
1296#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001297#if IMAGE_VERIF_ALGO_SHA1
1298 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1299#else
1300 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1301#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001302#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001303
1304 /* Assume device is rooted at this time. */
1305 device.is_tampered = 1;
1306
1307 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1308
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001309#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301310 uint32_t bootstate;
1311 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301312 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301313 {
1314 ret = boot_verify_image((unsigned char *)bootimg_addr,
1315 bootimg_size, "/recovery", &bootstate);
1316 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001317 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301318 {
1319 ret = boot_verify_image((unsigned char *)bootimg_addr,
1320 bootimg_size, "/boot", &bootstate);
1321 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001322 boot_verify_print_state();
1323#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001324 ret = image_verify((unsigned char *)bootimg_addr,
1325 (unsigned char *)(bootimg_addr + bootimg_size),
1326 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001327 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001328#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001329 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1330
1331 if (ret)
1332 {
1333 /* Authorized kernel */
1334 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001335 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001336 }
1337
Amit Blay4aa292f2015-04-28 21:55:59 +03001338#ifdef MDTP_SUPPORT
1339 {
1340 /* Verify MDTP lock.
1341 * For boot & recovery partitions, use aboot's verification result.
1342 */
1343 mdtp_ext_partition_verification_t ext_partition;
1344 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1345 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1346 ext_partition.page_size = 0; /* Not needed since already validated */
1347 ext_partition.image_addr = 0; /* Not needed since already validated */
1348 ext_partition.image_size = 0; /* Not needed since already validated */
1349 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1350 mdtp_fwlock_verify_lock(&ext_partition);
1351 }
1352#endif /* MDTP_SUPPORT */
1353
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001354#if USE_PCOM_SECBOOT
1355 set_tamper_flag(device.is_tampered);
1356#endif
1357
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001358#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001359 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001360 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001361 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001362#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001363 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001364#if ENABLE_VB_ATTEST
1365 mdelay(DELAY_WAIT);
1366 shutdown_device();
1367#else
lijuanga40d6302015-07-20 20:10:13 +08001368 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001369#endif
lijuanga40d6302015-07-20 20:10:13 +08001370#else
1371 dprintf(CRITICAL,
1372 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1373 mdelay(5000);
1374#endif
1375
1376 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001377 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001378#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001379 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001380 wait_for_users_action();
1381#else
1382 dprintf(CRITICAL,
1383 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1384 mdelay(5000);
1385#endif
1386 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001387 default:
lijuanga40d6302015-07-20 20:10:13 +08001388 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001389 }
1390#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001391#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301392 if(device.is_tampered)
1393 {
1394 write_device_info_mmc(&device);
1395 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301396 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301397 #endif
1398 #ifdef ASSERT_ON_TAMPER
1399 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1400 ASSERT(0);
1401 #endif
1402 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001403#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001404}
1405
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301406static bool check_format_bit()
1407{
1408 bool ret = false;
1409 int index;
1410 uint64_t offset;
1411 struct boot_selection_info *in = NULL;
1412 char *buf = NULL;
1413
1414 index = partition_get_index("bootselect");
1415 if (index == INVALID_PTN)
1416 {
1417 dprintf(INFO, "Unable to locate /bootselect partition\n");
1418 return ret;
1419 }
1420 offset = partition_get_offset(index);
1421 if(!offset)
1422 {
1423 dprintf(INFO, "partition /bootselect doesn't exist\n");
1424 return ret;
1425 }
1426 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301427 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301428 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301429 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301430 {
1431 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1432 free(buf);
1433 return ret;
1434 }
1435 in = (struct boot_selection_info *) buf;
1436 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1437 (in->version == BOOTSELECT_VERSION)) {
1438 if ((in->state_info & BOOTSELECT_FORMAT) &&
1439 !(in->state_info & BOOTSELECT_FACTORY))
1440 ret = true;
1441 } else {
1442 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1443 in->signature, in->version);
1444 ASSERT(0);
1445 }
1446 free(buf);
1447 return ret;
1448}
1449
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001450void boot_verifier_init()
1451{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001452 uint32_t boot_state;
1453 /* Check if device unlock */
1454 if(device.is_unlocked)
1455 {
1456 boot_verify_send_event(DEV_UNLOCK);
1457 boot_verify_print_state();
1458 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1459 return;
1460 }
1461 else
1462 {
1463 boot_verify_send_event(BOOT_INIT);
1464 }
1465
1466 /* Initialize keystore */
1467 boot_state = boot_verify_keystore_init();
1468 if(boot_state == YELLOW)
1469 {
1470 boot_verify_print_state();
1471 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1472 }
1473}
1474
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301475/* Function to return recovery appended dtbo buffer info */
1476void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1477{
1478 *dtbo_size = recovery_dtbo_size;
1479 *dtbo_buf = recovery_dtbo_buf;
1480 return;
1481}
1482
Shashank Mittal23b8f422010-04-16 19:27:21 -07001483int boot_linux_from_mmc(void)
1484{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301485 boot_img_hdr *hdr = (void*) buf;
1486 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001487 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001488 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001489 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001490 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001491
Shashank Mittalcd98d472011-08-02 14:29:24 -07001492 unsigned char *image_addr = 0;
1493 unsigned kernel_actual;
1494 unsigned ramdisk_actual;
1495 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001496 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301497 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001498
Matthew Qin271927e2015-03-31 22:07:07 -04001499 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301500 unsigned dtb_image_size = 0;
1501 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001502 unsigned int out_len = 0;
1503 unsigned int out_avai_len = 0;
1504 unsigned char *out_addr = NULL;
1505 uint32_t dtb_offset = 0;
1506 unsigned char *kernel_start_addr = NULL;
1507 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001508 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301509 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001510 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301511#if VERIFIED_BOOT_2
1512 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301513 void *dtbo_image_buf = NULL;
1514 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301515 void *vbmeta_image_buf = NULL;
1516 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301517#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301518 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001519#if DEVICE_TREE
1520 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001521 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001522 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001523 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001524 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001525 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001526#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001527 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301528 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001529
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301530 if (check_format_bit())
1531 boot_into_recovery = 1;
1532
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001533 if (!boot_into_recovery) {
1534 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1535 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1536 if (rcode <= 0) {
1537 boot_into_ffbm = false;
1538 if (rcode < 0)
1539 dprintf(CRITICAL,"failed to get ffbm cookie");
1540 } else
1541 boot_into_ffbm = true;
1542 } else
1543 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301544 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001545 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1546 dprintf(INFO, "Unified boot method!\n");
1547 hdr = uhdr;
1548 goto unified_boot;
1549 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301550
1551 /* For a/b recovery image code is on boot partition.
1552 If we support multislot, always use boot partition. */
1553 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301554 ((!partition_multislot_is_supported()) ||
1555 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301556 ptn_name = "recovery";
1557 else
1558 ptn_name = "boot";
1559
1560 index = partition_get_index(ptn_name);
1561 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301562 image_size = partition_get_size(index);
1563 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301564 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1565 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001566 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301567
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001568 /* Set Lun for boot & recovery partitions */
1569 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001570
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301571 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001572 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1573 return -1;
1574 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001575
1576 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001577 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301578 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001579 }
1580
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001581 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301582
1583 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1584 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1585 return -1;
1586 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001587 page_size = hdr->page_size;
1588 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001589 }
1590
Matthew Qin49e51fa2015-02-09 10:40:45 +08001591 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1592 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301593 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001594
Matthew Qin49e51fa2015-02-09 10:40:45 +08001595 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301596#if VERIFIED_BOOT_2
1597 /* Create hole in start of image for VB salt to copy */
1598 image_addr += SALT_BUFF_OFFSET;
1599#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301600 memcpy(image_addr, (void *)buf, page_size);
1601
1602 /* ensure commandline is terminated */
1603 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001604
1605#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301606#ifndef OSVERSION_IN_BOOTIMAGE
1607 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301608#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301609 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301610#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301611
Parth Dixit4097b622016-03-15 14:42:27 +05301612 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301613 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + (uint64_t)dt_actual + page_size)) {
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301614 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1615 return -1;
1616 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301617 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001618#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301619 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual + (uint64_t)second_actual + page_size)) {
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301620 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1621 return -1;
1622 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301623 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001624#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301625 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001626
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301627#ifdef OSVERSION_IN_BOOTIMAGE
1628 /* If header version is ONE and booting into recovery,
1629 dtbo is appended with recovery image.
1630 Doing following:
1631 * Validating the recovery offset and size.
1632 * Extracting recovery dtbo to be used as dtbo.
1633 */
1634 if (boot_into_recovery &&
1635 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1636 {
1637 struct boot_img_hdr_v1 *hdr1 =
1638 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301639 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301640
Mayank Grovera1a83c72018-09-24 11:23:15 +05301641 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301642 if ((hdr1->header_size !=
1643 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1644 {
1645 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1646 return -1;
1647 }
1648
Mayank Grovera1a83c72018-09-24 11:23:15 +05301649 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1650 {
1651 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301652 MAX_SUPPORTED_DTBO_IMG_BUF);
1653 return -1;
1654 }
1655
Mayank Grovera1a83c72018-09-24 11:23:15 +05301656 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301657 {
1658 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1659 return -1;
1660 }
1661
Mayank Grovera1a83c72018-09-24 11:23:15 +05301662 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1663 dprintf(CRITICAL,
1664 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
1665 return -1;
1666 }
1667
1668 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301669 {
1670 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
1671 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
1672 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
1673 return -1;
1674 }
1675
1676 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05301677 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301678 imagesize_actual += recovery_dtbo_size;
1679
1680 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1681 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
1682 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
1683
1684 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301685
1686 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
1687 struct boot_img_hdr_v1 *hdr1 =
1688 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
1689 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
1690 (image_addr + sizeof(boot_img_hdr) +
1691 BOOT_IMAGE_HEADER_V2_OFFSET);
1692 unsigned int recovery_dtbo_actual = 0;
1693
1694 imagesize_actual += ROUND_TO_PAGE(hdr1->recovery_dtbo_size,
1695 page_mask);
1696
1697 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
1698
1699
1700 dtb_image_offset = page_size + patched_kernel_hdr_size +
1701 kernel_actual + ramdisk_actual + second_actual +
1702 recovery_dtbo_actual;
1703
1704 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1705 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
1706 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301707#endif
1708
Parth Dixit1375d9e2019-07-08 20:56:13 +05301709
Saranya Chidurab1aaf232018-11-19 15:47:50 +05301710 /* Validate the boot/recovery image size is within the bounds of partition size */
1711 if (imagesize_actual > image_size) {
1712 dprintf(CRITICAL, "Image size is greater than partition size.\n");
1713 return -1;
1714 }
1715
Matthew Qin49e51fa2015-02-09 10:40:45 +08001716#if VERIFIED_BOOT
1717 boot_verifier_init();
1718#endif
1719
Mayank Grover1ceaee22019-04-01 17:54:32 +05301720#if VERIFIED_BOOT_2
1721 /* read full partition if device is unlocked */
1722 if (device.is_unlocked)
1723 imagesize_actual = image_size;
1724#endif
1725
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301726 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001727 {
1728 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1729 return -1;
1730 }
1731
Matthew Qinbb7923d2015-02-09 10:56:09 +08001732 /*
1733 * Update loading flow of bootimage to support compressed/uncompressed
1734 * bootimage on both 64bit and 32bit platform.
1735 * 1. Load bootimage from emmc partition onto DDR.
1736 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1737 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1738 * platform accordingly.
1739 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1740 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301741 if (partition_multislot_is_supported())
1742 {
1743 current_active_slot = partition_find_active_slot();
1744 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1745 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1746 }
1747 else
1748 {
1749 dprintf(INFO, "Loading (%s) image (%d): start\n",
1750 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1751 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001752 bs_set_timestamp(BS_KERNEL_LOAD_START);
1753
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301754 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1755 {
1756 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1757 return -1;
1758 }
Kishor PK9e91b592017-05-24 12:14:55 +05301759 offset = page_size;
1760 /* Read image without signature and header*/
1761 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001762 {
1763 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1764 return -1;
1765 }
1766
Mayank Grover351a75e2017-05-30 20:06:08 +05301767 if (partition_multislot_is_supported())
1768 {
1769 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1770 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1771 }
1772 else
1773 {
1774 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001775 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1776
Mayank Grover351a75e2017-05-30 20:06:08 +05301777 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001778 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1779
1780 /* Authenticate Kernel */
1781 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1782 (int) target_use_signed_kernel(),
1783 device.is_unlocked,
1784 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301785#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05301786 /* if device is unlocked skip reading signature, as full partition is read */
1787 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05301788 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05301789 offset = imagesize_actual;
1790 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1791 {
1792 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1793 return -1;
1794 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001795
Mayank Grover1ceaee22019-04-01 17:54:32 +05301796 /* Read signature */
1797 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1798 {
1799 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1800 return -1;
1801 }
Monika Singh292b3e92018-03-17 22:40:23 +05301802 }
1803
Mayank Grover027a9412018-09-04 15:12:05 +05301804 /* load and validate dtbo partition */
1805 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
1806
Mayank Grover9df84c02018-08-30 15:46:35 +05301807 /* load vbmeta partition */
1808 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
1809
Monika Singh292b3e92018-03-17 22:40:23 +05301810 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05301811
1812 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05301813 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05301814 info.images[IMG_BOOT].imgsize = imagesize_actual;
1815 info.images[IMG_BOOT].name = ptn_name;
1816 ++info.num_loaded_images;
1817
1818 /* Pass loaded dtbo image */
1819 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05301820 info.images[IMG_DTBO].image_buffer =
1821 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05301822 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
1823 info.images[IMG_DTBO].name = "dtbo";
1824 ++info.num_loaded_images;
1825 }
1826
Mayank Grover9df84c02018-08-30 15:46:35 +05301827 /* Pass loaded vbmeta image */
1828 if (vbmeta_image_buf != NULL) {
1829 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
1830 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
1831 info.images[IMG_VBMETA].name = "vbmeta";
1832 ++info.num_loaded_images;
1833 }
1834
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301835 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05301836 info.multi_slot_boot = partition_multislot_is_supported();
1837 info.bootreason_alarm = boot_reason_alarm;
1838 info.bootinto_recovery = boot_into_recovery;
1839 status = load_image_and_auth(&info);
1840 if(status)
1841 return -1;
1842
1843 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05301844
1845 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05301846 if (vbmeta_image_buf != NULL) {
1847 free(vbmeta_image_buf);
1848 --info.num_loaded_images;
1849 }
Monika Singh292b3e92018-03-17 22:40:23 +05301850#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001851 /* Change the condition a little bit to include the test framework support.
1852 * We would never reach this point if device is in fastboot mode, even if we did
1853 * that means we are in test mode, so execute kernel authentication part for the
1854 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301855 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001856 {
1857 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301858 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001859 {
1860 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1861 return -1;
1862 }
1863
1864 /* Read signature */
1865 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1866 {
1867 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1868 return -1;
1869 }
1870
1871 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001872 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301873 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001874 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001875 } else {
1876 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1877 #ifdef TZ_SAVE_KERNEL_HASH
1878 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1879 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001880
1881#ifdef MDTP_SUPPORT
1882 {
1883 /* Verify MDTP lock.
1884 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1885 * since verification was skipped in aboot. The signature is not part of the loaded image.
1886 */
1887 mdtp_ext_partition_verification_t ext_partition;
1888 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1889 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1890 ext_partition.page_size = page_size;
1891 ext_partition.image_addr = (uint32)image_addr;
1892 ext_partition.image_size = imagesize_actual;
1893 ext_partition.sig_avail = FALSE;
1894 mdtp_fwlock_verify_lock(&ext_partition);
1895 }
1896#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001897 }
Monika Singh292b3e92018-03-17 22:40:23 +05301898#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001899
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001900#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001901 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001902 {
1903#if FBCON_DISPLAY_MSG
1904 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1905 wait_for_users_action();
1906#else
1907 dprintf(CRITICAL,
1908 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1909 mdelay(5000);
1910#endif
1911 }
1912#endif
1913
1914#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301915 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301916 {
1917 /* set boot and system versions. */
1918 set_os_version((unsigned char *)image_addr);
1919 // send root of trust
1920 if(!send_rot_command((uint32_t)device.is_unlocked))
1921 ASSERT(0);
1922 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301923#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001924 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001925 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1926 * If not, continue booting.
1927 */
1928 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1929 {
1930 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1931 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05301932#if VERIFIED_BOOT_2
1933 if (dtbo_image_sz)
1934 out_avai_len -= DTBO_IMG_BUF;
1935#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04001936 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001937 rc = decompress((unsigned char *)(image_addr + page_size),
1938 hdr->kernel_size, out_addr, out_avai_len,
1939 &dtb_offset, &out_len);
1940 if (rc)
1941 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001942 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001943 ASSERT(0);
1944 }
1945
Matthew Qin0b15b322015-05-19 05:20:54 -04001946 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001947 kptr = (struct kernel64_hdr *)out_addr;
1948 kernel_start_addr = out_addr;
1949 kernel_size = out_len;
1950 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001951 dprintf(INFO, "Uncpmpressed kernel in use\n");
1952 if (!strncmp((char*)(image_addr + page_size),
1953 PATCHED_KERNEL_MAGIC,
1954 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1955 dprintf(INFO, "Patched kernel detected\n");
1956 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1957 PATCHED_KERNEL_HEADER_SIZE);
1958 //The size of the kernel is stored at start of kernel image + 16
1959 //The dtb would start just after the kernel
1960 dtb_offset = *((uint32_t*)((unsigned char*)
1961 (image_addr + page_size +
1962 sizeof(PATCHED_KERNEL_MAGIC) -
1963 1)));
1964 //The actual kernel starts after the 20 byte header.
1965 kernel_start_addr = (unsigned char*)(image_addr +
1966 page_size + PATCHED_KERNEL_HEADER_SIZE);
1967 kernel_size = hdr->kernel_size;
1968 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1969 } else {
1970 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1971 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1972 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1973 kernel_size = hdr->kernel_size;
1974 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001975 }
1976
1977 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001978 * Update the kernel/ramdisk/tags address if the boot image header
1979 * has default values, these default values come from mkbootimg when
1980 * the boot image is flashed using fastboot flash:raw
1981 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001982 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001983
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001984 /* Get virtual addresses since the hdr saves physical addresses. */
1985 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1986 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1987 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001988
Matthew Qinbb7923d2015-02-09 10:56:09 +08001989 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001990 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001991 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301992 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1993 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1994 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001995 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301996 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001997 return -1;
1998 }
1999
2000#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302001 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2002 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002003 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302004 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002005 return -1;
2006 }
2007#endif
2008
Matthew Qin49e51fa2015-02-09 10:40:45 +08002009 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002010 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002011 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002012
Matthew Qin49e51fa2015-02-09 10:40:45 +08002013 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302014 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002015 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2016 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002017
Matthew Qin49e51fa2015-02-09 10:40:45 +08002018 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2019 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2020 return -1;
2021 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002022
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302023 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2024 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302025 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302026 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2027 return -1;
2028 }
2029
Matthew Qin49e51fa2015-02-09 10:40:45 +08002030 /* Find index of device tree within device tree table */
2031 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2032 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2033 return -1;
2034 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002035
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302036 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2037 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2038 return -1;
2039 }
2040
2041 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302042 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302043 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2044 return -1;
2045 }
2046
Matthew Qin271927e2015-03-31 22:07:07 -04002047 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2048 {
2049 unsigned int compressed_size = 0;
2050 out_addr += out_len;
2051 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002052 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002053 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2054 dt_entry.size, out_addr, out_avai_len,
2055 &compressed_size, &dtb_size);
2056 if (rc)
2057 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002058 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002059 ASSERT(0);
2060 }
2061
Matthew Qin0b15b322015-05-19 05:20:54 -04002062 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002063 best_match_dt_addr = out_addr;
2064 } else {
2065 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2066 dtb_size = dt_entry.size;
2067 }
2068
Matthew Qin49e51fa2015-02-09 10:40:45 +08002069 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302070 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2071 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002072 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302073 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002074 return -1;
2075 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002076
Matthew Qin271927e2015-03-31 22:07:07 -04002077 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002078 } else {
2079 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302080 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2081 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002082 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302083 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002084 return -1;
2085 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002086 /*
2087 * If appended dev tree is found, update the atags with
2088 * memory address to the DTB appended location on RAM.
2089 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302090 *
2091 * Make sure everything from scratch address is read before next step!
2092 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002093 */
2094 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302095 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2096
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302097#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302098 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2099
2100 image_buf = (void*)(image_addr);
2101 dtb_offset = dtb_image_offset;
2102 dtb_image_size = imagesize_actual;
2103 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302104#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302105
2106 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002107 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002108 if (!dtb) {
2109 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002110 return -1;
2111 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002112 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002113 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002114
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002115 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2116 target_load_ssd_keystore();
2117
Shashank Mittal23b8f422010-04-16 19:27:21 -07002118unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002119
Dima Zavin77e41f32013-03-06 16:10:43 -08002120 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002121 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002122 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2123
2124 return 0;
2125}
2126
Dima Zavin214cc642009-01-26 11:16:21 -08002127int boot_linux_from_flash(void)
2128{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302129 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002130 struct ptentry *ptn;
2131 struct ptable *ptable;
2132 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002133
Shashank Mittalcd98d472011-08-02 14:29:24 -07002134 unsigned char *image_addr = 0;
2135 unsigned kernel_actual;
2136 unsigned ramdisk_actual;
2137 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302138 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002139
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002140#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302141 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002142 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302143 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002144 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302145 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302146 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302147 unsigned int dtb_size = 0;
2148 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002149#endif
2150
David Ng183a7422009-12-07 14:55:21 -08002151 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302152 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002153 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2154 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2155 return -1;
2156 }
2157 goto continue_boot;
2158 }
2159
Dima Zavin214cc642009-01-26 11:16:21 -08002160 ptable = flash_get_ptable();
2161 if (ptable == NULL) {
2162 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2163 return -1;
2164 }
2165
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002166 if(!boot_into_recovery)
2167 {
2168 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002169
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002170 if (ptn == NULL) {
2171 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2172 return -1;
2173 }
2174 }
2175 else
2176 {
2177 ptn = ptable_find(ptable, "recovery");
2178 if (ptn == NULL) {
2179 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2180 return -1;
2181 }
Dima Zavin214cc642009-01-26 11:16:21 -08002182 }
2183
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302184 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002185 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002186 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2187 return -1;
2188 }
Dima Zavin214cc642009-01-26 11:16:21 -08002189
2190 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002191 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002192 return -1;
2193 }
2194
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002195 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002196 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 -08002197 return -1;
2198 }
2199
Kishor PK9e91b592017-05-24 12:14:55 +05302200 image_addr = (unsigned char *)target_get_scratch_address();
2201 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302202
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002203 /*
2204 * Update the kernel/ramdisk/tags address if the boot image header
2205 * has default values, these default values come from mkbootimg when
2206 * the boot image is flashed using fastboot flash:raw
2207 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002208 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002209
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002210 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002211 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2212 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2213 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2214
2215 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2216 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302217 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002218
Kishor PK9e91b592017-05-24 12:14:55 +05302219 /* ensure commandline is terminated */
2220 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2221
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002222 /* Check if the addresses in the header are valid. */
2223 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302224 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2225 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2226 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002227 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302228 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002229 return -1;
2230 }
2231
2232#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302233 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302234 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2235 return -1;
2236 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302237 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302238
Mayank Grovere559cec2017-10-17 15:12:03 +05302239 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2240 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302241 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302242 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302243 return -1;
2244 }
2245#else
2246
2247#ifndef OSVERSION_IN_BOOTIMAGE
2248 dt_size = hdr->dt_size;
2249#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302250 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302251 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + (uint64_t)dt_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302252 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2253 return -1;
2254 }
2255
Kishor PKd8ddcad2017-07-27 13:53:57 +05302256 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302257
Mayank Grovere559cec2017-10-17 15:12:03 +05302258 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2259 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302260 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302261 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302262 return -1;
2263 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002264#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002265
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302266 /* Read full boot.img from flash */
2267 dprintf(INFO, "Loading (%s) image (%d): start\n",
2268 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2269 bs_set_timestamp(BS_KERNEL_LOAD_START);
2270
2271 if (UINT_MAX - page_size < imagesize_actual)
2272 {
2273 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2274 return -1;
2275 }
2276
2277 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2278 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2279 {
2280 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2281 return -1;
2282 }
2283
2284 offset = page_size;
2285 /* Read image without signature and header */
2286 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2287 {
2288 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2289 return -1;
2290 }
2291
2292 dprintf(INFO, "Loading (%s) image (%d): done\n",
2293 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2294 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2295
Shashank Mittalcd98d472011-08-02 14:29:24 -07002296 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002297 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002298 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002299 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302300
Shashank Mittalcd98d472011-08-02 14:29:24 -07002301 /* Read signature */
2302 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2303 {
2304 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002305 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002306 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002307
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302308 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302309 }
2310 offset = page_size;
2311 if(hdr->second_size != 0) {
2312 if (UINT_MAX - offset < second_actual)
2313 {
2314 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2315 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302316 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302317 offset += second_actual;
2318 /* Second image loading not implemented. */
2319 ASSERT(0);
2320 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302321 /* Move kernel and ramdisk to correct address */
2322 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2323 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2324
2325#if DEVICE_TREE
2326 if(dt_size != 0) {
2327
2328 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2329
2330 table = (struct dt_table*) dt_table_offset;
2331
2332 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2333 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2334 return -1;
2335 }
2336
2337 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2338 goes beyound hdr->dt_size*/
2339 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2340 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2341 return -1;
2342 }
2343
2344 /* Find index of device tree within device tree table */
2345 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2346 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2347 return -1;
2348 }
2349
2350 /* Validate and Read device device tree in the "tags_add */
2351 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2352 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2353 {
2354 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2355 return -1;
2356 }
2357
2358 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2359 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2360 return -1;
2361 }
2362
2363 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2364 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2365 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2366 return -1;
2367 }
2368
2369 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2370 dtb_size = dt_entry.size;
2371 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302372
2373 } else {
2374 /* Validate the tags_addr */
2375 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2376 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2377 {
2378 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2379 return -1;
2380 }
2381 /*
2382 * If appended dev tree is found, update the atags with
2383 * memory address to the DTB appended location on RAM.
2384 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302385 *
2386 * Make sure everything from scratch address is read before next step!
2387 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302388 */
2389 void *dtb = NULL;
2390 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2391 if (!dtb) {
2392 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2393 return -1;
2394 }
2395 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302396#endif
2397 if(target_use_signed_kernel() && (!device.is_unlocked))
2398 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002399 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002400 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002401 {
2402 write_device_info_flash(&device);
2403 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302404#if USE_PCOM_SECBOOT
2405 set_tamper_flag(device.is_tampered);
2406#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002407 }
David Ng183a7422009-12-07 14:55:21 -08002408continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002409
Dima Zavin214cc642009-01-26 11:16:21 -08002410 /* TODO: create/pass atags to kernel */
2411
Ajay Dudanie28a6072011-07-01 13:59:46 -07002412 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002413 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002414 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2415
2416 return 0;
2417}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002418
Shashank Mittal162244e2011-08-08 19:01:25 -07002419void write_device_info_mmc(device_info *dev)
2420{
Shashank Mittal162244e2011-08-08 19:01:25 -07002421 unsigned long long ptn = 0;
2422 unsigned long long size;
2423 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002424 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002425 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302426 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002427
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002428 if (devinfo_present)
2429 index = partition_get_index("devinfo");
2430 else
2431 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002432
Shashank Mittal162244e2011-08-08 19:01:25 -07002433 ptn = partition_get_offset(index);
2434 if(ptn == 0)
2435 {
2436 return;
2437 }
2438
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002439 lun = partition_get_lun(index);
2440 mmc_set_lun(lun);
2441
Shashank Mittal162244e2011-08-08 19:01:25 -07002442 size = partition_get_size(index);
2443
Mayank Groverddd348d2018-01-23 14:07:09 +05302444 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2445 mmc_blocksize_mask);
2446 if (device_info_sz == UINT_MAX)
2447 {
2448 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2449 return;
2450 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002451
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002452 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302453 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002454 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302455 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002456 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002457 {
2458 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002459 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002460 }
2461}
2462
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002463void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002464{
Shashank Mittal162244e2011-08-08 19:01:25 -07002465 unsigned long long ptn = 0;
2466 unsigned long long size;
2467 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002468 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302469 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002470
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002471 if ((index = partition_get_index("devinfo")) < 0)
2472 {
2473 devinfo_present = false;
2474 index = partition_get_index("aboot");
2475 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002476
Shashank Mittal162244e2011-08-08 19:01:25 -07002477 ptn = partition_get_offset(index);
2478 if(ptn == 0)
2479 {
2480 return;
2481 }
2482
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002483 mmc_set_lun(partition_get_lun(index));
2484
Shashank Mittal162244e2011-08-08 19:01:25 -07002485 size = partition_get_size(index);
2486
Mayank Groverddd348d2018-01-23 14:07:09 +05302487 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2488 mmc_blocksize_mask);
2489 if (device_info_sz == UINT_MAX)
2490 {
2491 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2492 return;
2493 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002494
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002495 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302496 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002497 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302498 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002499 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002500 {
2501 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002502 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002503 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002504}
2505
2506void write_device_info_flash(device_info *dev)
2507{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002508 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002509 struct ptentry *ptn;
2510 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002511 if(info == NULL)
2512 {
2513 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2514 ASSERT(0);
2515 }
2516 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002517 ptable = flash_get_ptable();
2518 if (ptable == NULL)
2519 {
2520 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2521 return;
2522 }
2523
2524 ptn = ptable_find(ptable, "devinfo");
2525 if (ptn == NULL)
2526 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002527 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002528 return;
2529 }
2530
Mayank Groverdedbc892017-10-24 13:41:34 +05302531 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002532 memcpy(info, dev, sizeof(device_info));
2533
2534 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2535 {
2536 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2537 return;
2538 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002539 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002540}
2541
vijay kumarc65876c2015-04-24 13:29:16 +05302542static int read_allow_oem_unlock(device_info *dev)
2543{
vijay kumarc65876c2015-04-24 13:29:16 +05302544 unsigned offset;
2545 int index;
2546 unsigned long long ptn;
2547 unsigned long long ptn_size;
2548 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002549 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302550
vijay kumarca2e6812015-07-08 20:28:25 +05302551 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302552 if (index == INVALID_PTN)
2553 {
vijay kumarca2e6812015-07-08 20:28:25 +05302554 index = partition_get_index(frp_ptns[1]);
2555 if (index == INVALID_PTN)
2556 {
2557 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2558 return -1;
2559 }
vijay kumarc65876c2015-04-24 13:29:16 +05302560 }
2561
2562 ptn = partition_get_offset(index);
2563 ptn_size = partition_get_size(index);
2564 offset = ptn_size - blocksize;
2565
Mayank Grover48860402016-11-29 12:34:53 +05302566 /* Set Lun for partition */
2567 mmc_set_lun(partition_get_lun(index));
2568
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002569 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302570 {
2571 dprintf(CRITICAL, "Reading MMC failed\n");
2572 return -1;
2573 }
2574
2575 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2576 is_allow_unlock = buf[blocksize-1] & 0x01;
2577 return 0;
2578}
2579
2580static int write_allow_oem_unlock(bool allow_unlock)
2581{
vijay kumarc65876c2015-04-24 13:29:16 +05302582 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302583 int index;
2584 unsigned long long ptn;
2585 unsigned long long ptn_size;
2586 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002587 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302588
vijay kumarca2e6812015-07-08 20:28:25 +05302589 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302590 if (index == INVALID_PTN)
2591 {
vijay kumarca2e6812015-07-08 20:28:25 +05302592 index = partition_get_index(frp_ptns[1]);
2593 if (index == INVALID_PTN)
2594 {
2595 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2596 return -1;
2597 }
vijay kumarc65876c2015-04-24 13:29:16 +05302598 }
2599
2600 ptn = partition_get_offset(index);
2601 ptn_size = partition_get_size(index);
2602 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302603 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302604
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002605 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302606 {
2607 dprintf(CRITICAL, "Reading MMC failed\n");
2608 return -1;
2609 }
2610
2611 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2612 buf[blocksize-1] = allow_unlock;
2613 if (mmc_write(ptn + offset, blocksize, buf))
2614 {
2615 dprintf(CRITICAL, "Writing MMC failed\n");
2616 return -1;
2617 }
2618
2619 return 0;
2620}
2621
Shashank Mittal162244e2011-08-08 19:01:25 -07002622void read_device_info_flash(device_info *dev)
2623{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002624 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002625 struct ptentry *ptn;
2626 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002627 if(info == NULL)
2628 {
2629 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2630 ASSERT(0);
2631 }
2632 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002633 ptable = flash_get_ptable();
2634 if (ptable == NULL)
2635 {
2636 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2637 return;
2638 }
2639
2640 ptn = ptable_find(ptable, "devinfo");
2641 if (ptn == NULL)
2642 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002643 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002644 return;
2645 }
2646
2647 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2648 {
2649 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2650 return;
2651 }
2652
2653 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2654 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002655 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2656 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002657 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002658 write_device_info_flash(info);
2659 }
2660 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002661 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002662}
2663
2664void write_device_info(device_info *dev)
2665{
2666 if(target_is_emmc_boot())
2667 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002668 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2669 if(info == NULL)
2670 {
2671 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2672 ASSERT(0);
2673 }
2674 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002675 memcpy(info, dev, sizeof(struct device_info));
2676
2677#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302678 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302679 is_secure_boot_enable()) {
2680 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2681 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002682 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002683 else
2684 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002685#else
2686 write_device_info_mmc(info);
2687#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002688 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002689 }
2690 else
2691 {
2692 write_device_info_flash(dev);
2693 }
2694}
2695
Monika Singh94316462018-03-15 18:39:01 +05302696int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2697{
2698 if (!devinfo_present) {
2699 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2700 return -EINVAL;
2701 }
2702 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2703 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2704 __func__, loc, ARRAY_SIZE(device.rollback_index));
2705 ASSERT(0);
2706 }
2707
2708 *roll_back_index = device.rollback_index[loc];
2709 return 0;
2710}
2711
2712int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2713{
2714 if (!devinfo_present) {
2715 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2716 return -EINVAL;
2717 }
2718 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2719 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2720 __func__, loc, ARRAY_SIZE(device.rollback_index));
2721 ASSERT(0);
2722 }
2723
2724 device.rollback_index[loc] = roll_back_index;
2725 write_device_info(&device);
2726 return 0;
2727}
2728
Monika Singhb0fad822018-03-15 18:50:55 +05302729int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2730{
2731 if (!devinfo_present) {
2732 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2733 return -EINVAL;
2734 }
2735
2736 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2737 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2738 return -ENODEV;
2739 }
2740
2741 memcpy(device.user_public_key, user_key, user_key_size);
2742 device.user_public_key_length = user_key_size;
2743 write_device_info(&device);
2744 return 0;
2745}
2746
2747int erase_userkey()
2748{
2749 if (!devinfo_present) {
2750 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2751 return -EINVAL;
2752 }
2753 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2754 device.user_public_key_length = 0;
2755 write_device_info(&device);
2756 return 0;
2757}
2758
2759int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2760{
2761 if (!devinfo_present) {
2762 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2763 return -EINVAL;
2764 }
2765 *user_key = device.user_public_key;
2766 *user_key_size = device.user_public_key_length;
2767 return 0;
2768}
2769
Shashank Mittal162244e2011-08-08 19:01:25 -07002770void read_device_info(device_info *dev)
2771{
2772 if(target_is_emmc_boot())
2773 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002774 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2775 if(info == NULL)
2776 {
2777 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2778 ASSERT(0);
2779 }
2780 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002781
2782#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302783 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302784 is_secure_boot_enable()) {
2785 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2786 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002787 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002788 else
2789 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002790#else
2791 read_device_info_mmc(info);
2792#endif
2793
2794 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2795 {
2796 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002797 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002798 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302799#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302800 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302801 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302802#endif
lijuang511a2b52015-08-14 20:50:51 +08002803 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002804 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302805#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302806 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302807 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302808#endif
lijuang511a2b52015-08-14 20:50:51 +08002809 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002810 info->is_tampered = 0;
2811 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302812#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302813 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302814 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302815#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002816 write_device_info(info);
2817 }
2818 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002819 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002820 }
2821 else
2822 {
2823 read_device_info_flash(dev);
2824 }
2825}
2826
2827void reset_device_info()
2828{
2829 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002830 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002831 write_device_info(&device);
2832}
2833
2834void set_device_root()
2835{
2836 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002837 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002838 write_device_info(&device);
2839}
2840
lijuang4ece1e72015-08-14 21:02:36 +08002841/* set device unlock value
2842 * Must check FRP before call this function
2843 * Need to wipe data when unlock status changed
2844 * type 0: oem unlock
2845 * type 1: unlock critical
2846 * status 0: unlock as false
2847 * status 1: lock as true
2848 */
2849void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002850{
lijuang4ece1e72015-08-14 21:02:36 +08002851 if (type == UNLOCK)
2852 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302853#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302854 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302855 type == UNLOCK_CRITICAL)
2856 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302857#endif
lijuang4ece1e72015-08-14 21:02:36 +08002858 write_device_info(&device);
2859}
2860
2861static void set_device_unlock(int type, bool status)
2862{
2863 int is_unlocked = -1;
2864 char response[MAX_RSP_SIZE];
2865
2866 /* check device unlock status if it is as expected */
2867 if (type == UNLOCK)
2868 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302869#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302870 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302871 type == UNLOCK_CRITICAL)
2872 {
2873 is_unlocked = device.is_unlock_critical;
2874 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302875#endif
lijuang4ece1e72015-08-14 21:02:36 +08002876 if (is_unlocked == status) {
2877 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2878 fastboot_info(response);
2879 fastboot_okay("");
2880 return;
2881 }
2882
2883 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002884 if (status && !is_allow_unlock) {
2885 fastboot_fail("oem unlock is not allowed");
2886 return;
2887 }
lijuang21f12f52015-08-22 16:22:19 +08002888
lijuang4ece1e72015-08-14 21:02:36 +08002889#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002890 display_unlock_menu(type, status);
2891 fastboot_okay("");
2892 return;
lijuang4ece1e72015-08-14 21:02:36 +08002893#else
lijuang07c5d6e2018-04-23 18:32:13 +08002894 if (status && type == UNLOCK) {
2895 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2896 return;
lijuang21f12f52015-08-22 16:22:19 +08002897 }
lijuang07c5d6e2018-04-23 18:32:13 +08002898#endif
lijuang4ece1e72015-08-14 21:02:36 +08002899
2900 set_device_unlock_value(type, status);
2901
2902 /* wipe data */
2903 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302904 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002905 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2906 write_misc(0, &msg, sizeof(msg));
2907
2908 fastboot_okay("");
2909 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002910}
2911
lijuang511a2b52015-08-14 20:50:51 +08002912static bool critical_flash_allowed(const char * entry)
2913{
2914 uint32_t i = 0;
2915 if (entry == NULL)
2916 return false;
2917
2918 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2919 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2920 return true;
2921 }
2922 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002923}
2924
2925#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002926int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2927{
2928 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002929 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302930 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002931 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302932 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002933 unsigned int compressed_size = 0;
2934 unsigned int dtb_size = 0;
2935 unsigned int out_avai_len = 0;
2936 unsigned char *out_addr = NULL;
2937 unsigned char *best_match_dt_addr = NULL;
2938 int rc;
2939
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302940 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07002941
Parth Dixit4097b622016-03-15 14:42:27 +05302942#ifndef OSVERSION_IN_BOOTIMAGE
2943 dt_size = hdr->dt_size;
2944#endif
2945
2946 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002947 /* add kernel offset */
2948 dt_image_offset += page_size;
2949 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2950 dt_image_offset += n;
2951
2952 /* add ramdisk offset */
2953 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2954 dt_image_offset += n;
2955
2956 /* add second offset */
2957 if(hdr->second_size != 0) {
2958 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2959 dt_image_offset += n;
2960 }
2961
2962 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002963 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002964
Deepa Dinamani19648b42013-09-05 17:05:55 -07002965 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002966 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2967 return -1;
2968 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302969
2970 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2971 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302972 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302973 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2974 return -1;
2975 }
2976
Joel Kingaa335dc2013-06-03 16:11:08 -07002977 /* Find index of device tree within device tree table */
2978 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002979 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2980 return -1;
2981 }
2982
Matthew Qin271927e2015-03-31 22:07:07 -04002983 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2984 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2985 {
2986 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2987 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002988 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002989 rc = decompress(best_match_dt_addr,
2990 dt_entry.size, out_addr, out_avai_len,
2991 &compressed_size, &dtb_size);
2992 if (rc)
2993 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002994 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002995 ASSERT(0);
2996 }
2997
Matthew Qin0b15b322015-05-19 05:20:54 -04002998 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002999 best_match_dt_addr = out_addr;
3000 } else {
3001 dtb_size = dt_entry.size;
3002 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003003 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303004 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3005 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003006 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303007 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003008 return -1;
3009 }
3010
Amol Jadicb524072012-08-09 16:40:18 -07003011 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003012 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003013 } else
3014 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003015
3016 /* Everything looks fine. Return success. */
3017 return 0;
3018}
3019#endif
3020
Brian Swetland9c4c0752009-01-25 16:23:50 -08003021void cmd_boot(const char *arg, void *data, unsigned sz)
3022{
3023 unsigned kernel_actual;
3024 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303025 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003026 uint32_t image_actual;
3027 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303028 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003029 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003030 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003031 int ret = 0;
3032 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003033 unsigned int out_len = 0;
3034 unsigned int out_avai_len = 0;
3035 unsigned char *out_addr = NULL;
3036 uint32_t dtb_offset = 0;
3037 unsigned char *kernel_start_addr = NULL;
3038 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003039 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303040#if VERIFIED_BOOT_2
3041 void *dtbo_image_buf = NULL;
3042 uint32_t dtbo_image_sz = 0;
3043 void *vbmeta_image_buf = NULL;
3044 uint32_t vbmeta_image_sz = 0;
3045#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303046#if !VERIFIED_BOOT_2
3047 uint32_t sig_actual = 0;
3048 uint32_t sig_size = 0;
3049#ifdef MDTP_SUPPORT
3050 static bool is_mdtp_activated = 0;
3051#endif /* MDTP_SUPPORT */
3052#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003053
lijuang2008ff22016-03-07 17:56:27 +08003054#if FBCON_DISPLAY_MSG
3055 /* Exit keys' detection thread firstly */
3056 exit_menu_keys_detection();
3057#endif
3058
Monika Singh292b3e92018-03-17 22:40:23 +05303059#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003060 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003061 {
3062 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003063 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003064 }
3065#endif
3066
Brian Swetland9c4c0752009-01-25 16:23:50 -08003067 if (sz < sizeof(hdr)) {
3068 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003069 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003070 }
3071
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303072 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003073
3074 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003075 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003076
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003077 if(target_is_emmc_boot() && hdr->page_size) {
3078 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003079 page_mask = page_size - 1;
3080 }
3081
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003082 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3083 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303084 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003085#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303086#ifndef OSVERSION_IN_BOOTIMAGE
3087 dt_size = hdr->dt_size;
3088#endif
3089 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003090#endif
3091
3092 image_actual = ADD_OF(page_size, kernel_actual);
3093 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303094 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003095 image_actual = ADD_OF(image_actual, dt_actual);
3096
Kishor PK5134b332017-05-09 17:50:08 +05303097 /* Checking to prevent oob access in read_der_message_length */
3098 if (image_actual > sz) {
3099 fastboot_fail("bootimage header fields are invalid");
3100 goto boot_failed;
3101 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303102
Monika Singh292b3e92018-03-17 22:40:23 +05303103#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303104 /* Pass size of boot partition, as imgsize, to avoid
3105 read fewer bytes error */
3106 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303107
3108 /* load and validate dtbo partition */
3109 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3110
3111 /* load vbmeta partition */
3112 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3113
Monika Singh292b3e92018-03-17 22:40:23 +05303114 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303115
3116 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3117 info.images[IMG_BOOT].imgsize = image_actual;
3118 info.images[IMG_BOOT].name = "boot";
3119 ++info.num_loaded_images;
3120
3121 /* Pass loaded dtbo image */
3122 if (dtbo_image_buf != NULL) {
3123 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3124 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3125 info.images[IMG_DTBO].name = "dtbo";
3126 ++info.num_loaded_images;
3127 }
3128
3129 /* Pass loaded vbmeta image */
3130 if (vbmeta_image_buf != NULL) {
3131 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3132 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3133 info.images[IMG_VBMETA].name = "vbmeta";
3134 ++info.num_loaded_images;
3135 }
3136
Monika Singh292b3e92018-03-17 22:40:23 +05303137 info.multi_slot_boot = partition_multislot_is_supported();
3138 if (load_image_and_auth(&info))
3139 goto boot_failed;
3140 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303141
3142 /* Free the buffer allocated to vbmeta post verification */
3143 if (vbmeta_image_buf != NULL) {
3144 free(vbmeta_image_buf);
3145 --info.num_loaded_images;
3146 }
Monika Singh292b3e92018-03-17 22:40:23 +05303147#else
Kishor PK5134b332017-05-09 17:50:08 +05303148 sig_size = sz - image_actual;
3149
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303150 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303151 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303152 /* Calculate the signature length from boot image */
3153 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303154 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303155 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003156
Monika Singh292b3e92018-03-17 22:40:23 +05303157 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303158 fastboot_fail("bootimage header fields are invalid");
3159 goto boot_failed;
3160 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003161 }
3162
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003163 // Initialize boot state before trying to verify boot.img
3164#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003165 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303166#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003167 /* Handle overflow if the input image size is greater than
3168 * boot image buffer can hold
3169 */
Monika Singh292b3e92018-03-17 22:40:23 +05303170 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003171 {
3172 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003173 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003174 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003175
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003176 /* Verify the boot image
3177 * device & page_size are initialized in aboot_init
3178 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003179 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003180 /* Pass size excluding signature size, otherwise we would try to
3181 * access signature beyond its length
3182 */
Monika Singh292b3e92018-03-17 22:40:23 +05303183 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003184 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003185#ifdef MDTP_SUPPORT
3186 else
3187 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003188 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003189 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003190
3191 if (!is_mdtp_activated) {
3192 ext_partition.partition = MDTP_PARTITION_NONE;
3193 mdtp_fwlock_verify_lock(&ext_partition);
3194 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003195 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003196
Amir Kotzer7c768c02016-04-13 09:08:05 +03003197 /* If mdtp state cannot be validate, block fastboot boot*/
3198 if(mdtp_activated(&is_mdtp_activated)){
3199 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3200 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3201 goto boot_failed;
3202 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003203 if(is_mdtp_activated){
3204 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003205 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003206 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003207#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303208#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003209
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003210#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303211 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303212 {
3213 /* set boot and system versions. */
3214 set_os_version((unsigned char *)data);
3215 // send root of trust
3216 if(!send_rot_command((uint32_t)device.is_unlocked))
3217 ASSERT(0);
3218 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303219#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003220 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003221 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3222 * If not, continue booting.
3223 */
3224 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3225 {
3226 out_addr = (unsigned char *)target_get_scratch_address();
3227 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3228 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303229#if VERIFIED_BOOT_2
3230 if (dtbo_image_sz)
3231 out_avai_len -= DTBO_IMG_BUF;
3232#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003233 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003234 ret = decompress((unsigned char *)(ptr + page_size),
3235 hdr->kernel_size, out_addr, out_avai_len,
3236 &dtb_offset, &out_len);
3237 if (ret)
3238 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003239 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003240 ASSERT(0);
3241 }
3242
Matthew Qin0b15b322015-05-19 05:20:54 -04003243 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003244 kptr = (struct kernel64_hdr *)out_addr;
3245 kernel_start_addr = out_addr;
3246 kernel_size = out_len;
3247 } else {
3248 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3249 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3250 kernel_size = hdr->kernel_size;
3251 }
3252
3253 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003254 * Update the kernel/ramdisk/tags address if the boot image header
3255 * has default values, these default values come from mkbootimg when
3256 * the boot image is flashed using fastboot flash:raw
3257 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003258 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003259
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003260 /* Get virtual addresses since the hdr saves physical addresses. */
3261 hdr->kernel_addr = VA(hdr->kernel_addr);
3262 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3263 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003264
Matthew Qinbb7923d2015-02-09 10:56:09 +08003265 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003266 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003267 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303268 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3269 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3270 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003271 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303272 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003273 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003274 }
3275
Amol Jadicb524072012-08-09 16:40:18 -07003276#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003277 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003278 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003279 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003280
3281 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003282#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303283 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3284 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003285 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303286 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003287 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003288 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003289#endif
3290
3291 /* Load ramdisk & kernel */
3292 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003293 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003294
3295#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303296 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3297 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003298 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303299 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003300 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003301 }
3302
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003303 /*
3304 * If dtb is not found look for appended DTB in the kernel.
3305 * If appended dev tree is found, update the atags with
3306 * memory address to the DTB appended location on RAM.
3307 * Else update with the atags address in the kernel header
3308 */
3309 if (!dtb_copied) {
3310 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003311 dtb = dev_tree_appended((void*)(ptr + page_size),
3312 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003313 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003314 if (!dtb) {
3315 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003316 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003317 }
Amol Jadicb524072012-08-09 16:40:18 -07003318 }
3319#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003320
3321 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003322 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003323
Dima Zavin77e41f32013-03-06 16:10:43 -08003324 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003325 (const char*) hdr->cmdline, board_machtype(),
3326 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003327
3328 /* fastboot already stop, it's no need to show fastboot menu */
3329 return;
3330boot_failed:
3331#if FBCON_DISPLAY_MSG
3332 /* revert to fastboot menu if boot failed */
3333 display_fastboot_menu();
3334#endif
3335 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003336}
3337
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003338void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003339{
3340 struct ptentry *ptn;
3341 struct ptable *ptable;
3342
3343 ptable = flash_get_ptable();
3344 if (ptable == NULL) {
3345 fastboot_fail("partition table doesn't exist");
3346 return;
3347 }
3348
3349 ptn = ptable_find(ptable, arg);
3350 if (ptn == NULL) {
3351 fastboot_fail("unknown partition name");
3352 return;
3353 }
3354
Monika Singh292b3e92018-03-17 22:40:23 +05303355 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3356 dprintf(INFO, "erasing avb_custom_key\n");
3357 if (erase_userkey()) {
3358 fastboot_fail("Erasing avb_custom_key failed");
3359 } else {
3360 fastboot_okay("");
3361 }
3362 return;
3363 }
3364
Dima Zavin214cc642009-01-26 11:16:21 -08003365 if (flash_erase(ptn)) {
3366 fastboot_fail("failed to erase partition");
3367 return;
3368 }
3369 fastboot_okay("");
3370}
3371
Bikas Gurungd48bd242010-09-04 19:54:32 -07003372
3373void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3374{
3375 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003376 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003377 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003378 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303379 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003380
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003381#if VERIFIED_BOOT
3382 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3383 {
3384 if(!device.is_unlocked)
3385 {
3386 fastboot_fail("unlock device to erase keystore");
3387 return;
3388 }
3389 }
3390#endif
3391
Kinson Chikf1a43512011-07-14 11:28:39 -07003392 index = partition_get_index(arg);
3393 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003394 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003395
Monika Singhc4778b72018-05-16 11:16:42 +05303396 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3397 dprintf(INFO, "erasing avb_custom_key\n");
3398 if (erase_userkey()) {
3399 fastboot_fail("Erasing avb_custom_key failed");
3400 } else {
3401 fastboot_okay("");
3402 }
3403 return;
3404 }
3405
Kinson Chikf1a43512011-07-14 11:28:39 -07003406 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003407 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003408 return;
3409 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003410
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003411 lun = partition_get_lun(index);
3412 mmc_set_lun(lun);
3413
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003414 if (platform_boot_dev_isemmc())
3415 {
3416 if (mmc_erase_card(ptn, size)) {
3417 fastboot_fail("failed to erase partition\n");
3418 return;
3419 }
3420 } else {
3421 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3422 size = partition_get_size(index);
3423 if (size > DEFAULT_ERASE_SIZE)
3424 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003425
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003426 /* Simple inefficient version of erase. Just writing
3427 0 in first several blocks */
3428 if (mmc_write(ptn , size, (unsigned int *)out)) {
3429 fastboot_fail("failed to erase partition");
3430 return;
3431 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303432 /*Erase FDE metadata at the userdata footer*/
3433 if(!(strncmp(arg, "userdata", 8)))
3434 {
3435 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3436 memset((void *)footer, 0, FOOTER_SIZE);
3437
3438 size = partition_get_size(index);
3439
3440 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3441 fastboot_fail("failed to erase userdata footer");
3442 free(footer);
3443 return;
3444 }
3445 free(footer);
3446 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003447 }
Monika Singh292b3e92018-03-17 22:40:23 +05303448#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303449 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303450 !(strncmp(arg, "userdata", 8)) &&
3451 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003452 ASSERT(0);
3453#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003454 fastboot_okay("");
3455}
3456
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003457void cmd_erase(const char *arg, void *data, unsigned sz)
3458{
Monika Singh292b3e92018-03-17 22:40:23 +05303459#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003460 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003461 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003462 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003463 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003464 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003465 return;
3466 }
3467 }
3468#endif
3469
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003470 if(target_is_emmc_boot())
3471 cmd_erase_mmc(arg, data, sz);
3472 else
3473 cmd_erase_nand(arg, data, sz);
3474}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003475
Mayank Grover11ff9692018-01-11 11:54:49 +05303476/* Get the size from partiton name */
3477static void get_partition_size(const char *arg, char *response)
3478{
3479 uint64_t ptn = 0;
3480 uint64_t size;
3481 int index = INVALID_PTN;
3482
3483 index = partition_get_index(arg);
3484
3485 if (index == INVALID_PTN)
3486 {
3487 dprintf(CRITICAL, "Invalid partition index\n");
3488 return;
3489 }
3490
3491 ptn = partition_get_offset(index);
3492
3493 if(!ptn)
3494 {
3495 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3496 return;
3497 }
3498
3499 size = partition_get_size(index);
3500
3501 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3502 return;
3503}
3504
Mayank Grover52cd10a2018-03-15 12:57:54 +05303505/* Function to check partition type of a partition*/
3506static fs_signature_type
3507check_partition_fs_signature(const char *arg)
3508{
3509 fs_signature_type ret = NO_FS;
3510 int index;
3511 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303512 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3513 uint32_t sb_blk_offset = 0;
3514 char *sb_buffer = buffer;
3515
Mayank Grover52cd10a2018-03-15 12:57:54 +05303516 if (!sb_buffer)
3517 {
3518 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3519 goto out;
3520 }
3521
3522 /* Read super block */
3523 if ((index = partition_get_index(arg)) < 0)
3524 {
3525 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3526 goto out;
3527 }
3528 ptn = partition_get_offset(index);
3529 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303530 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3531
3532 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303533 (void *)sb_buffer, mmc_blocksize))
3534 {
3535 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3536 goto out;
3537 }
3538
Mayank Grover399826a2018-08-27 12:15:15 +05303539 if (sb_blk_offset == 0)
3540 sb_buffer += FS_SUPERBLOCK_OFFSET;
3541
3542 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303543 {
3544 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3545 ret = EXT_FS_SIGNATURE;
3546 }
Mayank Grover399826a2018-08-27 12:15:15 +05303547 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303548 {
3549 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3550 ret = EXT_F2FS_SIGNATURE;
3551 }
3552 else
3553 {
3554 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3555 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3556 ret = NO_FS;
3557 }
3558
3559out:
Mayank Grover399826a2018-08-27 12:15:15 +05303560 if(buffer)
3561 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303562 return ret;
3563}
3564
Mayank Groverd38fe012018-03-13 15:33:16 +05303565/* Function to get partition type */
3566static void get_partition_type(const char *arg, char *response)
3567{
3568 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303569 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303570
3571 if (arg == NULL ||
3572 response == NULL)
3573 {
3574 dprintf(CRITICAL, "Invalid input parameter\n");
3575 return;
3576 }
3577
3578 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303579 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303580
3581 /* Mark partiton type for known paritions only */
3582 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3583 {
3584 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3585 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303586 /* Check partition for FS signature */
3587 fs_signature = check_partition_fs_signature(arg);
3588 switch (fs_signature)
3589 {
3590 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303591 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303592 break;
3593 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303594 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303595 break;
3596 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303597 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303598 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303599 }
3600 }
3601 return;
3602}
3603
Mayank Grover11ff9692018-01-11 11:54:49 +05303604/*
3605 * Publish the partition type & size info
3606 * fastboot getvar will publish the required information.
3607 * fastboot getvar partition_size:<partition_name>: partition size in hex
3608 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3609 */
3610static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3611{
Mayank Groverd38fe012018-03-13 15:33:16 +05303612 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303613 static bool published = false;
3614 struct partition_entry *ptn_entry =
3615 partition_get_partition_entries();
3616 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3617
3618 for (i = 0; i < num_parts; i++) {
3619 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3620 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3621 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3622
Mayank Groverd38fe012018-03-13 15:33:16 +05303623 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303624 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303625 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3626 {
3627 dprintf(CRITICAL, "partition size name truncated\n");
3628 return;
3629 }
3630 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3631 {
3632 dprintf(CRITICAL, "partition type name truncated\n");
3633 return;
3634 }
3635
3636 if (!published)
3637 {
3638 /* publish partition size & type info */
3639 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3640 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3641 }
3642 }
3643 if (!published)
3644 published = true;
3645}
3646
3647
Ajay Dudani5c761132011-04-07 20:19:04 -07003648void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003649{
3650 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003651 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003652 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003653 char *token = NULL;
3654 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003655 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003656 uint8_t lun = 0;
3657 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303658 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003659
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003660 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003661 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003662 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003663 if(token)
3664 {
3665 lun = atoi(token);
3666 mmc_set_lun(lun);
3667 lun_set = true;
3668 }
3669
Mao Jinlong226f33a2014-07-04 17:24:10 +08003670 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003671 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003672 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3673 {
3674 if (!aboot_frp_unlock(pname, data, sz))
3675 {
3676 fastboot_info("FRP unlock successful");
3677 fastboot_okay("");
3678 }
3679 else
3680 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3681
3682 return;
3683 }
3684
Mao Jinlong226f33a2014-07-04 17:24:10 +08003685 if (!strcmp(pname, "partition"))
3686 {
3687 dprintf(INFO, "Attempt to write partition image.\n");
3688 if (write_partition(sz, (unsigned char *) data)) {
3689 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003690 return;
3691 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303692 /* Re-publish partition table */
3693 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303694
3695 /* Rescan partition table to ensure we have multislot support*/
3696 if (partition_scan_for_multislot())
3697 {
3698 current_active_slot = partition_find_active_slot();
3699 dprintf(INFO, "Multislot supported: Slot %s active",
3700 (SUFFIX_SLOT(current_active_slot)));
3701 }
3702 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003703 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003704 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003705 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003706#if VERIFIED_BOOT
3707 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3708 {
3709 if(!device.is_unlocked)
3710 {
3711 fastboot_fail("unlock device to flash keystore");
3712 return;
3713 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303714 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003715 {
3716 fastboot_fail("image is not a keystore file");
3717 return;
3718 }
3719 }
3720#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003721 index = partition_get_index(pname);
3722 ptn = partition_get_offset(index);
3723 if(ptn == 0) {
3724 fastboot_fail("partition table doesn't exist");
3725 return;
3726 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003727
Mayank Grover351a75e2017-05-30 20:06:08 +05303728 if (!strncmp(pname, "boot", strlen("boot"))
3729 || !strcmp(pname, "recovery"))
3730 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003731 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3732 fastboot_fail("image is not a boot image");
3733 return;
3734 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303735
3736 /* Reset multislot_partition attributes in case of flashing boot */
3737 if (partition_multislot_is_supported())
3738 {
3739 partition_reset_attributes(index);
3740 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003741 }
3742
3743 if(!lun_set)
3744 {
3745 lun = partition_get_lun(index);
3746 mmc_set_lun(lun);
3747 }
3748
3749 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303750 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003751 fastboot_fail("size too large");
3752 return;
3753 }
3754 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3755 fastboot_fail("flash write failure");
3756 return;
3757 }
Greg Grisco6e754772011-06-23 12:19:39 -07003758 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003759 }
3760 fastboot_okay("");
3761 return;
3762}
3763
Ajay Dudanide984792015-03-02 09:57:41 -08003764void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3765{
3766 int i, images;
3767 meta_header_t *meta_header;
3768 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303769 /*End of the image address*/
3770 uintptr_t data_end;
3771
3772 if( (UINT_MAX - sz) > (uintptr_t)data )
3773 data_end = (uintptr_t)data + sz;
3774 else
3775 {
3776 fastboot_fail("Cannot flash: image header corrupt");
3777 return;
3778 }
3779
3780 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3781 {
3782 fastboot_fail("Cannot flash: image header corrupt");
3783 return;
3784 }
Ajay Dudanide984792015-03-02 09:57:41 -08003785
lijuang8ee21882016-04-19 16:57:11 +08003786 /* If device is locked:
3787 * Forbid to flash image to avoid the device to bypass the image
3788 * which with "any" name other than bootloader. Because it maybe
3789 * a meta package of all partitions.
3790 */
Monika Singh292b3e92018-03-17 22:40:23 +05303791#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003792 if (target_build_variant_user()) {
3793 if (!device.is_unlocked) {
3794 fastboot_fail("Device is locked, meta image flashing is not allowed");
3795 return;
3796 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303797
Mayank Grover912eaa62017-10-26 12:08:53 +05303798 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303799 !device.is_unlock_critical)
3800 {
lijuang8ee21882016-04-19 16:57:11 +08003801 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3802 return;
3803 }
lijuang8ee21882016-04-19 16:57:11 +08003804 }
3805#endif
3806
Ajay Dudanide984792015-03-02 09:57:41 -08003807 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303808 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3809 {
3810 fastboot_fail("Cannot flash: image header corrupt");
3811 return;
3812 }
Ajay Dudanide984792015-03-02 09:57:41 -08003813 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3814
3815 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3816
3817 for (i=0; i<images; i++) {
3818
3819 if((img_header_entry[i].ptn_name == NULL) ||
3820 (img_header_entry[i].start_offset == 0) ||
3821 (img_header_entry[i].size == 0))
3822 break;
Kishor PK49831502017-04-21 17:55:43 +05303823 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3824 fastboot_fail("Integer overflow detected in start_offset of img");
3825 break;
3826 }
3827 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3828 fastboot_fail("Integer overflow detected in size of img");
3829 break;
3830 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303831 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3832 + img_header_entry[i].size) )
3833 {
3834 fastboot_fail("Cannot flash: image size mismatch");
3835 break;
3836 }
3837
Ajay Dudanide984792015-03-02 09:57:41 -08003838 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3839 (void *) data + img_header_entry[i].start_offset,
3840 img_header_entry[i].size);
3841 }
3842
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003843 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3844 {
3845 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3846 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3847 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3848 }
3849 else
3850 {
3851 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3852 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3853 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3854 }
3855
3856 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003857 fastboot_okay("");
3858 return;
3859}
3860
Ajay Dudani5c761132011-04-07 20:19:04 -07003861void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3862{
3863 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003864 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003865 uint32_t *fill_buf = NULL;
3866 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303867 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003868 sparse_header_t *sparse_header;
3869 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003870 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003871 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303872 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003873 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303874 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003875 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303876 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303877 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003878
Kinson Chikf1a43512011-07-14 11:28:39 -07003879 index = partition_get_index(arg);
3880 ptn = partition_get_offset(index);
3881 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003882 fastboot_fail("partition table doesn't exist");
3883 return;
3884 }
3885
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303886 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303887
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003888 lun = partition_get_lun(index);
3889 mmc_set_lun(lun);
3890
vijay kumar800255e2015-04-24 20:26:19 +05303891 if (sz < sizeof(sparse_header_t)) {
3892 fastboot_fail("size too low");
3893 return;
3894 }
3895
Ajay Dudani5c761132011-04-07 20:19:04 -07003896 /* Read and skip over sparse image header */
3897 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303898
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303899 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3900 fastboot_fail("Invalid block size\n");
3901 return;
3902 }
3903
vijay kumar1321f342015-03-27 12:13:42 +05303904 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003905 fastboot_fail("size too large");
3906 return;
3907 }
3908
vijay kumarde4fcf62015-04-23 13:05:49 +05303909 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303910
Parth Dixit64b1a482016-03-07 16:31:26 +05303911 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303912 fastboot_fail("buffer overreads occured due to invalid sparse header");
3913 return;
3914 }
3915
vijay kumarde4fcf62015-04-23 13:05:49 +05303916 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003917 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303918 fastboot_fail("sparse header size mismatch");
3919 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003920 }
3921
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003922 dprintf (SPEW, "=== Sparse Image Header ===\n");
3923 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3924 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3925 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3926 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3927 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3928 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3929 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3930 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003931
3932 /* Start processing chunks */
3933 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3934 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303935 /* Make sure the total image size does not exceed the partition size */
3936 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3937 fastboot_fail("size too large");
3938 return;
3939 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003940 /* Read and skip over chunk header */
3941 chunk_header = (chunk_header_t *) data;
3942 data += sizeof(chunk_header_t);
3943
Parth Dixit64b1a482016-03-07 16:31:26 +05303944 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303945 fastboot_fail("buffer overreads occured due to invalid sparse header");
3946 return;
3947 }
3948
Ajay Dudani5c761132011-04-07 20:19:04 -07003949 dprintf (SPEW, "=== Chunk Header ===\n");
3950 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3951 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3952 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3953
vijay kumar800255e2015-04-24 20:26:19 +05303954 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003955 {
vijay kumar800255e2015-04-24 20:26:19 +05303956 fastboot_fail("chunk header size mismatch");
3957 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003958 }
3959
wufeng.jiang813dc352015-06-02 23:02:46 -04003960 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3961
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303962 /* Make sure that the chunk size calculated from sparse image does not
3963 * exceed partition size
3964 */
3965 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3966 {
3967 fastboot_fail("Chunk data size exceeds partition size");
3968 return;
3969 }
3970
Ajay Dudani5c761132011-04-07 20:19:04 -07003971 switch (chunk_header->chunk_type)
3972 {
3973 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003974 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003975 chunk_data_sz))
3976 {
3977 fastboot_fail("Bogus chunk size for chunk type Raw");
3978 return;
3979 }
3980
Parth Dixit64b1a482016-03-07 16:31:26 +05303981 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303982 fastboot_fail("buffer overreads occured due to invalid sparse header");
3983 return;
3984 }
3985
wufeng.jiang813dc352015-06-02 23:02:46 -04003986 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3987 otherwise it will return in the line above
3988 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003989 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003990 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003991 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003992 {
3993 fastboot_fail("flash write failure");
3994 return;
3995 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303996 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3997 fastboot_fail("Bogus size for RAW chunk type");
3998 return;
3999 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004000 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004001 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004002 break;
4003
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004004 case CHUNK_TYPE_FILL:
4005 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4006 sizeof(uint32_t)))
4007 {
4008 fastboot_fail("Bogus chunk size for chunk type FILL");
4009 return;
4010 }
4011
Mayank Grover4f50bba2017-07-19 18:17:08 +05304012 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4013 /* Integer overflow detected */
4014 if (blk_sz_actual < sparse_header->blk_sz)
4015 {
4016 fastboot_fail("Invalid block size");
4017 return;
4018 }
4019
4020 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004021 if (!fill_buf)
4022 {
4023 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4024 return;
4025 }
4026
Parth Dixit64b1a482016-03-07 16:31:26 +05304027 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304028 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304029 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304030 return;
4031 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004032 fill_val = *(uint32_t *)data;
4033 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004034
4035 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4036 {
4037 fill_buf[i] = fill_val;
4038 }
4039
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304040 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4041 {
4042 fastboot_fail("bogus size for chunk FILL type");
4043 free(fill_buf);
4044 return;
4045 }
4046
wufeng.jiang813dc352015-06-02 23:02:46 -04004047 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004048 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304049 /* Make sure that the data written to partition does not exceed partition size */
4050 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4051 {
4052 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304053 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304054 return;
4055 }
4056
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004057 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4058 sparse_header->blk_sz,
4059 fill_buf))
4060 {
4061 fastboot_fail("flash write failure");
4062 free(fill_buf);
4063 return;
4064 }
4065
4066 total_blocks++;
4067 }
4068
4069 free(fill_buf);
4070 break;
4071
Ajay Dudani5c761132011-04-07 20:19:04 -07004072 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304073 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4074 fastboot_fail("bogus size for chunk DONT CARE type");
4075 return;
4076 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004077 total_blocks += chunk_header->chunk_sz;
4078 break;
4079
Ajay Dudani5c761132011-04-07 20:19:04 -07004080 case CHUNK_TYPE_CRC:
4081 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4082 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004083 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004084 return;
4085 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304086 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4087 fastboot_fail("bogus size for chunk CRC type");
4088 return;
4089 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004090 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304091 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304092 fastboot_fail("integer overflow occured");
4093 return;
4094 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004095 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304096 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304097 fastboot_fail("buffer overreads occured due to invalid sparse header");
4098 return;
4099 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004100 break;
4101
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004102 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004103 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004104 fastboot_fail("Unknown chunk type");
4105 return;
4106 }
4107 }
4108
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004109 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4110 total_blocks, sparse_header->total_blks);
4111
4112 if(total_blocks != sparse_header->total_blks)
4113 {
4114 fastboot_fail("sparse image write failure");
4115 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004116
4117 fastboot_okay("");
4118 return;
4119}
4120
4121void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4122{
4123 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004124 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004125
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004126#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004127 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4128 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004129 int ret=0;
4130 uint32 major_version=0;
4131 uint32 minor_version=0;
4132
4133 ret = scm_svc_version(&major_version,&minor_version);
4134 if(!ret)
4135 {
4136 if(major_version >= 2)
4137 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004138 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004139 {
4140 ret = encrypt_scm((uint32 **) &data, &sz);
4141 if (ret != 0) {
4142 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4143 return;
4144 }
4145
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004146 /* Protect only for SSD */
4147 if (!strcmp(arg, "ssd")) {
4148 ret = scm_protect_keystore((uint32 *) data, sz);
4149 if (ret != 0) {
4150 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4151 return;
4152 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004153 }
4154 }
4155 else
4156 {
4157 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4158 if(ret != 0)
4159 {
4160 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4161 return;
4162 }
4163 }
4164 }
4165 else
4166 {
4167 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4168 magic_number[1] == DECRYPT_MAGIC_1)
4169 {
4170 ret = decrypt_scm((uint32 **) &data, &sz);
4171 if (ret != 0) {
4172 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4173 return;
4174 }
4175 }
4176 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4177 magic_number[1] == ENCRYPT_MAGIC_1)
4178 {
4179 ret = encrypt_scm((uint32 **) &data, &sz);
4180 if (ret != 0) {
4181 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4182 return;
4183 }
4184 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004185 }
4186 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004187 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004188 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004189 dprintf(CRITICAL,"INVALID SVC Version\n");
4190 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004191 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004192#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004193
Monika Singh292b3e92018-03-17 22:40:23 +05304194#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004195 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004196 {
lijuang511a2b52015-08-14 20:50:51 +08004197 /* if device is locked:
4198 * common partition will not allow to be flashed
4199 * critical partition will allow to flash image.
4200 */
4201 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4202 fastboot_fail("Partition flashing is not allowed");
4203 return;
4204 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304205
lijuang511a2b52015-08-14 20:50:51 +08004206 /* if device critical is locked:
4207 * common partition will allow to be flashed
4208 * critical partition will not allow to flash image.
4209 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304210 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304211 !device.is_unlock_critical &&
4212 critical_flash_allowed(arg)) {
4213 fastboot_fail("Critical partition flashing is not allowed");
4214 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004215 }
4216 }
4217#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304218 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4219 dprintf(INFO, "flashing avb_custom_key\n");
4220 if (store_userkey(data, sz)) {
4221 fastboot_fail("Flashing avb_custom_key failed");
4222 } else {
4223 fastboot_okay("");
4224 }
4225 return;
4226 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004227
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004228 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004229 meta_header = (meta_header_t *) data;
4230 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004231 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004232 else if (meta_header->magic == META_HEADER_MAGIC)
4233 cmd_flash_meta_img(arg, data, sz);
4234 else
4235 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004236
4237#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304238 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304239 (!strncmp(arg, "system", 6)) &&
4240 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004241 // reset dm_verity mode to enforcing
4242 device.verity_mode = 1;
4243 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304244#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004245
Ajay Dudani5c761132011-04-07 20:19:04 -07004246 return;
4247}
4248
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004249void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4250{
4251 struct ptentry *sys_ptn;
4252 struct ptable *ptable;
4253
4254 ptable = flash_get_ptable();
4255 if (ptable == NULL) {
4256 fastboot_fail("partition table doesn't exist");
4257 return;
4258 }
4259
4260 sys_ptn = ptable_find(ptable, "system");
4261 if (sys_ptn == NULL) {
4262 fastboot_fail("system partition not found");
4263 return;
4264 }
4265
4266 sz = ROUND_TO_PAGE(sz, page_mask);
4267 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4268 fastboot_fail("update_ubi_vol failed");
4269 else
4270 fastboot_okay("");
4271}
4272
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004273void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004274{
4275 struct ptentry *ptn;
4276 struct ptable *ptable;
4277 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304278 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304279 unsigned bytes_to_round_page = 0;
4280 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004281
Kishor PK38ed93d2017-04-25 14:19:26 +05304282 if((uintptr_t)data > (UINT_MAX - sz)) {
4283 fastboot_fail("Cannot flash: image header corrupt");
4284 return;
4285 }
4286
Dima Zavin214cc642009-01-26 11:16:21 -08004287 ptable = flash_get_ptable();
4288 if (ptable == NULL) {
4289 fastboot_fail("partition table doesn't exist");
4290 return;
4291 }
4292
4293 ptn = ptable_find(ptable, arg);
4294 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004295 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4296 arg);
4297 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004298 return;
4299 }
4300
Monika Singh292b3e92018-03-17 22:40:23 +05304301 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4302 dprintf(INFO, "flashing avb_custom_key\n");
4303 if (store_userkey(data, sz)) {
4304 fastboot_fail("Flashing avb_custom_key failed");
4305 } else {
4306 fastboot_okay("");
4307 }
4308 return;
4309 }
4310
Dima Zavin214cc642009-01-26 11:16:21 -08004311 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304312 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4313 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4314 } else {
4315 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004316 return;
4317 }
4318 }
4319
Amol Jadi5c61a952012-05-04 17:05:35 -07004320 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004321 || !strcmp(ptn->name, "userdata")
4322 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004323 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004324 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004325 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304326 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304327 rounded_size = ROUNDUP(sz, page_size);
4328 bytes_to_round_page = rounded_size - sz;
4329 if (bytes_to_round_page) {
4330 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4331 fastboot_fail("Integer overflow detected");
4332 return;
4333 }
4334 if (((uintptr_t)data + sz + bytes_to_round_page) >
4335 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4336 fastboot_fail("Buffer size is not aligned to page_size");
4337 return;
4338 }
4339 else {
4340 memset(data + sz, 0, bytes_to_round_page);
4341 sz = rounded_size;
4342 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304343 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304344 }
4345
Kishor PK38ed93d2017-04-25 14:19:26 +05304346 /*Checking partition_size for the possible integer overflow */
4347 partition_size = validate_partition_size(ptn);
4348
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304349 if (sz > partition_size) {
4350 fastboot_fail("Image size too large");
4351 return;
4352 }
4353
Dima Zavin214cc642009-01-26 11:16:21 -08004354 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304355 if ((sz > UBI_EC_HDR_SIZE) &&
4356 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004357 if (flash_ubi_img(ptn, data, sz)) {
4358 fastboot_fail("flash write failure");
4359 return;
4360 }
4361 } else {
4362 if (flash_write(ptn, extra, data, sz)) {
4363 fastboot_fail("flash write failure");
4364 return;
4365 }
Dima Zavin214cc642009-01-26 11:16:21 -08004366 }
4367 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4368 fastboot_okay("");
4369}
4370
Kishor PK38ed93d2017-04-25 14:19:26 +05304371
4372static inline uint64_t validate_partition_size(struct ptentry *ptn)
4373{
4374 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4375 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4376 return ptn->length * flash_num_pages_per_blk() * page_size;
4377 }
4378 }
4379 return 0;
4380}
4381
4382
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004383void cmd_flash(const char *arg, void *data, unsigned sz)
4384{
4385 if(target_is_emmc_boot())
4386 cmd_flash_mmc(arg, data, sz);
4387 else
4388 cmd_flash_nand(arg, data, sz);
4389}
4390
Dima Zavin214cc642009-01-26 11:16:21 -08004391void cmd_continue(const char *arg, void *data, unsigned sz)
4392{
4393 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004394 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004395
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004396 if (target_is_emmc_boot())
4397 {
lijuanga40d6302015-07-20 20:10:13 +08004398#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004399 /* Exit keys' detection thread firstly */
4400 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004401#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004402 boot_linux_from_mmc();
4403 }
4404 else
4405 {
4406 boot_linux_from_flash();
4407 }
Dima Zavin214cc642009-01-26 11:16:21 -08004408}
4409
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004410void cmd_reboot(const char *arg, void *data, unsigned sz)
4411{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004412 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004413 fastboot_okay("");
4414 reboot_device(0);
4415}
4416
Mayank Grover351a75e2017-05-30 20:06:08 +05304417void cmd_set_active(const char *arg, void *data, unsigned sz)
4418{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304419 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304420 unsigned i,current_active_slot;
4421 const char *current_slot_suffix;
4422
4423 if (!partition_multislot_is_supported())
4424 {
4425 fastboot_fail("Command not supported");
4426 return;
4427 }
4428
4429 if (arg)
4430 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304431 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304432 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304433 {
4434 current_active_slot = partition_find_active_slot();
4435
4436 /* Check if trying to make curent slot active */
4437 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304438 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4439 (char *)suffix_delimiter, &sp);
4440
Mayank Grover5eb5f692017-07-19 20:16:04 +05304441 if (current_slot_suffix &&
4442 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304443 {
4444 fastboot_okay("Slot already set active");
4445 return;
4446 }
4447 else
4448 {
4449 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4450 {
4451 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304452 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4453 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304454 if (current_slot_suffix &&
4455 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304456 {
lijuang8b03e5f2019-07-12 18:16:19 +08004457 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304458 publish_getvar_multislot_vars();
4459 fastboot_okay("");
4460 return;
4461 }
4462 }
4463 }
4464 }
4465 }
4466 fastboot_fail("Invalid slot suffix.");
4467 return;
4468}
4469
Mayank Grover98c4c742019-04-25 17:21:37 +05304470#if DYNAMIC_PARTITION_SUPPORT
4471void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4472{
4473 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4474 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4475 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4476 fastboot_fail("Failed to update recovery command");
4477 return;
4478 }
4479 fastboot_okay("");
4480 reboot_device(REBOOT_MODE_UNKNOWN);
4481
4482 //shouldn't come here.
4483 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4484 return;
4485}
4486
4487void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4488{
4489 dprintf(INFO, "rebooting the device - recovery\n");
4490 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
4491 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4492 fastboot_fail("Failed to update recovery command");
4493 return;
4494 }
4495 fastboot_okay("");
4496 reboot_device(REBOOT_MODE_UNKNOWN);
4497
4498 //shouldn't come here.
4499 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4500 return;
4501}
4502#endif
4503
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004504void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4505{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004506 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004507 fastboot_okay("");
4508 reboot_device(FASTBOOT_MODE);
4509}
4510
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004511void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4512{
4513 dprintf(INFO, "Enabling charger screen check\n");
4514 device.charger_screen_enabled = 1;
4515 write_device_info(&device);
4516 fastboot_okay("");
4517}
4518
4519void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4520{
4521 dprintf(INFO, "Disabling charger screen check\n");
4522 device.charger_screen_enabled = 0;
4523 write_device_info(&device);
4524 fastboot_okay("");
4525}
4526
lijuanga25d8bb2015-09-16 13:11:52 +08004527void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4528{
4529 char *p = NULL;
4530 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304531 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004532
4533 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304534 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004535 if (p) {
4536 if (!strncmp(p, "0", 1)) {
4537 device.charger_screen_enabled = 0;
4538 } else if (!strncmp(p, "1", 1)) {
4539 device.charger_screen_enabled = 1;
4540 }
4541 }
4542 }
4543
4544 /* update charger_screen_enabled value for getvar
4545 * command
4546 */
4547 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4548 device.charger_screen_enabled);
4549
4550 write_device_info(&device);
4551 fastboot_okay("");
4552}
4553
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304554void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4555{
4556 dprintf(INFO, "Selecting display panel %s\n", arg);
4557 if (arg)
4558 strlcpy(device.display_panel, arg,
4559 sizeof(device.display_panel));
4560 write_device_info(&device);
4561 fastboot_okay("");
4562}
4563
Shashank Mittal162244e2011-08-08 19:01:25 -07004564void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4565{
lijuang4ece1e72015-08-14 21:02:36 +08004566 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304567}
4568
4569void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4570{
lijuang4ece1e72015-08-14 21:02:36 +08004571 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304572 if(!is_allow_unlock) {
4573 fastboot_fail("oem unlock is not allowed");
4574 return;
4575 }
4576
lijuang4ece1e72015-08-14 21:02:36 +08004577 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304578
lijuang4ece1e72015-08-14 21:02:36 +08004579 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304580 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304581 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304582 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4583 write_misc(0, &msg, sizeof(msg));
4584
4585 fastboot_okay("");
4586 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004587 }
4588 fastboot_okay("");
4589}
4590
Channagoud Kadabiad259832015-05-29 11:14:17 -07004591static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4592{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304593 int ret=1;
4594 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004595
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304596 /*
4597 Authentication method not implemented.
4598
4599 OEM to implement, authentication system which on successful validataion,
4600 calls write_allow_oem_unlock() with is_allow_unlock.
4601 */
4602#if 0
4603 authentication_success = oem_specific_auth_mthd();
4604#endif
4605
4606 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004607 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304608 is_allow_unlock = true;
4609 write_allow_oem_unlock(is_allow_unlock);
4610 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004611 }
4612 return ret;
4613}
4614
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004615void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4616{
lijuang4ece1e72015-08-14 21:02:36 +08004617 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004618}
4619
Shashank Mittala0032282011-08-26 14:50:11 -07004620void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4621{
lijuang511a2b52015-08-14 20:50:51 +08004622 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004623 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004624 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004625 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4626 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304627#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304628 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304629 {
4630 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4631 (device.is_unlock_critical ? "true" : "false"));
4632 fastboot_info(response);
4633 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304634#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004635 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004636 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304637 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4638 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004639 fastboot_okay("");
4640}
4641
lijuang511a2b52015-08-14 20:50:51 +08004642void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4643{
4644 char response[MAX_RSP_SIZE];
4645 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4646 fastboot_info(response);
4647 fastboot_okay("");
4648}
4649
4650void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4651{
lijuang4ece1e72015-08-14 21:02:36 +08004652 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004653}
4654
4655void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4656{
lijuang4ece1e72015-08-14 21:02:36 +08004657 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004658}
4659
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004660void cmd_preflash(const char *arg, void *data, unsigned sz)
4661{
4662 fastboot_okay("");
4663}
4664
Mao Flynn7b379f32015-04-20 00:28:30 +08004665static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304666
Mao Flynn7b379f32015-04-20 00:28:30 +08004667int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304668{
Mao Flynn7b379f32015-04-20 00:28:30 +08004669 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304670 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004671 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304672 return -1;
4673 return 0;
4674}
4675
Mao Flynn7b379f32015-04-20 00:28:30 +08004676int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004677{
4678 struct ptentry *ptn;
4679 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004680 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004681 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004682
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304683 ptable = flash_get_ptable();
4684 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004685 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4686 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304687 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004688
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304689 ptn = ptable_find(ptable, "splash");
4690 if (ptn == NULL) {
4691 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004692 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304693 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004694 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304695 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004696 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304697 }
4698
Mao Flynn7b379f32015-04-20 00:28:30 +08004699 header = (struct logo_img_header *)logo_header;
4700 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304701 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004702 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304703 }
4704
4705 fb_display = fbcon_display();
4706 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304707 if (header->type && (header->blocks != 0) &&
4708 (UINT_MAX >= header->blocks * 512) &&
4709 ((header->blocks * 512) <= (fb_display->width *
4710 fb_display->height * (fb_display->bpp / 8)))) {
4711 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08004712 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4713
4714 /* if the logo is full-screen size, remove "fbcon_clear()" */
4715 if ((header->width != fb_display->width)
4716 || (header->height != fb_display->height))
4717 fbcon_clear();
4718
4719 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4720 (uint32_t *)base,
4721 (header->blocks * 512))) {
4722 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4723 return -1;
4724 }
4725 fbcon_extract_to_screen(header, base);
4726 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004727 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004728
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004729 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4730 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004731 return -1;
4732 }
4733
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304734 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304735 uint32_t fb_size = ROUNDUP(fb_display->width *
4736 fb_display->height *
4737 (fb_display->bpp / 8), 4096);
4738 uint32_t splash_size = ((((header->width * header->height *
4739 fb_display->bpp/8) + 511) >> 9) << 9);
4740
4741 if (splash_size > fb_size) {
4742 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4743 return -1;
4744 }
4745
Mao Flynn7b379f32015-04-20 00:28:30 +08004746 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4747 (uint32_t *)base,
4748 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304749 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304750 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004751 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004752 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304753 }
4754
Mao Flynn7b379f32015-04-20 00:28:30 +08004755 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304756}
4757
Mao Flynn7b379f32015-04-20 00:28:30 +08004758int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304759{
4760 int index = INVALID_PTN;
4761 unsigned long long ptn = 0;
4762 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004763 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004764 uint32_t blocksize, realsize, readsize;
4765 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304766
4767 index = partition_get_index("splash");
4768 if (index == 0) {
4769 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004770 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304771 }
4772
4773 ptn = partition_get_offset(index);
4774 if (ptn == 0) {
4775 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004776 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304777 }
4778
Mao Flynn1893a7f2015-06-03 12:03:36 +08004779 mmc_set_lun(partition_get_lun(index));
4780
4781 blocksize = mmc_get_device_blocksize();
4782 if (blocksize == 0) {
4783 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4784 return -1;
4785 }
4786
4787 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004788 if (!fb_display)
4789 {
4790 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4791 return -1;
4792 }
4793
Mao Flynn1893a7f2015-06-03 12:03:36 +08004794 base = (uint8_t *) fb_display->base;
4795
Kishor PKee5c0a32018-03-06 16:49:46 +05304796 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304797 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004798 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304799 }
4800
Mao Flynn1893a7f2015-06-03 12:03:36 +08004801 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004802 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304803 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004804 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304805 }
4806
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304807 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304808 if (header->type && (header->blocks != 0) &&
4809 (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
4810 ((header->blocks * 512) <= (fb_display->width *
4811 fb_display->height * (fb_display->bpp / 8)))) {
4812 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08004813 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004814
Mao Flynn1893a7f2015-06-03 12:03:36 +08004815 realsize = header->blocks * 512;
4816 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4817
4818 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004819 if ((header->width != fb_display->width)
4820 || (header->height != fb_display->height))
4821 fbcon_clear();
4822
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304823 uint32_t fb_size = ROUNDUP(fb_display->width *
4824 fb_display->height *
4825 (fb_display->bpp / 8), 4096);
4826
4827 if (readsize > fb_size) {
4828 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4829 return -1;
4830 }
4831
Kishor PKee5c0a32018-03-06 16:49:46 +05304832 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004833 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4834 return -1;
4835 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004836
Mao Flynn1893a7f2015-06-03 12:03:36 +08004837 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4838 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304839
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004840 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4841 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004842 return -1;
4843 }
4844
4845 realsize = header->width * header->height * fb_display->bpp / 8;
4846 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4847
4848 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304849 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004850 fbcon_clear();
4851 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4852 return -1;
4853 }
4854 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304855 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004856 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4857 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4858 return -1;
4859 }
4860 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4861 }
4862 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304863 }
4864
Mao Flynn7b379f32015-04-20 00:28:30 +08004865 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304866}
4867
Mao Flynn7b379f32015-04-20 00:28:30 +08004868int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304869{
4870 if (target_is_emmc_boot()) {
4871 return splash_screen_mmc();
4872 } else {
4873 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004874 }
4875}
4876
Mayank Grover351a75e2017-05-30 20:06:08 +05304877void publish_getvar_multislot_vars()
4878{
4879 int i,count;
4880 static bool published = false;
4881 static char slot_count[MAX_RSP_SIZE];
4882 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4883 static char active_slot_suffix[MAX_RSP_SIZE];
4884 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4885 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4886 const char *tmp;
4887 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304888 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304889
4890 if (!published)
4891 {
4892 /* Update slot meta info */
4893 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4894 partition_get_partition_count());
4895 for(i=0; i<count; i++)
4896 {
4897 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304898 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4899 has_slot_pname[i]);
4900 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304901 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4902 }
4903
4904 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4905 {
4906 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304907 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304908 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4909 "slot-unbootable:%s", tmp);
4910 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4911 "slot-active:%s", tmp);
4912 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4913 "slot-success:%s", tmp);
4914 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4915 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304916 fastboot_publish(slot_info[i].slot_is_unbootable,
4917 slot_info[i].slot_is_unbootable_rsp);
4918 fastboot_publish(slot_info[i].slot_is_active,
4919 slot_info[i].slot_is_active_rsp);
4920 fastboot_publish(slot_info[i].slot_is_succesful,
4921 slot_info[i].slot_is_succesful_rsp);
4922 fastboot_publish(slot_info[i].slot_retry_count,
4923 slot_info[i].slot_retry_count_rsp);
4924 }
4925 fastboot_publish("current-slot", active_slot_suffix);
4926 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4927 fastboot_publish("slot-count", slot_count);
4928 published = true;
4929 }
4930
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304931 active_slt = partition_find_active_slot();
4932 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304933 {
4934 tmp = SUFFIX_SLOT(active_slt);
4935 tmp++; // to remove "_" from slot_suffix.
4936 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4937 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304938 else
4939 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4940
Mayank Grover351a75e2017-05-30 20:06:08 +05304941 /* Update partition meta information */
4942 partition_fill_slot_meta(slot_info);
4943 return;
4944}
4945
lijuang4ece1e72015-08-14 21:02:36 +08004946void get_product_name(unsigned char *buf)
4947{
4948 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4949 return;
4950}
4951
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304952#if PRODUCT_IOT
4953void get_bootloader_version_iot(unsigned char *buf)
4954{
4955 if (buf != NULL)
4956 {
4957 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4958 strlcat(buf, "-", MAX_VERSION_LEN);
4959 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4960 }
4961 return;
4962}
4963#endif
4964
lijuang4ece1e72015-08-14 21:02:36 +08004965void get_bootloader_version(unsigned char *buf)
4966{
4967 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4968 return;
4969}
4970
4971void get_baseband_version(unsigned char *buf)
4972{
4973 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4974 return;
4975}
4976
Monika Singh32a09f72018-03-14 13:08:29 +05304977bool is_device_locked_critical()
4978{
4979 return device.is_unlock_critical ? false:true;
4980}
4981
lijuang4ece1e72015-08-14 21:02:36 +08004982bool is_device_locked()
4983{
4984 return device.is_unlocked ? false:true;
4985}
4986
Monika Singh32a09f72018-03-14 13:08:29 +05304987bool is_verity_enforcing()
4988{
4989 return device.verity_mode ? true:false;
4990}
4991
Amol Jadi5edf3552013-07-23 14:15:34 -07004992/* register commands and variables for fastboot */
4993void aboot_fastboot_register_commands(void)
4994{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004995 int i;
lijuangf16461c2015-08-03 17:09:34 +08004996 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004997
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004998 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004999 /* By default the enabled list is empty. */
5000 {"", NULL},
5001 /* move commands enclosed within the below ifndef to here
5002 * if they need to be enabled in user build.
5003 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005004#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005005 /* Register the following commands only for non-user builds */
5006 {"flash:", cmd_flash},
5007 {"erase:", cmd_erase},
5008 {"boot", cmd_boot},
5009 {"continue", cmd_continue},
5010 {"reboot", cmd_reboot},
5011 {"reboot-bootloader", cmd_reboot_bootloader},
5012 {"oem unlock", cmd_oem_unlock},
5013 {"oem unlock-go", cmd_oem_unlock_go},
5014 {"oem lock", cmd_oem_lock},
5015 {"flashing unlock", cmd_oem_unlock},
5016 {"flashing lock", cmd_oem_lock},
5017 {"flashing lock_critical", cmd_flashing_lock_critical},
5018 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5019 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5020 {"oem device-info", cmd_oem_devinfo},
5021 {"preflash", cmd_preflash},
5022 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5023 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005024 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005025 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305026 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305027#if DYNAMIC_PARTITION_SUPPORT
5028 {"reboot-fastboot",cmd_reboot_fastboot},
5029 {"reboot-recovery",cmd_reboot_recovery},
5030#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005031#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005032 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005033#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005034#endif
lijuang511a2b52015-08-14 20:50:51 +08005035 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005036
5037 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5038 for (i = 1; i < fastboot_cmds_count; i++)
5039 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5040
Amol Jadi5edf3552013-07-23 14:15:34 -07005041 /* publish variables and their values */
5042 fastboot_publish("product", TARGET(BOARD));
5043 fastboot_publish("kernel", "lk");
5044 fastboot_publish("serialno", sn_buf);
5045
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305046 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5047 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5048 fastboot_publish("hw-revision", soc_version_str);
5049
Amol Jadi5edf3552013-07-23 14:15:34 -07005050 /*
5051 * partition info is supported only for emmc partitions
5052 * Calling this for NAND prints some error messages which
5053 * is harmless but misleading. Avoid calling this for NAND
5054 * devices.
5055 */
5056 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305057 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005058
Mayank Grover351a75e2017-05-30 20:06:08 +05305059 if (partition_multislot_is_supported())
5060 publish_getvar_multislot_vars();
5061
Amol Jadi5edf3552013-07-23 14:15:34 -07005062 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005063#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005064 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5065 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005066#else
5067 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5068 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5069#endif
5070
Amol Jadi5edf3552013-07-23 14:15:34 -07005071 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005072 /* Is the charger screen check enabled */
5073 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5074 device.charger_screen_enabled);
5075 fastboot_publish("charger-screen-enabled",
5076 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08005077 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305078 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5079 device.display_panel);
5080 fastboot_publish("display-panel",
5081 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305082
5083 if (target_is_emmc_boot())
5084 {
5085 mmc_blocksize = mmc_get_device_blocksize();
5086 }
5087 else
5088 {
5089 mmc_blocksize = flash_block_size();
5090 }
5091 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5092 fastboot_publish("erase-block-size", (const char *) block_size_string);
5093 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305094#if PRODUCT_IOT
5095 get_bootloader_version_iot(&bootloader_version_string);
5096 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5097
5098 /* Version baseband is n/a for apq iot devices */
5099 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305100#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005101 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5102 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305103#endif
lijuangf16461c2015-08-03 17:09:34 +08005104 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305105 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005106 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5107 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5108 target_is_emmc_boot()? "eMMC":"UFS");
5109 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005110#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005111 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005112 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005113 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005114#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305115 if (target_dynamic_partition_supported())
5116 fastboot_publish("is-userspace", "no");
Amol Jadi5edf3552013-07-23 14:15:34 -07005117}
5118
Brian Swetland9c4c0752009-01-25 16:23:50 -08005119void aboot_init(const struct app_descriptor *app)
5120{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005121 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305122 int boot_err_type = 0;
5123 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07005124
lijuang39831732016-01-08 17:49:02 +08005125 /* Initialise wdog to catch early lk crashes */
5126#if WDOG_SUPPORT
5127 msm_wdog_init();
5128#endif
5129
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005130 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005131 if (target_is_emmc_boot())
5132 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005133 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005134 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305135 mmc_blocksize = mmc_get_device_blocksize();
5136 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005137 }
5138 else
5139 {
5140 page_size = flash_page_size();
5141 page_mask = page_size - 1;
5142 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005143 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5144
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005145 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05305146 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005147
Mayank Grover351a75e2017-05-30 20:06:08 +05305148 /* Detect multi-slot support */
5149 if (partition_multislot_is_supported())
5150 {
5151 boot_slot = partition_find_active_slot();
5152 if (boot_slot == INVALID)
5153 {
5154 boot_into_fastboot = true;
5155 dprintf(INFO, "Active Slot: (INVALID)\n");
5156 }
5157 else
5158 {
5159 /* Setting the state of system to boot active slot */
5160 partition_mark_active_slot(boot_slot);
5161 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5162 }
5163 }
5164
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005165 /* Display splash screen if enabled */
5166#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08005167#if NO_ALARM_DISPLAY
5168 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005169#endif
lijuang99c02d82015-02-13 19:04:34 +08005170 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07005171#if DISPLAY_HDMI_PRIMARY
5172 if (!strlen(device.display_panel))
5173 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5174 sizeof(device.display_panel));
5175#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005176#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07005177 /* Wait if the display shutdown is in progress */
5178 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005179 if (!pm_appsbl_display_init_done())
5180 target_display_init(device.display_panel);
5181 else
5182 display_image_on_screen();
5183#else
lijuang99c02d82015-02-13 19:04:34 +08005184 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005185#endif
lijuang99c02d82015-02-13 19:04:34 +08005186 dprintf(SPEW, "Display Init: Done\n");
5187#if NO_ALARM_DISPLAY
5188 }
5189#endif
5190#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005191
Greg Griscod6250552011-06-29 14:40:23 -07005192 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005193 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08005194
Dhaval Patel223ec952013-07-18 14:49:44 -07005195 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5196
Matthew Qindefd5562014-07-11 18:02:40 +08005197 /*
5198 * Check power off reason if user force reset,
5199 * if yes phone will do normal boot.
5200 */
5201 if (is_user_force_reset())
5202 goto normal_boot;
5203
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005204 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005205 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005206 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005207 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005208 reboot_device(EMERGENCY_DLOAD);
5209 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5210
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005211 boot_into_fastboot = true;
5212 }
5213 if (!boot_into_fastboot)
5214 {
5215 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5216 boot_into_recovery = 1;
5217 if (!boot_into_recovery &&
5218 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005219 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005220 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005221 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005222 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005223 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005224 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005225
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005226#if USE_PON_REBOOT_REG
5227 reboot_mode = check_hard_reboot_mode();
5228#else
Ajay Dudani77421292010-10-27 19:34:06 -07005229 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005230#endif
5231 if (reboot_mode == RECOVERY_MODE)
5232 {
Ajay Dudani77421292010-10-27 19:34:06 -07005233 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005234 }
5235 else if(reboot_mode == FASTBOOT_MODE)
5236 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005237 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005238 }
5239 else if(reboot_mode == ALARM_BOOT)
5240 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005241 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005242 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305243#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305244 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005245 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305246 if (reboot_mode == DM_VERITY_ENFORCING)
5247 {
5248 device.verity_mode = 1;
5249 write_device_info(&device);
5250 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005251#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305252 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005253#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305254 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005255#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305256 {
5257 device.verity_mode = 0;
5258 write_device_info(&device);
5259 }
5260 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5261 {
5262 if(send_delete_keys_to_tz())
5263 ASSERT(0);
5264 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005265 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305266#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005267
Matthew Qindefd5562014-07-11 18:02:40 +08005268normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005269 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005270 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005271 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005272 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005273 if(emmc_recovery_init())
5274 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5275 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005276 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005277 if((device.is_unlocked) || (device.is_tampered))
5278 {
5279 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305280 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005281 #endif
5282 #if USE_PCOM_SECBOOT
5283 set_tamper_flag(device.is_tampered);
5284 #endif
5285 }
Shashank Mittala0032282011-08-26 14:50:11 -07005286 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005287
Mayank Grover351a75e2017-05-30 20:06:08 +05305288retry_boot:
5289 /* Trying to boot active partition */
5290 if (partition_multislot_is_supported())
5291 {
5292 boot_slot = partition_find_boot_slot();
5293 partition_mark_active_slot(boot_slot);
5294 if (boot_slot == INVALID)
5295 goto fastboot;
5296 }
5297
5298 boot_err_type = boot_linux_from_mmc();
5299 switch (boot_err_type)
5300 {
5301 case ERR_INVALID_PAGE_SIZE:
5302 case ERR_DT_PARSE:
5303 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305304 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305305 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305306 {
5307 /*
5308 * Deactivate current slot, as it failed to
5309 * boot, and retry next slot.
5310 */
5311 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305312 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305313 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305314 else
5315 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305316 default:
5317 break;
5318 /* going to fastboot menu */
5319 }
Shashank Mittala0032282011-08-26 14:50:11 -07005320 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005321 else
5322 {
5323 recovery_init();
5324 #if USE_PCOM_SECBOOT
5325 if((device.is_unlocked) || (device.is_tampered))
5326 set_tamper_flag(device.is_tampered);
5327 #endif
5328 boot_linux_from_flash();
5329 }
5330 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5331 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005332 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005333
Mayank Grover351a75e2017-05-30 20:06:08 +05305334fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07005335 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005336
Amol Jadi5edf3552013-07-23 14:15:34 -07005337 /* register aboot specific fastboot commands */
5338 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005339
Amol Jadi5edf3552013-07-23 14:15:34 -07005340 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005341 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005342
5343 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305344#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005345 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305346#else
5347 /* Add salt buffer offset at start of image address to copy VB salt */
5348 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5349 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5350#endif
lijuang4ece1e72015-08-14 21:02:36 +08005351#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005352 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005353#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005354}
5355
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005356uint32_t get_page_size()
5357{
5358 return page_size;
5359}
5360
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005361/*
5362 * Calculated and save hash (SHA256) for non-signed boot image.
5363 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005364 * @param image_addr - Boot image address
5365 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005366 *
5367 * @return int - 0 on success, negative value on failure.
5368 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005369static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005370{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005371 unsigned int digest[8];
5372#if IMAGE_VERIF_ALGO_SHA1
5373 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5374#else
5375 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5376#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005377
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005378 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005379 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005380
5381 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005382 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005383
5384 return 0;
5385}
5386
Mayank Grover351a75e2017-05-30 20:06:08 +05305387
Brian Swetland9c4c0752009-01-25 16:23:50 -08005388APP_START(aboot)
5389 .init = aboot_init,
5390APP_END