blob: ce00d9b61f8d2f9502e0fe27ab9b614ba41e5849 [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
Umang Chheda4c140de2019-12-19 14:30:38 +053070#include <pm_smbchg_common.h>
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070071#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
lijuang0b17ba22019-09-04 14:20:47 +08001694 recovery_dtbo_actual =
1695 ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
1696 imagesize_actual += recovery_dtbo_actual;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301697
1698 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
1699
1700
1701 dtb_image_offset = page_size + patched_kernel_hdr_size +
1702 kernel_actual + ramdisk_actual + second_actual +
1703 recovery_dtbo_actual;
1704
1705 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1706 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
1707 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301708#endif
1709
Parth Dixit1375d9e2019-07-08 20:56:13 +05301710
Saranya Chidurab1aaf232018-11-19 15:47:50 +05301711 /* Validate the boot/recovery image size is within the bounds of partition size */
1712 if (imagesize_actual > image_size) {
1713 dprintf(CRITICAL, "Image size is greater than partition size.\n");
1714 return -1;
1715 }
1716
Matthew Qin49e51fa2015-02-09 10:40:45 +08001717#if VERIFIED_BOOT
1718 boot_verifier_init();
1719#endif
1720
Mayank Grover1ceaee22019-04-01 17:54:32 +05301721#if VERIFIED_BOOT_2
1722 /* read full partition if device is unlocked */
1723 if (device.is_unlocked)
1724 imagesize_actual = image_size;
1725#endif
1726
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301727 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001728 {
1729 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1730 return -1;
1731 }
1732
Matthew Qinbb7923d2015-02-09 10:56:09 +08001733 /*
1734 * Update loading flow of bootimage to support compressed/uncompressed
1735 * bootimage on both 64bit and 32bit platform.
1736 * 1. Load bootimage from emmc partition onto DDR.
1737 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1738 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1739 * platform accordingly.
1740 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1741 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301742 if (partition_multislot_is_supported())
1743 {
1744 current_active_slot = partition_find_active_slot();
1745 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1746 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1747 }
1748 else
1749 {
1750 dprintf(INFO, "Loading (%s) image (%d): start\n",
1751 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1752 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001753 bs_set_timestamp(BS_KERNEL_LOAD_START);
1754
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301755 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1756 {
1757 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1758 return -1;
1759 }
Kishor PK9e91b592017-05-24 12:14:55 +05301760 offset = page_size;
1761 /* Read image without signature and header*/
1762 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001763 {
1764 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1765 return -1;
1766 }
1767
Mayank Grover351a75e2017-05-30 20:06:08 +05301768 if (partition_multislot_is_supported())
1769 {
1770 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1771 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1772 }
1773 else
1774 {
1775 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001776 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1777
Mayank Grover351a75e2017-05-30 20:06:08 +05301778 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001779 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1780
1781 /* Authenticate Kernel */
1782 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1783 (int) target_use_signed_kernel(),
1784 device.is_unlocked,
1785 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301786#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05301787 /* if device is unlocked skip reading signature, as full partition is read */
1788 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05301789 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05301790 offset = imagesize_actual;
1791 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1792 {
1793 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1794 return -1;
1795 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001796
Mayank Grover1ceaee22019-04-01 17:54:32 +05301797 /* Read signature */
1798 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1799 {
1800 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1801 return -1;
1802 }
Monika Singh292b3e92018-03-17 22:40:23 +05301803 }
1804
Mayank Grover027a9412018-09-04 15:12:05 +05301805 /* load and validate dtbo partition */
1806 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
1807
Mayank Grover9df84c02018-08-30 15:46:35 +05301808 /* load vbmeta partition */
1809 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
1810
Monika Singh292b3e92018-03-17 22:40:23 +05301811 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05301812
1813 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05301814 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05301815 info.images[IMG_BOOT].imgsize = imagesize_actual;
1816 info.images[IMG_BOOT].name = ptn_name;
1817 ++info.num_loaded_images;
1818
1819 /* Pass loaded dtbo image */
1820 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05301821 info.images[IMG_DTBO].image_buffer =
1822 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05301823 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
1824 info.images[IMG_DTBO].name = "dtbo";
1825 ++info.num_loaded_images;
1826 }
1827
Mayank Grover9df84c02018-08-30 15:46:35 +05301828 /* Pass loaded vbmeta image */
1829 if (vbmeta_image_buf != NULL) {
1830 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
1831 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
1832 info.images[IMG_VBMETA].name = "vbmeta";
1833 ++info.num_loaded_images;
1834 }
1835
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301836 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05301837 info.multi_slot_boot = partition_multislot_is_supported();
1838 info.bootreason_alarm = boot_reason_alarm;
1839 info.bootinto_recovery = boot_into_recovery;
1840 status = load_image_and_auth(&info);
1841 if(status)
1842 return -1;
1843
1844 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05301845
1846 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05301847 if (vbmeta_image_buf != NULL) {
1848 free(vbmeta_image_buf);
1849 --info.num_loaded_images;
1850 }
Monika Singh292b3e92018-03-17 22:40:23 +05301851#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001852 /* Change the condition a little bit to include the test framework support.
1853 * We would never reach this point if device is in fastboot mode, even if we did
1854 * that means we are in test mode, so execute kernel authentication part for the
1855 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301856 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001857 {
1858 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301859 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001860 {
1861 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1862 return -1;
1863 }
1864
1865 /* Read signature */
1866 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1867 {
1868 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1869 return -1;
1870 }
1871
1872 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001873 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301874 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001875 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001876 } else {
1877 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1878 #ifdef TZ_SAVE_KERNEL_HASH
1879 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1880 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001881
1882#ifdef MDTP_SUPPORT
1883 {
1884 /* Verify MDTP lock.
1885 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1886 * since verification was skipped in aboot. The signature is not part of the loaded image.
1887 */
1888 mdtp_ext_partition_verification_t ext_partition;
1889 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1890 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1891 ext_partition.page_size = page_size;
1892 ext_partition.image_addr = (uint32)image_addr;
1893 ext_partition.image_size = imagesize_actual;
1894 ext_partition.sig_avail = FALSE;
1895 mdtp_fwlock_verify_lock(&ext_partition);
1896 }
1897#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001898 }
Monika Singh292b3e92018-03-17 22:40:23 +05301899#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001900
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001901#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001902 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001903 {
1904#if FBCON_DISPLAY_MSG
1905 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1906 wait_for_users_action();
1907#else
1908 dprintf(CRITICAL,
1909 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1910 mdelay(5000);
1911#endif
1912 }
1913#endif
1914
1915#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301916 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301917 {
1918 /* set boot and system versions. */
1919 set_os_version((unsigned char *)image_addr);
1920 // send root of trust
1921 if(!send_rot_command((uint32_t)device.is_unlocked))
1922 ASSERT(0);
1923 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301924#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001925 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001926 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1927 * If not, continue booting.
1928 */
1929 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1930 {
1931 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1932 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05301933#if VERIFIED_BOOT_2
1934 if (dtbo_image_sz)
1935 out_avai_len -= DTBO_IMG_BUF;
1936#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04001937 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001938 rc = decompress((unsigned char *)(image_addr + page_size),
1939 hdr->kernel_size, out_addr, out_avai_len,
1940 &dtb_offset, &out_len);
1941 if (rc)
1942 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001943 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001944 ASSERT(0);
1945 }
1946
Matthew Qin0b15b322015-05-19 05:20:54 -04001947 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001948 kptr = (struct kernel64_hdr *)out_addr;
1949 kernel_start_addr = out_addr;
1950 kernel_size = out_len;
1951 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001952 dprintf(INFO, "Uncpmpressed kernel in use\n");
1953 if (!strncmp((char*)(image_addr + page_size),
1954 PATCHED_KERNEL_MAGIC,
1955 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1956 dprintf(INFO, "Patched kernel detected\n");
1957 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1958 PATCHED_KERNEL_HEADER_SIZE);
1959 //The size of the kernel is stored at start of kernel image + 16
1960 //The dtb would start just after the kernel
1961 dtb_offset = *((uint32_t*)((unsigned char*)
1962 (image_addr + page_size +
1963 sizeof(PATCHED_KERNEL_MAGIC) -
1964 1)));
1965 //The actual kernel starts after the 20 byte header.
1966 kernel_start_addr = (unsigned char*)(image_addr +
1967 page_size + PATCHED_KERNEL_HEADER_SIZE);
1968 kernel_size = hdr->kernel_size;
1969 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1970 } else {
1971 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1972 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1973 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1974 kernel_size = hdr->kernel_size;
1975 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001976 }
1977
1978 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001979 * Update the kernel/ramdisk/tags address if the boot image header
1980 * has default values, these default values come from mkbootimg when
1981 * the boot image is flashed using fastboot flash:raw
1982 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001983 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001984
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001985 /* Get virtual addresses since the hdr saves physical addresses. */
1986 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1987 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1988 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001989
Matthew Qinbb7923d2015-02-09 10:56:09 +08001990 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001991 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001992 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301993 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1994 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1995 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001996 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301997 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001998 return -1;
1999 }
2000
2001#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302002 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2003 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002004 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302005 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002006 return -1;
2007 }
2008#endif
2009
Matthew Qin49e51fa2015-02-09 10:40:45 +08002010 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002011 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002012 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002013
Matthew Qin49e51fa2015-02-09 10:40:45 +08002014 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302015 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002016 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2017 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002018
Matthew Qin49e51fa2015-02-09 10:40:45 +08002019 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2020 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2021 return -1;
2022 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002023
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302024 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2025 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302026 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302027 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2028 return -1;
2029 }
2030
Matthew Qin49e51fa2015-02-09 10:40:45 +08002031 /* Find index of device tree within device tree table */
2032 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2033 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2034 return -1;
2035 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002036
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302037 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2038 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2039 return -1;
2040 }
2041
2042 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302043 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302044 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2045 return -1;
2046 }
2047
Matthew Qin271927e2015-03-31 22:07:07 -04002048 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2049 {
2050 unsigned int compressed_size = 0;
2051 out_addr += out_len;
2052 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002053 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002054 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2055 dt_entry.size, out_addr, out_avai_len,
2056 &compressed_size, &dtb_size);
2057 if (rc)
2058 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002059 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002060 ASSERT(0);
2061 }
2062
Matthew Qin0b15b322015-05-19 05:20:54 -04002063 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002064 best_match_dt_addr = out_addr;
2065 } else {
2066 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2067 dtb_size = dt_entry.size;
2068 }
2069
Matthew Qin49e51fa2015-02-09 10:40:45 +08002070 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302071 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2072 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002073 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302074 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002075 return -1;
2076 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002077
Matthew Qin271927e2015-03-31 22:07:07 -04002078 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002079 } else {
2080 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302081 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2082 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002083 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302084 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002085 return -1;
2086 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002087 /*
2088 * If appended dev tree is found, update the atags with
2089 * memory address to the DTB appended location on RAM.
2090 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302091 *
2092 * Make sure everything from scratch address is read before next step!
2093 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002094 */
2095 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302096 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2097
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302098#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302099 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2100
2101 image_buf = (void*)(image_addr);
2102 dtb_offset = dtb_image_offset;
2103 dtb_image_size = imagesize_actual;
2104 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302105#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302106
2107 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002108 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002109 if (!dtb) {
2110 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002111 return -1;
2112 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002113 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002114 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002115
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002116 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2117 target_load_ssd_keystore();
2118
Shashank Mittal23b8f422010-04-16 19:27:21 -07002119unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002120
Dima Zavin77e41f32013-03-06 16:10:43 -08002121 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002122 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002123 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2124
2125 return 0;
2126}
2127
Dima Zavin214cc642009-01-26 11:16:21 -08002128int boot_linux_from_flash(void)
2129{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302130 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002131 struct ptentry *ptn;
2132 struct ptable *ptable;
2133 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002134
Shashank Mittalcd98d472011-08-02 14:29:24 -07002135 unsigned char *image_addr = 0;
2136 unsigned kernel_actual;
2137 unsigned ramdisk_actual;
2138 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302139 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002140
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002141#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302142 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002143 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302144 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002145 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302146 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302147 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302148 unsigned int dtb_size = 0;
2149 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002150#endif
2151
David Ng183a7422009-12-07 14:55:21 -08002152 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302153 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002154 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2155 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2156 return -1;
2157 }
2158 goto continue_boot;
2159 }
2160
Dima Zavin214cc642009-01-26 11:16:21 -08002161 ptable = flash_get_ptable();
2162 if (ptable == NULL) {
2163 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2164 return -1;
2165 }
2166
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002167 if(!boot_into_recovery)
2168 {
2169 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002170
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002171 if (ptn == NULL) {
2172 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2173 return -1;
2174 }
2175 }
2176 else
2177 {
2178 ptn = ptable_find(ptable, "recovery");
2179 if (ptn == NULL) {
2180 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2181 return -1;
2182 }
Dima Zavin214cc642009-01-26 11:16:21 -08002183 }
2184
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302185 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002186 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002187 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2188 return -1;
2189 }
Dima Zavin214cc642009-01-26 11:16:21 -08002190
2191 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002192 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002193 return -1;
2194 }
2195
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002196 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002197 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 -08002198 return -1;
2199 }
2200
Kishor PK9e91b592017-05-24 12:14:55 +05302201 image_addr = (unsigned char *)target_get_scratch_address();
2202 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302203
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002204 /*
2205 * Update the kernel/ramdisk/tags address if the boot image header
2206 * has default values, these default values come from mkbootimg when
2207 * the boot image is flashed using fastboot flash:raw
2208 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002209 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002210
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002211 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002212 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2213 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2214 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2215
2216 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2217 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302218 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002219
Kishor PK9e91b592017-05-24 12:14:55 +05302220 /* ensure commandline is terminated */
2221 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2222
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002223 /* Check if the addresses in the header are valid. */
2224 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302225 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2226 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2227 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002228 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302229 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002230 return -1;
2231 }
2232
2233#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302234 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302235 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2236 return -1;
2237 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302238 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302239
Mayank Grovere559cec2017-10-17 15:12:03 +05302240 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2241 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302242 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302243 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302244 return -1;
2245 }
2246#else
2247
2248#ifndef OSVERSION_IN_BOOTIMAGE
2249 dt_size = hdr->dt_size;
2250#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302251 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302252 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 +05302253 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2254 return -1;
2255 }
2256
Kishor PKd8ddcad2017-07-27 13:53:57 +05302257 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302258
Mayank Grovere559cec2017-10-17 15:12:03 +05302259 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2260 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302261 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302262 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302263 return -1;
2264 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002265#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002266
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302267 /* Read full boot.img from flash */
2268 dprintf(INFO, "Loading (%s) image (%d): start\n",
2269 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2270 bs_set_timestamp(BS_KERNEL_LOAD_START);
2271
2272 if (UINT_MAX - page_size < imagesize_actual)
2273 {
2274 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2275 return -1;
2276 }
2277
2278 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2279 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2280 {
2281 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2282 return -1;
2283 }
2284
2285 offset = page_size;
2286 /* Read image without signature and header */
2287 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2288 {
2289 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2290 return -1;
2291 }
2292
2293 dprintf(INFO, "Loading (%s) image (%d): done\n",
2294 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2295 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2296
Shashank Mittalcd98d472011-08-02 14:29:24 -07002297 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002298 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002299 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002300 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302301
Shashank Mittalcd98d472011-08-02 14:29:24 -07002302 /* Read signature */
2303 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2304 {
2305 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002306 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002307 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002308
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302309 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302310 }
2311 offset = page_size;
2312 if(hdr->second_size != 0) {
2313 if (UINT_MAX - offset < second_actual)
2314 {
2315 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2316 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302317 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302318 offset += second_actual;
2319 /* Second image loading not implemented. */
2320 ASSERT(0);
2321 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302322 /* Move kernel and ramdisk to correct address */
2323 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2324 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2325
2326#if DEVICE_TREE
2327 if(dt_size != 0) {
2328
2329 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2330
2331 table = (struct dt_table*) dt_table_offset;
2332
2333 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2334 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2335 return -1;
2336 }
2337
2338 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2339 goes beyound hdr->dt_size*/
2340 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2341 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2342 return -1;
2343 }
2344
2345 /* Find index of device tree within device tree table */
2346 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2347 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2348 return -1;
2349 }
2350
2351 /* Validate and Read device device tree in the "tags_add */
2352 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2353 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2354 {
2355 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2356 return -1;
2357 }
2358
2359 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2360 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2361 return -1;
2362 }
2363
2364 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2365 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2366 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2367 return -1;
2368 }
2369
2370 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2371 dtb_size = dt_entry.size;
2372 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302373
2374 } else {
2375 /* Validate the tags_addr */
2376 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2377 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2378 {
2379 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2380 return -1;
2381 }
2382 /*
2383 * If appended dev tree is found, update the atags with
2384 * memory address to the DTB appended location on RAM.
2385 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302386 *
2387 * Make sure everything from scratch address is read before next step!
2388 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302389 */
2390 void *dtb = NULL;
2391 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2392 if (!dtb) {
2393 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2394 return -1;
2395 }
2396 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302397#endif
2398 if(target_use_signed_kernel() && (!device.is_unlocked))
2399 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002400 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002401 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002402 {
2403 write_device_info_flash(&device);
2404 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302405#if USE_PCOM_SECBOOT
2406 set_tamper_flag(device.is_tampered);
2407#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002408 }
David Ng183a7422009-12-07 14:55:21 -08002409continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002410
Dima Zavin214cc642009-01-26 11:16:21 -08002411 /* TODO: create/pass atags to kernel */
2412
Ajay Dudanie28a6072011-07-01 13:59:46 -07002413 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002414 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002415 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2416
2417 return 0;
2418}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002419
Shashank Mittal162244e2011-08-08 19:01:25 -07002420void write_device_info_mmc(device_info *dev)
2421{
Shashank Mittal162244e2011-08-08 19:01:25 -07002422 unsigned long long ptn = 0;
2423 unsigned long long size;
2424 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002425 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002426 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302427 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002428
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002429 if (devinfo_present)
2430 index = partition_get_index("devinfo");
2431 else
2432 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002433
Shashank Mittal162244e2011-08-08 19:01:25 -07002434 ptn = partition_get_offset(index);
2435 if(ptn == 0)
2436 {
2437 return;
2438 }
2439
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002440 lun = partition_get_lun(index);
2441 mmc_set_lun(lun);
2442
Shashank Mittal162244e2011-08-08 19:01:25 -07002443 size = partition_get_size(index);
2444
Mayank Groverddd348d2018-01-23 14:07:09 +05302445 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2446 mmc_blocksize_mask);
2447 if (device_info_sz == UINT_MAX)
2448 {
2449 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2450 return;
2451 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002452
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002453 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302454 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002455 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302456 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002457 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002458 {
2459 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002460 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002461 }
2462}
2463
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002464void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002465{
Shashank Mittal162244e2011-08-08 19:01:25 -07002466 unsigned long long ptn = 0;
2467 unsigned long long size;
2468 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002469 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302470 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002471
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002472 if ((index = partition_get_index("devinfo")) < 0)
2473 {
2474 devinfo_present = false;
2475 index = partition_get_index("aboot");
2476 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002477
Shashank Mittal162244e2011-08-08 19:01:25 -07002478 ptn = partition_get_offset(index);
2479 if(ptn == 0)
2480 {
2481 return;
2482 }
2483
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002484 mmc_set_lun(partition_get_lun(index));
2485
Shashank Mittal162244e2011-08-08 19:01:25 -07002486 size = partition_get_size(index);
2487
Mayank Groverddd348d2018-01-23 14:07:09 +05302488 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2489 mmc_blocksize_mask);
2490 if (device_info_sz == UINT_MAX)
2491 {
2492 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2493 return;
2494 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002495
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002496 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302497 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002498 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302499 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002500 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002501 {
2502 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002503 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002504 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002505}
2506
2507void write_device_info_flash(device_info *dev)
2508{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002509 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002510 struct ptentry *ptn;
2511 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002512 if(info == NULL)
2513 {
2514 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2515 ASSERT(0);
2516 }
2517 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002518 ptable = flash_get_ptable();
2519 if (ptable == NULL)
2520 {
2521 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2522 return;
2523 }
2524
2525 ptn = ptable_find(ptable, "devinfo");
2526 if (ptn == NULL)
2527 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002528 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002529 return;
2530 }
2531
Mayank Groverdedbc892017-10-24 13:41:34 +05302532 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002533 memcpy(info, dev, sizeof(device_info));
2534
2535 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2536 {
2537 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2538 return;
2539 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002540 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002541}
2542
vijay kumarc65876c2015-04-24 13:29:16 +05302543static int read_allow_oem_unlock(device_info *dev)
2544{
vijay kumarc65876c2015-04-24 13:29:16 +05302545 unsigned offset;
2546 int index;
2547 unsigned long long ptn;
2548 unsigned long long ptn_size;
2549 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002550 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302551
vijay kumarca2e6812015-07-08 20:28:25 +05302552 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302553 if (index == INVALID_PTN)
2554 {
vijay kumarca2e6812015-07-08 20:28:25 +05302555 index = partition_get_index(frp_ptns[1]);
2556 if (index == INVALID_PTN)
2557 {
2558 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2559 return -1;
2560 }
vijay kumarc65876c2015-04-24 13:29:16 +05302561 }
2562
2563 ptn = partition_get_offset(index);
2564 ptn_size = partition_get_size(index);
2565 offset = ptn_size - blocksize;
2566
Mayank Grover48860402016-11-29 12:34:53 +05302567 /* Set Lun for partition */
2568 mmc_set_lun(partition_get_lun(index));
2569
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002570 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302571 {
2572 dprintf(CRITICAL, "Reading MMC failed\n");
2573 return -1;
2574 }
2575
2576 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2577 is_allow_unlock = buf[blocksize-1] & 0x01;
2578 return 0;
2579}
2580
2581static int write_allow_oem_unlock(bool allow_unlock)
2582{
vijay kumarc65876c2015-04-24 13:29:16 +05302583 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302584 int index;
2585 unsigned long long ptn;
2586 unsigned long long ptn_size;
2587 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002588 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302589
vijay kumarca2e6812015-07-08 20:28:25 +05302590 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302591 if (index == INVALID_PTN)
2592 {
vijay kumarca2e6812015-07-08 20:28:25 +05302593 index = partition_get_index(frp_ptns[1]);
2594 if (index == INVALID_PTN)
2595 {
2596 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2597 return -1;
2598 }
vijay kumarc65876c2015-04-24 13:29:16 +05302599 }
2600
2601 ptn = partition_get_offset(index);
2602 ptn_size = partition_get_size(index);
2603 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302604 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302605
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002606 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302607 {
2608 dprintf(CRITICAL, "Reading MMC failed\n");
2609 return -1;
2610 }
2611
2612 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2613 buf[blocksize-1] = allow_unlock;
2614 if (mmc_write(ptn + offset, blocksize, buf))
2615 {
2616 dprintf(CRITICAL, "Writing MMC failed\n");
2617 return -1;
2618 }
2619
2620 return 0;
2621}
2622
Shashank Mittal162244e2011-08-08 19:01:25 -07002623void read_device_info_flash(device_info *dev)
2624{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002625 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002626 struct ptentry *ptn;
2627 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002628 if(info == NULL)
2629 {
2630 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2631 ASSERT(0);
2632 }
2633 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002634 ptable = flash_get_ptable();
2635 if (ptable == NULL)
2636 {
2637 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2638 return;
2639 }
2640
2641 ptn = ptable_find(ptable, "devinfo");
2642 if (ptn == NULL)
2643 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002644 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002645 return;
2646 }
2647
2648 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2649 {
2650 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2651 return;
2652 }
2653
2654 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2655 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002656 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2657 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002658 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002659 write_device_info_flash(info);
2660 }
2661 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002662 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002663}
2664
2665void write_device_info(device_info *dev)
2666{
2667 if(target_is_emmc_boot())
2668 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002669 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2670 if(info == NULL)
2671 {
2672 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2673 ASSERT(0);
2674 }
2675 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002676 memcpy(info, dev, sizeof(struct device_info));
2677
2678#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302679 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302680 is_secure_boot_enable()) {
2681 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2682 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002683 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002684 else
2685 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002686#else
2687 write_device_info_mmc(info);
2688#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002689 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002690 }
2691 else
2692 {
2693 write_device_info_flash(dev);
2694 }
2695}
2696
Monika Singh94316462018-03-15 18:39:01 +05302697int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2698{
2699 if (!devinfo_present) {
2700 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2701 return -EINVAL;
2702 }
2703 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2704 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2705 __func__, loc, ARRAY_SIZE(device.rollback_index));
2706 ASSERT(0);
2707 }
2708
2709 *roll_back_index = device.rollback_index[loc];
2710 return 0;
2711}
2712
2713int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2714{
2715 if (!devinfo_present) {
2716 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2717 return -EINVAL;
2718 }
2719 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2720 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2721 __func__, loc, ARRAY_SIZE(device.rollback_index));
2722 ASSERT(0);
2723 }
2724
2725 device.rollback_index[loc] = roll_back_index;
2726 write_device_info(&device);
2727 return 0;
2728}
2729
Monika Singhb0fad822018-03-15 18:50:55 +05302730int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2731{
2732 if (!devinfo_present) {
2733 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2734 return -EINVAL;
2735 }
2736
2737 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2738 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2739 return -ENODEV;
2740 }
2741
2742 memcpy(device.user_public_key, user_key, user_key_size);
2743 device.user_public_key_length = user_key_size;
2744 write_device_info(&device);
2745 return 0;
2746}
2747
2748int erase_userkey()
2749{
2750 if (!devinfo_present) {
2751 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2752 return -EINVAL;
2753 }
2754 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2755 device.user_public_key_length = 0;
2756 write_device_info(&device);
2757 return 0;
2758}
2759
2760int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2761{
2762 if (!devinfo_present) {
2763 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2764 return -EINVAL;
2765 }
2766 *user_key = device.user_public_key;
2767 *user_key_size = device.user_public_key_length;
2768 return 0;
2769}
2770
Shashank Mittal162244e2011-08-08 19:01:25 -07002771void read_device_info(device_info *dev)
2772{
2773 if(target_is_emmc_boot())
2774 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002775 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2776 if(info == NULL)
2777 {
2778 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2779 ASSERT(0);
2780 }
2781 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002782
2783#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302784 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302785 is_secure_boot_enable()) {
2786 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2787 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002788 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002789 else
2790 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002791#else
2792 read_device_info_mmc(info);
2793#endif
2794
2795 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2796 {
2797 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002798 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002799 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302800#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302801 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302802 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302803#endif
lijuang511a2b52015-08-14 20:50:51 +08002804 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002805 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302806#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302807 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302808 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302809#endif
lijuang511a2b52015-08-14 20:50:51 +08002810 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002811 info->is_tampered = 0;
2812 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302813#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302814 if (VB_M <= target_get_vb_version())
Monika Singh1c71a1c2019-12-03 12:12:48 +05302815 {
Mayank Grover889be1b2017-09-12 20:12:23 +05302816 info->verity_mode = 1; //enforcing by default
Monika Singh1c71a1c2019-12-03 12:12:48 +05302817 info->user_public_key_length = 0;
2818 memset(info->rollback_index, 0, sizeof(info->rollback_index));
2819 memset(info->user_public_key, 0, sizeof(info->user_public_key));
2820 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302821#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002822 write_device_info(info);
2823 }
jessicatseng1b9da342020-04-22 11:13:20 +08002824
2825//<2020/04/22-JessicaTseng, Enable off-charging
2826 info->charger_screen_enabled = 1;
2827 write_device_info(info);
2828//>2020/04/22-JessicaTseng
2829
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002830 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002831 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002832 }
2833 else
2834 {
2835 read_device_info_flash(dev);
2836 }
2837}
2838
2839void reset_device_info()
2840{
2841 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002842 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002843 write_device_info(&device);
2844}
2845
2846void set_device_root()
2847{
2848 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002849 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002850 write_device_info(&device);
2851}
2852
lijuang4ece1e72015-08-14 21:02:36 +08002853/* set device unlock value
2854 * Must check FRP before call this function
2855 * Need to wipe data when unlock status changed
2856 * type 0: oem unlock
2857 * type 1: unlock critical
2858 * status 0: unlock as false
2859 * status 1: lock as true
2860 */
2861void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002862{
lijuang4ece1e72015-08-14 21:02:36 +08002863 if (type == UNLOCK)
2864 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302865#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302866 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302867 type == UNLOCK_CRITICAL)
2868 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302869#endif
lijuang4ece1e72015-08-14 21:02:36 +08002870 write_device_info(&device);
2871}
2872
2873static void set_device_unlock(int type, bool status)
2874{
2875 int is_unlocked = -1;
2876 char response[MAX_RSP_SIZE];
2877
2878 /* check device unlock status if it is as expected */
2879 if (type == UNLOCK)
2880 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302881#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302882 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302883 type == UNLOCK_CRITICAL)
2884 {
2885 is_unlocked = device.is_unlock_critical;
2886 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302887#endif
lijuang4ece1e72015-08-14 21:02:36 +08002888 if (is_unlocked == status) {
2889 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2890 fastboot_info(response);
2891 fastboot_okay("");
2892 return;
2893 }
2894
2895 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002896 if (status && !is_allow_unlock) {
2897 fastboot_fail("oem unlock is not allowed");
2898 return;
2899 }
lijuang21f12f52015-08-22 16:22:19 +08002900
lijuang4ece1e72015-08-14 21:02:36 +08002901#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002902 display_unlock_menu(type, status);
2903 fastboot_okay("");
2904 return;
lijuang4ece1e72015-08-14 21:02:36 +08002905#else
lijuang07c5d6e2018-04-23 18:32:13 +08002906 if (status && type == UNLOCK) {
2907 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2908 return;
lijuang21f12f52015-08-22 16:22:19 +08002909 }
lijuang07c5d6e2018-04-23 18:32:13 +08002910#endif
lijuang4ece1e72015-08-14 21:02:36 +08002911
2912 set_device_unlock_value(type, status);
2913
2914 /* wipe data */
2915 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302916 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002917 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2918 write_misc(0, &msg, sizeof(msg));
2919
2920 fastboot_okay("");
2921 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002922}
2923
lijuang511a2b52015-08-14 20:50:51 +08002924static bool critical_flash_allowed(const char * entry)
2925{
2926 uint32_t i = 0;
2927 if (entry == NULL)
2928 return false;
2929
2930 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2931 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2932 return true;
2933 }
2934 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002935}
2936
2937#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002938int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2939{
2940 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002941 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302942 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002943 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302944 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002945 unsigned int compressed_size = 0;
2946 unsigned int dtb_size = 0;
2947 unsigned int out_avai_len = 0;
2948 unsigned char *out_addr = NULL;
2949 unsigned char *best_match_dt_addr = NULL;
2950 int rc;
2951
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302952 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07002953
Parth Dixit4097b622016-03-15 14:42:27 +05302954#ifndef OSVERSION_IN_BOOTIMAGE
2955 dt_size = hdr->dt_size;
2956#endif
2957
2958 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002959 /* add kernel offset */
2960 dt_image_offset += page_size;
2961 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2962 dt_image_offset += n;
2963
2964 /* add ramdisk offset */
2965 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2966 dt_image_offset += n;
2967
2968 /* add second offset */
2969 if(hdr->second_size != 0) {
2970 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2971 dt_image_offset += n;
2972 }
2973
2974 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002975 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002976
Deepa Dinamani19648b42013-09-05 17:05:55 -07002977 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002978 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2979 return -1;
2980 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302981
2982 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2983 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302984 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302985 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2986 return -1;
2987 }
2988
Joel Kingaa335dc2013-06-03 16:11:08 -07002989 /* Find index of device tree within device tree table */
2990 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002991 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2992 return -1;
2993 }
2994
Matthew Qin271927e2015-03-31 22:07:07 -04002995 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2996 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2997 {
2998 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2999 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04003000 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003001 rc = decompress(best_match_dt_addr,
3002 dt_entry.size, out_addr, out_avai_len,
3003 &compressed_size, &dtb_size);
3004 if (rc)
3005 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003006 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003007 ASSERT(0);
3008 }
3009
Matthew Qin0b15b322015-05-19 05:20:54 -04003010 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003011 best_match_dt_addr = out_addr;
3012 } else {
3013 dtb_size = dt_entry.size;
3014 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003015 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303016 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3017 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003018 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303019 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003020 return -1;
3021 }
3022
Amol Jadicb524072012-08-09 16:40:18 -07003023 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003024 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003025 } else
3026 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003027
3028 /* Everything looks fine. Return success. */
3029 return 0;
3030}
3031#endif
3032
Brian Swetland9c4c0752009-01-25 16:23:50 -08003033void cmd_boot(const char *arg, void *data, unsigned sz)
3034{
3035 unsigned kernel_actual;
3036 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303037 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003038 uint32_t image_actual;
3039 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303040 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003041 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003042 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003043 int ret = 0;
3044 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003045 unsigned int out_len = 0;
3046 unsigned int out_avai_len = 0;
3047 unsigned char *out_addr = NULL;
3048 uint32_t dtb_offset = 0;
3049 unsigned char *kernel_start_addr = NULL;
3050 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003051 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303052#if VERIFIED_BOOT_2
3053 void *dtbo_image_buf = NULL;
3054 uint32_t dtbo_image_sz = 0;
3055 void *vbmeta_image_buf = NULL;
3056 uint32_t vbmeta_image_sz = 0;
3057#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303058#if !VERIFIED_BOOT_2
3059 uint32_t sig_actual = 0;
3060 uint32_t sig_size = 0;
3061#ifdef MDTP_SUPPORT
3062 static bool is_mdtp_activated = 0;
3063#endif /* MDTP_SUPPORT */
3064#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003065
lijuang2008ff22016-03-07 17:56:27 +08003066#if FBCON_DISPLAY_MSG
3067 /* Exit keys' detection thread firstly */
3068 exit_menu_keys_detection();
3069#endif
3070
Monika Singh292b3e92018-03-17 22:40:23 +05303071#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003072 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003073 {
3074 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003075 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003076 }
3077#endif
3078
Brian Swetland9c4c0752009-01-25 16:23:50 -08003079 if (sz < sizeof(hdr)) {
3080 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003081 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003082 }
3083
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303084 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003085
3086 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003087 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003088
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003089 if(target_is_emmc_boot() && hdr->page_size) {
3090 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003091 page_mask = page_size - 1;
3092 }
3093
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003094 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3095 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303096 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003097#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303098#ifndef OSVERSION_IN_BOOTIMAGE
3099 dt_size = hdr->dt_size;
3100#endif
3101 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003102#endif
3103
3104 image_actual = ADD_OF(page_size, kernel_actual);
3105 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303106 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003107 image_actual = ADD_OF(image_actual, dt_actual);
3108
Kishor PK5134b332017-05-09 17:50:08 +05303109 /* Checking to prevent oob access in read_der_message_length */
3110 if (image_actual > sz) {
3111 fastboot_fail("bootimage header fields are invalid");
3112 goto boot_failed;
3113 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303114
Monika Singh292b3e92018-03-17 22:40:23 +05303115#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303116 /* Pass size of boot partition, as imgsize, to avoid
3117 read fewer bytes error */
3118 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303119
3120 /* load and validate dtbo partition */
3121 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3122
3123 /* load vbmeta partition */
3124 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3125
Monika Singh292b3e92018-03-17 22:40:23 +05303126 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303127
3128 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3129 info.images[IMG_BOOT].imgsize = image_actual;
3130 info.images[IMG_BOOT].name = "boot";
3131 ++info.num_loaded_images;
3132
3133 /* Pass loaded dtbo image */
3134 if (dtbo_image_buf != NULL) {
3135 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3136 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3137 info.images[IMG_DTBO].name = "dtbo";
3138 ++info.num_loaded_images;
3139 }
3140
3141 /* Pass loaded vbmeta image */
3142 if (vbmeta_image_buf != NULL) {
3143 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3144 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3145 info.images[IMG_VBMETA].name = "vbmeta";
3146 ++info.num_loaded_images;
3147 }
3148
Monika Singh292b3e92018-03-17 22:40:23 +05303149 info.multi_slot_boot = partition_multislot_is_supported();
3150 if (load_image_and_auth(&info))
3151 goto boot_failed;
3152 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303153
3154 /* Free the buffer allocated to vbmeta post verification */
3155 if (vbmeta_image_buf != NULL) {
3156 free(vbmeta_image_buf);
3157 --info.num_loaded_images;
3158 }
Monika Singh292b3e92018-03-17 22:40:23 +05303159#else
Kishor PK5134b332017-05-09 17:50:08 +05303160 sig_size = sz - image_actual;
3161
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303162 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303163 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303164 /* Calculate the signature length from boot image */
3165 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303166 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303167 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003168
Monika Singh292b3e92018-03-17 22:40:23 +05303169 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303170 fastboot_fail("bootimage header fields are invalid");
3171 goto boot_failed;
3172 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003173 }
3174
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003175 // Initialize boot state before trying to verify boot.img
3176#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003177 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303178#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003179 /* Handle overflow if the input image size is greater than
3180 * boot image buffer can hold
3181 */
Monika Singh292b3e92018-03-17 22:40:23 +05303182 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003183 {
3184 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003185 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003186 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003187
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003188 /* Verify the boot image
3189 * device & page_size are initialized in aboot_init
3190 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003191 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003192 /* Pass size excluding signature size, otherwise we would try to
3193 * access signature beyond its length
3194 */
Monika Singh292b3e92018-03-17 22:40:23 +05303195 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003196 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003197#ifdef MDTP_SUPPORT
3198 else
3199 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003200 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003201 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003202
3203 if (!is_mdtp_activated) {
3204 ext_partition.partition = MDTP_PARTITION_NONE;
3205 mdtp_fwlock_verify_lock(&ext_partition);
3206 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003207 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003208
Amir Kotzer7c768c02016-04-13 09:08:05 +03003209 /* If mdtp state cannot be validate, block fastboot boot*/
3210 if(mdtp_activated(&is_mdtp_activated)){
3211 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3212 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3213 goto boot_failed;
3214 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003215 if(is_mdtp_activated){
3216 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003217 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003218 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003219#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303220#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003221
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003222#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303223 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303224 {
3225 /* set boot and system versions. */
3226 set_os_version((unsigned char *)data);
3227 // send root of trust
3228 if(!send_rot_command((uint32_t)device.is_unlocked))
3229 ASSERT(0);
3230 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303231#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003232 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003233 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3234 * If not, continue booting.
3235 */
3236 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3237 {
3238 out_addr = (unsigned char *)target_get_scratch_address();
3239 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3240 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303241#if VERIFIED_BOOT_2
3242 if (dtbo_image_sz)
3243 out_avai_len -= DTBO_IMG_BUF;
3244#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003245 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003246 ret = decompress((unsigned char *)(ptr + page_size),
3247 hdr->kernel_size, out_addr, out_avai_len,
3248 &dtb_offset, &out_len);
3249 if (ret)
3250 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003251 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003252 ASSERT(0);
3253 }
3254
Matthew Qin0b15b322015-05-19 05:20:54 -04003255 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003256 kptr = (struct kernel64_hdr *)out_addr;
3257 kernel_start_addr = out_addr;
3258 kernel_size = out_len;
3259 } else {
3260 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3261 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3262 kernel_size = hdr->kernel_size;
3263 }
3264
3265 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003266 * Update the kernel/ramdisk/tags address if the boot image header
3267 * has default values, these default values come from mkbootimg when
3268 * the boot image is flashed using fastboot flash:raw
3269 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003270 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003271
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003272 /* Get virtual addresses since the hdr saves physical addresses. */
3273 hdr->kernel_addr = VA(hdr->kernel_addr);
3274 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3275 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003276
Matthew Qinbb7923d2015-02-09 10:56:09 +08003277 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003278 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003279 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303280 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3281 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3282 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003283 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303284 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003285 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003286 }
3287
Amol Jadicb524072012-08-09 16:40:18 -07003288#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003289 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003290 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003291 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003292
3293 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003294#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303295 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3296 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003297 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303298 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003299 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003300 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003301#endif
3302
3303 /* Load ramdisk & kernel */
3304 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003305 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003306
3307#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303308 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3309 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003310 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303311 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003312 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003313 }
3314
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003315 /*
3316 * If dtb is not found look for appended DTB in the kernel.
3317 * If appended dev tree is found, update the atags with
3318 * memory address to the DTB appended location on RAM.
3319 * Else update with the atags address in the kernel header
3320 */
3321 if (!dtb_copied) {
3322 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003323 dtb = dev_tree_appended((void*)(ptr + page_size),
3324 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003325 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003326 if (!dtb) {
3327 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003328 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003329 }
Amol Jadicb524072012-08-09 16:40:18 -07003330 }
3331#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003332
3333 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003334 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003335
Dima Zavin77e41f32013-03-06 16:10:43 -08003336 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003337 (const char*) hdr->cmdline, board_machtype(),
3338 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003339
3340 /* fastboot already stop, it's no need to show fastboot menu */
3341 return;
3342boot_failed:
3343#if FBCON_DISPLAY_MSG
3344 /* revert to fastboot menu if boot failed */
3345 display_fastboot_menu();
3346#endif
3347 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003348}
3349
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003350void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003351{
3352 struct ptentry *ptn;
3353 struct ptable *ptable;
3354
3355 ptable = flash_get_ptable();
3356 if (ptable == NULL) {
3357 fastboot_fail("partition table doesn't exist");
3358 return;
3359 }
3360
3361 ptn = ptable_find(ptable, arg);
3362 if (ptn == NULL) {
3363 fastboot_fail("unknown partition name");
3364 return;
3365 }
3366
Monika Singh292b3e92018-03-17 22:40:23 +05303367 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3368 dprintf(INFO, "erasing avb_custom_key\n");
3369 if (erase_userkey()) {
3370 fastboot_fail("Erasing avb_custom_key failed");
3371 } else {
3372 fastboot_okay("");
3373 }
3374 return;
3375 }
3376
Dima Zavin214cc642009-01-26 11:16:21 -08003377 if (flash_erase(ptn)) {
3378 fastboot_fail("failed to erase partition");
3379 return;
3380 }
3381 fastboot_okay("");
3382}
3383
Bikas Gurungd48bd242010-09-04 19:54:32 -07003384
3385void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3386{
3387 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003388 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003389 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003390 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303391 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003392
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003393#if VERIFIED_BOOT
3394 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3395 {
3396 if(!device.is_unlocked)
3397 {
3398 fastboot_fail("unlock device to erase keystore");
3399 return;
3400 }
3401 }
3402#endif
3403
Kinson Chikf1a43512011-07-14 11:28:39 -07003404 index = partition_get_index(arg);
3405 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003406 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003407
Monika Singhc4778b72018-05-16 11:16:42 +05303408 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3409 dprintf(INFO, "erasing avb_custom_key\n");
3410 if (erase_userkey()) {
3411 fastboot_fail("Erasing avb_custom_key failed");
3412 } else {
3413 fastboot_okay("");
3414 }
3415 return;
3416 }
3417
Kinson Chikf1a43512011-07-14 11:28:39 -07003418 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003419 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003420 return;
3421 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003422
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003423 lun = partition_get_lun(index);
3424 mmc_set_lun(lun);
3425
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003426 if (platform_boot_dev_isemmc())
3427 {
3428 if (mmc_erase_card(ptn, size)) {
3429 fastboot_fail("failed to erase partition\n");
3430 return;
3431 }
3432 } else {
3433 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3434 size = partition_get_size(index);
3435 if (size > DEFAULT_ERASE_SIZE)
3436 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003437
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003438 /* Simple inefficient version of erase. Just writing
3439 0 in first several blocks */
3440 if (mmc_write(ptn , size, (unsigned int *)out)) {
3441 fastboot_fail("failed to erase partition");
3442 return;
3443 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303444 /*Erase FDE metadata at the userdata footer*/
3445 if(!(strncmp(arg, "userdata", 8)))
3446 {
3447 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3448 memset((void *)footer, 0, FOOTER_SIZE);
3449
3450 size = partition_get_size(index);
3451
3452 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3453 fastboot_fail("failed to erase userdata footer");
3454 free(footer);
3455 return;
3456 }
3457 free(footer);
3458 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003459 }
Monika Singh292b3e92018-03-17 22:40:23 +05303460#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303461 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303462 !(strncmp(arg, "userdata", 8)) &&
3463 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003464 ASSERT(0);
3465#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003466 fastboot_okay("");
3467}
3468
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003469void cmd_erase(const char *arg, void *data, unsigned sz)
3470{
Monika Singh292b3e92018-03-17 22:40:23 +05303471#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003472 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003473 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003474 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003475 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003476 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003477 return;
3478 }
3479 }
3480#endif
3481
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003482 if(target_is_emmc_boot())
3483 cmd_erase_mmc(arg, data, sz);
3484 else
3485 cmd_erase_nand(arg, data, sz);
3486}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003487
Mayank Grover11ff9692018-01-11 11:54:49 +05303488/* Get the size from partiton name */
3489static void get_partition_size(const char *arg, char *response)
3490{
3491 uint64_t ptn = 0;
3492 uint64_t size;
3493 int index = INVALID_PTN;
3494
3495 index = partition_get_index(arg);
3496
3497 if (index == INVALID_PTN)
3498 {
3499 dprintf(CRITICAL, "Invalid partition index\n");
3500 return;
3501 }
3502
3503 ptn = partition_get_offset(index);
3504
3505 if(!ptn)
3506 {
3507 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3508 return;
3509 }
3510
3511 size = partition_get_size(index);
3512
3513 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3514 return;
3515}
3516
Mayank Grover52cd10a2018-03-15 12:57:54 +05303517/* Function to check partition type of a partition*/
3518static fs_signature_type
3519check_partition_fs_signature(const char *arg)
3520{
3521 fs_signature_type ret = NO_FS;
3522 int index;
3523 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303524 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3525 uint32_t sb_blk_offset = 0;
3526 char *sb_buffer = buffer;
3527
Mayank Grover52cd10a2018-03-15 12:57:54 +05303528 if (!sb_buffer)
3529 {
3530 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3531 goto out;
3532 }
3533
3534 /* Read super block */
3535 if ((index = partition_get_index(arg)) < 0)
3536 {
3537 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3538 goto out;
3539 }
3540 ptn = partition_get_offset(index);
3541 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303542 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3543
3544 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303545 (void *)sb_buffer, mmc_blocksize))
3546 {
3547 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3548 goto out;
3549 }
3550
Mayank Grover399826a2018-08-27 12:15:15 +05303551 if (sb_blk_offset == 0)
3552 sb_buffer += FS_SUPERBLOCK_OFFSET;
3553
3554 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303555 {
3556 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3557 ret = EXT_FS_SIGNATURE;
3558 }
Mayank Grover399826a2018-08-27 12:15:15 +05303559 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303560 {
3561 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3562 ret = EXT_F2FS_SIGNATURE;
3563 }
3564 else
3565 {
3566 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3567 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3568 ret = NO_FS;
3569 }
3570
3571out:
Mayank Grover399826a2018-08-27 12:15:15 +05303572 if(buffer)
3573 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303574 return ret;
3575}
3576
Mayank Groverd38fe012018-03-13 15:33:16 +05303577/* Function to get partition type */
3578static void get_partition_type(const char *arg, char *response)
3579{
3580 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303581 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303582
3583 if (arg == NULL ||
3584 response == NULL)
3585 {
3586 dprintf(CRITICAL, "Invalid input parameter\n");
3587 return;
3588 }
3589
3590 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303591 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303592
3593 /* Mark partiton type for known paritions only */
3594 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3595 {
3596 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3597 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303598 /* Check partition for FS signature */
3599 fs_signature = check_partition_fs_signature(arg);
3600 switch (fs_signature)
3601 {
3602 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303603 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303604 break;
3605 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303606 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303607 break;
3608 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303609 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303610 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303611 }
3612 }
3613 return;
3614}
3615
Mayank Grover11ff9692018-01-11 11:54:49 +05303616/*
3617 * Publish the partition type & size info
3618 * fastboot getvar will publish the required information.
3619 * fastboot getvar partition_size:<partition_name>: partition size in hex
3620 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3621 */
3622static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3623{
Mayank Groverd38fe012018-03-13 15:33:16 +05303624 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303625 static bool published = false;
3626 struct partition_entry *ptn_entry =
3627 partition_get_partition_entries();
3628 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3629
3630 for (i = 0; i < num_parts; i++) {
3631 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3632 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3633 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3634
Mayank Groverd38fe012018-03-13 15:33:16 +05303635 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303636 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303637 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3638 {
3639 dprintf(CRITICAL, "partition size name truncated\n");
3640 return;
3641 }
3642 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3643 {
3644 dprintf(CRITICAL, "partition type name truncated\n");
3645 return;
3646 }
3647
3648 if (!published)
3649 {
3650 /* publish partition size & type info */
3651 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3652 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3653 }
3654 }
3655 if (!published)
3656 published = true;
3657}
3658
3659
Ajay Dudani5c761132011-04-07 20:19:04 -07003660void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003661{
3662 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003663 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003664 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003665 char *token = NULL;
3666 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003667 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003668 uint8_t lun = 0;
3669 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303670 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003671
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003672 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003673 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003674 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003675 if(token)
3676 {
3677 lun = atoi(token);
3678 mmc_set_lun(lun);
3679 lun_set = true;
3680 }
3681
Mao Jinlong226f33a2014-07-04 17:24:10 +08003682 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003683 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003684 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3685 {
3686 if (!aboot_frp_unlock(pname, data, sz))
3687 {
3688 fastboot_info("FRP unlock successful");
3689 fastboot_okay("");
3690 }
3691 else
3692 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3693
3694 return;
3695 }
3696
Mao Jinlong226f33a2014-07-04 17:24:10 +08003697 if (!strcmp(pname, "partition"))
3698 {
3699 dprintf(INFO, "Attempt to write partition image.\n");
3700 if (write_partition(sz, (unsigned char *) data)) {
3701 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003702 return;
3703 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303704 /* Re-publish partition table */
3705 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303706
3707 /* Rescan partition table to ensure we have multislot support*/
3708 if (partition_scan_for_multislot())
3709 {
3710 current_active_slot = partition_find_active_slot();
3711 dprintf(INFO, "Multislot supported: Slot %s active",
3712 (SUFFIX_SLOT(current_active_slot)));
3713 }
3714 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003715 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003716 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003717 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003718#if VERIFIED_BOOT
3719 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3720 {
3721 if(!device.is_unlocked)
3722 {
3723 fastboot_fail("unlock device to flash keystore");
3724 return;
3725 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303726 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003727 {
3728 fastboot_fail("image is not a keystore file");
3729 return;
3730 }
3731 }
3732#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003733 index = partition_get_index(pname);
3734 ptn = partition_get_offset(index);
3735 if(ptn == 0) {
3736 fastboot_fail("partition table doesn't exist");
3737 return;
3738 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003739
Mayank Grover351a75e2017-05-30 20:06:08 +05303740 if (!strncmp(pname, "boot", strlen("boot"))
3741 || !strcmp(pname, "recovery"))
3742 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003743 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3744 fastboot_fail("image is not a boot image");
3745 return;
3746 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303747
3748 /* Reset multislot_partition attributes in case of flashing boot */
3749 if (partition_multislot_is_supported())
3750 {
3751 partition_reset_attributes(index);
3752 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003753 }
3754
3755 if(!lun_set)
3756 {
3757 lun = partition_get_lun(index);
3758 mmc_set_lun(lun);
3759 }
3760
3761 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303762 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003763 fastboot_fail("size too large");
3764 return;
3765 }
3766 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3767 fastboot_fail("flash write failure");
3768 return;
3769 }
Greg Grisco6e754772011-06-23 12:19:39 -07003770 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003771 }
3772 fastboot_okay("");
3773 return;
3774}
3775
Ajay Dudanide984792015-03-02 09:57:41 -08003776void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3777{
3778 int i, images;
3779 meta_header_t *meta_header;
3780 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303781 /*End of the image address*/
3782 uintptr_t data_end;
3783
3784 if( (UINT_MAX - sz) > (uintptr_t)data )
3785 data_end = (uintptr_t)data + sz;
3786 else
3787 {
3788 fastboot_fail("Cannot flash: image header corrupt");
3789 return;
3790 }
3791
3792 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3793 {
3794 fastboot_fail("Cannot flash: image header corrupt");
3795 return;
3796 }
Ajay Dudanide984792015-03-02 09:57:41 -08003797
lijuang8ee21882016-04-19 16:57:11 +08003798 /* If device is locked:
3799 * Forbid to flash image to avoid the device to bypass the image
3800 * which with "any" name other than bootloader. Because it maybe
3801 * a meta package of all partitions.
3802 */
Monika Singh292b3e92018-03-17 22:40:23 +05303803#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003804 if (target_build_variant_user()) {
3805 if (!device.is_unlocked) {
3806 fastboot_fail("Device is locked, meta image flashing is not allowed");
3807 return;
3808 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303809
Mayank Grover912eaa62017-10-26 12:08:53 +05303810 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303811 !device.is_unlock_critical)
3812 {
lijuang8ee21882016-04-19 16:57:11 +08003813 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3814 return;
3815 }
lijuang8ee21882016-04-19 16:57:11 +08003816 }
3817#endif
3818
Ajay Dudanide984792015-03-02 09:57:41 -08003819 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303820 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3821 {
3822 fastboot_fail("Cannot flash: image header corrupt");
3823 return;
3824 }
Ajay Dudanide984792015-03-02 09:57:41 -08003825 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3826
3827 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3828
3829 for (i=0; i<images; i++) {
3830
3831 if((img_header_entry[i].ptn_name == NULL) ||
3832 (img_header_entry[i].start_offset == 0) ||
3833 (img_header_entry[i].size == 0))
3834 break;
Kishor PK49831502017-04-21 17:55:43 +05303835 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3836 fastboot_fail("Integer overflow detected in start_offset of img");
3837 break;
3838 }
3839 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3840 fastboot_fail("Integer overflow detected in size of img");
3841 break;
3842 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303843 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3844 + img_header_entry[i].size) )
3845 {
3846 fastboot_fail("Cannot flash: image size mismatch");
3847 break;
3848 }
3849
Ajay Dudanide984792015-03-02 09:57:41 -08003850 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3851 (void *) data + img_header_entry[i].start_offset,
3852 img_header_entry[i].size);
3853 }
3854
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003855 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3856 {
3857 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3858 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3859 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3860 }
3861 else
3862 {
3863 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3864 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3865 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3866 }
3867
3868 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003869 fastboot_okay("");
3870 return;
3871}
3872
Ajay Dudani5c761132011-04-07 20:19:04 -07003873void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3874{
3875 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003876 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003877 uint32_t *fill_buf = NULL;
3878 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303879 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003880 sparse_header_t *sparse_header;
3881 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003882 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003883 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303884 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003885 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303886 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003887 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303888 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303889 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003890
Kinson Chikf1a43512011-07-14 11:28:39 -07003891 index = partition_get_index(arg);
3892 ptn = partition_get_offset(index);
3893 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003894 fastboot_fail("partition table doesn't exist");
3895 return;
3896 }
3897
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303898 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303899
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003900 lun = partition_get_lun(index);
3901 mmc_set_lun(lun);
3902
vijay kumar800255e2015-04-24 20:26:19 +05303903 if (sz < sizeof(sparse_header_t)) {
3904 fastboot_fail("size too low");
3905 return;
3906 }
3907
Ajay Dudani5c761132011-04-07 20:19:04 -07003908 /* Read and skip over sparse image header */
3909 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303910
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303911 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3912 fastboot_fail("Invalid block size\n");
3913 return;
3914 }
3915
vijay kumar1321f342015-03-27 12:13:42 +05303916 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003917 fastboot_fail("size too large");
3918 return;
3919 }
3920
vijay kumarde4fcf62015-04-23 13:05:49 +05303921 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303922
Parth Dixit64b1a482016-03-07 16:31:26 +05303923 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303924 fastboot_fail("buffer overreads occured due to invalid sparse header");
3925 return;
3926 }
3927
vijay kumarde4fcf62015-04-23 13:05:49 +05303928 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003929 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303930 fastboot_fail("sparse header size mismatch");
3931 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003932 }
3933
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003934 dprintf (SPEW, "=== Sparse Image Header ===\n");
3935 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3936 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3937 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3938 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3939 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3940 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3941 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3942 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003943
3944 /* Start processing chunks */
3945 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3946 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303947 /* Make sure the total image size does not exceed the partition size */
3948 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3949 fastboot_fail("size too large");
3950 return;
3951 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003952 /* Read and skip over chunk header */
3953 chunk_header = (chunk_header_t *) data;
3954 data += sizeof(chunk_header_t);
3955
Parth Dixit64b1a482016-03-07 16:31:26 +05303956 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303957 fastboot_fail("buffer overreads occured due to invalid sparse header");
3958 return;
3959 }
3960
Ajay Dudani5c761132011-04-07 20:19:04 -07003961 dprintf (SPEW, "=== Chunk Header ===\n");
3962 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3963 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3964 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3965
vijay kumar800255e2015-04-24 20:26:19 +05303966 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003967 {
vijay kumar800255e2015-04-24 20:26:19 +05303968 fastboot_fail("chunk header size mismatch");
3969 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003970 }
3971
wufeng.jiang813dc352015-06-02 23:02:46 -04003972 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3973
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303974 /* Make sure that the chunk size calculated from sparse image does not
3975 * exceed partition size
3976 */
3977 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3978 {
3979 fastboot_fail("Chunk data size exceeds partition size");
3980 return;
3981 }
3982
Ajay Dudani5c761132011-04-07 20:19:04 -07003983 switch (chunk_header->chunk_type)
3984 {
3985 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003986 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003987 chunk_data_sz))
3988 {
3989 fastboot_fail("Bogus chunk size for chunk type Raw");
3990 return;
3991 }
3992
Parth Dixit64b1a482016-03-07 16:31:26 +05303993 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303994 fastboot_fail("buffer overreads occured due to invalid sparse header");
3995 return;
3996 }
3997
wufeng.jiang813dc352015-06-02 23:02:46 -04003998 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3999 otherwise it will return in the line above
4000 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07004001 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04004002 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07004003 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07004004 {
4005 fastboot_fail("flash write failure");
4006 return;
4007 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304008 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4009 fastboot_fail("Bogus size for RAW chunk type");
4010 return;
4011 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004012 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004013 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004014 break;
4015
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004016 case CHUNK_TYPE_FILL:
4017 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4018 sizeof(uint32_t)))
4019 {
4020 fastboot_fail("Bogus chunk size for chunk type FILL");
4021 return;
4022 }
4023
Mayank Grover4f50bba2017-07-19 18:17:08 +05304024 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4025 /* Integer overflow detected */
4026 if (blk_sz_actual < sparse_header->blk_sz)
4027 {
4028 fastboot_fail("Invalid block size");
4029 return;
4030 }
4031
4032 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004033 if (!fill_buf)
4034 {
4035 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4036 return;
4037 }
4038
Parth Dixit64b1a482016-03-07 16:31:26 +05304039 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304040 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304041 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304042 return;
4043 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004044 fill_val = *(uint32_t *)data;
4045 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004046
4047 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4048 {
4049 fill_buf[i] = fill_val;
4050 }
4051
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304052 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4053 {
4054 fastboot_fail("bogus size for chunk FILL type");
4055 free(fill_buf);
4056 return;
4057 }
4058
wufeng.jiang813dc352015-06-02 23:02:46 -04004059 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004060 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304061 /* Make sure that the data written to partition does not exceed partition size */
4062 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4063 {
4064 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304065 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304066 return;
4067 }
4068
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004069 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4070 sparse_header->blk_sz,
4071 fill_buf))
4072 {
4073 fastboot_fail("flash write failure");
4074 free(fill_buf);
4075 return;
4076 }
4077
4078 total_blocks++;
4079 }
4080
4081 free(fill_buf);
4082 break;
4083
Ajay Dudani5c761132011-04-07 20:19:04 -07004084 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304085 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4086 fastboot_fail("bogus size for chunk DONT CARE type");
4087 return;
4088 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004089 total_blocks += chunk_header->chunk_sz;
4090 break;
4091
Ajay Dudani5c761132011-04-07 20:19:04 -07004092 case CHUNK_TYPE_CRC:
4093 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4094 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004095 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004096 return;
4097 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304098 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4099 fastboot_fail("bogus size for chunk CRC type");
4100 return;
4101 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004102 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304103 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304104 fastboot_fail("integer overflow occured");
4105 return;
4106 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004107 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304108 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304109 fastboot_fail("buffer overreads occured due to invalid sparse header");
4110 return;
4111 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004112 break;
4113
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004114 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004115 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004116 fastboot_fail("Unknown chunk type");
4117 return;
4118 }
4119 }
4120
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004121 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4122 total_blocks, sparse_header->total_blks);
4123
4124 if(total_blocks != sparse_header->total_blks)
4125 {
4126 fastboot_fail("sparse image write failure");
4127 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004128
4129 fastboot_okay("");
4130 return;
4131}
4132
4133void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4134{
4135 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004136 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004137
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004138#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004139 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4140 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004141 int ret=0;
4142 uint32 major_version=0;
4143 uint32 minor_version=0;
4144
4145 ret = scm_svc_version(&major_version,&minor_version);
4146 if(!ret)
4147 {
4148 if(major_version >= 2)
4149 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004150 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004151 {
4152 ret = encrypt_scm((uint32 **) &data, &sz);
4153 if (ret != 0) {
4154 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4155 return;
4156 }
4157
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004158 /* Protect only for SSD */
4159 if (!strcmp(arg, "ssd")) {
4160 ret = scm_protect_keystore((uint32 *) data, sz);
4161 if (ret != 0) {
4162 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4163 return;
4164 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004165 }
4166 }
4167 else
4168 {
4169 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4170 if(ret != 0)
4171 {
4172 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4173 return;
4174 }
4175 }
4176 }
4177 else
4178 {
4179 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4180 magic_number[1] == DECRYPT_MAGIC_1)
4181 {
4182 ret = decrypt_scm((uint32 **) &data, &sz);
4183 if (ret != 0) {
4184 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4185 return;
4186 }
4187 }
4188 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4189 magic_number[1] == ENCRYPT_MAGIC_1)
4190 {
4191 ret = encrypt_scm((uint32 **) &data, &sz);
4192 if (ret != 0) {
4193 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4194 return;
4195 }
4196 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004197 }
4198 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004199 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004200 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004201 dprintf(CRITICAL,"INVALID SVC Version\n");
4202 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004203 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004204#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004205
Monika Singh292b3e92018-03-17 22:40:23 +05304206#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004207 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004208 {
lijuang511a2b52015-08-14 20:50:51 +08004209 /* if device is locked:
4210 * common partition will not allow to be flashed
4211 * critical partition will allow to flash image.
4212 */
4213 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4214 fastboot_fail("Partition flashing is not allowed");
4215 return;
4216 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304217
lijuang511a2b52015-08-14 20:50:51 +08004218 /* if device critical is locked:
4219 * common partition will allow to be flashed
4220 * critical partition will not allow to flash image.
4221 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304222 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304223 !device.is_unlock_critical &&
4224 critical_flash_allowed(arg)) {
4225 fastboot_fail("Critical partition flashing is not allowed");
4226 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004227 }
4228 }
4229#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304230 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4231 dprintf(INFO, "flashing avb_custom_key\n");
4232 if (store_userkey(data, sz)) {
4233 fastboot_fail("Flashing avb_custom_key failed");
4234 } else {
4235 fastboot_okay("");
4236 }
4237 return;
4238 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004239
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004240 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004241 meta_header = (meta_header_t *) data;
4242 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004243 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004244 else if (meta_header->magic == META_HEADER_MAGIC)
4245 cmd_flash_meta_img(arg, data, sz);
4246 else
4247 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004248
4249#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304250 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304251 (!strncmp(arg, "system", 6)) &&
4252 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004253 // reset dm_verity mode to enforcing
4254 device.verity_mode = 1;
4255 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304256#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004257
Ajay Dudani5c761132011-04-07 20:19:04 -07004258 return;
4259}
4260
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004261void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4262{
4263 struct ptentry *sys_ptn;
4264 struct ptable *ptable;
4265
4266 ptable = flash_get_ptable();
4267 if (ptable == NULL) {
4268 fastboot_fail("partition table doesn't exist");
4269 return;
4270 }
4271
4272 sys_ptn = ptable_find(ptable, "system");
4273 if (sys_ptn == NULL) {
4274 fastboot_fail("system partition not found");
4275 return;
4276 }
4277
4278 sz = ROUND_TO_PAGE(sz, page_mask);
4279 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4280 fastboot_fail("update_ubi_vol failed");
4281 else
4282 fastboot_okay("");
4283}
4284
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004285void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004286{
4287 struct ptentry *ptn;
4288 struct ptable *ptable;
4289 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304290 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304291 unsigned bytes_to_round_page = 0;
4292 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004293
Kishor PK38ed93d2017-04-25 14:19:26 +05304294 if((uintptr_t)data > (UINT_MAX - sz)) {
4295 fastboot_fail("Cannot flash: image header corrupt");
4296 return;
4297 }
4298
Dima Zavin214cc642009-01-26 11:16:21 -08004299 ptable = flash_get_ptable();
4300 if (ptable == NULL) {
4301 fastboot_fail("partition table doesn't exist");
4302 return;
4303 }
4304
4305 ptn = ptable_find(ptable, arg);
4306 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004307 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4308 arg);
4309 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004310 return;
4311 }
4312
Monika Singh292b3e92018-03-17 22:40:23 +05304313 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4314 dprintf(INFO, "flashing avb_custom_key\n");
4315 if (store_userkey(data, sz)) {
4316 fastboot_fail("Flashing avb_custom_key failed");
4317 } else {
4318 fastboot_okay("");
4319 }
4320 return;
4321 }
4322
Dima Zavin214cc642009-01-26 11:16:21 -08004323 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304324 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4325 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4326 } else {
4327 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004328 return;
4329 }
4330 }
4331
Amol Jadi5c61a952012-05-04 17:05:35 -07004332 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004333 || !strcmp(ptn->name, "userdata")
4334 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004335 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004336 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004337 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304338 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304339 rounded_size = ROUNDUP(sz, page_size);
4340 bytes_to_round_page = rounded_size - sz;
4341 if (bytes_to_round_page) {
4342 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4343 fastboot_fail("Integer overflow detected");
4344 return;
4345 }
4346 if (((uintptr_t)data + sz + bytes_to_round_page) >
4347 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4348 fastboot_fail("Buffer size is not aligned to page_size");
4349 return;
4350 }
4351 else {
4352 memset(data + sz, 0, bytes_to_round_page);
4353 sz = rounded_size;
4354 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304355 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304356 }
4357
Kishor PK38ed93d2017-04-25 14:19:26 +05304358 /*Checking partition_size for the possible integer overflow */
4359 partition_size = validate_partition_size(ptn);
4360
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304361 if (sz > partition_size) {
4362 fastboot_fail("Image size too large");
4363 return;
4364 }
4365
Dima Zavin214cc642009-01-26 11:16:21 -08004366 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304367 if ((sz > UBI_EC_HDR_SIZE) &&
4368 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004369 if (flash_ubi_img(ptn, data, sz)) {
4370 fastboot_fail("flash write failure");
4371 return;
4372 }
4373 } else {
4374 if (flash_write(ptn, extra, data, sz)) {
4375 fastboot_fail("flash write failure");
4376 return;
4377 }
Dima Zavin214cc642009-01-26 11:16:21 -08004378 }
4379 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4380 fastboot_okay("");
4381}
4382
Kishor PK38ed93d2017-04-25 14:19:26 +05304383
4384static inline uint64_t validate_partition_size(struct ptentry *ptn)
4385{
4386 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4387 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4388 return ptn->length * flash_num_pages_per_blk() * page_size;
4389 }
4390 }
4391 return 0;
4392}
4393
4394
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004395void cmd_flash(const char *arg, void *data, unsigned sz)
4396{
4397 if(target_is_emmc_boot())
4398 cmd_flash_mmc(arg, data, sz);
4399 else
4400 cmd_flash_nand(arg, data, sz);
4401}
4402
Dima Zavin214cc642009-01-26 11:16:21 -08004403void cmd_continue(const char *arg, void *data, unsigned sz)
4404{
4405 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004406 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004407
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004408 if (target_is_emmc_boot())
4409 {
lijuanga40d6302015-07-20 20:10:13 +08004410#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004411 /* Exit keys' detection thread firstly */
4412 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004413#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004414 boot_linux_from_mmc();
4415 }
4416 else
4417 {
4418 boot_linux_from_flash();
4419 }
Dima Zavin214cc642009-01-26 11:16:21 -08004420}
4421
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004422void cmd_reboot(const char *arg, void *data, unsigned sz)
4423{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004424 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004425 fastboot_okay("");
4426 reboot_device(0);
4427}
4428
Mayank Grover351a75e2017-05-30 20:06:08 +05304429void cmd_set_active(const char *arg, void *data, unsigned sz)
4430{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304431 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304432 unsigned i,current_active_slot;
4433 const char *current_slot_suffix;
4434
4435 if (!partition_multislot_is_supported())
4436 {
4437 fastboot_fail("Command not supported");
4438 return;
4439 }
4440
4441 if (arg)
4442 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304443 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304444 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304445 {
4446 current_active_slot = partition_find_active_slot();
4447
4448 /* Check if trying to make curent slot active */
4449 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304450 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4451 (char *)suffix_delimiter, &sp);
4452
Mayank Grover5eb5f692017-07-19 20:16:04 +05304453 if (current_slot_suffix &&
4454 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304455 {
4456 fastboot_okay("Slot already set active");
4457 return;
4458 }
4459 else
4460 {
4461 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4462 {
4463 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304464 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4465 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304466 if (current_slot_suffix &&
4467 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304468 {
lijuang8b03e5f2019-07-12 18:16:19 +08004469 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304470 publish_getvar_multislot_vars();
4471 fastboot_okay("");
4472 return;
4473 }
4474 }
4475 }
4476 }
4477 }
4478 fastboot_fail("Invalid slot suffix.");
4479 return;
4480}
4481
Mayank Grover98c4c742019-04-25 17:21:37 +05304482#if DYNAMIC_PARTITION_SUPPORT
4483void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4484{
4485 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4486 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4487 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4488 fastboot_fail("Failed to update recovery command");
4489 return;
4490 }
4491 fastboot_okay("");
4492 reboot_device(REBOOT_MODE_UNKNOWN);
4493
4494 //shouldn't come here.
4495 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4496 return;
4497}
4498
4499void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4500{
4501 dprintf(INFO, "rebooting the device - recovery\n");
4502 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
4503 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4504 fastboot_fail("Failed to update recovery command");
4505 return;
4506 }
4507 fastboot_okay("");
4508 reboot_device(REBOOT_MODE_UNKNOWN);
4509
4510 //shouldn't come here.
4511 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4512 return;
4513}
4514#endif
4515
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004516void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4517{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004518 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004519 fastboot_okay("");
4520 reboot_device(FASTBOOT_MODE);
4521}
4522
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004523void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4524{
4525 dprintf(INFO, "Enabling charger screen check\n");
4526 device.charger_screen_enabled = 1;
4527 write_device_info(&device);
4528 fastboot_okay("");
4529}
4530
4531void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4532{
4533 dprintf(INFO, "Disabling charger screen check\n");
4534 device.charger_screen_enabled = 0;
4535 write_device_info(&device);
4536 fastboot_okay("");
4537}
4538
lijuanga25d8bb2015-09-16 13:11:52 +08004539void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4540{
4541 char *p = NULL;
4542 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304543 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004544
4545 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304546 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004547 if (p) {
4548 if (!strncmp(p, "0", 1)) {
4549 device.charger_screen_enabled = 0;
4550 } else if (!strncmp(p, "1", 1)) {
4551 device.charger_screen_enabled = 1;
4552 }
4553 }
4554 }
4555
4556 /* update charger_screen_enabled value for getvar
4557 * command
4558 */
4559 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4560 device.charger_screen_enabled);
4561
4562 write_device_info(&device);
4563 fastboot_okay("");
4564}
4565
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304566void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4567{
4568 dprintf(INFO, "Selecting display panel %s\n", arg);
4569 if (arg)
4570 strlcpy(device.display_panel, arg,
4571 sizeof(device.display_panel));
4572 write_device_info(&device);
4573 fastboot_okay("");
4574}
4575
Shashank Mittal162244e2011-08-08 19:01:25 -07004576void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4577{
lijuang4ece1e72015-08-14 21:02:36 +08004578 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304579}
4580
4581void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4582{
lijuang4ece1e72015-08-14 21:02:36 +08004583 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304584 if(!is_allow_unlock) {
4585 fastboot_fail("oem unlock is not allowed");
4586 return;
4587 }
4588
lijuang4ece1e72015-08-14 21:02:36 +08004589 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304590
lijuang4ece1e72015-08-14 21:02:36 +08004591 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304592 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304593 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304594 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4595 write_misc(0, &msg, sizeof(msg));
4596
4597 fastboot_okay("");
4598 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004599 }
4600 fastboot_okay("");
4601}
4602
Channagoud Kadabiad259832015-05-29 11:14:17 -07004603static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4604{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304605 int ret=1;
4606 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004607
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304608 /*
4609 Authentication method not implemented.
4610
4611 OEM to implement, authentication system which on successful validataion,
4612 calls write_allow_oem_unlock() with is_allow_unlock.
4613 */
4614#if 0
4615 authentication_success = oem_specific_auth_mthd();
4616#endif
4617
4618 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004619 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304620 is_allow_unlock = true;
4621 write_allow_oem_unlock(is_allow_unlock);
4622 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004623 }
4624 return ret;
4625}
4626
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004627void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4628{
lijuang4ece1e72015-08-14 21:02:36 +08004629 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004630}
4631
Shashank Mittala0032282011-08-26 14:50:11 -07004632void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4633{
lijuang511a2b52015-08-14 20:50:51 +08004634 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004635 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004636 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004637 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4638 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304639#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304640 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304641 {
4642 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4643 (device.is_unlock_critical ? "true" : "false"));
4644 fastboot_info(response);
4645 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304646#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004647 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004648 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304649 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4650 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004651 fastboot_okay("");
4652}
4653
lijuang511a2b52015-08-14 20:50:51 +08004654void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4655{
4656 char response[MAX_RSP_SIZE];
4657 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4658 fastboot_info(response);
4659 fastboot_okay("");
4660}
4661
4662void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4663{
lijuang4ece1e72015-08-14 21:02:36 +08004664 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004665}
4666
4667void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4668{
lijuang4ece1e72015-08-14 21:02:36 +08004669 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004670}
4671
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004672void cmd_preflash(const char *arg, void *data, unsigned sz)
4673{
4674 fastboot_okay("");
4675}
4676
Mao Flynn7b379f32015-04-20 00:28:30 +08004677static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304678
Mao Flynn7b379f32015-04-20 00:28:30 +08004679int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304680{
Mao Flynn7b379f32015-04-20 00:28:30 +08004681 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304682 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004683 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304684 return -1;
4685 return 0;
4686}
4687
Mao Flynn7b379f32015-04-20 00:28:30 +08004688int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004689{
4690 struct ptentry *ptn;
4691 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004692 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004693 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004694
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304695 ptable = flash_get_ptable();
4696 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004697 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4698 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304699 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004700
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304701 ptn = ptable_find(ptable, "splash");
4702 if (ptn == NULL) {
4703 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004704 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304705 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004706 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304707 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004708 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304709 }
4710
Mao Flynn7b379f32015-04-20 00:28:30 +08004711 header = (struct logo_img_header *)logo_header;
4712 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304713 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004714 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304715 }
4716
4717 fb_display = fbcon_display();
4718 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304719 if (header->type && (header->blocks != 0) &&
4720 (UINT_MAX >= header->blocks * 512) &&
4721 ((header->blocks * 512) <= (fb_display->width *
4722 fb_display->height * (fb_display->bpp / 8)))) {
4723 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08004724 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4725
4726 /* if the logo is full-screen size, remove "fbcon_clear()" */
4727 if ((header->width != fb_display->width)
4728 || (header->height != fb_display->height))
4729 fbcon_clear();
4730
4731 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4732 (uint32_t *)base,
4733 (header->blocks * 512))) {
4734 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4735 return -1;
4736 }
4737 fbcon_extract_to_screen(header, base);
4738 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004739 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004740
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004741 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4742 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004743 return -1;
4744 }
4745
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304746 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304747 uint32_t fb_size = ROUNDUP(fb_display->width *
4748 fb_display->height *
4749 (fb_display->bpp / 8), 4096);
4750 uint32_t splash_size = ((((header->width * header->height *
4751 fb_display->bpp/8) + 511) >> 9) << 9);
4752
4753 if (splash_size > fb_size) {
4754 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4755 return -1;
4756 }
4757
Mao Flynn7b379f32015-04-20 00:28:30 +08004758 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4759 (uint32_t *)base,
4760 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304761 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304762 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004763 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004764 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304765 }
4766
Mao Flynn7b379f32015-04-20 00:28:30 +08004767 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304768}
4769
Mao Flynn7b379f32015-04-20 00:28:30 +08004770int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304771{
4772 int index = INVALID_PTN;
4773 unsigned long long ptn = 0;
4774 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004775 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004776 uint32_t blocksize, realsize, readsize;
4777 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304778
4779 index = partition_get_index("splash");
4780 if (index == 0) {
4781 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004782 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304783 }
4784
4785 ptn = partition_get_offset(index);
4786 if (ptn == 0) {
4787 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004788 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304789 }
4790
Mao Flynn1893a7f2015-06-03 12:03:36 +08004791 mmc_set_lun(partition_get_lun(index));
4792
4793 blocksize = mmc_get_device_blocksize();
4794 if (blocksize == 0) {
4795 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4796 return -1;
4797 }
4798
4799 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004800 if (!fb_display)
4801 {
4802 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4803 return -1;
4804 }
4805
Mao Flynn1893a7f2015-06-03 12:03:36 +08004806 base = (uint8_t *) fb_display->base;
4807
Kishor PKee5c0a32018-03-06 16:49:46 +05304808 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304809 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004810 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304811 }
4812
Mao Flynn1893a7f2015-06-03 12:03:36 +08004813 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004814 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304815 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004816 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304817 }
4818
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304819 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304820 if (header->type && (header->blocks != 0) &&
4821 (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
4822 ((header->blocks * 512) <= (fb_display->width *
4823 fb_display->height * (fb_display->bpp / 8)))) {
4824 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08004825 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004826
Mao Flynn1893a7f2015-06-03 12:03:36 +08004827 realsize = header->blocks * 512;
4828 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4829
4830 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004831 if ((header->width != fb_display->width)
4832 || (header->height != fb_display->height))
4833 fbcon_clear();
4834
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304835 uint32_t fb_size = ROUNDUP(fb_display->width *
4836 fb_display->height *
4837 (fb_display->bpp / 8), 4096);
4838
4839 if (readsize > fb_size) {
4840 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4841 return -1;
4842 }
4843
Kishor PKee5c0a32018-03-06 16:49:46 +05304844 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004845 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4846 return -1;
4847 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004848
Mao Flynn1893a7f2015-06-03 12:03:36 +08004849 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4850 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304851
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004852 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4853 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004854 return -1;
4855 }
4856
4857 realsize = header->width * header->height * fb_display->bpp / 8;
4858 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4859
4860 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304861 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004862 fbcon_clear();
4863 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4864 return -1;
4865 }
4866 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304867 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004868 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4869 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4870 return -1;
4871 }
4872 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4873 }
4874 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304875 }
4876
Mao Flynn7b379f32015-04-20 00:28:30 +08004877 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304878}
4879
Mao Flynn7b379f32015-04-20 00:28:30 +08004880int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304881{
4882 if (target_is_emmc_boot()) {
4883 return splash_screen_mmc();
4884 } else {
4885 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004886 }
4887}
4888
Mayank Grover351a75e2017-05-30 20:06:08 +05304889void publish_getvar_multislot_vars()
4890{
4891 int i,count;
4892 static bool published = false;
4893 static char slot_count[MAX_RSP_SIZE];
4894 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4895 static char active_slot_suffix[MAX_RSP_SIZE];
4896 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4897 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4898 const char *tmp;
4899 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304900 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304901
4902 if (!published)
4903 {
4904 /* Update slot meta info */
4905 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4906 partition_get_partition_count());
4907 for(i=0; i<count; i++)
4908 {
4909 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304910 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4911 has_slot_pname[i]);
4912 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304913 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4914 }
4915
4916 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4917 {
4918 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304919 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304920 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4921 "slot-unbootable:%s", tmp);
4922 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4923 "slot-active:%s", tmp);
4924 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4925 "slot-success:%s", tmp);
4926 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4927 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304928 fastboot_publish(slot_info[i].slot_is_unbootable,
4929 slot_info[i].slot_is_unbootable_rsp);
4930 fastboot_publish(slot_info[i].slot_is_active,
4931 slot_info[i].slot_is_active_rsp);
4932 fastboot_publish(slot_info[i].slot_is_succesful,
4933 slot_info[i].slot_is_succesful_rsp);
4934 fastboot_publish(slot_info[i].slot_retry_count,
4935 slot_info[i].slot_retry_count_rsp);
4936 }
4937 fastboot_publish("current-slot", active_slot_suffix);
4938 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4939 fastboot_publish("slot-count", slot_count);
4940 published = true;
4941 }
4942
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304943 active_slt = partition_find_active_slot();
4944 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304945 {
4946 tmp = SUFFIX_SLOT(active_slt);
4947 tmp++; // to remove "_" from slot_suffix.
4948 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4949 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304950 else
4951 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4952
Mayank Grover351a75e2017-05-30 20:06:08 +05304953 /* Update partition meta information */
4954 partition_fill_slot_meta(slot_info);
4955 return;
4956}
4957
lijuang4ece1e72015-08-14 21:02:36 +08004958void get_product_name(unsigned char *buf)
4959{
4960 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4961 return;
4962}
4963
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304964#if PRODUCT_IOT
4965void get_bootloader_version_iot(unsigned char *buf)
4966{
4967 if (buf != NULL)
4968 {
4969 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4970 strlcat(buf, "-", MAX_VERSION_LEN);
4971 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4972 }
4973 return;
4974}
4975#endif
4976
lijuang4ece1e72015-08-14 21:02:36 +08004977void get_bootloader_version(unsigned char *buf)
4978{
4979 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4980 return;
4981}
4982
4983void get_baseband_version(unsigned char *buf)
4984{
4985 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4986 return;
4987}
4988
Monika Singh32a09f72018-03-14 13:08:29 +05304989bool is_device_locked_critical()
4990{
4991 return device.is_unlock_critical ? false:true;
4992}
4993
lijuang4ece1e72015-08-14 21:02:36 +08004994bool is_device_locked()
4995{
4996 return device.is_unlocked ? false:true;
4997}
4998
Monika Singh32a09f72018-03-14 13:08:29 +05304999bool is_verity_enforcing()
5000{
5001 return device.verity_mode ? true:false;
5002}
5003
Amol Jadi5edf3552013-07-23 14:15:34 -07005004/* register commands and variables for fastboot */
5005void aboot_fastboot_register_commands(void)
5006{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005007 int i;
lijuangf16461c2015-08-03 17:09:34 +08005008 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07005009
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005010 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08005011 /* By default the enabled list is empty. */
5012 {"", NULL},
5013 /* move commands enclosed within the below ifndef to here
5014 * if they need to be enabled in user build.
5015 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005016#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005017 /* Register the following commands only for non-user builds */
5018 {"flash:", cmd_flash},
5019 {"erase:", cmd_erase},
5020 {"boot", cmd_boot},
5021 {"continue", cmd_continue},
5022 {"reboot", cmd_reboot},
5023 {"reboot-bootloader", cmd_reboot_bootloader},
5024 {"oem unlock", cmd_oem_unlock},
5025 {"oem unlock-go", cmd_oem_unlock_go},
5026 {"oem lock", cmd_oem_lock},
5027 {"flashing unlock", cmd_oem_unlock},
5028 {"flashing lock", cmd_oem_lock},
5029 {"flashing lock_critical", cmd_flashing_lock_critical},
5030 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5031 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5032 {"oem device-info", cmd_oem_devinfo},
5033 {"preflash", cmd_preflash},
5034 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5035 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005036 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005037 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305038 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305039#if DYNAMIC_PARTITION_SUPPORT
5040 {"reboot-fastboot",cmd_reboot_fastboot},
5041 {"reboot-recovery",cmd_reboot_recovery},
5042#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005043#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005044 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005045#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005046#endif
lijuang511a2b52015-08-14 20:50:51 +08005047 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005048
5049 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5050 for (i = 1; i < fastboot_cmds_count; i++)
5051 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5052
Amol Jadi5edf3552013-07-23 14:15:34 -07005053 /* publish variables and their values */
5054 fastboot_publish("product", TARGET(BOARD));
5055 fastboot_publish("kernel", "lk");
5056 fastboot_publish("serialno", sn_buf);
5057
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305058 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5059 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5060 fastboot_publish("hw-revision", soc_version_str);
5061
Amol Jadi5edf3552013-07-23 14:15:34 -07005062 /*
5063 * partition info is supported only for emmc partitions
5064 * Calling this for NAND prints some error messages which
5065 * is harmless but misleading. Avoid calling this for NAND
5066 * devices.
5067 */
5068 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305069 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005070
Mayank Grover351a75e2017-05-30 20:06:08 +05305071 if (partition_multislot_is_supported())
5072 publish_getvar_multislot_vars();
5073
Amol Jadi5edf3552013-07-23 14:15:34 -07005074 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005075#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005076 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5077 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005078#else
5079 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5080 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5081#endif
5082
Amol Jadi5edf3552013-07-23 14:15:34 -07005083 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005084 /* Is the charger screen check enabled */
5085 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5086 device.charger_screen_enabled);
5087 fastboot_publish("charger-screen-enabled",
5088 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08005089 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305090 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5091 device.display_panel);
5092 fastboot_publish("display-panel",
5093 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305094
5095 if (target_is_emmc_boot())
5096 {
5097 mmc_blocksize = mmc_get_device_blocksize();
5098 }
5099 else
5100 {
5101 mmc_blocksize = flash_block_size();
5102 }
5103 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5104 fastboot_publish("erase-block-size", (const char *) block_size_string);
5105 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305106#if PRODUCT_IOT
5107 get_bootloader_version_iot(&bootloader_version_string);
5108 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5109
5110 /* Version baseband is n/a for apq iot devices */
5111 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305112#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005113 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5114 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305115#endif
lijuangf16461c2015-08-03 17:09:34 +08005116 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305117 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005118 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5119 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5120 target_is_emmc_boot()? "eMMC":"UFS");
5121 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005122#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005123 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005124 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005125 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005126#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305127 if (target_dynamic_partition_supported())
5128 fastboot_publish("is-userspace", "no");
Amol Jadi5edf3552013-07-23 14:15:34 -07005129}
5130
Brian Swetland9c4c0752009-01-25 16:23:50 -08005131void aboot_init(const struct app_descriptor *app)
5132{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005133 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305134 int boot_err_type = 0;
5135 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07005136
lijuang39831732016-01-08 17:49:02 +08005137 /* Initialise wdog to catch early lk crashes */
5138#if WDOG_SUPPORT
5139 msm_wdog_init();
5140#endif
5141
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005142 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005143 if (target_is_emmc_boot())
5144 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005145 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005146 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305147 mmc_blocksize = mmc_get_device_blocksize();
5148 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005149 }
5150 else
5151 {
5152 page_size = flash_page_size();
5153 page_mask = page_size - 1;
5154 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005155 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5156
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005157 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05305158 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005159
Mayank Grover351a75e2017-05-30 20:06:08 +05305160 /* Detect multi-slot support */
5161 if (partition_multislot_is_supported())
5162 {
5163 boot_slot = partition_find_active_slot();
5164 if (boot_slot == INVALID)
5165 {
5166 boot_into_fastboot = true;
5167 dprintf(INFO, "Active Slot: (INVALID)\n");
5168 }
5169 else
5170 {
5171 /* Setting the state of system to boot active slot */
5172 partition_mark_active_slot(boot_slot);
5173 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5174 }
5175 }
5176
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005177 /* Display splash screen if enabled */
5178#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08005179#if NO_ALARM_DISPLAY
5180 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005181#endif
lijuang99c02d82015-02-13 19:04:34 +08005182 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07005183#if DISPLAY_HDMI_PRIMARY
5184 if (!strlen(device.display_panel))
5185 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5186 sizeof(device.display_panel));
5187#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005188#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07005189 /* Wait if the display shutdown is in progress */
5190 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005191 if (!pm_appsbl_display_init_done())
5192 target_display_init(device.display_panel);
5193 else
5194 display_image_on_screen();
5195#else
lijuang99c02d82015-02-13 19:04:34 +08005196 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005197#endif
lijuang99c02d82015-02-13 19:04:34 +08005198 dprintf(SPEW, "Display Init: Done\n");
5199#if NO_ALARM_DISPLAY
5200 }
5201#endif
5202#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005203
Greg Griscod6250552011-06-29 14:40:23 -07005204 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005205 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08005206
Dhaval Patel223ec952013-07-18 14:49:44 -07005207 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5208
Matthew Qindefd5562014-07-11 18:02:40 +08005209 /*
5210 * Check power off reason if user force reset,
5211 * if yes phone will do normal boot.
5212 */
5213 if (is_user_force_reset())
5214 goto normal_boot;
5215
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005216 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005217 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005218 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005219 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005220 reboot_device(EMERGENCY_DLOAD);
5221 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5222
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005223 boot_into_fastboot = true;
5224 }
5225 if (!boot_into_fastboot)
5226 {
5227 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5228 boot_into_recovery = 1;
5229 if (!boot_into_recovery &&
5230 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005231 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005232 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005233 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005234 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005235 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005236 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005237
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005238#if USE_PON_REBOOT_REG
5239 reboot_mode = check_hard_reboot_mode();
5240#else
Ajay Dudani77421292010-10-27 19:34:06 -07005241 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005242#endif
5243 if (reboot_mode == RECOVERY_MODE)
5244 {
Ajay Dudani77421292010-10-27 19:34:06 -07005245 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005246 }
5247 else if(reboot_mode == FASTBOOT_MODE)
5248 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005249 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005250 }
5251 else if(reboot_mode == ALARM_BOOT)
5252 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005253 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005254 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305255#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305256 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005257 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305258 if (reboot_mode == DM_VERITY_ENFORCING)
5259 {
5260 device.verity_mode = 1;
5261 write_device_info(&device);
5262 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005263#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305264 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005265#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305266 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005267#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305268 {
5269 device.verity_mode = 0;
5270 write_device_info(&device);
5271 }
5272 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5273 {
5274 if(send_delete_keys_to_tz())
5275 ASSERT(0);
5276 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005277 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305278#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005279
Matthew Qindefd5562014-07-11 18:02:40 +08005280normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005281 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005282 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005283 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005284 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005285 if(emmc_recovery_init())
5286 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5287 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005288 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005289 if((device.is_unlocked) || (device.is_tampered))
5290 {
5291 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305292 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005293 #endif
5294 #if USE_PCOM_SECBOOT
5295 set_tamper_flag(device.is_tampered);
5296 #endif
5297 }
Shashank Mittala0032282011-08-26 14:50:11 -07005298 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005299
Mayank Grover351a75e2017-05-30 20:06:08 +05305300retry_boot:
5301 /* Trying to boot active partition */
5302 if (partition_multislot_is_supported())
5303 {
5304 boot_slot = partition_find_boot_slot();
5305 partition_mark_active_slot(boot_slot);
5306 if (boot_slot == INVALID)
5307 goto fastboot;
5308 }
5309
5310 boot_err_type = boot_linux_from_mmc();
5311 switch (boot_err_type)
5312 {
5313 case ERR_INVALID_PAGE_SIZE:
5314 case ERR_DT_PARSE:
5315 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305316 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305317 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305318 {
5319 /*
5320 * Deactivate current slot, as it failed to
5321 * boot, and retry next slot.
5322 */
5323 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305324 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305325 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305326 else
5327 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305328 default:
5329 break;
5330 /* going to fastboot menu */
5331 }
Shashank Mittala0032282011-08-26 14:50:11 -07005332 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005333 else
5334 {
5335 recovery_init();
5336 #if USE_PCOM_SECBOOT
5337 if((device.is_unlocked) || (device.is_tampered))
5338 set_tamper_flag(device.is_tampered);
5339 #endif
5340 boot_linux_from_flash();
5341 }
5342 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5343 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005344 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005345
Mayank Grover351a75e2017-05-30 20:06:08 +05305346fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07005347 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005348
Amol Jadi5edf3552013-07-23 14:15:34 -07005349 /* register aboot specific fastboot commands */
5350 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005351
Amol Jadi5edf3552013-07-23 14:15:34 -07005352 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005353 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005354
5355 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305356#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005357 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305358#else
5359 /* Add salt buffer offset at start of image address to copy VB salt */
5360 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5361 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5362#endif
lijuang4ece1e72015-08-14 21:02:36 +08005363#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005364 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005365#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005366}
5367
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005368uint32_t get_page_size()
5369{
5370 return page_size;
5371}
5372
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005373/*
5374 * Calculated and save hash (SHA256) for non-signed boot image.
5375 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005376 * @param image_addr - Boot image address
5377 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005378 *
5379 * @return int - 0 on success, negative value on failure.
5380 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005381static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005382{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005383 unsigned int digest[8];
5384#if IMAGE_VERIF_ALGO_SHA1
5385 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5386#else
5387 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5388#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005389
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005390 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005391 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005392
5393 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005394 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005395
5396 return 0;
5397}
5398
Mayank Grover351a75e2017-05-30 20:06:08 +05305399
Brian Swetland9c4c0752009-01-25 16:23:50 -08005400APP_START(aboot)
5401 .init = aboot_init,
5402APP_END