blob: 5d522f86c9cb036ae0d9409ac91aa952c59514f1 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Ashish Bhimanpalliward57423f2020-09-24 13:50:36 +05305 * Copyright (c) 2009-2020, 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
Ashish Bhimanpalliwar8cb34fd2020-11-12 19:26:41 +0530243static unsigned page_size = BOARD_KERNEL_PAGESIZE;
244
245uint32_t kernel_hdr_page_size()
246{
247 return page_size;
248}
249
250
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700251static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530252static unsigned mmc_blocksize = 0;
253static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700254static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
255static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530256static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800257static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700258static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700259bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530260static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530261static char *vbcmdline;
262static bootinfo info = {0};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530263static void *recovery_dtbo_buf = NULL;
264static uint32_t recovery_dtbo_size = 0;
265
Shashank Mittalcd98d472011-08-02 14:29:24 -0700266/* Assuming unauthorized kernel image by default */
267static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530268static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
269
vijay kumarc65876c2015-04-24 13:29:16 +0530270static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700271
vijay kumarca2e6812015-07-08 20:28:25 +0530272static char frp_ptns[2][8] = {"config","frp"};
273
lijuang511a2b52015-08-14 20:50:51 +0800274static const char *critical_flash_allowed_ptn[] = {
275 "aboot",
276 "rpm",
277 "tz",
278 "sbl",
279 "sdi",
280 "sbl1",
281 "xbl",
282 "hyp",
283 "pmic",
284 "bootloader",
285 "devinfo",
286 "partition"};
287
Dima Zavin42168f22009-01-30 11:52:22 -0800288struct atag_ptbl_entry
289{
290 char name[16];
291 unsigned offset;
292 unsigned size;
293 unsigned flags;
294};
295
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700296/*
297 * Partition info, required to be published
298 * for fastboot
299 */
300struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530301 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700302 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
303 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
304 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
305 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
306};
307
308/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530309 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700310 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530311#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530312#define EXT_STR "ext4"
313#define F2FS_STR "f2fs"
314
315#define FS_SUPERBLOCK_OFFSET 0x400
316#define EXT_MAGIC 0xEF53
317#define EXT_MAGIC_OFFSET_SB 0x38
318#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
319#define F2FS_MAGIC_OFFSET_SB 0x0
320
321typedef enum fs_signature_type {
322 EXT_FS_SIGNATURE = 1,
323 EXT_F2FS_SIGNATURE = 2,
324 NO_FS = -1
325} fs_signature_type;
326
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530327struct getvar_partition_info part_info[NUM_PARTITIONS];
328struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700329{
Mayank Grover49920212018-02-09 18:15:55 +0530330 { "system" , "partition-size:", "partition-type:", "", "ext4" },
331 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
332 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
333 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700334};
335
336char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700337char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800338char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700339char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530340char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530341char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530342char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530343#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530344
345/* For IOT we are using custom version */
346#define PRODUCT_IOT_VERSION "IOT001"
347char bootloader_version_string[MAX_RSP_SIZE];
348#endif
lijuang102dfa92015-10-09 18:31:03 +0800349
350#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800351char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800352char battery_soc_ok [MAX_RSP_SIZE];
353#endif
354
lijuangf16461c2015-08-03 17:09:34 +0800355char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700356
Greg Griscod2471ef2011-07-14 13:00:42 -0700357extern int emmc_recovery_init(void);
358
Kinson Chik0b1c8162011-08-31 16:31:57 -0700359#if NO_KEYPAD_DRIVER
360extern int fastboot_trigger(void);
361#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700362
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530363static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700364{
365 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700366#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800367 if (is_arm64)
368 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
369 else
370 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700371 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
372 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700373#endif
374}
375
Dima Zavin42168f22009-01-30 11:52:22 -0800376static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
377{
378 struct atag_ptbl_entry atag_ptn;
379
380 memcpy(atag_ptn.name, ptn->name, 16);
381 atag_ptn.name[15] = '\0';
382 atag_ptn.offset = ptn->start;
383 atag_ptn.size = ptn->length;
384 atag_ptn.flags = ptn->flags;
385 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
386 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
387}
Mayank Grover9df84c02018-08-30 15:46:35 +0530388#ifdef VERIFIED_BOOT_2
389void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
390{
391 int index = 0;
392 char *vbm_img_buf = NULL;
393 unsigned long long ptn = 0;
394 unsigned long long ptn_size = 0;
395
396 /* Immediately return if dtbo is not supported */
397 index = partition_get_index("vbmeta");
398 ptn = partition_get_offset(index);
399 if(!ptn)
400 {
401 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
402 return;
403 }
404
405 ptn_size = partition_get_size(index);
406 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
407 {
408 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
409 return;
410 }
411
412 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
413 if (!vbm_img_buf)
414 {
415 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
416 return;
417 }
418
419 mmc_set_lun(partition_get_lun(index));
420 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
421 {
422 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
423 free(vbm_img_buf);
424 return;
425 }
426
427 *vbmeta_image_buf = vbm_img_buf;
428 *vbmeta_image_sz = (uint32_t)ptn_size;
429 return;
430}
431#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800432
lijuang102dfa92015-10-09 18:31:03 +0800433#if CHECK_BAT_VOLTAGE
434void update_battery_status(void)
435{
436 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
437 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
438}
439#endif
440
Neeti Desaie245d492012-06-01 12:52:13 -0700441unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800442{
David Ng183a7422009-12-07 14:55:21 -0800443 int cmdline_len = 0;
444 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800445 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700446 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800447 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300448 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700449 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700450 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530451#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530452 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530453#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530454 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530455 int system_ptn_index = -1;
456 unsigned int lun = 0;
457 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530458#if VERITY_LE
459 int syspath_buflen = strlen(verity_dev)
460 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
461 + strlen(verity_params) + sizeof(int) + 2;
462#else
lijuang83ef4b22018-08-23 11:01:55 +0800463 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
464 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
465 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530466 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
467 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530468#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530469 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530470#if HIBERNATION_SUPPORT
471 int resume_buflen = strlen(resume) + sizeof(int) + 2;
472 char resume_buf[resume_buflen];
473 int swap_ptn_index = INVALID_PTN;
474#endif
475
Mayank Grover889be1b2017-09-12 20:12:23 +0530476#if VERIFIED_BOOT
477 uint32_t boot_state = RED;
478#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530479
480#if USE_LE_SYSTEMD
481 is_systemd_present=true;
482#endif
483
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700484#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530485 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530486 {
487 boot_state = boot_verify_get_state();
488 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530489#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700490
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200491#ifdef MDTP_SUPPORT
492 mdtp_activated(&is_mdtp_activated);
493#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800494
Brian Swetland9c4c0752009-01-25 16:23:50 -0800495 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800496 cmdline_len = strlen(cmdline);
497 have_cmdline = 1;
498 }
499 if (target_is_emmc_boot()) {
500 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700501 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530502 if (!boot_dev_buf) {
503 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
504 } else {
505 platform_boot_dev_cmdline(boot_dev_buf);
506#if USE_BOOTDEV_CMDLINE
507 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700508#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530509 if (target_dynamic_partition_supported()) {
510 cmdline_len += strlen(dynamic_bootdev_cmdline);
511 cmdline_len += strlen(boot_dev_buf);
512 }
513 }
David Ng183a7422009-12-07 14:55:21 -0800514 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800515
516 cmdline_len += strlen(usb_sn_cmdline);
517 cmdline_len += strlen(sn_buf);
518
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700519#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530520 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700521 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530522 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
523 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
524 {
525 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
526 ASSERT(0);
527 }
528 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
529 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700530 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530531#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700532
Monika Singh292b3e92018-03-17 22:40:23 +0530533
534 if (vbcmdline != NULL) {
535 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
536 strlen(vbcmdline));
537 cmdline_len += strlen(vbcmdline);
538 }
539
Pavel Nedev5614d222013-06-17 18:01:02 +0300540 if (boot_into_recovery && gpt_exists)
541 cmdline_len += strlen(secondary_gpt_enable);
542
Monika Singh292b3e92018-03-17 22:40:23 +0530543#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200544 if(is_mdtp_activated)
545 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530546#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300547 if (boot_into_ffbm) {
548 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530549
550 if(is_systemd_present)
551 cmdline_len += strlen(systemd_ffbm_mode);
552
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700553 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800554 /* reduce kernel console messages to speed-up boot */
555 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800556 } else if (boot_reason_alarm) {
557 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800558 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800559 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700560 pause_at_bootup = 1;
561 cmdline_len += strlen(battchg_pause);
562 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800563
Shashank Mittalcd98d472011-08-02 14:29:24 -0700564 if(target_use_signed_kernel() && auth_kernel_img) {
565 cmdline_len += strlen(auth_kernel);
566 }
567
Joonwoo Park61112782013-10-02 19:50:39 -0700568 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
569 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530570 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700571 have_target_boot_params = 1;
572 cmdline_len += strlen(target_boot_params);
573 }
574
Ajay Dudanid04110c2011-01-17 23:55:07 -0800575 /* Determine correct androidboot.baseband to use */
576 switch(target_baseband())
577 {
578 case BASEBAND_APQ:
579 cmdline_len += strlen(baseband_apq);
580 break;
581
582 case BASEBAND_MSM:
583 cmdline_len += strlen(baseband_msm);
584 break;
585
586 case BASEBAND_CSFB:
587 cmdline_len += strlen(baseband_csfb);
588 break;
589
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800590 case BASEBAND_SVLTE2A:
591 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800592 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700593
594 case BASEBAND_MDM:
595 cmdline_len += strlen(baseband_mdm);
596 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700597
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800598 case BASEBAND_MDM2:
599 cmdline_len += strlen(baseband_mdm2);
600 break;
601
Amol Jadi5c61a952012-05-04 17:05:35 -0700602 case BASEBAND_SGLTE:
603 cmdline_len += strlen(baseband_sglte);
604 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530605
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800606 case BASEBAND_SGLTE2:
607 cmdline_len += strlen(baseband_sglte2);
608 break;
609
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530610 case BASEBAND_DSDA:
611 cmdline_len += strlen(baseband_dsda);
612 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800613
614 case BASEBAND_DSDA2:
615 cmdline_len += strlen(baseband_dsda2);
616 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530617 case BASEBAND_APQ_NOWGR:
618 cmdline_len += strlen(baseband_apq_nowgr);
619 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800620 }
621
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300622#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800623 if (cmdline) {
624 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530625 target_display_panel_node(display_panel_buf,
626 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800627 strlen(display_panel_buf)) {
628 cmdline_len += strlen(display_panel_buf);
629 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700630 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300631#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700632
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800633 if (target_warm_boot()) {
634 warm_boot = true;
635 cmdline_len += strlen(warmboot_cmdline);
636 }
637
Mayank Grover5384ed82018-05-09 12:09:24 +0530638 if (target_uses_system_as_root() ||
639 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530640 {
641 current_active_slot = partition_find_active_slot();
642 cmdline_len += (strlen(androidboot_slot_suffix)+
643 strlen(SUFFIX_SLOT(current_active_slot)));
644
Mayank Grover3804be72017-06-22 11:59:23 +0530645 system_ptn_index = partition_get_index("system");
646 if (platform_boot_dev_isemmc())
647 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530648#if VERITY_LE
649 /*
650 Condition 4: Verity and A/B both enabled
651 Eventual command line looks like:
652 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
653 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
654 */
655 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
656 verity_dev,
657 verity_system_part, suffix_slot[current_active_slot],
658 verity_params, system_ptn_index + 1);
659#else
660 /*
661 Condition 5: A/B enabled, but verity disabled
662 Eventual command line looks like:
663 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
664 ... root=/dev/mmcblk0p<NN> ...
665 */
666 snprintf(syspath_buf, syspath_buflen, " %s%d",
667 sys_path, system_ptn_index + 1);
668#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530669 }
670 else
671 {
672 lun = partition_get_lun(system_ptn_index);
673 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
674 lun_char_base + lun,
675 partition_get_index_in_lun("system", lun));
676 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530677
Monika Singh292b3e92018-03-17 22:40:23 +0530678#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530679 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530680#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530681 }
682
683 if (target_uses_system_as_root() ||
684 partition_multislot_is_supported())
685 {
686 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530687
688 /* For dynamic partition, support skip skip_initramfs */
689 if (!target_dynamic_partition_supported() &&
690 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530691 cmdline_len += strlen(skip_ramfs);
692 }
693
Mayank Grover466d6562018-05-10 14:52:20 +0530694#if HIBERNATION_SUPPORT
695 if (platform_boot_dev_isemmc())
696 {
697 swap_ptn_index = partition_get_index("swap");
698 if (swap_ptn_index != INVALID_PTN)
699 {
700 snprintf(resume_buf, resume_buflen,
701 " %s%d", resume,
702 (swap_ptn_index + 1));
703 cmdline_len += strlen(resume_buf);
704 }
705 else
706 {
707 dprintf(INFO, "WARNING: swap partition not found\n");
708 }
709 }
710#endif
711
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800712#if TARGET_CMDLINE_SUPPORT
713 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
714 int target_cmd_line_len;
715 ASSERT(target_cmdline_buf);
716 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
717 cmdline_len += target_cmd_line_len;
718#endif
719
lijuang83ef4b22018-08-23 11:01:55 +0800720#if !VERITY_LE
721 dtbo_idx = get_dtbo_idx ();
722 if (dtbo_idx != INVALID_PTN) {
723 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
724 android_boot_dtbo_idx, dtbo_idx);
725 cmdline_len += strlen (dtbo_idx_str);
726 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530727
728 dtb_idx = get_dtb_idx ();
729 if (dtb_idx != INVALID_PTN) {
730 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
731 android_boot_dtb_idx, dtb_idx);
732 cmdline_len += strlen (dtb_idx_str);
733 }
lijuang83ef4b22018-08-23 11:01:55 +0800734#endif
735
David Ng183a7422009-12-07 14:55:21 -0800736 if (cmdline_len > 0) {
737 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800738 unsigned char *dst;
739
740 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
741 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530742 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800743 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700744
Amol Jadi168b7712012-03-06 16:15:00 -0800745 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800746 if (have_cmdline) {
747 src = cmdline;
748 while ((*dst++ = *src++));
749 }
750 if (target_is_emmc_boot()) {
751 src = emmc_cmdline;
752 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700753 have_cmdline = 1;
754 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800755#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700756 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530757 if (have_cmdline &&
758 boot_dev_buf) {
759 --dst;
760 while ((*dst++ = *src++));
761 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700762#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530763 /* Dynamic partition append boot_devices */
764 if (target_dynamic_partition_supported() &&
765 boot_dev_buf) {
766 src = dynamic_bootdev_cmdline;
767 if (have_cmdline) --dst;
768 while ((*dst++ = *src++));
769 src = boot_dev_buf;
770 if (have_cmdline) --dst;
771 while ((*dst++ = *src++));
772 }
David Ngf773dde2010-07-26 19:55:08 -0700773 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800774
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700775#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530776 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700777 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530778 src = verified_state;
779 if(have_cmdline) --dst;
780 have_cmdline = 1;
781 while ((*dst++ = *src++));
782 src = vbsn[boot_state].name;
783 if(have_cmdline) --dst;
784 while ((*dst++ = *src++));
785
786 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
787 {
788 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
789 ASSERT(0);
790 }
791 src = verity_mode;
792 if(have_cmdline) --dst;
793 while ((*dst++ = *src++));
794 src = vbvm[device.verity_mode].name;
795 if(have_cmdline) -- dst;
796 while ((*dst++ = *src++));
797 src = keymaster_v1;
798 if(have_cmdline) --dst;
799 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700800 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530801#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530802
803 if (vbcmdline != NULL) {
804 src = vbcmdline;
805 if (have_cmdline) --dst;
806 while ((*dst++ = *src++));
807 }
808
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800809 src = usb_sn_cmdline;
810 if (have_cmdline) --dst;
811 have_cmdline = 1;
812 while ((*dst++ = *src++));
813 src = sn_buf;
814 if (have_cmdline) --dst;
815 have_cmdline = 1;
816 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800817 if (warm_boot) {
818 if (have_cmdline) --dst;
819 src = warmboot_cmdline;
820 while ((*dst++ = *src++));
821 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800822
Pavel Nedev5614d222013-06-17 18:01:02 +0300823 if (boot_into_recovery && gpt_exists) {
824 src = secondary_gpt_enable;
825 if (have_cmdline) --dst;
826 while ((*dst++ = *src++));
827 }
Monika Singh292b3e92018-03-17 22:40:23 +0530828#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200829 if (is_mdtp_activated) {
830 src = mdtp_activated_flag;
831 if (have_cmdline) --dst;
832 while ((*dst++ = *src++));
833 }
Monika Singh292b3e92018-03-17 22:40:23 +0530834#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300835 if (boot_into_ffbm) {
836 src = androidboot_mode;
837 if (have_cmdline) --dst;
838 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700839 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300840 if (have_cmdline) --dst;
841 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530842
843 if(is_systemd_present) {
844 src = systemd_ffbm_mode;
845 if (have_cmdline) --dst;
846 while ((*dst++ = *src++));
847 }
848
Pavel Nedev898298c2013-02-27 12:36:09 -0800849 src = loglevel;
850 if (have_cmdline) --dst;
851 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800852 } else if (boot_reason_alarm) {
853 src = alarmboot_cmdline;
854 if (have_cmdline) --dst;
855 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300856 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700857 src = battchg_pause;
858 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800859 while ((*dst++ = *src++));
860 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800861
Shashank Mittalcd98d472011-08-02 14:29:24 -0700862 if(target_use_signed_kernel() && auth_kernel_img) {
863 src = auth_kernel;
864 if (have_cmdline) --dst;
865 while ((*dst++ = *src++));
866 }
867
Ajay Dudanid04110c2011-01-17 23:55:07 -0800868 switch(target_baseband())
869 {
870 case BASEBAND_APQ:
871 src = baseband_apq;
872 if (have_cmdline) --dst;
873 while ((*dst++ = *src++));
874 break;
875
876 case BASEBAND_MSM:
877 src = baseband_msm;
878 if (have_cmdline) --dst;
879 while ((*dst++ = *src++));
880 break;
881
882 case BASEBAND_CSFB:
883 src = baseband_csfb;
884 if (have_cmdline) --dst;
885 while ((*dst++ = *src++));
886 break;
887
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800888 case BASEBAND_SVLTE2A:
889 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800890 if (have_cmdline) --dst;
891 while ((*dst++ = *src++));
892 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700893
894 case BASEBAND_MDM:
895 src = baseband_mdm;
896 if (have_cmdline) --dst;
897 while ((*dst++ = *src++));
898 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700899
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800900 case BASEBAND_MDM2:
901 src = baseband_mdm2;
902 if (have_cmdline) --dst;
903 while ((*dst++ = *src++));
904 break;
905
Amol Jadi5c61a952012-05-04 17:05:35 -0700906 case BASEBAND_SGLTE:
907 src = baseband_sglte;
908 if (have_cmdline) --dst;
909 while ((*dst++ = *src++));
910 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530911
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800912 case BASEBAND_SGLTE2:
913 src = baseband_sglte2;
914 if (have_cmdline) --dst;
915 while ((*dst++ = *src++));
916 break;
917
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530918 case BASEBAND_DSDA:
919 src = baseband_dsda;
920 if (have_cmdline) --dst;
921 while ((*dst++ = *src++));
922 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800923
924 case BASEBAND_DSDA2:
925 src = baseband_dsda2;
926 if (have_cmdline) --dst;
927 while ((*dst++ = *src++));
928 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530929 case BASEBAND_APQ_NOWGR:
930 src = baseband_apq_nowgr;
931 if (have_cmdline) --dst;
932 while ((*dst++ = *src++));
933 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800934 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700935
936 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700937 src = display_panel_buf;
938 if (have_cmdline) --dst;
939 while ((*dst++ = *src++));
940 }
Joonwoo Park61112782013-10-02 19:50:39 -0700941
942 if (have_target_boot_params) {
943 if (have_cmdline) --dst;
944 src = target_boot_params;
945 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530946 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700947 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800948
Mayank Grover351a75e2017-05-30 20:06:08 +0530949 if (partition_multislot_is_supported() && have_cmdline)
950 {
951 src = androidboot_slot_suffix;
952 --dst;
953 while ((*dst++ = *src++));
954 --dst;
955 src = SUFFIX_SLOT(current_active_slot);
956 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530957 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530958
Mayank Grover351a75e2017-05-30 20:06:08 +0530959
Mayank Grover5384ed82018-05-09 12:09:24 +0530960 /*
961 * System-As-Root behaviour, system.img should contain both
962 * system content and ramdisk content, and should be mounted at
963 * root(a/b).
964 * Apending skip_ramfs for non a/b builds which use, system as root.
965 */
966 if ((target_uses_system_as_root() ||
967 partition_multislot_is_supported()) &&
968 have_cmdline)
969 {
Mayank Groverb716edf2019-05-08 16:06:55 +0530970 if (!target_dynamic_partition_supported() &&
971 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +0530972 {
973 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +0530974 --dst;
975 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530976 }
977
978 src = sys_path_cmdline;
979 --dst;
980 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530981
Monika Singh292b3e92018-03-17 22:40:23 +0530982#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +0530983 src = syspath_buf;
984 --dst;
985 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +0530986#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530987 }
988
Mayank Grover466d6562018-05-10 14:52:20 +0530989#if HIBERNATION_SUPPORT
990 if (swap_ptn_index != INVALID_PTN)
991 {
992 src = resume_buf;
993 --dst;
994 while ((*dst++ = *src++));
995 }
996#endif
997
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800998#if TARGET_CMDLINE_SUPPORT
999 if (target_cmdline_buf && target_cmd_line_len)
1000 {
1001 if (have_cmdline) --dst;
1002 src = target_cmdline_buf;
1003 while((*dst++ = *src++));
1004 free(target_cmdline_buf);
1005 }
1006#endif
lijuang83ef4b22018-08-23 11:01:55 +08001007
1008#if !VERITY_LE
1009 if (dtbo_idx != INVALID_PTN) {
1010 src = dtbo_idx_str;
1011 --dst;
1012 while ((*dst++ = *src++));
1013 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301014
1015 if (dtb_idx != INVALID_PTN) {
1016 src = dtb_idx_str;
1017 --dst;
1018 while ((*dst++ = *src++));
1019 }
lijuang83ef4b22018-08-23 11:01:55 +08001020#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001021 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001022
1023
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001024 if (boot_dev_buf)
1025 free(boot_dev_buf);
1026
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001027 if (cmdline_final)
1028 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1029 else
1030 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001031 return cmdline_final;
1032}
1033
1034unsigned *atag_core(unsigned *ptr)
1035{
1036 /* CORE */
1037 *ptr++ = 2;
1038 *ptr++ = 0x54410001;
1039
1040 return ptr;
1041
1042}
1043
1044unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1045 unsigned ramdisk_size)
1046{
1047 if (ramdisk_size) {
1048 *ptr++ = 4;
1049 *ptr++ = 0x54420005;
1050 *ptr++ = (unsigned)ramdisk;
1051 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001052 }
1053
Neeti Desaie245d492012-06-01 12:52:13 -07001054 return ptr;
1055}
1056
1057unsigned *atag_ptable(unsigned **ptr_addr)
1058{
1059 int i;
1060 struct ptable *ptable;
1061
1062 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001063 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1064 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001065 *(*ptr_addr)++ = 0x4d534d70;
1066 for (i = 0; i < ptable->count; ++i)
1067 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1068 }
1069
1070 return (*ptr_addr);
1071}
1072
1073unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1074{
1075 int cmdline_length = 0;
1076 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001077 char *dest;
1078
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001079 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001080 n = (cmdline_length + 4) & (~3);
1081
1082 *ptr++ = (n / 4) + 2;
1083 *ptr++ = 0x54410009;
1084 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001085 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001086 ptr += (n / 4);
1087
1088 return ptr;
1089}
1090
1091unsigned *atag_end(unsigned *ptr)
1092{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001093 /* END */
1094 *ptr++ = 0;
1095 *ptr++ = 0;
1096
Neeti Desaie245d492012-06-01 12:52:13 -07001097 return ptr;
1098}
1099
1100void generate_atags(unsigned *ptr, const char *cmdline,
1101 void *ramdisk, unsigned ramdisk_size)
1102{
vijay kumar21a37452015-12-29 16:23:21 +05301103 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001104 ptr = atag_core(ptr);
1105 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1106 ptr = target_atag_mem(ptr);
1107
1108 /* Skip NAND partition ATAGS for eMMC boot */
1109 if (!target_is_emmc_boot()){
1110 ptr = atag_ptable(&ptr);
1111 }
1112
vijay kumar21a37452015-12-29 16:23:21 +05301113 /*
1114 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1115 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1116 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301117 if (!cmdline)
1118 return;
1119
vijay kumar21a37452015-12-29 16:23:21 +05301120 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1121 ptr = atag_cmdline(ptr, cmdline);
1122 ptr = atag_end(ptr);
1123 }
1124 else {
1125 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1126 ASSERT(0);
1127 }
Neeti Desaie245d492012-06-01 12:52:13 -07001128}
1129
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001130typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001131void boot_linux(void *kernel, unsigned *tags,
1132 const char *cmdline, unsigned machtype,
1133 void *ramdisk, unsigned ramdisk_size)
1134{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001135 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001136#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001137 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001138#endif
1139
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001140 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001141 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301142 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001143
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301144 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001145
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001146 final_cmdline = update_cmdline((const char*)cmdline);
1147
Neeti Desai17379b82012-06-04 18:42:53 -07001148#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001149 dprintf(INFO, "Updating device tree: start\n");
1150
Neeti Desai17379b82012-06-04 18:42:53 -07001151 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301152 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001153 if(ret)
1154 {
1155 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1156 ASSERT(0);
1157 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001158 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001159#else
Neeti Desaie245d492012-06-01 12:52:13 -07001160 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001161 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001162#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001163
Monika Singh0ec6db82019-07-18 16:57:58 +05301164#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301165 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301166 {
1167 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001168#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001169#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301170 display_bootverify_menu(DISPLAY_MENU_EIO);
1171 wait_for_users_action();
1172 if(!pwr_key_is_pressed)
1173 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001174#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301175 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001176#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301177 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001178#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301179 dprintf(CRITICAL,
1180 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1181 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001182#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301183 }
lijuangbdd9bb42016-03-01 18:22:17 +08001184 }
lijuangbdd9bb42016-03-01 18:22:17 +08001185#endif
1186
1187#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001188 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001189 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001190 {
1191 dprintf(INFO, "Failed to write protect dev info\n");
1192 ASSERT(0);
1193 }
1194#endif
1195
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001196 /* Turn off splash screen if enabled */
1197#if DISPLAY_SPLASH_SCREEN
1198 target_display_shutdown();
1199#endif
1200
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001201 /* Perform target specific cleanup */
1202 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301203 free_verified_boot_resource(&info);
1204 if (final_cmdline)
1205 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001206
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001207 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301208 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001209
1210 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001211
Amol Jadi4421e652011-06-16 15:00:48 -07001212 /* do any platform specific cleanup before kernel entry */
1213 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001214
Brian Swetland9c4c0752009-01-25 16:23:50 -08001215 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001216
Amol Jadi504f9fe2012-08-16 13:56:48 -07001217#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001218 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001219#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001220 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001221
1222 if (IS_ARM64(kptr))
1223 /* Jump to a 64bit kernel */
1224 scm_elexec_call((paddr_t)kernel, tags_phys);
1225 else
1226 /* Jump to a 32bit kernel */
1227 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001228}
1229
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001230/* Function to check if the memory address range falls within the aboot
1231 * boundaries.
1232 * start: Start of the memory region
1233 * size: Size of the memory region
1234 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301235int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001236{
1237 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001238 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001239 return -1;
1240
1241 /* Check for memory overlap. */
1242 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1243 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001244 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001245 return 0;
1246 else
1247 return -1;
1248}
1249
Mayank Grovere559cec2017-10-17 15:12:03 +05301250/* Function to check if the memory address range falls beyond ddr region.
1251 * start: Start of the memory region
1252 * size: Size of the memory region
1253 */
1254int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1255{
1256 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1257 uint64_t ddr_size = smem_get_ddr_size();
1258 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1259 uintptr_t pa_start_addr = PA(start);
1260
1261 /* Check for boundary conditions. */
1262 if ((UINT_MAX - start) < size)
1263 return -1;
1264
1265 /* Check if memory range is beyond the ddr range. */
1266 if (pa_start_addr < ddr_pa_start_addr ||
1267 pa_start_addr >= (ddr_pa_end_addr) ||
1268 (pa_start_addr + size) > ddr_pa_end_addr)
1269 return -1;
1270 else
1271 return 0;
1272}
1273
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001274BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001275
Mayank Groverdd080e82018-09-04 20:12:13 +05301276int getimage(void **image_buffer, uint32_t *imgsize,
1277 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301278{
Mayank Groverdd080e82018-09-04 20:12:13 +05301279 uint32_t loadedindex;
1280 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301281 imgname == NULL) {
1282 dprintf(CRITICAL, "getimage: invalid parameters\n");
1283 return -1;
1284 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301285 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1286 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301287 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301288 *image_buffer = info.images[loadedindex].image_buffer;
1289 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301290 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1291 info.images[loadedindex].name,
1292 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301293 return 0;
1294 }
1295 }
1296 return -1;
1297}
1298
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001299static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1300{
1301 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001302
1303#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001304#if IMAGE_VERIF_ALGO_SHA1
1305 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1306#else
1307 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1308#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001309#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001310
1311 /* Assume device is rooted at this time. */
1312 device.is_tampered = 1;
1313
1314 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1315
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001316#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301317 uint32_t bootstate;
1318 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301319 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301320 {
1321 ret = boot_verify_image((unsigned char *)bootimg_addr,
1322 bootimg_size, "/recovery", &bootstate);
1323 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001324 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301325 {
1326 ret = boot_verify_image((unsigned char *)bootimg_addr,
1327 bootimg_size, "/boot", &bootstate);
1328 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001329 boot_verify_print_state();
1330#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001331 ret = image_verify((unsigned char *)bootimg_addr,
1332 (unsigned char *)(bootimg_addr + bootimg_size),
1333 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001334 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001335#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001336 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1337
1338 if (ret)
1339 {
1340 /* Authorized kernel */
1341 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001342 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001343 }
1344
Amit Blay4aa292f2015-04-28 21:55:59 +03001345#ifdef MDTP_SUPPORT
1346 {
1347 /* Verify MDTP lock.
1348 * For boot & recovery partitions, use aboot's verification result.
1349 */
1350 mdtp_ext_partition_verification_t ext_partition;
1351 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1352 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1353 ext_partition.page_size = 0; /* Not needed since already validated */
1354 ext_partition.image_addr = 0; /* Not needed since already validated */
1355 ext_partition.image_size = 0; /* Not needed since already validated */
1356 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1357 mdtp_fwlock_verify_lock(&ext_partition);
1358 }
1359#endif /* MDTP_SUPPORT */
1360
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001361#if USE_PCOM_SECBOOT
1362 set_tamper_flag(device.is_tampered);
1363#endif
1364
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001365#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001366 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001367 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001368 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001369#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001370 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001371#if ENABLE_VB_ATTEST
1372 mdelay(DELAY_WAIT);
1373 shutdown_device();
1374#else
lijuanga40d6302015-07-20 20:10:13 +08001375 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001376#endif
lijuanga40d6302015-07-20 20:10:13 +08001377#else
1378 dprintf(CRITICAL,
1379 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1380 mdelay(5000);
1381#endif
1382
1383 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001384 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001385#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001386 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001387 wait_for_users_action();
1388#else
1389 dprintf(CRITICAL,
1390 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1391 mdelay(5000);
1392#endif
1393 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001394 default:
lijuanga40d6302015-07-20 20:10:13 +08001395 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001396 }
1397#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001398#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301399 if(device.is_tampered)
1400 {
1401 write_device_info_mmc(&device);
1402 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301403 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301404 #endif
1405 #ifdef ASSERT_ON_TAMPER
1406 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1407 ASSERT(0);
1408 #endif
1409 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001410#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001411}
1412
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301413static bool check_format_bit()
1414{
1415 bool ret = false;
1416 int index;
1417 uint64_t offset;
1418 struct boot_selection_info *in = NULL;
1419 char *buf = NULL;
1420
1421 index = partition_get_index("bootselect");
1422 if (index == INVALID_PTN)
1423 {
1424 dprintf(INFO, "Unable to locate /bootselect partition\n");
1425 return ret;
1426 }
1427 offset = partition_get_offset(index);
1428 if(!offset)
1429 {
1430 dprintf(INFO, "partition /bootselect doesn't exist\n");
1431 return ret;
1432 }
1433 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301434 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301435 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301436 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301437 {
1438 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1439 free(buf);
1440 return ret;
1441 }
1442 in = (struct boot_selection_info *) buf;
1443 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1444 (in->version == BOOTSELECT_VERSION)) {
1445 if ((in->state_info & BOOTSELECT_FORMAT) &&
1446 !(in->state_info & BOOTSELECT_FACTORY))
1447 ret = true;
1448 } else {
1449 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1450 in->signature, in->version);
1451 ASSERT(0);
1452 }
1453 free(buf);
1454 return ret;
1455}
1456
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001457void boot_verifier_init()
1458{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001459 uint32_t boot_state;
1460 /* Check if device unlock */
1461 if(device.is_unlocked)
1462 {
1463 boot_verify_send_event(DEV_UNLOCK);
1464 boot_verify_print_state();
1465 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1466 return;
1467 }
1468 else
1469 {
1470 boot_verify_send_event(BOOT_INIT);
1471 }
1472
1473 /* Initialize keystore */
1474 boot_state = boot_verify_keystore_init();
1475 if(boot_state == YELLOW)
1476 {
1477 boot_verify_print_state();
1478 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1479 }
1480}
1481
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301482/* Function to return recovery appended dtbo buffer info */
1483void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1484{
1485 *dtbo_size = recovery_dtbo_size;
1486 *dtbo_buf = recovery_dtbo_buf;
1487 return;
1488}
1489
Shashank Mittal23b8f422010-04-16 19:27:21 -07001490int boot_linux_from_mmc(void)
1491{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301492 boot_img_hdr *hdr = (void*) buf;
1493 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001494 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001495 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001496 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001497 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001498
Shashank Mittalcd98d472011-08-02 14:29:24 -07001499 unsigned char *image_addr = 0;
1500 unsigned kernel_actual;
1501 unsigned ramdisk_actual;
1502 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001503 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301504 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001505
Matthew Qin271927e2015-03-31 22:07:07 -04001506 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301507 unsigned dtb_image_size = 0;
1508 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001509 unsigned int out_len = 0;
1510 unsigned int out_avai_len = 0;
1511 unsigned char *out_addr = NULL;
1512 uint32_t dtb_offset = 0;
1513 unsigned char *kernel_start_addr = NULL;
1514 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001515 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301516 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001517 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301518#if VERIFIED_BOOT_2
1519 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301520 void *dtbo_image_buf = NULL;
1521 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301522 void *vbmeta_image_buf = NULL;
1523 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301524#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301525 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001526#if DEVICE_TREE
1527 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001528 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001529 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001530 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001531 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001532 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001533#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001534 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301535 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001536
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301537 if (check_format_bit())
1538 boot_into_recovery = 1;
1539
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001540 if (!boot_into_recovery) {
1541 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1542 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1543 if (rcode <= 0) {
1544 boot_into_ffbm = false;
1545 if (rcode < 0)
1546 dprintf(CRITICAL,"failed to get ffbm cookie");
1547 } else
1548 boot_into_ffbm = true;
1549 } else
1550 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301551 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001552 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1553 dprintf(INFO, "Unified boot method!\n");
1554 hdr = uhdr;
1555 goto unified_boot;
1556 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301557
1558 /* For a/b recovery image code is on boot partition.
1559 If we support multislot, always use boot partition. */
1560 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301561 ((!partition_multislot_is_supported()) ||
1562 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301563 ptn_name = "recovery";
1564 else
1565 ptn_name = "boot";
1566
1567 index = partition_get_index(ptn_name);
1568 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301569 image_size = partition_get_size(index);
1570 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301571 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1572 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001573 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301574
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001575 /* Set Lun for boot & recovery partitions */
1576 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001577
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301578 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001579 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1580 return -1;
1581 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001582
1583 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001584 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301585 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001586 }
1587
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001588 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301589
1590 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1591 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1592 return -1;
1593 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001594 page_size = hdr->page_size;
1595 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001596 }
1597
Matthew Qin49e51fa2015-02-09 10:40:45 +08001598 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1599 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301600 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001601
Matthew Qin49e51fa2015-02-09 10:40:45 +08001602 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301603#if VERIFIED_BOOT_2
1604 /* Create hole in start of image for VB salt to copy */
1605 image_addr += SALT_BUFF_OFFSET;
1606#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301607 memcpy(image_addr, (void *)buf, page_size);
1608
1609 /* ensure commandline is terminated */
1610 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001611
1612#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301613#ifndef OSVERSION_IN_BOOTIMAGE
1614 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301615#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301616 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301617#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301618
Parth Dixit4097b622016-03-15 14:42:27 +05301619 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301620 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 +05301621 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1622 return -1;
1623 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301624 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001625#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301626 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 +05301627 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1628 return -1;
1629 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301630 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001631#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301632 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001633
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301634#ifdef OSVERSION_IN_BOOTIMAGE
1635 /* If header version is ONE and booting into recovery,
1636 dtbo is appended with recovery image.
1637 Doing following:
1638 * Validating the recovery offset and size.
1639 * Extracting recovery dtbo to be used as dtbo.
1640 */
1641 if (boot_into_recovery &&
1642 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1643 {
1644 struct boot_img_hdr_v1 *hdr1 =
1645 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301646 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301647
Mayank Grovera1a83c72018-09-24 11:23:15 +05301648 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301649 if ((hdr1->header_size !=
1650 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1651 {
1652 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1653 return -1;
1654 }
1655
Mayank Grovera1a83c72018-09-24 11:23:15 +05301656 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1657 {
1658 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301659 MAX_SUPPORTED_DTBO_IMG_BUF);
1660 return -1;
1661 }
1662
Mayank Grovera1a83c72018-09-24 11:23:15 +05301663 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301664 {
1665 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1666 return -1;
1667 }
1668
Mayank Grovera1a83c72018-09-24 11:23:15 +05301669 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1670 dprintf(CRITICAL,
1671 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
1672 return -1;
1673 }
1674
1675 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301676 {
1677 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
1678 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
1679 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
1680 return -1;
1681 }
1682
1683 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05301684 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301685 imagesize_actual += recovery_dtbo_size;
1686
1687 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1688 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
1689 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
1690
1691 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301692
1693 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
1694 struct boot_img_hdr_v1 *hdr1 =
1695 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
1696 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
1697 (image_addr + sizeof(boot_img_hdr) +
1698 BOOT_IMAGE_HEADER_V2_OFFSET);
1699 unsigned int recovery_dtbo_actual = 0;
1700
lijuang0b17ba22019-09-04 14:20:47 +08001701 recovery_dtbo_actual =
1702 ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
1703 imagesize_actual += recovery_dtbo_actual;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301704
1705 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
1706
1707
1708 dtb_image_offset = page_size + patched_kernel_hdr_size +
1709 kernel_actual + ramdisk_actual + second_actual +
1710 recovery_dtbo_actual;
1711
1712 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1713 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
1714 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301715#endif
1716
Parth Dixit1375d9e2019-07-08 20:56:13 +05301717
Saranya Chidurab1aaf232018-11-19 15:47:50 +05301718 /* Validate the boot/recovery image size is within the bounds of partition size */
1719 if (imagesize_actual > image_size) {
1720 dprintf(CRITICAL, "Image size is greater than partition size.\n");
1721 return -1;
1722 }
1723
Matthew Qin49e51fa2015-02-09 10:40:45 +08001724#if VERIFIED_BOOT
1725 boot_verifier_init();
1726#endif
1727
Mayank Grover1ceaee22019-04-01 17:54:32 +05301728#if VERIFIED_BOOT_2
1729 /* read full partition if device is unlocked */
1730 if (device.is_unlocked)
1731 imagesize_actual = image_size;
1732#endif
1733
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301734 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001735 {
1736 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1737 return -1;
1738 }
1739
Matthew Qinbb7923d2015-02-09 10:56:09 +08001740 /*
1741 * Update loading flow of bootimage to support compressed/uncompressed
1742 * bootimage on both 64bit and 32bit platform.
1743 * 1. Load bootimage from emmc partition onto DDR.
1744 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1745 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1746 * platform accordingly.
1747 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1748 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301749 if (partition_multislot_is_supported())
1750 {
1751 current_active_slot = partition_find_active_slot();
1752 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1753 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1754 }
1755 else
1756 {
1757 dprintf(INFO, "Loading (%s) image (%d): start\n",
1758 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1759 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001760 bs_set_timestamp(BS_KERNEL_LOAD_START);
1761
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301762 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1763 {
1764 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1765 return -1;
1766 }
Kishor PK9e91b592017-05-24 12:14:55 +05301767 offset = page_size;
1768 /* Read image without signature and header*/
1769 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001770 {
1771 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1772 return -1;
1773 }
1774
Mayank Grover351a75e2017-05-30 20:06:08 +05301775 if (partition_multislot_is_supported())
1776 {
1777 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1778 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1779 }
1780 else
1781 {
1782 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001783 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1784
Mayank Grover351a75e2017-05-30 20:06:08 +05301785 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001786 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1787
1788 /* Authenticate Kernel */
1789 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1790 (int) target_use_signed_kernel(),
1791 device.is_unlocked,
1792 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301793#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05301794 /* if device is unlocked skip reading signature, as full partition is read */
1795 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05301796 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05301797 offset = imagesize_actual;
1798 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1799 {
1800 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1801 return -1;
1802 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001803
Mayank Grover1ceaee22019-04-01 17:54:32 +05301804 /* Read signature */
1805 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1806 {
1807 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1808 return -1;
1809 }
Monika Singh292b3e92018-03-17 22:40:23 +05301810 }
1811
Mayank Grover027a9412018-09-04 15:12:05 +05301812 /* load and validate dtbo partition */
1813 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
1814
Mayank Grover9df84c02018-08-30 15:46:35 +05301815 /* load vbmeta partition */
1816 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
1817
Monika Singh292b3e92018-03-17 22:40:23 +05301818 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05301819
1820 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05301821 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05301822 info.images[IMG_BOOT].imgsize = imagesize_actual;
1823 info.images[IMG_BOOT].name = ptn_name;
1824 ++info.num_loaded_images;
1825
1826 /* Pass loaded dtbo image */
1827 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05301828 info.images[IMG_DTBO].image_buffer =
1829 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05301830 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
1831 info.images[IMG_DTBO].name = "dtbo";
1832 ++info.num_loaded_images;
1833 }
1834
Mayank Grover9df84c02018-08-30 15:46:35 +05301835 /* Pass loaded vbmeta image */
1836 if (vbmeta_image_buf != NULL) {
1837 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
1838 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
1839 info.images[IMG_VBMETA].name = "vbmeta";
1840 ++info.num_loaded_images;
1841 }
1842
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301843 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05301844 info.multi_slot_boot = partition_multislot_is_supported();
1845 info.bootreason_alarm = boot_reason_alarm;
1846 info.bootinto_recovery = boot_into_recovery;
1847 status = load_image_and_auth(&info);
1848 if(status)
1849 return -1;
1850
1851 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05301852
1853 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05301854 if (vbmeta_image_buf != NULL) {
1855 free(vbmeta_image_buf);
1856 --info.num_loaded_images;
1857 }
Monika Singh292b3e92018-03-17 22:40:23 +05301858#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001859 /* Change the condition a little bit to include the test framework support.
1860 * We would never reach this point if device is in fastboot mode, even if we did
1861 * that means we are in test mode, so execute kernel authentication part for the
1862 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301863 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001864 {
1865 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301866 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001867 {
1868 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1869 return -1;
1870 }
1871
1872 /* Read signature */
1873 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1874 {
1875 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1876 return -1;
1877 }
1878
1879 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001880 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301881 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001882 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001883 } else {
1884 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1885 #ifdef TZ_SAVE_KERNEL_HASH
1886 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1887 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001888
1889#ifdef MDTP_SUPPORT
1890 {
1891 /* Verify MDTP lock.
1892 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1893 * since verification was skipped in aboot. The signature is not part of the loaded image.
1894 */
1895 mdtp_ext_partition_verification_t ext_partition;
1896 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1897 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1898 ext_partition.page_size = page_size;
1899 ext_partition.image_addr = (uint32)image_addr;
1900 ext_partition.image_size = imagesize_actual;
1901 ext_partition.sig_avail = FALSE;
1902 mdtp_fwlock_verify_lock(&ext_partition);
1903 }
1904#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001905 }
Monika Singh292b3e92018-03-17 22:40:23 +05301906#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001907
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001908#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001909 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001910 {
1911#if FBCON_DISPLAY_MSG
1912 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1913 wait_for_users_action();
1914#else
1915 dprintf(CRITICAL,
1916 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1917 mdelay(5000);
1918#endif
1919 }
1920#endif
1921
1922#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301923 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301924 {
1925 /* set boot and system versions. */
1926 set_os_version((unsigned char *)image_addr);
1927 // send root of trust
1928 if(!send_rot_command((uint32_t)device.is_unlocked))
1929 ASSERT(0);
1930 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301931#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001932 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001933 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1934 * If not, continue booting.
1935 */
1936 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1937 {
1938 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1939 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05301940#if VERIFIED_BOOT_2
1941 if (dtbo_image_sz)
1942 out_avai_len -= DTBO_IMG_BUF;
1943#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04001944 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001945 rc = decompress((unsigned char *)(image_addr + page_size),
1946 hdr->kernel_size, out_addr, out_avai_len,
1947 &dtb_offset, &out_len);
1948 if (rc)
1949 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001950 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001951 ASSERT(0);
1952 }
1953
Matthew Qin0b15b322015-05-19 05:20:54 -04001954 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001955 kptr = (struct kernel64_hdr *)out_addr;
1956 kernel_start_addr = out_addr;
1957 kernel_size = out_len;
1958 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001959 dprintf(INFO, "Uncpmpressed kernel in use\n");
1960 if (!strncmp((char*)(image_addr + page_size),
1961 PATCHED_KERNEL_MAGIC,
1962 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1963 dprintf(INFO, "Patched kernel detected\n");
1964 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1965 PATCHED_KERNEL_HEADER_SIZE);
1966 //The size of the kernel is stored at start of kernel image + 16
1967 //The dtb would start just after the kernel
1968 dtb_offset = *((uint32_t*)((unsigned char*)
1969 (image_addr + page_size +
1970 sizeof(PATCHED_KERNEL_MAGIC) -
1971 1)));
1972 //The actual kernel starts after the 20 byte header.
1973 kernel_start_addr = (unsigned char*)(image_addr +
1974 page_size + PATCHED_KERNEL_HEADER_SIZE);
1975 kernel_size = hdr->kernel_size;
1976 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1977 } else {
1978 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1979 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1980 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1981 kernel_size = hdr->kernel_size;
1982 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001983 }
1984
1985 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001986 * Update the kernel/ramdisk/tags address if the boot image header
1987 * has default values, these default values come from mkbootimg when
1988 * the boot image is flashed using fastboot flash:raw
1989 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001990 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001991
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001992 /* Get virtual addresses since the hdr saves physical addresses. */
1993 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1994 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1995 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001996
Matthew Qinbb7923d2015-02-09 10:56:09 +08001997 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001998 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001999 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302000 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2001 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2002 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002003 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302004 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002005 return -1;
2006 }
2007
2008#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302009 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2010 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002011 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302012 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002013 return -1;
2014 }
2015#endif
2016
Matthew Qin49e51fa2015-02-09 10:40:45 +08002017 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002018 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002019 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002020
Matthew Qin49e51fa2015-02-09 10:40:45 +08002021 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302022 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002023 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2024 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002025
Matthew Qin49e51fa2015-02-09 10:40:45 +08002026 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2027 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2028 return -1;
2029 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002030
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302031 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2032 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302033 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302034 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2035 return -1;
2036 }
2037
Matthew Qin49e51fa2015-02-09 10:40:45 +08002038 /* Find index of device tree within device tree table */
2039 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2040 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2041 return -1;
2042 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002043
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302044 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2045 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2046 return -1;
2047 }
2048
2049 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302050 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302051 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2052 return -1;
2053 }
2054
Matthew Qin271927e2015-03-31 22:07:07 -04002055 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2056 {
2057 unsigned int compressed_size = 0;
2058 out_addr += out_len;
2059 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002060 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002061 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2062 dt_entry.size, out_addr, out_avai_len,
2063 &compressed_size, &dtb_size);
2064 if (rc)
2065 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002066 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002067 ASSERT(0);
2068 }
2069
Matthew Qin0b15b322015-05-19 05:20:54 -04002070 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002071 best_match_dt_addr = out_addr;
2072 } else {
2073 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2074 dtb_size = dt_entry.size;
2075 }
2076
Matthew Qin49e51fa2015-02-09 10:40:45 +08002077 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302078 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2079 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002080 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302081 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002082 return -1;
2083 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002084
Matthew Qin271927e2015-03-31 22:07:07 -04002085 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002086 } else {
2087 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302088 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2089 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002090 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302091 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002092 return -1;
2093 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002094 /*
2095 * If appended dev tree is found, update the atags with
2096 * memory address to the DTB appended location on RAM.
2097 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302098 *
2099 * Make sure everything from scratch address is read before next step!
2100 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002101 */
2102 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302103 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2104
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302105#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302106 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2107
2108 image_buf = (void*)(image_addr);
2109 dtb_offset = dtb_image_offset;
2110 dtb_image_size = imagesize_actual;
2111 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302112#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302113
2114 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002115 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002116 if (!dtb) {
2117 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002118 return -1;
2119 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002120 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002121 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002122
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002123 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2124 target_load_ssd_keystore();
2125
Shashank Mittal23b8f422010-04-16 19:27:21 -07002126unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002127
Dima Zavin77e41f32013-03-06 16:10:43 -08002128 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002129 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002130 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2131
2132 return 0;
2133}
2134
Dima Zavin214cc642009-01-26 11:16:21 -08002135int boot_linux_from_flash(void)
2136{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302137 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002138 struct ptentry *ptn;
2139 struct ptable *ptable;
2140 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002141
Shashank Mittalcd98d472011-08-02 14:29:24 -07002142 unsigned char *image_addr = 0;
2143 unsigned kernel_actual;
2144 unsigned ramdisk_actual;
2145 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302146 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002147
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002148#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302149 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002150 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302151 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002152 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302153 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302154 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302155 unsigned int dtb_size = 0;
2156 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002157#endif
2158
David Ng183a7422009-12-07 14:55:21 -08002159 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302160 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002161 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2162 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2163 return -1;
2164 }
2165 goto continue_boot;
2166 }
2167
Dima Zavin214cc642009-01-26 11:16:21 -08002168 ptable = flash_get_ptable();
2169 if (ptable == NULL) {
2170 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2171 return -1;
2172 }
2173
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002174 if(!boot_into_recovery)
2175 {
2176 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002177
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002178 if (ptn == NULL) {
2179 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2180 return -1;
2181 }
2182 }
2183 else
2184 {
2185 ptn = ptable_find(ptable, "recovery");
2186 if (ptn == NULL) {
2187 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2188 return -1;
2189 }
Dima Zavin214cc642009-01-26 11:16:21 -08002190 }
2191
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302192 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002193 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002194 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2195 return -1;
2196 }
Dima Zavin214cc642009-01-26 11:16:21 -08002197
2198 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002199 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002200 return -1;
2201 }
2202
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002203 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002204 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 -08002205 return -1;
2206 }
2207
Kishor PK9e91b592017-05-24 12:14:55 +05302208 image_addr = (unsigned char *)target_get_scratch_address();
2209 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302210
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002211 /*
2212 * Update the kernel/ramdisk/tags address if the boot image header
2213 * has default values, these default values come from mkbootimg when
2214 * the boot image is flashed using fastboot flash:raw
2215 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002216 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002217
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002218 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002219 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2220 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2221 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2222
2223 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2224 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302225 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002226
Kishor PK9e91b592017-05-24 12:14:55 +05302227 /* ensure commandline is terminated */
2228 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2229
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002230 /* Check if the addresses in the header are valid. */
2231 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302232 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2233 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2234 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002235 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302236 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002237 return -1;
2238 }
2239
2240#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302241 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302242 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2243 return -1;
2244 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302245 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302246
Mayank Grovere559cec2017-10-17 15:12:03 +05302247 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2248 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302249 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302250 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302251 return -1;
2252 }
2253#else
2254
2255#ifndef OSVERSION_IN_BOOTIMAGE
2256 dt_size = hdr->dt_size;
2257#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302258 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302259 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 +05302260 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2261 return -1;
2262 }
2263
Kishor PKd8ddcad2017-07-27 13:53:57 +05302264 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302265
Mayank Grovere559cec2017-10-17 15:12:03 +05302266 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2267 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302268 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302269 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302270 return -1;
2271 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002272#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002273
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302274 /* Read full boot.img from flash */
2275 dprintf(INFO, "Loading (%s) image (%d): start\n",
2276 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2277 bs_set_timestamp(BS_KERNEL_LOAD_START);
2278
2279 if (UINT_MAX - page_size < imagesize_actual)
2280 {
2281 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2282 return -1;
2283 }
2284
2285 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2286 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2287 {
2288 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2289 return -1;
2290 }
2291
2292 offset = page_size;
2293 /* Read image without signature and header */
2294 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2295 {
2296 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2297 return -1;
2298 }
2299
2300 dprintf(INFO, "Loading (%s) image (%d): done\n",
2301 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2302 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2303
Shashank Mittalcd98d472011-08-02 14:29:24 -07002304 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002305 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002306 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002307 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302308
Shashank Mittalcd98d472011-08-02 14:29:24 -07002309 /* Read signature */
2310 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2311 {
2312 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002313 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002314 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002315
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302316 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302317 }
2318 offset = page_size;
2319 if(hdr->second_size != 0) {
2320 if (UINT_MAX - offset < second_actual)
2321 {
2322 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2323 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302324 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302325 offset += second_actual;
2326 /* Second image loading not implemented. */
2327 ASSERT(0);
2328 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302329 /* Move kernel and ramdisk to correct address */
2330 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2331 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2332
2333#if DEVICE_TREE
2334 if(dt_size != 0) {
2335
2336 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2337
2338 table = (struct dt_table*) dt_table_offset;
2339
2340 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2341 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2342 return -1;
2343 }
2344
2345 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2346 goes beyound hdr->dt_size*/
2347 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2348 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2349 return -1;
2350 }
2351
2352 /* Find index of device tree within device tree table */
2353 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2354 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2355 return -1;
2356 }
2357
2358 /* Validate and Read device device tree in the "tags_add */
2359 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2360 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2361 {
2362 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2363 return -1;
2364 }
2365
2366 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2367 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2368 return -1;
2369 }
2370
2371 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2372 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2373 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2374 return -1;
2375 }
2376
2377 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2378 dtb_size = dt_entry.size;
2379 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302380
2381 } else {
2382 /* Validate the tags_addr */
2383 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2384 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2385 {
2386 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2387 return -1;
2388 }
2389 /*
2390 * If appended dev tree is found, update the atags with
2391 * memory address to the DTB appended location on RAM.
2392 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302393 *
2394 * Make sure everything from scratch address is read before next step!
2395 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302396 */
2397 void *dtb = NULL;
2398 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2399 if (!dtb) {
2400 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2401 return -1;
2402 }
2403 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302404#endif
2405 if(target_use_signed_kernel() && (!device.is_unlocked))
2406 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002407 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002408 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002409 {
2410 write_device_info_flash(&device);
2411 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302412#if USE_PCOM_SECBOOT
2413 set_tamper_flag(device.is_tampered);
2414#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002415 }
David Ng183a7422009-12-07 14:55:21 -08002416continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002417
Dima Zavin214cc642009-01-26 11:16:21 -08002418 /* TODO: create/pass atags to kernel */
2419
Ajay Dudanie28a6072011-07-01 13:59:46 -07002420 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002421 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002422 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2423
2424 return 0;
2425}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002426
Shashank Mittal162244e2011-08-08 19:01:25 -07002427void write_device_info_mmc(device_info *dev)
2428{
Shashank Mittal162244e2011-08-08 19:01:25 -07002429 unsigned long long ptn = 0;
2430 unsigned long long size;
2431 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002432 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002433 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302434 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002435
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002436 if (devinfo_present)
2437 index = partition_get_index("devinfo");
2438 else
2439 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002440
Shashank Mittal162244e2011-08-08 19:01:25 -07002441 ptn = partition_get_offset(index);
2442 if(ptn == 0)
2443 {
2444 return;
2445 }
2446
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002447 lun = partition_get_lun(index);
2448 mmc_set_lun(lun);
2449
Shashank Mittal162244e2011-08-08 19:01:25 -07002450 size = partition_get_size(index);
2451
Mayank Groverddd348d2018-01-23 14:07:09 +05302452 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2453 mmc_blocksize_mask);
2454 if (device_info_sz == UINT_MAX)
2455 {
2456 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2457 return;
2458 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002459
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002460 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302461 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002462 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302463 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002464 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002465 {
2466 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002467 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002468 }
2469}
2470
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002471void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002472{
Shashank Mittal162244e2011-08-08 19:01:25 -07002473 unsigned long long ptn = 0;
2474 unsigned long long size;
2475 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002476 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302477 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002478
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002479 if ((index = partition_get_index("devinfo")) < 0)
2480 {
2481 devinfo_present = false;
2482 index = partition_get_index("aboot");
2483 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002484
Shashank Mittal162244e2011-08-08 19:01:25 -07002485 ptn = partition_get_offset(index);
2486 if(ptn == 0)
2487 {
2488 return;
2489 }
2490
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002491 mmc_set_lun(partition_get_lun(index));
2492
Shashank Mittal162244e2011-08-08 19:01:25 -07002493 size = partition_get_size(index);
2494
Mayank Groverddd348d2018-01-23 14:07:09 +05302495 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2496 mmc_blocksize_mask);
2497 if (device_info_sz == UINT_MAX)
2498 {
2499 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2500 return;
2501 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002502
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002503 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302504 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002505 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302506 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002507 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002508 {
2509 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002510 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002511 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002512}
2513
2514void write_device_info_flash(device_info *dev)
2515{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002516 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002517 struct ptentry *ptn;
2518 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002519 if(info == NULL)
2520 {
2521 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2522 ASSERT(0);
2523 }
2524 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002525 ptable = flash_get_ptable();
2526 if (ptable == NULL)
2527 {
2528 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2529 return;
2530 }
2531
2532 ptn = ptable_find(ptable, "devinfo");
2533 if (ptn == NULL)
2534 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002535 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002536 return;
2537 }
2538
Mayank Groverdedbc892017-10-24 13:41:34 +05302539 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002540 memcpy(info, dev, sizeof(device_info));
2541
2542 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2543 {
2544 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2545 return;
2546 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002547 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002548}
2549
vijay kumarc65876c2015-04-24 13:29:16 +05302550static int read_allow_oem_unlock(device_info *dev)
2551{
vijay kumarc65876c2015-04-24 13:29:16 +05302552 unsigned offset;
2553 int index;
2554 unsigned long long ptn;
2555 unsigned long long ptn_size;
2556 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002557 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302558
vijay kumarca2e6812015-07-08 20:28:25 +05302559 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302560 if (index == INVALID_PTN)
2561 {
vijay kumarca2e6812015-07-08 20:28:25 +05302562 index = partition_get_index(frp_ptns[1]);
2563 if (index == INVALID_PTN)
2564 {
2565 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2566 return -1;
2567 }
vijay kumarc65876c2015-04-24 13:29:16 +05302568 }
2569
2570 ptn = partition_get_offset(index);
2571 ptn_size = partition_get_size(index);
2572 offset = ptn_size - blocksize;
2573
Mayank Grover48860402016-11-29 12:34:53 +05302574 /* Set Lun for partition */
2575 mmc_set_lun(partition_get_lun(index));
2576
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002577 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302578 {
2579 dprintf(CRITICAL, "Reading MMC failed\n");
2580 return -1;
2581 }
2582
2583 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2584 is_allow_unlock = buf[blocksize-1] & 0x01;
2585 return 0;
2586}
2587
2588static int write_allow_oem_unlock(bool allow_unlock)
2589{
vijay kumarc65876c2015-04-24 13:29:16 +05302590 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302591 int index;
2592 unsigned long long ptn;
2593 unsigned long long ptn_size;
2594 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002595 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302596
vijay kumarca2e6812015-07-08 20:28:25 +05302597 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302598 if (index == INVALID_PTN)
2599 {
vijay kumarca2e6812015-07-08 20:28:25 +05302600 index = partition_get_index(frp_ptns[1]);
2601 if (index == INVALID_PTN)
2602 {
2603 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2604 return -1;
2605 }
vijay kumarc65876c2015-04-24 13:29:16 +05302606 }
2607
2608 ptn = partition_get_offset(index);
2609 ptn_size = partition_get_size(index);
2610 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302611 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302612
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002613 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302614 {
2615 dprintf(CRITICAL, "Reading MMC failed\n");
2616 return -1;
2617 }
2618
2619 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2620 buf[blocksize-1] = allow_unlock;
2621 if (mmc_write(ptn + offset, blocksize, buf))
2622 {
2623 dprintf(CRITICAL, "Writing MMC failed\n");
2624 return -1;
2625 }
2626
2627 return 0;
2628}
2629
Shashank Mittal162244e2011-08-08 19:01:25 -07002630void read_device_info_flash(device_info *dev)
2631{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002632 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002633 struct ptentry *ptn;
2634 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002635 if(info == NULL)
2636 {
2637 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2638 ASSERT(0);
2639 }
2640 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002641 ptable = flash_get_ptable();
2642 if (ptable == NULL)
2643 {
2644 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2645 return;
2646 }
2647
2648 ptn = ptable_find(ptable, "devinfo");
2649 if (ptn == NULL)
2650 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002651 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002652 return;
2653 }
2654
2655 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2656 {
2657 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2658 return;
2659 }
2660
2661 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2662 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002663 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2664 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002665 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002666 write_device_info_flash(info);
2667 }
2668 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002669 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002670}
2671
2672void write_device_info(device_info *dev)
2673{
2674 if(target_is_emmc_boot())
2675 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002676 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2677 if(info == NULL)
2678 {
2679 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2680 ASSERT(0);
2681 }
2682 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002683 memcpy(info, dev, sizeof(struct device_info));
2684
2685#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302686 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302687 is_secure_boot_enable()) {
2688 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2689 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002690 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002691 else
2692 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002693#else
2694 write_device_info_mmc(info);
2695#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002696 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002697 }
2698 else
2699 {
2700 write_device_info_flash(dev);
2701 }
2702}
2703
Monika Singh94316462018-03-15 18:39:01 +05302704int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2705{
2706 if (!devinfo_present) {
2707 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2708 return -EINVAL;
2709 }
2710 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2711 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2712 __func__, loc, ARRAY_SIZE(device.rollback_index));
2713 ASSERT(0);
2714 }
2715
2716 *roll_back_index = device.rollback_index[loc];
2717 return 0;
2718}
2719
2720int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2721{
2722 if (!devinfo_present) {
2723 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2724 return -EINVAL;
2725 }
2726 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2727 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2728 __func__, loc, ARRAY_SIZE(device.rollback_index));
2729 ASSERT(0);
2730 }
2731
2732 device.rollback_index[loc] = roll_back_index;
2733 write_device_info(&device);
2734 return 0;
2735}
2736
Monika Singhb0fad822018-03-15 18:50:55 +05302737int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2738{
2739 if (!devinfo_present) {
2740 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2741 return -EINVAL;
2742 }
2743
2744 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2745 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2746 return -ENODEV;
2747 }
2748
2749 memcpy(device.user_public_key, user_key, user_key_size);
2750 device.user_public_key_length = user_key_size;
2751 write_device_info(&device);
2752 return 0;
2753}
2754
2755int erase_userkey()
2756{
2757 if (!devinfo_present) {
2758 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2759 return -EINVAL;
2760 }
2761 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2762 device.user_public_key_length = 0;
2763 write_device_info(&device);
2764 return 0;
2765}
2766
2767int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2768{
2769 if (!devinfo_present) {
2770 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2771 return -EINVAL;
2772 }
2773 *user_key = device.user_public_key;
2774 *user_key_size = device.user_public_key_length;
2775 return 0;
2776}
2777
Shashank Mittal162244e2011-08-08 19:01:25 -07002778void read_device_info(device_info *dev)
2779{
2780 if(target_is_emmc_boot())
2781 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002782 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2783 if(info == NULL)
2784 {
2785 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2786 ASSERT(0);
2787 }
2788 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002789
2790#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302791 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302792 is_secure_boot_enable()) {
2793 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2794 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002795 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002796 else
2797 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002798#else
2799 read_device_info_mmc(info);
2800#endif
2801
2802 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2803 {
2804 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002805 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002806 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302807#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302808 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302809 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302810#endif
lijuang511a2b52015-08-14 20:50:51 +08002811 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002812 info->is_unlocked = 1;
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())
Mayank Grover889be1b2017-09-12 20:12:23 +05302815 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302816#endif
lijuang511a2b52015-08-14 20:50:51 +08002817 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002818 info->is_tampered = 0;
2819 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302820#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302821 if (VB_M <= target_get_vb_version())
Monika Singh1c71a1c2019-12-03 12:12:48 +05302822 {
Mayank Grover889be1b2017-09-12 20:12:23 +05302823 info->verity_mode = 1; //enforcing by default
Monika Singh1c71a1c2019-12-03 12:12:48 +05302824 info->user_public_key_length = 0;
2825 memset(info->rollback_index, 0, sizeof(info->rollback_index));
2826 memset(info->user_public_key, 0, sizeof(info->user_public_key));
2827 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302828#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002829 write_device_info(info);
2830 }
2831 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002832 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002833 }
2834 else
2835 {
2836 read_device_info_flash(dev);
2837 }
2838}
2839
2840void reset_device_info()
2841{
2842 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002843 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002844 write_device_info(&device);
2845}
2846
2847void set_device_root()
2848{
2849 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002850 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002851 write_device_info(&device);
2852}
2853
lijuang4ece1e72015-08-14 21:02:36 +08002854/* set device unlock value
2855 * Must check FRP before call this function
2856 * Need to wipe data when unlock status changed
2857 * type 0: oem unlock
2858 * type 1: unlock critical
2859 * status 0: unlock as false
2860 * status 1: lock as true
2861 */
2862void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002863{
lijuang4ece1e72015-08-14 21:02:36 +08002864 if (type == UNLOCK)
2865 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302866#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302867 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302868 type == UNLOCK_CRITICAL)
2869 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302870#endif
lijuang4ece1e72015-08-14 21:02:36 +08002871 write_device_info(&device);
2872}
2873
2874static void set_device_unlock(int type, bool status)
2875{
2876 int is_unlocked = -1;
2877 char response[MAX_RSP_SIZE];
2878
2879 /* check device unlock status if it is as expected */
2880 if (type == UNLOCK)
2881 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302882#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302883 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302884 type == UNLOCK_CRITICAL)
2885 {
2886 is_unlocked = device.is_unlock_critical;
2887 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302888#endif
lijuang4ece1e72015-08-14 21:02:36 +08002889 if (is_unlocked == status) {
2890 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2891 fastboot_info(response);
2892 fastboot_okay("");
2893 return;
2894 }
2895
2896 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002897 if (status && !is_allow_unlock) {
2898 fastboot_fail("oem unlock is not allowed");
2899 return;
2900 }
lijuang21f12f52015-08-22 16:22:19 +08002901
lijuang4ece1e72015-08-14 21:02:36 +08002902#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002903 display_unlock_menu(type, status);
2904 fastboot_okay("");
2905 return;
lijuang4ece1e72015-08-14 21:02:36 +08002906#else
lijuang07c5d6e2018-04-23 18:32:13 +08002907 if (status && type == UNLOCK) {
2908 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2909 return;
lijuang21f12f52015-08-22 16:22:19 +08002910 }
lijuang07c5d6e2018-04-23 18:32:13 +08002911#endif
lijuang4ece1e72015-08-14 21:02:36 +08002912
2913 set_device_unlock_value(type, status);
2914
2915 /* wipe data */
2916 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302917 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002918 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2919 write_misc(0, &msg, sizeof(msg));
2920
2921 fastboot_okay("");
2922 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002923}
2924
lijuang511a2b52015-08-14 20:50:51 +08002925static bool critical_flash_allowed(const char * entry)
2926{
2927 uint32_t i = 0;
2928 if (entry == NULL)
2929 return false;
2930
2931 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2932 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2933 return true;
2934 }
2935 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002936}
2937
2938#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002939int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2940{
2941 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002942 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302943 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002944 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302945 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002946 unsigned int compressed_size = 0;
2947 unsigned int dtb_size = 0;
2948 unsigned int out_avai_len = 0;
2949 unsigned char *out_addr = NULL;
2950 unsigned char *best_match_dt_addr = NULL;
2951 int rc;
2952
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302953 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07002954
Parth Dixit4097b622016-03-15 14:42:27 +05302955#ifndef OSVERSION_IN_BOOTIMAGE
2956 dt_size = hdr->dt_size;
2957#endif
2958
2959 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002960 /* add kernel offset */
2961 dt_image_offset += page_size;
2962 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2963 dt_image_offset += n;
2964
2965 /* add ramdisk offset */
2966 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2967 dt_image_offset += n;
2968
2969 /* add second offset */
2970 if(hdr->second_size != 0) {
2971 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2972 dt_image_offset += n;
2973 }
2974
2975 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002976 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002977
Deepa Dinamani19648b42013-09-05 17:05:55 -07002978 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002979 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2980 return -1;
2981 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302982
2983 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2984 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302985 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302986 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2987 return -1;
2988 }
2989
Joel Kingaa335dc2013-06-03 16:11:08 -07002990 /* Find index of device tree within device tree table */
2991 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002992 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2993 return -1;
2994 }
2995
Matthew Qin271927e2015-03-31 22:07:07 -04002996 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2997 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2998 {
2999 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
3000 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04003001 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003002 rc = decompress(best_match_dt_addr,
3003 dt_entry.size, out_addr, out_avai_len,
3004 &compressed_size, &dtb_size);
3005 if (rc)
3006 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003007 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003008 ASSERT(0);
3009 }
3010
Matthew Qin0b15b322015-05-19 05:20:54 -04003011 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003012 best_match_dt_addr = out_addr;
3013 } else {
3014 dtb_size = dt_entry.size;
3015 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003016 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303017 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3018 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003019 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303020 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003021 return -1;
3022 }
3023
Amol Jadicb524072012-08-09 16:40:18 -07003024 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003025 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003026 } else
3027 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003028
3029 /* Everything looks fine. Return success. */
3030 return 0;
3031}
3032#endif
3033
Brian Swetland9c4c0752009-01-25 16:23:50 -08003034void cmd_boot(const char *arg, void *data, unsigned sz)
3035{
3036 unsigned kernel_actual;
3037 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303038 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003039 uint32_t image_actual;
3040 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303041 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003042 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003043 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003044 int ret = 0;
3045 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003046 unsigned int out_len = 0;
3047 unsigned int out_avai_len = 0;
3048 unsigned char *out_addr = NULL;
3049 uint32_t dtb_offset = 0;
3050 unsigned char *kernel_start_addr = NULL;
3051 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003052 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303053#if VERIFIED_BOOT_2
3054 void *dtbo_image_buf = NULL;
3055 uint32_t dtbo_image_sz = 0;
3056 void *vbmeta_image_buf = NULL;
3057 uint32_t vbmeta_image_sz = 0;
3058#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303059#if !VERIFIED_BOOT_2
3060 uint32_t sig_actual = 0;
3061 uint32_t sig_size = 0;
3062#ifdef MDTP_SUPPORT
3063 static bool is_mdtp_activated = 0;
3064#endif /* MDTP_SUPPORT */
3065#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003066
lijuang2008ff22016-03-07 17:56:27 +08003067#if FBCON_DISPLAY_MSG
3068 /* Exit keys' detection thread firstly */
3069 exit_menu_keys_detection();
3070#endif
3071
Monika Singh292b3e92018-03-17 22:40:23 +05303072#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003073 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003074 {
3075 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003076 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003077 }
3078#endif
3079
Brian Swetland9c4c0752009-01-25 16:23:50 -08003080 if (sz < sizeof(hdr)) {
3081 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003082 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003083 }
3084
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303085 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003086
3087 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003088 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003089
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003090 if(target_is_emmc_boot() && hdr->page_size) {
3091 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003092 page_mask = page_size - 1;
3093 }
3094
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003095 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3096 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303097 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003098#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303099#ifndef OSVERSION_IN_BOOTIMAGE
3100 dt_size = hdr->dt_size;
3101#endif
3102 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003103#endif
3104
3105 image_actual = ADD_OF(page_size, kernel_actual);
3106 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303107 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003108 image_actual = ADD_OF(image_actual, dt_actual);
3109
Kishor PK5134b332017-05-09 17:50:08 +05303110 /* Checking to prevent oob access in read_der_message_length */
3111 if (image_actual > sz) {
3112 fastboot_fail("bootimage header fields are invalid");
3113 goto boot_failed;
3114 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303115
Monika Singh292b3e92018-03-17 22:40:23 +05303116#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303117 /* Pass size of boot partition, as imgsize, to avoid
3118 read fewer bytes error */
3119 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303120
3121 /* load and validate dtbo partition */
3122 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3123
3124 /* load vbmeta partition */
3125 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3126
Monika Singh292b3e92018-03-17 22:40:23 +05303127 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303128
3129 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3130 info.images[IMG_BOOT].imgsize = image_actual;
3131 info.images[IMG_BOOT].name = "boot";
3132 ++info.num_loaded_images;
3133
3134 /* Pass loaded dtbo image */
3135 if (dtbo_image_buf != NULL) {
3136 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3137 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3138 info.images[IMG_DTBO].name = "dtbo";
3139 ++info.num_loaded_images;
3140 }
3141
3142 /* Pass loaded vbmeta image */
3143 if (vbmeta_image_buf != NULL) {
3144 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3145 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3146 info.images[IMG_VBMETA].name = "vbmeta";
3147 ++info.num_loaded_images;
3148 }
3149
Monika Singh292b3e92018-03-17 22:40:23 +05303150 info.multi_slot_boot = partition_multislot_is_supported();
3151 if (load_image_and_auth(&info))
3152 goto boot_failed;
3153 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303154
3155 /* Free the buffer allocated to vbmeta post verification */
3156 if (vbmeta_image_buf != NULL) {
3157 free(vbmeta_image_buf);
3158 --info.num_loaded_images;
3159 }
Monika Singh292b3e92018-03-17 22:40:23 +05303160#else
Kishor PK5134b332017-05-09 17:50:08 +05303161 sig_size = sz - image_actual;
3162
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303163 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303164 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303165 /* Calculate the signature length from boot image */
3166 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303167 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303168 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003169
Monika Singh292b3e92018-03-17 22:40:23 +05303170 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303171 fastboot_fail("bootimage header fields are invalid");
3172 goto boot_failed;
3173 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003174 }
3175
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003176 // Initialize boot state before trying to verify boot.img
3177#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003178 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303179#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003180 /* Handle overflow if the input image size is greater than
3181 * boot image buffer can hold
3182 */
Monika Singh292b3e92018-03-17 22:40:23 +05303183 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003184 {
3185 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003186 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003187 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003188
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003189 /* Verify the boot image
3190 * device & page_size are initialized in aboot_init
3191 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003192 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003193 /* Pass size excluding signature size, otherwise we would try to
3194 * access signature beyond its length
3195 */
Monika Singh292b3e92018-03-17 22:40:23 +05303196 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003197 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003198#ifdef MDTP_SUPPORT
3199 else
3200 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003201 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003202 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003203
3204 if (!is_mdtp_activated) {
3205 ext_partition.partition = MDTP_PARTITION_NONE;
3206 mdtp_fwlock_verify_lock(&ext_partition);
3207 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003208 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003209
Amir Kotzer7c768c02016-04-13 09:08:05 +03003210 /* If mdtp state cannot be validate, block fastboot boot*/
3211 if(mdtp_activated(&is_mdtp_activated)){
3212 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3213 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3214 goto boot_failed;
3215 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003216 if(is_mdtp_activated){
3217 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003218 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003219 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003220#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303221#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003222
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003223#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303224 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303225 {
3226 /* set boot and system versions. */
3227 set_os_version((unsigned char *)data);
3228 // send root of trust
3229 if(!send_rot_command((uint32_t)device.is_unlocked))
3230 ASSERT(0);
3231 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303232#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003233 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003234 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3235 * If not, continue booting.
3236 */
3237 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3238 {
3239 out_addr = (unsigned char *)target_get_scratch_address();
3240 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3241 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303242#if VERIFIED_BOOT_2
3243 if (dtbo_image_sz)
3244 out_avai_len -= DTBO_IMG_BUF;
3245#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003246 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003247 ret = decompress((unsigned char *)(ptr + page_size),
3248 hdr->kernel_size, out_addr, out_avai_len,
3249 &dtb_offset, &out_len);
3250 if (ret)
3251 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003252 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003253 ASSERT(0);
3254 }
3255
Matthew Qin0b15b322015-05-19 05:20:54 -04003256 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003257 kptr = (struct kernel64_hdr *)out_addr;
3258 kernel_start_addr = out_addr;
3259 kernel_size = out_len;
3260 } else {
3261 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3262 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3263 kernel_size = hdr->kernel_size;
3264 }
3265
3266 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003267 * Update the kernel/ramdisk/tags address if the boot image header
3268 * has default values, these default values come from mkbootimg when
3269 * the boot image is flashed using fastboot flash:raw
3270 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003271 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003272
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003273 /* Get virtual addresses since the hdr saves physical addresses. */
3274 hdr->kernel_addr = VA(hdr->kernel_addr);
3275 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3276 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003277
Matthew Qinbb7923d2015-02-09 10:56:09 +08003278 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003279 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003280 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303281 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3282 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3283 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003284 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303285 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003286 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003287 }
3288
Amol Jadicb524072012-08-09 16:40:18 -07003289#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003290 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003291 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003292 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003293
3294 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003295#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303296 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3297 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003298 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303299 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003300 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003301 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003302#endif
3303
3304 /* Load ramdisk & kernel */
3305 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003306 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003307
3308#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303309 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3310 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003311 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303312 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003313 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003314 }
3315
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003316 /*
3317 * If dtb is not found look for appended DTB in the kernel.
3318 * If appended dev tree is found, update the atags with
3319 * memory address to the DTB appended location on RAM.
3320 * Else update with the atags address in the kernel header
3321 */
3322 if (!dtb_copied) {
3323 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003324 dtb = dev_tree_appended((void*)(ptr + page_size),
3325 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003326 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003327 if (!dtb) {
3328 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003329 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003330 }
Amol Jadicb524072012-08-09 16:40:18 -07003331 }
3332#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003333
3334 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003335 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003336
Dima Zavin77e41f32013-03-06 16:10:43 -08003337 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003338 (const char*) hdr->cmdline, board_machtype(),
3339 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003340
3341 /* fastboot already stop, it's no need to show fastboot menu */
3342 return;
3343boot_failed:
3344#if FBCON_DISPLAY_MSG
3345 /* revert to fastboot menu if boot failed */
3346 display_fastboot_menu();
3347#endif
3348 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003349}
3350
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003351void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003352{
3353 struct ptentry *ptn;
3354 struct ptable *ptable;
3355
3356 ptable = flash_get_ptable();
3357 if (ptable == NULL) {
3358 fastboot_fail("partition table doesn't exist");
3359 return;
3360 }
3361
3362 ptn = ptable_find(ptable, arg);
3363 if (ptn == NULL) {
3364 fastboot_fail("unknown partition name");
3365 return;
3366 }
3367
Monika Singh292b3e92018-03-17 22:40:23 +05303368 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3369 dprintf(INFO, "erasing avb_custom_key\n");
3370 if (erase_userkey()) {
3371 fastboot_fail("Erasing avb_custom_key failed");
3372 } else {
3373 fastboot_okay("");
3374 }
3375 return;
3376 }
3377
Dima Zavin214cc642009-01-26 11:16:21 -08003378 if (flash_erase(ptn)) {
3379 fastboot_fail("failed to erase partition");
3380 return;
3381 }
3382 fastboot_okay("");
3383}
3384
Bikas Gurungd48bd242010-09-04 19:54:32 -07003385
3386void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3387{
3388 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003389 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003390 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003391 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303392 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003393
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003394#if VERIFIED_BOOT
3395 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3396 {
3397 if(!device.is_unlocked)
3398 {
3399 fastboot_fail("unlock device to erase keystore");
3400 return;
3401 }
3402 }
3403#endif
3404
Kinson Chikf1a43512011-07-14 11:28:39 -07003405 index = partition_get_index(arg);
3406 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003407 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003408
Monika Singhc4778b72018-05-16 11:16:42 +05303409 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3410 dprintf(INFO, "erasing avb_custom_key\n");
3411 if (erase_userkey()) {
3412 fastboot_fail("Erasing avb_custom_key failed");
3413 } else {
3414 fastboot_okay("");
3415 }
3416 return;
3417 }
3418
Kinson Chikf1a43512011-07-14 11:28:39 -07003419 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003420 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003421 return;
3422 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003423
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003424 lun = partition_get_lun(index);
3425 mmc_set_lun(lun);
3426
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003427 if (platform_boot_dev_isemmc())
3428 {
3429 if (mmc_erase_card(ptn, size)) {
3430 fastboot_fail("failed to erase partition\n");
3431 return;
3432 }
3433 } else {
3434 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3435 size = partition_get_size(index);
3436 if (size > DEFAULT_ERASE_SIZE)
3437 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003438
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003439 /* Simple inefficient version of erase. Just writing
3440 0 in first several blocks */
3441 if (mmc_write(ptn , size, (unsigned int *)out)) {
3442 fastboot_fail("failed to erase partition");
3443 return;
3444 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303445 /*Erase FDE metadata at the userdata footer*/
3446 if(!(strncmp(arg, "userdata", 8)))
3447 {
3448 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3449 memset((void *)footer, 0, FOOTER_SIZE);
3450
3451 size = partition_get_size(index);
3452
3453 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3454 fastboot_fail("failed to erase userdata footer");
3455 free(footer);
3456 return;
3457 }
3458 free(footer);
3459 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003460 }
Monika Singh292b3e92018-03-17 22:40:23 +05303461#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303462 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303463 !(strncmp(arg, "userdata", 8)) &&
3464 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003465 ASSERT(0);
3466#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003467 fastboot_okay("");
3468}
3469
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003470void cmd_erase(const char *arg, void *data, unsigned sz)
3471{
Monika Singh292b3e92018-03-17 22:40:23 +05303472#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003473 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003474 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003475 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003476 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003477 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003478 return;
3479 }
3480 }
3481#endif
3482
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003483 if(target_is_emmc_boot())
3484 cmd_erase_mmc(arg, data, sz);
3485 else
3486 cmd_erase_nand(arg, data, sz);
3487}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003488
Mayank Grover11ff9692018-01-11 11:54:49 +05303489/* Get the size from partiton name */
3490static void get_partition_size(const char *arg, char *response)
3491{
3492 uint64_t ptn = 0;
3493 uint64_t size;
3494 int index = INVALID_PTN;
3495
3496 index = partition_get_index(arg);
3497
3498 if (index == INVALID_PTN)
3499 {
3500 dprintf(CRITICAL, "Invalid partition index\n");
3501 return;
3502 }
3503
3504 ptn = partition_get_offset(index);
3505
3506 if(!ptn)
3507 {
3508 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3509 return;
3510 }
3511
3512 size = partition_get_size(index);
3513
3514 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3515 return;
3516}
3517
Mayank Grover52cd10a2018-03-15 12:57:54 +05303518/* Function to check partition type of a partition*/
3519static fs_signature_type
3520check_partition_fs_signature(const char *arg)
3521{
3522 fs_signature_type ret = NO_FS;
3523 int index;
3524 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303525 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3526 uint32_t sb_blk_offset = 0;
3527 char *sb_buffer = buffer;
3528
Mayank Grover52cd10a2018-03-15 12:57:54 +05303529 if (!sb_buffer)
3530 {
3531 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3532 goto out;
3533 }
3534
3535 /* Read super block */
3536 if ((index = partition_get_index(arg)) < 0)
3537 {
3538 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3539 goto out;
3540 }
3541 ptn = partition_get_offset(index);
3542 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303543 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3544
3545 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303546 (void *)sb_buffer, mmc_blocksize))
3547 {
3548 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3549 goto out;
3550 }
3551
Mayank Grover399826a2018-08-27 12:15:15 +05303552 if (sb_blk_offset == 0)
3553 sb_buffer += FS_SUPERBLOCK_OFFSET;
3554
3555 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303556 {
3557 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3558 ret = EXT_FS_SIGNATURE;
3559 }
Mayank Grover399826a2018-08-27 12:15:15 +05303560 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303561 {
3562 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3563 ret = EXT_F2FS_SIGNATURE;
3564 }
3565 else
3566 {
3567 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3568 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3569 ret = NO_FS;
3570 }
3571
3572out:
Mayank Grover399826a2018-08-27 12:15:15 +05303573 if(buffer)
3574 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303575 return ret;
3576}
3577
Mayank Groverd38fe012018-03-13 15:33:16 +05303578/* Function to get partition type */
3579static void get_partition_type(const char *arg, char *response)
3580{
3581 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303582 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303583
3584 if (arg == NULL ||
3585 response == NULL)
3586 {
3587 dprintf(CRITICAL, "Invalid input parameter\n");
3588 return;
3589 }
3590
3591 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303592 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303593
3594 /* Mark partiton type for known paritions only */
3595 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3596 {
3597 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3598 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303599 /* Check partition for FS signature */
3600 fs_signature = check_partition_fs_signature(arg);
3601 switch (fs_signature)
3602 {
3603 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303604 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303605 break;
3606 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303607 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303608 break;
3609 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303610 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303611 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303612 }
3613 }
3614 return;
3615}
3616
Mayank Grover11ff9692018-01-11 11:54:49 +05303617/*
3618 * Publish the partition type & size info
3619 * fastboot getvar will publish the required information.
3620 * fastboot getvar partition_size:<partition_name>: partition size in hex
3621 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3622 */
3623static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3624{
Mayank Groverd38fe012018-03-13 15:33:16 +05303625 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303626 static bool published = false;
3627 struct partition_entry *ptn_entry =
3628 partition_get_partition_entries();
3629 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3630
3631 for (i = 0; i < num_parts; i++) {
3632 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3633 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3634 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3635
Mayank Groverd38fe012018-03-13 15:33:16 +05303636 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303637 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303638 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3639 {
3640 dprintf(CRITICAL, "partition size name truncated\n");
3641 return;
3642 }
3643 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3644 {
3645 dprintf(CRITICAL, "partition type name truncated\n");
3646 return;
3647 }
3648
3649 if (!published)
3650 {
3651 /* publish partition size & type info */
3652 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3653 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3654 }
3655 }
3656 if (!published)
3657 published = true;
3658}
3659
3660
Ajay Dudani5c761132011-04-07 20:19:04 -07003661void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003662{
3663 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003664 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003665 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003666 char *token = NULL;
3667 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003668 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003669 uint8_t lun = 0;
3670 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303671 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003672
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003673 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003674 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003675 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003676 if(token)
3677 {
3678 lun = atoi(token);
3679 mmc_set_lun(lun);
3680 lun_set = true;
3681 }
3682
Mao Jinlong226f33a2014-07-04 17:24:10 +08003683 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003684 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003685 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3686 {
3687 if (!aboot_frp_unlock(pname, data, sz))
3688 {
3689 fastboot_info("FRP unlock successful");
3690 fastboot_okay("");
3691 }
3692 else
3693 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3694
3695 return;
3696 }
3697
Mao Jinlong226f33a2014-07-04 17:24:10 +08003698 if (!strcmp(pname, "partition"))
3699 {
3700 dprintf(INFO, "Attempt to write partition image.\n");
3701 if (write_partition(sz, (unsigned char *) data)) {
3702 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003703 return;
3704 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303705 /* Re-publish partition table */
3706 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303707
3708 /* Rescan partition table to ensure we have multislot support*/
3709 if (partition_scan_for_multislot())
3710 {
3711 current_active_slot = partition_find_active_slot();
3712 dprintf(INFO, "Multislot supported: Slot %s active",
3713 (SUFFIX_SLOT(current_active_slot)));
3714 }
3715 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003716 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003717 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003718 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003719#if VERIFIED_BOOT
3720 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3721 {
3722 if(!device.is_unlocked)
3723 {
3724 fastboot_fail("unlock device to flash keystore");
3725 return;
3726 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303727 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003728 {
3729 fastboot_fail("image is not a keystore file");
3730 return;
3731 }
3732 }
3733#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003734 index = partition_get_index(pname);
3735 ptn = partition_get_offset(index);
3736 if(ptn == 0) {
3737 fastboot_fail("partition table doesn't exist");
3738 return;
3739 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003740
Mayank Grover351a75e2017-05-30 20:06:08 +05303741 if (!strncmp(pname, "boot", strlen("boot"))
3742 || !strcmp(pname, "recovery"))
3743 {
Ashish Bhimanpalliwar84f7db92020-09-23 11:59:16 +05303744 if ((sz < BOOT_MAGIC_SIZE) || memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003745 fastboot_fail("image is not a boot image");
3746 return;
3747 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303748
3749 /* Reset multislot_partition attributes in case of flashing boot */
3750 if (partition_multislot_is_supported())
3751 {
3752 partition_reset_attributes(index);
3753 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003754 }
3755
3756 if(!lun_set)
3757 {
3758 lun = partition_get_lun(index);
3759 mmc_set_lun(lun);
3760 }
3761
3762 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303763 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003764 fastboot_fail("size too large");
3765 return;
3766 }
3767 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3768 fastboot_fail("flash write failure");
3769 return;
3770 }
Greg Grisco6e754772011-06-23 12:19:39 -07003771 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003772 }
3773 fastboot_okay("");
3774 return;
3775}
3776
Ajay Dudanide984792015-03-02 09:57:41 -08003777void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3778{
3779 int i, images;
3780 meta_header_t *meta_header;
3781 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303782 /*End of the image address*/
3783 uintptr_t data_end;
3784
3785 if( (UINT_MAX - sz) > (uintptr_t)data )
3786 data_end = (uintptr_t)data + sz;
3787 else
3788 {
3789 fastboot_fail("Cannot flash: image header corrupt");
3790 return;
3791 }
3792
Ashish Bhimanpalliwar21494e12020-10-07 13:24:03 +05303793 if((UINT_MAX - sizeof(meta_header_t) < (uintptr_t)data) || (data_end < ((uintptr_t)data + sizeof(meta_header_t))))
Parth Dixit3e2d3032016-03-04 17:11:52 +05303794 {
3795 fastboot_fail("Cannot flash: image header corrupt");
3796 return;
3797 }
Ajay Dudanide984792015-03-02 09:57:41 -08003798
lijuang8ee21882016-04-19 16:57:11 +08003799 /* If device is locked:
3800 * Forbid to flash image to avoid the device to bypass the image
3801 * which with "any" name other than bootloader. Because it maybe
3802 * a meta package of all partitions.
3803 */
Monika Singh292b3e92018-03-17 22:40:23 +05303804#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003805 if (target_build_variant_user()) {
3806 if (!device.is_unlocked) {
3807 fastboot_fail("Device is locked, meta image flashing is not allowed");
3808 return;
3809 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303810
Mayank Grover912eaa62017-10-26 12:08:53 +05303811 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303812 !device.is_unlock_critical)
3813 {
lijuang8ee21882016-04-19 16:57:11 +08003814 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3815 return;
3816 }
lijuang8ee21882016-04-19 16:57:11 +08003817 }
3818#endif
3819
Ajay Dudanide984792015-03-02 09:57:41 -08003820 meta_header = (meta_header_t*) data;
Ashish Bhimanpalliwar21494e12020-10-07 13:24:03 +05303821 if(((UINT_MAX - sizeof(meta_header_t) - meta_header->img_hdr_sz) < (uintptr_t)data) || data_end < ((uintptr_t)data + sizeof(meta_header_t) + meta_header->img_hdr_sz))
Parth Dixit3e2d3032016-03-04 17:11:52 +05303822 {
3823 fastboot_fail("Cannot flash: image header corrupt");
3824 return;
3825 }
Ajay Dudanide984792015-03-02 09:57:41 -08003826 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3827
3828 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3829
3830 for (i=0; i<images; i++) {
3831
3832 if((img_header_entry[i].ptn_name == NULL) ||
3833 (img_header_entry[i].start_offset == 0) ||
3834 (img_header_entry[i].size == 0))
3835 break;
Kishor PK49831502017-04-21 17:55:43 +05303836 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3837 fastboot_fail("Integer overflow detected in start_offset of img");
3838 break;
3839 }
3840 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3841 fastboot_fail("Integer overflow detected in size of img");
3842 break;
3843 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303844 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3845 + img_header_entry[i].size) )
3846 {
3847 fastboot_fail("Cannot flash: image size mismatch");
3848 break;
3849 }
3850
Ajay Dudanide984792015-03-02 09:57:41 -08003851 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3852 (void *) data + img_header_entry[i].start_offset,
3853 img_header_entry[i].size);
3854 }
3855
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003856 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3857 {
3858 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3859 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3860 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3861 }
3862 else
3863 {
3864 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3865 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3866 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3867 }
3868
3869 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003870 fastboot_okay("");
3871 return;
3872}
3873
Ajay Dudani5c761132011-04-07 20:19:04 -07003874void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3875{
3876 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003877 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003878 uint32_t *fill_buf = NULL;
3879 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303880 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003881 sparse_header_t *sparse_header;
3882 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003883 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003884 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303885 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003886 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303887 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003888 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303889 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303890 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003891
Kinson Chikf1a43512011-07-14 11:28:39 -07003892 index = partition_get_index(arg);
3893 ptn = partition_get_offset(index);
3894 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003895 fastboot_fail("partition table doesn't exist");
3896 return;
3897 }
3898
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303899 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303900
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003901 lun = partition_get_lun(index);
3902 mmc_set_lun(lun);
3903
vijay kumar800255e2015-04-24 20:26:19 +05303904 if (sz < sizeof(sparse_header_t)) {
3905 fastboot_fail("size too low");
3906 return;
3907 }
3908
Ajay Dudani5c761132011-04-07 20:19:04 -07003909 /* Read and skip over sparse image header */
3910 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303911
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303912 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3913 fastboot_fail("Invalid block size\n");
3914 return;
3915 }
3916
vijay kumar1321f342015-03-27 12:13:42 +05303917 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003918 fastboot_fail("size too large");
3919 return;
3920 }
3921
vijay kumarde4fcf62015-04-23 13:05:49 +05303922 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303923
Parth Dixit64b1a482016-03-07 16:31:26 +05303924 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303925 fastboot_fail("buffer overreads occured due to invalid sparse header");
3926 return;
3927 }
3928
vijay kumarde4fcf62015-04-23 13:05:49 +05303929 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003930 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303931 fastboot_fail("sparse header size mismatch");
3932 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003933 }
3934
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003935 dprintf (SPEW, "=== Sparse Image Header ===\n");
3936 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3937 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3938 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3939 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3940 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3941 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3942 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3943 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003944
3945 /* Start processing chunks */
3946 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3947 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303948 /* Make sure the total image size does not exceed the partition size */
3949 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3950 fastboot_fail("size too large");
3951 return;
3952 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003953 /* Read and skip over chunk header */
3954 chunk_header = (chunk_header_t *) data;
3955 data += sizeof(chunk_header_t);
3956
Parth Dixit64b1a482016-03-07 16:31:26 +05303957 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303958 fastboot_fail("buffer overreads occured due to invalid sparse header");
3959 return;
3960 }
3961
Ajay Dudani5c761132011-04-07 20:19:04 -07003962 dprintf (SPEW, "=== Chunk Header ===\n");
3963 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3964 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3965 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3966
vijay kumar800255e2015-04-24 20:26:19 +05303967 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003968 {
vijay kumar800255e2015-04-24 20:26:19 +05303969 fastboot_fail("chunk header size mismatch");
3970 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003971 }
3972
wufeng.jiang813dc352015-06-02 23:02:46 -04003973 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3974
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303975 /* Make sure that the chunk size calculated from sparse image does not
3976 * exceed partition size
3977 */
3978 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3979 {
3980 fastboot_fail("Chunk data size exceeds partition size");
3981 return;
3982 }
3983
Ajay Dudani5c761132011-04-07 20:19:04 -07003984 switch (chunk_header->chunk_type)
3985 {
3986 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003987 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003988 chunk_data_sz))
3989 {
3990 fastboot_fail("Bogus chunk size for chunk type Raw");
3991 return;
3992 }
3993
Parth Dixit64b1a482016-03-07 16:31:26 +05303994 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303995 fastboot_fail("buffer overreads occured due to invalid sparse header");
3996 return;
3997 }
3998
wufeng.jiang813dc352015-06-02 23:02:46 -04003999 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
4000 otherwise it will return in the line above
4001 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07004002 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04004003 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07004004 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07004005 {
4006 fastboot_fail("flash write failure");
4007 return;
4008 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304009 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4010 fastboot_fail("Bogus size for RAW chunk type");
4011 return;
4012 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004013 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004014 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004015 break;
4016
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004017 case CHUNK_TYPE_FILL:
4018 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4019 sizeof(uint32_t)))
4020 {
4021 fastboot_fail("Bogus chunk size for chunk type FILL");
4022 return;
4023 }
4024
Mayank Grover4f50bba2017-07-19 18:17:08 +05304025 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4026 /* Integer overflow detected */
4027 if (blk_sz_actual < sparse_header->blk_sz)
4028 {
4029 fastboot_fail("Invalid block size");
4030 return;
4031 }
4032
4033 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004034 if (!fill_buf)
4035 {
4036 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4037 return;
4038 }
4039
Parth Dixit64b1a482016-03-07 16:31:26 +05304040 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304041 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304042 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304043 return;
4044 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004045 fill_val = *(uint32_t *)data;
4046 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004047
4048 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4049 {
4050 fill_buf[i] = fill_val;
4051 }
4052
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304053 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4054 {
4055 fastboot_fail("bogus size for chunk FILL type");
4056 free(fill_buf);
4057 return;
4058 }
4059
wufeng.jiang813dc352015-06-02 23:02:46 -04004060 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004061 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304062 /* Make sure that the data written to partition does not exceed partition size */
4063 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4064 {
4065 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304066 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304067 return;
4068 }
4069
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004070 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4071 sparse_header->blk_sz,
4072 fill_buf))
4073 {
4074 fastboot_fail("flash write failure");
4075 free(fill_buf);
4076 return;
4077 }
4078
4079 total_blocks++;
4080 }
4081
4082 free(fill_buf);
4083 break;
4084
Ajay Dudani5c761132011-04-07 20:19:04 -07004085 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304086 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4087 fastboot_fail("bogus size for chunk DONT CARE type");
4088 return;
4089 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004090 total_blocks += chunk_header->chunk_sz;
4091 break;
4092
Ajay Dudani5c761132011-04-07 20:19:04 -07004093 case CHUNK_TYPE_CRC:
4094 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4095 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004096 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004097 return;
4098 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304099 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4100 fastboot_fail("bogus size for chunk CRC type");
4101 return;
4102 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004103 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304104 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304105 fastboot_fail("integer overflow occured");
4106 return;
4107 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004108 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304109 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304110 fastboot_fail("buffer overreads occured due to invalid sparse header");
4111 return;
4112 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004113 break;
4114
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004115 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004116 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004117 fastboot_fail("Unknown chunk type");
4118 return;
4119 }
4120 }
4121
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004122 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4123 total_blocks, sparse_header->total_blks);
4124
4125 if(total_blocks != sparse_header->total_blks)
4126 {
4127 fastboot_fail("sparse image write failure");
4128 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004129
4130 fastboot_okay("");
4131 return;
4132}
4133
4134void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4135{
4136 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004137 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004138
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004139#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004140 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4141 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004142 int ret=0;
4143 uint32 major_version=0;
4144 uint32 minor_version=0;
4145
4146 ret = scm_svc_version(&major_version,&minor_version);
4147 if(!ret)
4148 {
4149 if(major_version >= 2)
4150 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004151 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004152 {
4153 ret = encrypt_scm((uint32 **) &data, &sz);
4154 if (ret != 0) {
4155 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4156 return;
4157 }
4158
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004159 /* Protect only for SSD */
4160 if (!strcmp(arg, "ssd")) {
4161 ret = scm_protect_keystore((uint32 *) data, sz);
4162 if (ret != 0) {
4163 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4164 return;
4165 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004166 }
4167 }
4168 else
4169 {
4170 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4171 if(ret != 0)
4172 {
4173 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4174 return;
4175 }
4176 }
4177 }
4178 else
4179 {
4180 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4181 magic_number[1] == DECRYPT_MAGIC_1)
4182 {
4183 ret = decrypt_scm((uint32 **) &data, &sz);
4184 if (ret != 0) {
4185 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4186 return;
4187 }
4188 }
4189 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4190 magic_number[1] == ENCRYPT_MAGIC_1)
4191 {
4192 ret = encrypt_scm((uint32 **) &data, &sz);
4193 if (ret != 0) {
4194 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4195 return;
4196 }
4197 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004198 }
4199 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004200 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004201 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004202 dprintf(CRITICAL,"INVALID SVC Version\n");
4203 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004204 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004205#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004206
Monika Singh292b3e92018-03-17 22:40:23 +05304207#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004208 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004209 {
lijuang511a2b52015-08-14 20:50:51 +08004210 /* if device is locked:
4211 * common partition will not allow to be flashed
4212 * critical partition will allow to flash image.
4213 */
4214 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4215 fastboot_fail("Partition flashing is not allowed");
4216 return;
4217 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304218
lijuang511a2b52015-08-14 20:50:51 +08004219 /* if device critical is locked:
4220 * common partition will allow to be flashed
4221 * critical partition will not allow to flash image.
4222 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304223 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304224 !device.is_unlock_critical &&
4225 critical_flash_allowed(arg)) {
4226 fastboot_fail("Critical partition flashing is not allowed");
4227 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004228 }
4229 }
4230#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304231 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4232 dprintf(INFO, "flashing avb_custom_key\n");
4233 if (store_userkey(data, sz)) {
4234 fastboot_fail("Flashing avb_custom_key failed");
4235 } else {
4236 fastboot_okay("");
4237 }
4238 return;
4239 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004240
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004241 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004242 meta_header = (meta_header_t *) data;
4243 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004244 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004245 else if (meta_header->magic == META_HEADER_MAGIC)
4246 cmd_flash_meta_img(arg, data, sz);
4247 else
4248 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004249
4250#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304251 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304252 (!strncmp(arg, "system", 6)) &&
4253 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004254 // reset dm_verity mode to enforcing
4255 device.verity_mode = 1;
4256 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304257#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004258
Ajay Dudani5c761132011-04-07 20:19:04 -07004259 return;
4260}
4261
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004262void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4263{
4264 struct ptentry *sys_ptn;
4265 struct ptable *ptable;
4266
4267 ptable = flash_get_ptable();
4268 if (ptable == NULL) {
4269 fastboot_fail("partition table doesn't exist");
4270 return;
4271 }
4272
4273 sys_ptn = ptable_find(ptable, "system");
4274 if (sys_ptn == NULL) {
4275 fastboot_fail("system partition not found");
4276 return;
4277 }
4278
4279 sz = ROUND_TO_PAGE(sz, page_mask);
4280 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4281 fastboot_fail("update_ubi_vol failed");
4282 else
4283 fastboot_okay("");
4284}
4285
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004286void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004287{
4288 struct ptentry *ptn;
4289 struct ptable *ptable;
4290 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304291 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304292 unsigned bytes_to_round_page = 0;
4293 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004294
Kishor PK38ed93d2017-04-25 14:19:26 +05304295 if((uintptr_t)data > (UINT_MAX - sz)) {
4296 fastboot_fail("Cannot flash: image header corrupt");
4297 return;
4298 }
4299
Dima Zavin214cc642009-01-26 11:16:21 -08004300 ptable = flash_get_ptable();
4301 if (ptable == NULL) {
4302 fastboot_fail("partition table doesn't exist");
4303 return;
4304 }
4305
4306 ptn = ptable_find(ptable, arg);
4307 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004308 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4309 arg);
4310 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004311 return;
4312 }
4313
Monika Singh292b3e92018-03-17 22:40:23 +05304314 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4315 dprintf(INFO, "flashing avb_custom_key\n");
4316 if (store_userkey(data, sz)) {
4317 fastboot_fail("Flashing avb_custom_key failed");
4318 } else {
4319 fastboot_okay("");
4320 }
4321 return;
4322 }
4323
Dima Zavin214cc642009-01-26 11:16:21 -08004324 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304325 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4326 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4327 } else {
4328 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004329 return;
4330 }
4331 }
4332
Amol Jadi5c61a952012-05-04 17:05:35 -07004333 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004334 || !strcmp(ptn->name, "userdata")
4335 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004336 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004337 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004338 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304339 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304340 rounded_size = ROUNDUP(sz, page_size);
4341 bytes_to_round_page = rounded_size - sz;
4342 if (bytes_to_round_page) {
4343 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4344 fastboot_fail("Integer overflow detected");
4345 return;
4346 }
4347 if (((uintptr_t)data + sz + bytes_to_round_page) >
4348 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4349 fastboot_fail("Buffer size is not aligned to page_size");
4350 return;
4351 }
4352 else {
4353 memset(data + sz, 0, bytes_to_round_page);
4354 sz = rounded_size;
4355 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304356 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304357 }
4358
Kishor PK38ed93d2017-04-25 14:19:26 +05304359 /*Checking partition_size for the possible integer overflow */
4360 partition_size = validate_partition_size(ptn);
4361
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304362 if (sz > partition_size) {
4363 fastboot_fail("Image size too large");
4364 return;
4365 }
4366
Dima Zavin214cc642009-01-26 11:16:21 -08004367 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304368 if ((sz > UBI_EC_HDR_SIZE) &&
4369 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004370 if (flash_ubi_img(ptn, data, sz)) {
4371 fastboot_fail("flash write failure");
4372 return;
4373 }
4374 } else {
4375 if (flash_write(ptn, extra, data, sz)) {
4376 fastboot_fail("flash write failure");
4377 return;
4378 }
Dima Zavin214cc642009-01-26 11:16:21 -08004379 }
4380 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4381 fastboot_okay("");
4382}
4383
Kishor PK38ed93d2017-04-25 14:19:26 +05304384
4385static inline uint64_t validate_partition_size(struct ptentry *ptn)
4386{
4387 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4388 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4389 return ptn->length * flash_num_pages_per_blk() * page_size;
4390 }
4391 }
4392 return 0;
4393}
4394
4395
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004396void cmd_flash(const char *arg, void *data, unsigned sz)
4397{
4398 if(target_is_emmc_boot())
4399 cmd_flash_mmc(arg, data, sz);
4400 else
4401 cmd_flash_nand(arg, data, sz);
4402}
4403
Dima Zavin214cc642009-01-26 11:16:21 -08004404void cmd_continue(const char *arg, void *data, unsigned sz)
4405{
4406 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004407 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004408
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004409 if (target_is_emmc_boot())
4410 {
lijuanga40d6302015-07-20 20:10:13 +08004411#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004412 /* Exit keys' detection thread firstly */
4413 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004414#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004415 boot_linux_from_mmc();
4416 }
4417 else
4418 {
4419 boot_linux_from_flash();
4420 }
Dima Zavin214cc642009-01-26 11:16:21 -08004421}
4422
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004423void cmd_reboot(const char *arg, void *data, unsigned sz)
4424{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004425 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004426 fastboot_okay("");
4427 reboot_device(0);
4428}
4429
Mayank Grover351a75e2017-05-30 20:06:08 +05304430void cmd_set_active(const char *arg, void *data, unsigned sz)
4431{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304432 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304433 unsigned i,current_active_slot;
4434 const char *current_slot_suffix;
4435
4436 if (!partition_multislot_is_supported())
4437 {
4438 fastboot_fail("Command not supported");
4439 return;
4440 }
4441
4442 if (arg)
4443 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304444 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304445 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304446 {
4447 current_active_slot = partition_find_active_slot();
4448
4449 /* Check if trying to make curent slot active */
4450 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304451 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4452 (char *)suffix_delimiter, &sp);
4453
Mayank Grover5eb5f692017-07-19 20:16:04 +05304454 if (current_slot_suffix &&
4455 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304456 {
4457 fastboot_okay("Slot already set active");
4458 return;
4459 }
4460 else
4461 {
4462 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4463 {
4464 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304465 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4466 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304467 if (current_slot_suffix &&
4468 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304469 {
lijuang8b03e5f2019-07-12 18:16:19 +08004470 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304471 publish_getvar_multislot_vars();
4472 fastboot_okay("");
4473 return;
4474 }
4475 }
4476 }
4477 }
4478 }
4479 fastboot_fail("Invalid slot suffix.");
4480 return;
4481}
4482
Mayank Grover98c4c742019-04-25 17:21:37 +05304483#if DYNAMIC_PARTITION_SUPPORT
4484void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4485{
4486 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4487 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4488 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4489 fastboot_fail("Failed to update recovery command");
4490 return;
4491 }
4492 fastboot_okay("");
4493 reboot_device(REBOOT_MODE_UNKNOWN);
4494
4495 //shouldn't come here.
4496 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4497 return;
4498}
4499
4500void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4501{
4502 dprintf(INFO, "rebooting the device - recovery\n");
4503 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
4504 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4505 fastboot_fail("Failed to update recovery command");
4506 return;
4507 }
4508 fastboot_okay("");
4509 reboot_device(REBOOT_MODE_UNKNOWN);
4510
4511 //shouldn't come here.
4512 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4513 return;
4514}
4515#endif
4516
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004517void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4518{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004519 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004520 fastboot_okay("");
4521 reboot_device(FASTBOOT_MODE);
4522}
4523
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004524void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4525{
4526 dprintf(INFO, "Enabling charger screen check\n");
4527 device.charger_screen_enabled = 1;
4528 write_device_info(&device);
4529 fastboot_okay("");
4530}
4531
4532void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4533{
4534 dprintf(INFO, "Disabling charger screen check\n");
4535 device.charger_screen_enabled = 0;
4536 write_device_info(&device);
4537 fastboot_okay("");
4538}
4539
lijuanga25d8bb2015-09-16 13:11:52 +08004540void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4541{
4542 char *p = NULL;
4543 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304544 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004545
4546 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304547 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004548 if (p) {
4549 if (!strncmp(p, "0", 1)) {
4550 device.charger_screen_enabled = 0;
4551 } else if (!strncmp(p, "1", 1)) {
4552 device.charger_screen_enabled = 1;
4553 }
4554 }
4555 }
4556
4557 /* update charger_screen_enabled value for getvar
4558 * command
4559 */
4560 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4561 device.charger_screen_enabled);
4562
4563 write_device_info(&device);
4564 fastboot_okay("");
4565}
4566
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304567void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4568{
4569 dprintf(INFO, "Selecting display panel %s\n", arg);
4570 if (arg)
4571 strlcpy(device.display_panel, arg,
4572 sizeof(device.display_panel));
4573 write_device_info(&device);
4574 fastboot_okay("");
4575}
4576
Shashank Mittal162244e2011-08-08 19:01:25 -07004577void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4578{
lijuang4ece1e72015-08-14 21:02:36 +08004579 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304580}
4581
4582void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4583{
lijuang4ece1e72015-08-14 21:02:36 +08004584 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304585 if(!is_allow_unlock) {
4586 fastboot_fail("oem unlock is not allowed");
4587 return;
4588 }
4589
lijuang4ece1e72015-08-14 21:02:36 +08004590 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304591
lijuang4ece1e72015-08-14 21:02:36 +08004592 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304593 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304594 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304595 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4596 write_misc(0, &msg, sizeof(msg));
4597
4598 fastboot_okay("");
4599 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004600 }
4601 fastboot_okay("");
4602}
4603
Channagoud Kadabiad259832015-05-29 11:14:17 -07004604static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4605{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304606 int ret=1;
4607 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004608
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304609 /*
4610 Authentication method not implemented.
4611
4612 OEM to implement, authentication system which on successful validataion,
4613 calls write_allow_oem_unlock() with is_allow_unlock.
4614 */
4615#if 0
4616 authentication_success = oem_specific_auth_mthd();
4617#endif
4618
4619 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004620 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304621 is_allow_unlock = true;
4622 write_allow_oem_unlock(is_allow_unlock);
4623 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004624 }
4625 return ret;
4626}
4627
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004628void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4629{
lijuang4ece1e72015-08-14 21:02:36 +08004630 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004631}
4632
Shashank Mittala0032282011-08-26 14:50:11 -07004633void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4634{
lijuang511a2b52015-08-14 20:50:51 +08004635 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004636 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004637 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004638 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4639 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304640#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304641 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304642 {
4643 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4644 (device.is_unlock_critical ? "true" : "false"));
4645 fastboot_info(response);
4646 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304647#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004648 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004649 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304650 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4651 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004652 fastboot_okay("");
4653}
4654
lijuang511a2b52015-08-14 20:50:51 +08004655void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4656{
4657 char response[MAX_RSP_SIZE];
4658 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4659 fastboot_info(response);
4660 fastboot_okay("");
4661}
4662
4663void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4664{
lijuang4ece1e72015-08-14 21:02:36 +08004665 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004666}
4667
4668void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4669{
lijuang4ece1e72015-08-14 21:02:36 +08004670 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004671}
4672
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004673void cmd_preflash(const char *arg, void *data, unsigned sz)
4674{
4675 fastboot_okay("");
4676}
4677
Mao Flynn7b379f32015-04-20 00:28:30 +08004678static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304679
Mao Flynn7b379f32015-04-20 00:28:30 +08004680int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304681{
Mao Flynn7b379f32015-04-20 00:28:30 +08004682 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304683 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004684 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304685 return -1;
4686 return 0;
4687}
4688
Mao Flynn7b379f32015-04-20 00:28:30 +08004689int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004690{
4691 struct ptentry *ptn;
4692 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004693 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004694 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004695
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304696 ptable = flash_get_ptable();
4697 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004698 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4699 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304700 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004701
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304702 ptn = ptable_find(ptable, "splash");
4703 if (ptn == NULL) {
4704 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004705 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304706 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004707 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304708 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004709 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304710 }
4711
Mao Flynn7b379f32015-04-20 00:28:30 +08004712 header = (struct logo_img_header *)logo_header;
4713 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304714 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004715 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304716 }
4717
4718 fb_display = fbcon_display();
4719 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304720 if (header->type && (header->blocks != 0) &&
4721 (UINT_MAX >= header->blocks * 512) &&
4722 ((header->blocks * 512) <= (fb_display->width *
4723 fb_display->height * (fb_display->bpp / 8)))) {
4724 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08004725 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4726
4727 /* if the logo is full-screen size, remove "fbcon_clear()" */
4728 if ((header->width != fb_display->width)
4729 || (header->height != fb_display->height))
4730 fbcon_clear();
4731
4732 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4733 (uint32_t *)base,
4734 (header->blocks * 512))) {
4735 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4736 return -1;
4737 }
4738 fbcon_extract_to_screen(header, base);
4739 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004740 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004741
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004742 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4743 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004744 return -1;
4745 }
4746
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304747 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304748 uint32_t fb_size = ROUNDUP(fb_display->width *
4749 fb_display->height *
4750 (fb_display->bpp / 8), 4096);
4751 uint32_t splash_size = ((((header->width * header->height *
4752 fb_display->bpp/8) + 511) >> 9) << 9);
4753
4754 if (splash_size > fb_size) {
4755 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4756 return -1;
4757 }
4758
Mao Flynn7b379f32015-04-20 00:28:30 +08004759 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4760 (uint32_t *)base,
4761 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304762 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304763 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004764 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004765 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304766 }
4767
Mao Flynn7b379f32015-04-20 00:28:30 +08004768 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304769}
4770
Mao Flynn7b379f32015-04-20 00:28:30 +08004771int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304772{
4773 int index = INVALID_PTN;
4774 unsigned long long ptn = 0;
4775 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004776 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004777 uint32_t blocksize, realsize, readsize;
4778 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304779
4780 index = partition_get_index("splash");
4781 if (index == 0) {
4782 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004783 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304784 }
4785
4786 ptn = partition_get_offset(index);
4787 if (ptn == 0) {
4788 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004789 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304790 }
4791
Mao Flynn1893a7f2015-06-03 12:03:36 +08004792 mmc_set_lun(partition_get_lun(index));
4793
4794 blocksize = mmc_get_device_blocksize();
4795 if (blocksize == 0) {
4796 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4797 return -1;
4798 }
4799
4800 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004801 if (!fb_display)
4802 {
4803 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4804 return -1;
4805 }
4806
Mao Flynn1893a7f2015-06-03 12:03:36 +08004807 base = (uint8_t *) fb_display->base;
4808
Kishor PKee5c0a32018-03-06 16:49:46 +05304809 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304810 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004811 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304812 }
4813
Mao Flynn1893a7f2015-06-03 12:03:36 +08004814 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004815 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304816 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004817 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304818 }
4819
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304820 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304821 if (header->type && (header->blocks != 0) &&
4822 (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
4823 ((header->blocks * 512) <= (fb_display->width *
4824 fb_display->height * (fb_display->bpp / 8)))) {
4825 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08004826 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004827
Mao Flynn1893a7f2015-06-03 12:03:36 +08004828 realsize = header->blocks * 512;
4829 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4830
4831 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004832 if ((header->width != fb_display->width)
4833 || (header->height != fb_display->height))
4834 fbcon_clear();
4835
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304836 uint32_t fb_size = ROUNDUP(fb_display->width *
4837 fb_display->height *
4838 (fb_display->bpp / 8), 4096);
4839
4840 if (readsize > fb_size) {
4841 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4842 return -1;
4843 }
4844
Kishor PKee5c0a32018-03-06 16:49:46 +05304845 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004846 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4847 return -1;
4848 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004849
Mao Flynn1893a7f2015-06-03 12:03:36 +08004850 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4851 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304852
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004853 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4854 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004855 return -1;
4856 }
4857
4858 realsize = header->width * header->height * fb_display->bpp / 8;
4859 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4860
4861 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304862 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004863 fbcon_clear();
4864 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4865 return -1;
4866 }
4867 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304868 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004869 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4870 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4871 return -1;
4872 }
4873 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4874 }
4875 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304876 }
4877
Mao Flynn7b379f32015-04-20 00:28:30 +08004878 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304879}
4880
Mao Flynn7b379f32015-04-20 00:28:30 +08004881int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304882{
4883 if (target_is_emmc_boot()) {
4884 return splash_screen_mmc();
4885 } else {
4886 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004887 }
4888}
4889
Mayank Grover351a75e2017-05-30 20:06:08 +05304890void publish_getvar_multislot_vars()
4891{
4892 int i,count;
4893 static bool published = false;
4894 static char slot_count[MAX_RSP_SIZE];
4895 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4896 static char active_slot_suffix[MAX_RSP_SIZE];
4897 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4898 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4899 const char *tmp;
4900 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304901 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304902
4903 if (!published)
4904 {
4905 /* Update slot meta info */
4906 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4907 partition_get_partition_count());
4908 for(i=0; i<count; i++)
4909 {
4910 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304911 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4912 has_slot_pname[i]);
4913 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304914 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4915 }
4916
4917 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4918 {
4919 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304920 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304921 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4922 "slot-unbootable:%s", tmp);
4923 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4924 "slot-active:%s", tmp);
4925 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4926 "slot-success:%s", tmp);
4927 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4928 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304929 fastboot_publish(slot_info[i].slot_is_unbootable,
4930 slot_info[i].slot_is_unbootable_rsp);
4931 fastboot_publish(slot_info[i].slot_is_active,
4932 slot_info[i].slot_is_active_rsp);
4933 fastboot_publish(slot_info[i].slot_is_succesful,
4934 slot_info[i].slot_is_succesful_rsp);
4935 fastboot_publish(slot_info[i].slot_retry_count,
4936 slot_info[i].slot_retry_count_rsp);
4937 }
4938 fastboot_publish("current-slot", active_slot_suffix);
4939 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4940 fastboot_publish("slot-count", slot_count);
4941 published = true;
4942 }
4943
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304944 active_slt = partition_find_active_slot();
4945 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304946 {
4947 tmp = SUFFIX_SLOT(active_slt);
4948 tmp++; // to remove "_" from slot_suffix.
4949 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4950 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304951 else
4952 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4953
Mayank Grover351a75e2017-05-30 20:06:08 +05304954 /* Update partition meta information */
4955 partition_fill_slot_meta(slot_info);
4956 return;
4957}
4958
lijuang4ece1e72015-08-14 21:02:36 +08004959void get_product_name(unsigned char *buf)
4960{
4961 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4962 return;
4963}
4964
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304965#if PRODUCT_IOT
4966void get_bootloader_version_iot(unsigned char *buf)
4967{
4968 if (buf != NULL)
4969 {
4970 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4971 strlcat(buf, "-", MAX_VERSION_LEN);
4972 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4973 }
4974 return;
4975}
4976#endif
4977
lijuang4ece1e72015-08-14 21:02:36 +08004978void get_bootloader_version(unsigned char *buf)
4979{
4980 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4981 return;
4982}
4983
4984void get_baseband_version(unsigned char *buf)
4985{
4986 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4987 return;
4988}
4989
Monika Singh32a09f72018-03-14 13:08:29 +05304990bool is_device_locked_critical()
4991{
4992 return device.is_unlock_critical ? false:true;
4993}
4994
lijuang4ece1e72015-08-14 21:02:36 +08004995bool is_device_locked()
4996{
4997 return device.is_unlocked ? false:true;
4998}
4999
Monika Singh32a09f72018-03-14 13:08:29 +05305000bool is_verity_enforcing()
5001{
5002 return device.verity_mode ? true:false;
5003}
5004
Amol Jadi5edf3552013-07-23 14:15:34 -07005005/* register commands and variables for fastboot */
5006void aboot_fastboot_register_commands(void)
5007{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005008 int i;
lijuangf16461c2015-08-03 17:09:34 +08005009 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07005010
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005011 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08005012 /* By default the enabled list is empty. */
5013 {"", NULL},
5014 /* move commands enclosed within the below ifndef to here
5015 * if they need to be enabled in user build.
5016 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005017#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005018 /* Register the following commands only for non-user builds */
5019 {"flash:", cmd_flash},
5020 {"erase:", cmd_erase},
5021 {"boot", cmd_boot},
5022 {"continue", cmd_continue},
5023 {"reboot", cmd_reboot},
5024 {"reboot-bootloader", cmd_reboot_bootloader},
5025 {"oem unlock", cmd_oem_unlock},
5026 {"oem unlock-go", cmd_oem_unlock_go},
5027 {"oem lock", cmd_oem_lock},
5028 {"flashing unlock", cmd_oem_unlock},
5029 {"flashing lock", cmd_oem_lock},
5030 {"flashing lock_critical", cmd_flashing_lock_critical},
5031 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5032 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5033 {"oem device-info", cmd_oem_devinfo},
5034 {"preflash", cmd_preflash},
5035 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5036 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005037 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005038 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305039 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305040#if DYNAMIC_PARTITION_SUPPORT
5041 {"reboot-fastboot",cmd_reboot_fastboot},
5042 {"reboot-recovery",cmd_reboot_recovery},
5043#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005044#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005045 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005046#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005047#endif
lijuang511a2b52015-08-14 20:50:51 +08005048 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005049
5050 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5051 for (i = 1; i < fastboot_cmds_count; i++)
5052 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5053
Amol Jadi5edf3552013-07-23 14:15:34 -07005054 /* publish variables and their values */
5055 fastboot_publish("product", TARGET(BOARD));
5056 fastboot_publish("kernel", "lk");
5057 fastboot_publish("serialno", sn_buf);
5058
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305059 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5060 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5061 fastboot_publish("hw-revision", soc_version_str);
5062
Amol Jadi5edf3552013-07-23 14:15:34 -07005063 /*
5064 * partition info is supported only for emmc partitions
5065 * Calling this for NAND prints some error messages which
5066 * is harmless but misleading. Avoid calling this for NAND
5067 * devices.
5068 */
5069 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305070 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005071
Mayank Grover351a75e2017-05-30 20:06:08 +05305072 if (partition_multislot_is_supported())
5073 publish_getvar_multislot_vars();
5074
Amol Jadi5edf3552013-07-23 14:15:34 -07005075 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005076#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005077 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5078 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005079#else
5080 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5081 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5082#endif
5083
Amol Jadi5edf3552013-07-23 14:15:34 -07005084 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005085 /* Is the charger screen check enabled */
5086 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5087 device.charger_screen_enabled);
5088 fastboot_publish("charger-screen-enabled",
5089 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08005090 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305091 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5092 device.display_panel);
5093 fastboot_publish("display-panel",
5094 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305095
5096 if (target_is_emmc_boot())
5097 {
5098 mmc_blocksize = mmc_get_device_blocksize();
5099 }
5100 else
5101 {
5102 mmc_blocksize = flash_block_size();
5103 }
5104 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5105 fastboot_publish("erase-block-size", (const char *) block_size_string);
5106 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305107#if PRODUCT_IOT
5108 get_bootloader_version_iot(&bootloader_version_string);
5109 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5110
5111 /* Version baseband is n/a for apq iot devices */
5112 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305113#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005114 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5115 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305116#endif
lijuangf16461c2015-08-03 17:09:34 +08005117 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305118 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005119 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5120 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5121 target_is_emmc_boot()? "eMMC":"UFS");
5122 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005123#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005124 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005125 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005126 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005127#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305128 if (target_dynamic_partition_supported())
5129 fastboot_publish("is-userspace", "no");
Amol Jadi5edf3552013-07-23 14:15:34 -07005130}
5131
Brian Swetland9c4c0752009-01-25 16:23:50 -08005132void aboot_init(const struct app_descriptor *app)
5133{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005134 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305135 int boot_err_type = 0;
5136 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07005137
lijuang39831732016-01-08 17:49:02 +08005138 /* Initialise wdog to catch early lk crashes */
5139#if WDOG_SUPPORT
5140 msm_wdog_init();
5141#endif
5142
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005143 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005144 if (target_is_emmc_boot())
5145 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005146 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005147 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305148 mmc_blocksize = mmc_get_device_blocksize();
5149 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005150 }
5151 else
5152 {
5153 page_size = flash_page_size();
5154 page_mask = page_size - 1;
5155 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005156 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5157
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005158 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05305159 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005160
Mayank Grover351a75e2017-05-30 20:06:08 +05305161 /* Detect multi-slot support */
5162 if (partition_multislot_is_supported())
5163 {
5164 boot_slot = partition_find_active_slot();
5165 if (boot_slot == INVALID)
5166 {
5167 boot_into_fastboot = true;
5168 dprintf(INFO, "Active Slot: (INVALID)\n");
5169 }
5170 else
5171 {
5172 /* Setting the state of system to boot active slot */
5173 partition_mark_active_slot(boot_slot);
5174 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5175 }
5176 }
5177
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005178 /* Display splash screen if enabled */
5179#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08005180#if NO_ALARM_DISPLAY
5181 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005182#endif
lijuang99c02d82015-02-13 19:04:34 +08005183 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07005184#if DISPLAY_HDMI_PRIMARY
5185 if (!strlen(device.display_panel))
5186 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5187 sizeof(device.display_panel));
5188#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005189#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07005190 /* Wait if the display shutdown is in progress */
5191 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005192 if (!pm_appsbl_display_init_done())
5193 target_display_init(device.display_panel);
5194 else
5195 display_image_on_screen();
5196#else
lijuang99c02d82015-02-13 19:04:34 +08005197 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005198#endif
lijuang99c02d82015-02-13 19:04:34 +08005199 dprintf(SPEW, "Display Init: Done\n");
5200#if NO_ALARM_DISPLAY
5201 }
5202#endif
5203#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005204
Greg Griscod6250552011-06-29 14:40:23 -07005205 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005206 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08005207
Dhaval Patel223ec952013-07-18 14:49:44 -07005208 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5209
Matthew Qindefd5562014-07-11 18:02:40 +08005210 /*
5211 * Check power off reason if user force reset,
5212 * if yes phone will do normal boot.
5213 */
5214 if (is_user_force_reset())
5215 goto normal_boot;
5216
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005217 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005218 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005219 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005220 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005221 reboot_device(EMERGENCY_DLOAD);
5222 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5223
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005224 boot_into_fastboot = true;
5225 }
5226 if (!boot_into_fastboot)
5227 {
5228 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5229 boot_into_recovery = 1;
5230 if (!boot_into_recovery &&
5231 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005232 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005233 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005234 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005235 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005236 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005237 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005238
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005239#if USE_PON_REBOOT_REG
5240 reboot_mode = check_hard_reboot_mode();
5241#else
Ajay Dudani77421292010-10-27 19:34:06 -07005242 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005243#endif
5244 if (reboot_mode == RECOVERY_MODE)
5245 {
Ajay Dudani77421292010-10-27 19:34:06 -07005246 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005247 }
5248 else if(reboot_mode == FASTBOOT_MODE)
5249 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005250 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005251 }
5252 else if(reboot_mode == ALARM_BOOT)
5253 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005254 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005255 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305256#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305257 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005258 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305259 if (reboot_mode == DM_VERITY_ENFORCING)
5260 {
5261 device.verity_mode = 1;
5262 write_device_info(&device);
5263 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005264#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305265 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005266#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305267 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005268#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305269 {
5270 device.verity_mode = 0;
5271 write_device_info(&device);
5272 }
5273 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5274 {
5275 if(send_delete_keys_to_tz())
5276 ASSERT(0);
5277 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005278 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305279#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005280
Matthew Qindefd5562014-07-11 18:02:40 +08005281normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005282 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005283 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005284 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005285 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005286 if(emmc_recovery_init())
5287 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5288 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005289 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005290 if((device.is_unlocked) || (device.is_tampered))
5291 {
5292 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305293 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005294 #endif
5295 #if USE_PCOM_SECBOOT
5296 set_tamper_flag(device.is_tampered);
5297 #endif
5298 }
Shashank Mittala0032282011-08-26 14:50:11 -07005299 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005300
Mayank Grover351a75e2017-05-30 20:06:08 +05305301retry_boot:
5302 /* Trying to boot active partition */
5303 if (partition_multislot_is_supported())
5304 {
5305 boot_slot = partition_find_boot_slot();
5306 partition_mark_active_slot(boot_slot);
5307 if (boot_slot == INVALID)
5308 goto fastboot;
5309 }
5310
5311 boot_err_type = boot_linux_from_mmc();
5312 switch (boot_err_type)
5313 {
5314 case ERR_INVALID_PAGE_SIZE:
5315 case ERR_DT_PARSE:
5316 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305317 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305318 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305319 {
5320 /*
5321 * Deactivate current slot, as it failed to
5322 * boot, and retry next slot.
5323 */
5324 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305325 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305326 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305327 else
5328 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305329 default:
5330 break;
5331 /* going to fastboot menu */
5332 }
Shashank Mittala0032282011-08-26 14:50:11 -07005333 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005334 else
5335 {
5336 recovery_init();
5337 #if USE_PCOM_SECBOOT
5338 if((device.is_unlocked) || (device.is_tampered))
5339 set_tamper_flag(device.is_tampered);
5340 #endif
5341 boot_linux_from_flash();
5342 }
5343 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5344 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005345 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005346
Mayank Grover351a75e2017-05-30 20:06:08 +05305347fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07005348 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005349
Amol Jadi5edf3552013-07-23 14:15:34 -07005350 /* register aboot specific fastboot commands */
5351 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005352
Amol Jadi5edf3552013-07-23 14:15:34 -07005353 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005354 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005355
5356 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305357#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005358 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305359#else
5360 /* Add salt buffer offset at start of image address to copy VB salt */
5361 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5362 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5363#endif
lijuang4ece1e72015-08-14 21:02:36 +08005364#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005365 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005366#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005367}
5368
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005369uint32_t get_page_size()
5370{
5371 return page_size;
5372}
5373
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005374/*
5375 * Calculated and save hash (SHA256) for non-signed boot image.
5376 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005377 * @param image_addr - Boot image address
5378 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005379 *
5380 * @return int - 0 on success, negative value on failure.
5381 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005382static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005383{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005384 unsigned int digest[8];
5385#if IMAGE_VERIF_ALGO_SHA1
5386 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5387#else
5388 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5389#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005390
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005391 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005392 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005393
5394 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005395 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005396
5397 return 0;
5398}
5399
Mayank Grover351a75e2017-05-30 20:06:08 +05305400
Brian Swetland9c4c0752009-01-25 16:23:50 -08005401APP_START(aboot)
5402 .init = aboot_init,
5403APP_END