blob: a69056b6193fb7800aa2328f7945289d9a6aecae [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Mayank Grover98c4c742019-04-25 17:21:37 +05305 * Copyright (c) 2009-2019, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Mayank Grover351a75e2017-05-30 20:06:08 +053051#include <ab_partition_parser.h>
Monika Singh292b3e92018-03-17 22:40:23 +053052#include <verifiedboot.h>
Greg Griscod6250552011-06-29 14:40:23 -070053#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070054#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070055#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070056#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030057#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070058#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070059#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070060#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053061#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080062#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053063#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080064#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080065#if USE_RPMB_FOR_DEVINFO
66#include <rpmb.h>
67#endif
Dima Zavin214cc642009-01-26 11:16:21 -080068
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070069#if ENABLE_WBC
Umang Chheda4c140de2019-12-19 14:30:38 +053070#include <pm_smbchg_common.h>
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070071#endif
72
Neeti Desai17379b82012-06-04 18:42:53 -070073#if DEVICE_TREE
74#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070076#endif
77
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053078#if WDOG_SUPPORT
79#include <wdog.h>
80#endif
81
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070082#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070083#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080084#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080085#include "bootimg.h"
86#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070087#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080088#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070089#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070091#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070092#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020093#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070094#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080095#include <menu_keys_detect.h>
96#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070097#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070098
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700100extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700101extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700102extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530103 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700105void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700106void write_device_info_mmc(device_info *dev);
107void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700108static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700109static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530110static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530111bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530112static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530113static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700114/* fastboot command function pointer */
115typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
Monika Singh292b3e92018-03-17 22:40:23 +0530116bool get_perm_attr_status();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700117
118struct fastboot_cmd_desc {
119 char * name;
120 fastboot_cmd_fn cb;
121};
122
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700123#define EXPAND(NAME) #NAME
124#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700125
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700126#define DISPLAY_PANEL_HDMI "hdmi"
127
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800128#ifdef MEMBASE
129#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
130#else
David Ng183a7422009-12-07 14:55:21 -0800131#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800132#endif
133
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700134#ifndef MEMSIZE
135#define MEMSIZE 1024*1024
136#endif
137
138#define MAX_TAGS_SIZE 1024
Kishor PKee5c0a32018-03-06 16:49:46 +0530139#define PLL_CODES_OFFSET 4096
Kun Liang2f1601a2013-08-12 16:29:54 +0800140/* make 4096 as default size to ensure EFS,EXT4's erasing */
141#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700142#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530143#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800144
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700145#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700146#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800147
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800148#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
149
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700150#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
151
Ameya Thakur10a33452016-06-13 14:24:26 -0700152//Size of the header that is used in case the boot image has
153//a uncompressed kernel + appended dtb
154#define PATCHED_KERNEL_HEADER_SIZE 20
155
156//String used to determine if the boot image has
157//a uncompressed kernel + appended dtb
158#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
159
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800160#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700161static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700162#else
David Ng183a7422009-12-07 14:55:21 -0800163static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700164#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530165static const char *dynamic_bootdev_cmdline =
166 " androidboot.boot_devices=soc/";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800167static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300168static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530169
170static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800171static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800172static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800173static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700174static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300175static const char *secondary_gpt_enable = " gpt";
Monika Singh292b3e92018-03-17 22:40:23 +0530176#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200177static const char *mdtp_activated_flag = " mdtp";
Monika Singh292b3e92018-03-17 22:40:23 +0530178#endif
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800179static const char *baseband_apq = " androidboot.baseband=apq";
180static const char *baseband_msm = " androidboot.baseband=msm";
181static const char *baseband_csfb = " androidboot.baseband=csfb";
182static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700183static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800184static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700185static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800186static const char *baseband_dsda = " androidboot.baseband=dsda";
187static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800188static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800189static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530190static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530191static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
192static const char *skip_ramfs = " skip_initramfs";
Mayank Grover466d6562018-05-10 14:52:20 +0530193
194#if HIBERNATION_SUPPORT
195static const char *resume = " resume=/dev/mmcblk0p";
196#endif
197
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530198#ifdef INIT_BIN_LE
199static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE;
200#else
Mayank Grover3804be72017-06-22 11:59:23 +0530201static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530202#endif
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530203
204#if VERITY_LE
205static const char *verity_dev = " root=/dev/dm-0";
206static const char *verity_system_part = " dm=\"system";
207static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
208#else
Mayank Grover5384ed82018-05-09 12:09:24 +0530209static const char *sys_path = " root=/dev/mmcblk0p";
lijuang83ef4b22018-08-23 11:01:55 +0800210
211#define MAX_DTBO_IDX_STR 64
212static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx=";
Parth Dixit1375d9e2019-07-08 20:56:13 +0530213
214#define MAX_DTB_IDX_STR MAX_DTBO_IDX_STR
215static const char *android_boot_dtb_idx = " androidboot.dtb_idx=";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530216#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800217
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700218#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700219static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700220static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530221static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700222//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700223
224struct verified_boot_verity_mode vbvm[] =
225{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700226#if ENABLE_VB_ATTEST
227 {false, "eio"},
228#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700229 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700230#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700231 {true, "enforcing"},
232};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700233struct verified_boot_state_name vbsn[] =
234{
235 {GREEN, "green"},
236 {ORANGE, "orange"},
237 {YELLOW,"yellow"},
238 {RED,"red" },
239};
240#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700241/*As per spec delay wait time before shutdown in Red state*/
242#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700243static unsigned page_size = 0;
244static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530245static unsigned mmc_blocksize = 0;
246static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700247static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
248static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530249static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800250static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700251static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700252bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530253static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530254static char *vbcmdline;
255static bootinfo info = {0};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530256static void *recovery_dtbo_buf = NULL;
257static uint32_t recovery_dtbo_size = 0;
258
Shashank Mittalcd98d472011-08-02 14:29:24 -0700259/* Assuming unauthorized kernel image by default */
260static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530261static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
262
vijay kumarc65876c2015-04-24 13:29:16 +0530263static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700264
vijay kumarca2e6812015-07-08 20:28:25 +0530265static char frp_ptns[2][8] = {"config","frp"};
266
lijuang511a2b52015-08-14 20:50:51 +0800267static const char *critical_flash_allowed_ptn[] = {
268 "aboot",
269 "rpm",
270 "tz",
271 "sbl",
272 "sdi",
273 "sbl1",
274 "xbl",
275 "hyp",
276 "pmic",
277 "bootloader",
278 "devinfo",
279 "partition"};
280
Dima Zavin42168f22009-01-30 11:52:22 -0800281struct atag_ptbl_entry
282{
283 char name[16];
284 unsigned offset;
285 unsigned size;
286 unsigned flags;
287};
288
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700289/*
290 * Partition info, required to be published
291 * for fastboot
292 */
293struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530294 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700295 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
296 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
297 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
298 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
299};
300
301/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530302 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700303 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530304#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530305#define EXT_STR "ext4"
306#define F2FS_STR "f2fs"
307
308#define FS_SUPERBLOCK_OFFSET 0x400
309#define EXT_MAGIC 0xEF53
310#define EXT_MAGIC_OFFSET_SB 0x38
311#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
312#define F2FS_MAGIC_OFFSET_SB 0x0
313
314typedef enum fs_signature_type {
315 EXT_FS_SIGNATURE = 1,
316 EXT_F2FS_SIGNATURE = 2,
317 NO_FS = -1
318} fs_signature_type;
319
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530320struct getvar_partition_info part_info[NUM_PARTITIONS];
321struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700322{
Mayank Grover49920212018-02-09 18:15:55 +0530323 { "system" , "partition-size:", "partition-type:", "", "ext4" },
324 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
325 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
326 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700327};
328
329char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700330char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800331char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700332char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530333char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530334char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530335char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530336#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530337
338/* For IOT we are using custom version */
339#define PRODUCT_IOT_VERSION "IOT001"
340char bootloader_version_string[MAX_RSP_SIZE];
341#endif
lijuang102dfa92015-10-09 18:31:03 +0800342
343#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800344char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800345char battery_soc_ok [MAX_RSP_SIZE];
346#endif
347
lijuangf16461c2015-08-03 17:09:34 +0800348char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700349
Greg Griscod2471ef2011-07-14 13:00:42 -0700350extern int emmc_recovery_init(void);
351
Kinson Chik0b1c8162011-08-31 16:31:57 -0700352#if NO_KEYPAD_DRIVER
353extern int fastboot_trigger(void);
354#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700355
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530356static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700357{
358 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700359#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800360 if (is_arm64)
361 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
362 else
363 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700364 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
365 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700366#endif
367}
368
Dima Zavin42168f22009-01-30 11:52:22 -0800369static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
370{
371 struct atag_ptbl_entry atag_ptn;
372
373 memcpy(atag_ptn.name, ptn->name, 16);
374 atag_ptn.name[15] = '\0';
375 atag_ptn.offset = ptn->start;
376 atag_ptn.size = ptn->length;
377 atag_ptn.flags = ptn->flags;
378 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
379 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
380}
Mayank Grover9df84c02018-08-30 15:46:35 +0530381#ifdef VERIFIED_BOOT_2
382void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
383{
384 int index = 0;
385 char *vbm_img_buf = NULL;
386 unsigned long long ptn = 0;
387 unsigned long long ptn_size = 0;
388
389 /* Immediately return if dtbo is not supported */
390 index = partition_get_index("vbmeta");
391 ptn = partition_get_offset(index);
392 if(!ptn)
393 {
394 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
395 return;
396 }
397
398 ptn_size = partition_get_size(index);
399 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
400 {
401 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
402 return;
403 }
404
405 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
406 if (!vbm_img_buf)
407 {
408 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
409 return;
410 }
411
412 mmc_set_lun(partition_get_lun(index));
413 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
414 {
415 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
416 free(vbm_img_buf);
417 return;
418 }
419
420 *vbmeta_image_buf = vbm_img_buf;
421 *vbmeta_image_sz = (uint32_t)ptn_size;
422 return;
423}
424#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800425
lijuang102dfa92015-10-09 18:31:03 +0800426#if CHECK_BAT_VOLTAGE
427void update_battery_status(void)
428{
429 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
430 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
431}
432#endif
433
Neeti Desaie245d492012-06-01 12:52:13 -0700434unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800435{
David Ng183a7422009-12-07 14:55:21 -0800436 int cmdline_len = 0;
437 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800438 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700439 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800440 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300441 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700442 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700443 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530444#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530445 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530446#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530447 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530448 int system_ptn_index = -1;
449 unsigned int lun = 0;
450 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530451#if VERITY_LE
452 int syspath_buflen = strlen(verity_dev)
453 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
454 + strlen(verity_params) + sizeof(int) + 2;
455#else
lijuang83ef4b22018-08-23 11:01:55 +0800456 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
457 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
458 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530459 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
460 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530461#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530462 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530463#if HIBERNATION_SUPPORT
464 int resume_buflen = strlen(resume) + sizeof(int) + 2;
465 char resume_buf[resume_buflen];
466 int swap_ptn_index = INVALID_PTN;
467#endif
468
Mayank Grover889be1b2017-09-12 20:12:23 +0530469#if VERIFIED_BOOT
470 uint32_t boot_state = RED;
471#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530472
473#if USE_LE_SYSTEMD
474 is_systemd_present=true;
475#endif
476
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700477#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530478 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530479 {
480 boot_state = boot_verify_get_state();
481 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530482#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700483
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200484#ifdef MDTP_SUPPORT
485 mdtp_activated(&is_mdtp_activated);
486#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800487
Brian Swetland9c4c0752009-01-25 16:23:50 -0800488 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800489 cmdline_len = strlen(cmdline);
490 have_cmdline = 1;
491 }
492 if (target_is_emmc_boot()) {
493 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700494 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530495 if (!boot_dev_buf) {
496 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
497 } else {
498 platform_boot_dev_cmdline(boot_dev_buf);
499#if USE_BOOTDEV_CMDLINE
500 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700501#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530502 if (target_dynamic_partition_supported()) {
503 cmdline_len += strlen(dynamic_bootdev_cmdline);
504 cmdline_len += strlen(boot_dev_buf);
505 }
506 }
David Ng183a7422009-12-07 14:55:21 -0800507 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800508
509 cmdline_len += strlen(usb_sn_cmdline);
510 cmdline_len += strlen(sn_buf);
511
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700512#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530513 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700514 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530515 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
516 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
517 {
518 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
519 ASSERT(0);
520 }
521 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
522 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700523 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530524#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700525
Monika Singh292b3e92018-03-17 22:40:23 +0530526
527 if (vbcmdline != NULL) {
528 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
529 strlen(vbcmdline));
530 cmdline_len += strlen(vbcmdline);
531 }
532
Pavel Nedev5614d222013-06-17 18:01:02 +0300533 if (boot_into_recovery && gpt_exists)
534 cmdline_len += strlen(secondary_gpt_enable);
535
Monika Singh292b3e92018-03-17 22:40:23 +0530536#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200537 if(is_mdtp_activated)
538 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530539#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300540 if (boot_into_ffbm) {
541 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530542
543 if(is_systemd_present)
544 cmdline_len += strlen(systemd_ffbm_mode);
545
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700546 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800547 /* reduce kernel console messages to speed-up boot */
548 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800549 } else if (boot_reason_alarm) {
550 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800551 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800552 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700553 pause_at_bootup = 1;
554 cmdline_len += strlen(battchg_pause);
555 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800556
Shashank Mittalcd98d472011-08-02 14:29:24 -0700557 if(target_use_signed_kernel() && auth_kernel_img) {
558 cmdline_len += strlen(auth_kernel);
559 }
560
Joonwoo Park61112782013-10-02 19:50:39 -0700561 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
562 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530563 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700564 have_target_boot_params = 1;
565 cmdline_len += strlen(target_boot_params);
566 }
567
Ajay Dudanid04110c2011-01-17 23:55:07 -0800568 /* Determine correct androidboot.baseband to use */
569 switch(target_baseband())
570 {
571 case BASEBAND_APQ:
572 cmdline_len += strlen(baseband_apq);
573 break;
574
575 case BASEBAND_MSM:
576 cmdline_len += strlen(baseband_msm);
577 break;
578
579 case BASEBAND_CSFB:
580 cmdline_len += strlen(baseband_csfb);
581 break;
582
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800583 case BASEBAND_SVLTE2A:
584 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800585 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700586
587 case BASEBAND_MDM:
588 cmdline_len += strlen(baseband_mdm);
589 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700590
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800591 case BASEBAND_MDM2:
592 cmdline_len += strlen(baseband_mdm2);
593 break;
594
Amol Jadi5c61a952012-05-04 17:05:35 -0700595 case BASEBAND_SGLTE:
596 cmdline_len += strlen(baseband_sglte);
597 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530598
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800599 case BASEBAND_SGLTE2:
600 cmdline_len += strlen(baseband_sglte2);
601 break;
602
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530603 case BASEBAND_DSDA:
604 cmdline_len += strlen(baseband_dsda);
605 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800606
607 case BASEBAND_DSDA2:
608 cmdline_len += strlen(baseband_dsda2);
609 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530610 case BASEBAND_APQ_NOWGR:
611 cmdline_len += strlen(baseband_apq_nowgr);
612 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800613 }
614
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300615#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800616 if (cmdline) {
617 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530618 target_display_panel_node(display_panel_buf,
619 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800620 strlen(display_panel_buf)) {
621 cmdline_len += strlen(display_panel_buf);
622 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700623 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300624#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700625
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800626 if (target_warm_boot()) {
627 warm_boot = true;
628 cmdline_len += strlen(warmboot_cmdline);
629 }
630
Mayank Grover5384ed82018-05-09 12:09:24 +0530631 if (target_uses_system_as_root() ||
632 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530633 {
634 current_active_slot = partition_find_active_slot();
635 cmdline_len += (strlen(androidboot_slot_suffix)+
636 strlen(SUFFIX_SLOT(current_active_slot)));
637
Mayank Grover3804be72017-06-22 11:59:23 +0530638 system_ptn_index = partition_get_index("system");
639 if (platform_boot_dev_isemmc())
640 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530641#if VERITY_LE
642 /*
643 Condition 4: Verity and A/B both enabled
644 Eventual command line looks like:
645 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
646 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
647 */
648 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
649 verity_dev,
650 verity_system_part, suffix_slot[current_active_slot],
651 verity_params, system_ptn_index + 1);
652#else
653 /*
654 Condition 5: A/B enabled, but verity disabled
655 Eventual command line looks like:
656 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
657 ... root=/dev/mmcblk0p<NN> ...
658 */
659 snprintf(syspath_buf, syspath_buflen, " %s%d",
660 sys_path, system_ptn_index + 1);
661#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530662 }
663 else
664 {
665 lun = partition_get_lun(system_ptn_index);
666 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
667 lun_char_base + lun,
668 partition_get_index_in_lun("system", lun));
669 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530670
Monika Singh292b3e92018-03-17 22:40:23 +0530671#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530672 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530673#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530674 }
675
676 if (target_uses_system_as_root() ||
677 partition_multislot_is_supported())
678 {
679 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530680
681 /* For dynamic partition, support skip skip_initramfs */
682 if (!target_dynamic_partition_supported() &&
683 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530684 cmdline_len += strlen(skip_ramfs);
685 }
686
Mayank Grover466d6562018-05-10 14:52:20 +0530687#if HIBERNATION_SUPPORT
688 if (platform_boot_dev_isemmc())
689 {
690 swap_ptn_index = partition_get_index("swap");
691 if (swap_ptn_index != INVALID_PTN)
692 {
693 snprintf(resume_buf, resume_buflen,
694 " %s%d", resume,
695 (swap_ptn_index + 1));
696 cmdline_len += strlen(resume_buf);
697 }
698 else
699 {
700 dprintf(INFO, "WARNING: swap partition not found\n");
701 }
702 }
703#endif
704
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800705#if TARGET_CMDLINE_SUPPORT
706 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
707 int target_cmd_line_len;
708 ASSERT(target_cmdline_buf);
709 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
710 cmdline_len += target_cmd_line_len;
711#endif
712
lijuang83ef4b22018-08-23 11:01:55 +0800713#if !VERITY_LE
714 dtbo_idx = get_dtbo_idx ();
715 if (dtbo_idx != INVALID_PTN) {
716 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
717 android_boot_dtbo_idx, dtbo_idx);
718 cmdline_len += strlen (dtbo_idx_str);
719 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530720
721 dtb_idx = get_dtb_idx ();
722 if (dtb_idx != INVALID_PTN) {
723 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
724 android_boot_dtb_idx, dtb_idx);
725 cmdline_len += strlen (dtb_idx_str);
726 }
lijuang83ef4b22018-08-23 11:01:55 +0800727#endif
728
David Ng183a7422009-12-07 14:55:21 -0800729 if (cmdline_len > 0) {
730 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800731 unsigned char *dst;
732
733 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
734 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530735 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800736 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700737
Amol Jadi168b7712012-03-06 16:15:00 -0800738 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800739 if (have_cmdline) {
740 src = cmdline;
741 while ((*dst++ = *src++));
742 }
743 if (target_is_emmc_boot()) {
744 src = emmc_cmdline;
745 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700746 have_cmdline = 1;
747 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800748#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700749 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530750 if (have_cmdline &&
751 boot_dev_buf) {
752 --dst;
753 while ((*dst++ = *src++));
754 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700755#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530756 /* Dynamic partition append boot_devices */
757 if (target_dynamic_partition_supported() &&
758 boot_dev_buf) {
759 src = dynamic_bootdev_cmdline;
760 if (have_cmdline) --dst;
761 while ((*dst++ = *src++));
762 src = boot_dev_buf;
763 if (have_cmdline) --dst;
764 while ((*dst++ = *src++));
765 }
David Ngf773dde2010-07-26 19:55:08 -0700766 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800767
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700768#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530769 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700770 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530771 src = verified_state;
772 if(have_cmdline) --dst;
773 have_cmdline = 1;
774 while ((*dst++ = *src++));
775 src = vbsn[boot_state].name;
776 if(have_cmdline) --dst;
777 while ((*dst++ = *src++));
778
779 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
780 {
781 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
782 ASSERT(0);
783 }
784 src = verity_mode;
785 if(have_cmdline) --dst;
786 while ((*dst++ = *src++));
787 src = vbvm[device.verity_mode].name;
788 if(have_cmdline) -- dst;
789 while ((*dst++ = *src++));
790 src = keymaster_v1;
791 if(have_cmdline) --dst;
792 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700793 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530794#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530795
796 if (vbcmdline != NULL) {
797 src = vbcmdline;
798 if (have_cmdline) --dst;
799 while ((*dst++ = *src++));
800 }
801
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800802 src = usb_sn_cmdline;
803 if (have_cmdline) --dst;
804 have_cmdline = 1;
805 while ((*dst++ = *src++));
806 src = sn_buf;
807 if (have_cmdline) --dst;
808 have_cmdline = 1;
809 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800810 if (warm_boot) {
811 if (have_cmdline) --dst;
812 src = warmboot_cmdline;
813 while ((*dst++ = *src++));
814 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800815
Pavel Nedev5614d222013-06-17 18:01:02 +0300816 if (boot_into_recovery && gpt_exists) {
817 src = secondary_gpt_enable;
818 if (have_cmdline) --dst;
819 while ((*dst++ = *src++));
820 }
Monika Singh292b3e92018-03-17 22:40:23 +0530821#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200822 if (is_mdtp_activated) {
823 src = mdtp_activated_flag;
824 if (have_cmdline) --dst;
825 while ((*dst++ = *src++));
826 }
Monika Singh292b3e92018-03-17 22:40:23 +0530827#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300828 if (boot_into_ffbm) {
829 src = androidboot_mode;
830 if (have_cmdline) --dst;
831 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700832 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300833 if (have_cmdline) --dst;
834 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530835
836 if(is_systemd_present) {
837 src = systemd_ffbm_mode;
838 if (have_cmdline) --dst;
839 while ((*dst++ = *src++));
840 }
841
Pavel Nedev898298c2013-02-27 12:36:09 -0800842 src = loglevel;
843 if (have_cmdline) --dst;
844 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800845 } else if (boot_reason_alarm) {
846 src = alarmboot_cmdline;
847 if (have_cmdline) --dst;
848 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300849 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700850 src = battchg_pause;
851 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800852 while ((*dst++ = *src++));
853 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800854
Shashank Mittalcd98d472011-08-02 14:29:24 -0700855 if(target_use_signed_kernel() && auth_kernel_img) {
856 src = auth_kernel;
857 if (have_cmdline) --dst;
858 while ((*dst++ = *src++));
859 }
860
Ajay Dudanid04110c2011-01-17 23:55:07 -0800861 switch(target_baseband())
862 {
863 case BASEBAND_APQ:
864 src = baseband_apq;
865 if (have_cmdline) --dst;
866 while ((*dst++ = *src++));
867 break;
868
869 case BASEBAND_MSM:
870 src = baseband_msm;
871 if (have_cmdline) --dst;
872 while ((*dst++ = *src++));
873 break;
874
875 case BASEBAND_CSFB:
876 src = baseband_csfb;
877 if (have_cmdline) --dst;
878 while ((*dst++ = *src++));
879 break;
880
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800881 case BASEBAND_SVLTE2A:
882 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800883 if (have_cmdline) --dst;
884 while ((*dst++ = *src++));
885 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700886
887 case BASEBAND_MDM:
888 src = baseband_mdm;
889 if (have_cmdline) --dst;
890 while ((*dst++ = *src++));
891 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700892
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800893 case BASEBAND_MDM2:
894 src = baseband_mdm2;
895 if (have_cmdline) --dst;
896 while ((*dst++ = *src++));
897 break;
898
Amol Jadi5c61a952012-05-04 17:05:35 -0700899 case BASEBAND_SGLTE:
900 src = baseband_sglte;
901 if (have_cmdline) --dst;
902 while ((*dst++ = *src++));
903 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530904
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800905 case BASEBAND_SGLTE2:
906 src = baseband_sglte2;
907 if (have_cmdline) --dst;
908 while ((*dst++ = *src++));
909 break;
910
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530911 case BASEBAND_DSDA:
912 src = baseband_dsda;
913 if (have_cmdline) --dst;
914 while ((*dst++ = *src++));
915 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800916
917 case BASEBAND_DSDA2:
918 src = baseband_dsda2;
919 if (have_cmdline) --dst;
920 while ((*dst++ = *src++));
921 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530922 case BASEBAND_APQ_NOWGR:
923 src = baseband_apq_nowgr;
924 if (have_cmdline) --dst;
925 while ((*dst++ = *src++));
926 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800927 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700928
929 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700930 src = display_panel_buf;
931 if (have_cmdline) --dst;
932 while ((*dst++ = *src++));
933 }
Joonwoo Park61112782013-10-02 19:50:39 -0700934
935 if (have_target_boot_params) {
936 if (have_cmdline) --dst;
937 src = target_boot_params;
938 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530939 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700940 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800941
Mayank Grover351a75e2017-05-30 20:06:08 +0530942 if (partition_multislot_is_supported() && have_cmdline)
943 {
944 src = androidboot_slot_suffix;
945 --dst;
946 while ((*dst++ = *src++));
947 --dst;
948 src = SUFFIX_SLOT(current_active_slot);
949 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530950 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530951
Mayank Grover351a75e2017-05-30 20:06:08 +0530952
Mayank Grover5384ed82018-05-09 12:09:24 +0530953 /*
954 * System-As-Root behaviour, system.img should contain both
955 * system content and ramdisk content, and should be mounted at
956 * root(a/b).
957 * Apending skip_ramfs for non a/b builds which use, system as root.
958 */
959 if ((target_uses_system_as_root() ||
960 partition_multislot_is_supported()) &&
961 have_cmdline)
962 {
Mayank Groverb716edf2019-05-08 16:06:55 +0530963 if (!target_dynamic_partition_supported() &&
964 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +0530965 {
966 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +0530967 --dst;
968 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530969 }
970
971 src = sys_path_cmdline;
972 --dst;
973 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530974
Monika Singh292b3e92018-03-17 22:40:23 +0530975#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +0530976 src = syspath_buf;
977 --dst;
978 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +0530979#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530980 }
981
Mayank Grover466d6562018-05-10 14:52:20 +0530982#if HIBERNATION_SUPPORT
983 if (swap_ptn_index != INVALID_PTN)
984 {
985 src = resume_buf;
986 --dst;
987 while ((*dst++ = *src++));
988 }
989#endif
990
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800991#if TARGET_CMDLINE_SUPPORT
992 if (target_cmdline_buf && target_cmd_line_len)
993 {
994 if (have_cmdline) --dst;
995 src = target_cmdline_buf;
996 while((*dst++ = *src++));
997 free(target_cmdline_buf);
998 }
999#endif
lijuang83ef4b22018-08-23 11:01:55 +08001000
1001#if !VERITY_LE
1002 if (dtbo_idx != INVALID_PTN) {
1003 src = dtbo_idx_str;
1004 --dst;
1005 while ((*dst++ = *src++));
1006 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301007
1008 if (dtb_idx != INVALID_PTN) {
1009 src = dtb_idx_str;
1010 --dst;
1011 while ((*dst++ = *src++));
1012 }
lijuang83ef4b22018-08-23 11:01:55 +08001013#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001014 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001015
1016
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001017 if (boot_dev_buf)
1018 free(boot_dev_buf);
1019
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001020 if (cmdline_final)
1021 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1022 else
1023 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001024 return cmdline_final;
1025}
1026
1027unsigned *atag_core(unsigned *ptr)
1028{
1029 /* CORE */
1030 *ptr++ = 2;
1031 *ptr++ = 0x54410001;
1032
1033 return ptr;
1034
1035}
1036
1037unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1038 unsigned ramdisk_size)
1039{
1040 if (ramdisk_size) {
1041 *ptr++ = 4;
1042 *ptr++ = 0x54420005;
1043 *ptr++ = (unsigned)ramdisk;
1044 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001045 }
1046
Neeti Desaie245d492012-06-01 12:52:13 -07001047 return ptr;
1048}
1049
1050unsigned *atag_ptable(unsigned **ptr_addr)
1051{
1052 int i;
1053 struct ptable *ptable;
1054
1055 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001056 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1057 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001058 *(*ptr_addr)++ = 0x4d534d70;
1059 for (i = 0; i < ptable->count; ++i)
1060 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1061 }
1062
1063 return (*ptr_addr);
1064}
1065
1066unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1067{
1068 int cmdline_length = 0;
1069 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001070 char *dest;
1071
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001072 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001073 n = (cmdline_length + 4) & (~3);
1074
1075 *ptr++ = (n / 4) + 2;
1076 *ptr++ = 0x54410009;
1077 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001078 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001079 ptr += (n / 4);
1080
1081 return ptr;
1082}
1083
1084unsigned *atag_end(unsigned *ptr)
1085{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001086 /* END */
1087 *ptr++ = 0;
1088 *ptr++ = 0;
1089
Neeti Desaie245d492012-06-01 12:52:13 -07001090 return ptr;
1091}
1092
1093void generate_atags(unsigned *ptr, const char *cmdline,
1094 void *ramdisk, unsigned ramdisk_size)
1095{
vijay kumar21a37452015-12-29 16:23:21 +05301096 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001097 ptr = atag_core(ptr);
1098 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1099 ptr = target_atag_mem(ptr);
1100
1101 /* Skip NAND partition ATAGS for eMMC boot */
1102 if (!target_is_emmc_boot()){
1103 ptr = atag_ptable(&ptr);
1104 }
1105
vijay kumar21a37452015-12-29 16:23:21 +05301106 /*
1107 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1108 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1109 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301110 if (!cmdline)
1111 return;
1112
vijay kumar21a37452015-12-29 16:23:21 +05301113 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1114 ptr = atag_cmdline(ptr, cmdline);
1115 ptr = atag_end(ptr);
1116 }
1117 else {
1118 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1119 ASSERT(0);
1120 }
Neeti Desaie245d492012-06-01 12:52:13 -07001121}
1122
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001123typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001124void boot_linux(void *kernel, unsigned *tags,
1125 const char *cmdline, unsigned machtype,
1126 void *ramdisk, unsigned ramdisk_size)
1127{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001128 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001129#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001130 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001131#endif
1132
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001133 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001134 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301135 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001136
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301137 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001138
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001139 final_cmdline = update_cmdline((const char*)cmdline);
1140
Neeti Desai17379b82012-06-04 18:42:53 -07001141#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001142 dprintf(INFO, "Updating device tree: start\n");
1143
Neeti Desai17379b82012-06-04 18:42:53 -07001144 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301145 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001146 if(ret)
1147 {
1148 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1149 ASSERT(0);
1150 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001151 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001152#else
Neeti Desaie245d492012-06-01 12:52:13 -07001153 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001154 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001155#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001156
Monika Singh0ec6db82019-07-18 16:57:58 +05301157#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301158 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301159 {
1160 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001161#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001162#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301163 display_bootverify_menu(DISPLAY_MENU_EIO);
1164 wait_for_users_action();
1165 if(!pwr_key_is_pressed)
1166 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001167#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301168 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001169#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301170 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001171#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301172 dprintf(CRITICAL,
1173 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1174 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001175#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301176 }
lijuangbdd9bb42016-03-01 18:22:17 +08001177 }
lijuangbdd9bb42016-03-01 18:22:17 +08001178#endif
1179
1180#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001181 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001182 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001183 {
1184 dprintf(INFO, "Failed to write protect dev info\n");
1185 ASSERT(0);
1186 }
1187#endif
1188
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001189 /* Turn off splash screen if enabled */
1190#if DISPLAY_SPLASH_SCREEN
1191 target_display_shutdown();
1192#endif
1193
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001194 /* Perform target specific cleanup */
1195 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301196 free_verified_boot_resource(&info);
1197 if (final_cmdline)
1198 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001199
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001200 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301201 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001202
1203 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001204
Amol Jadi4421e652011-06-16 15:00:48 -07001205 /* do any platform specific cleanup before kernel entry */
1206 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001207
Brian Swetland9c4c0752009-01-25 16:23:50 -08001208 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001209
Amol Jadi504f9fe2012-08-16 13:56:48 -07001210#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001211 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001212#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001213 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001214
1215 if (IS_ARM64(kptr))
1216 /* Jump to a 64bit kernel */
1217 scm_elexec_call((paddr_t)kernel, tags_phys);
1218 else
1219 /* Jump to a 32bit kernel */
1220 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001221}
1222
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001223/* Function to check if the memory address range falls within the aboot
1224 * boundaries.
1225 * start: Start of the memory region
1226 * size: Size of the memory region
1227 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301228int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001229{
1230 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001231 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001232 return -1;
1233
1234 /* Check for memory overlap. */
1235 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1236 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001237 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001238 return 0;
1239 else
1240 return -1;
1241}
1242
Mayank Grovere559cec2017-10-17 15:12:03 +05301243/* Function to check if the memory address range falls beyond ddr region.
1244 * start: Start of the memory region
1245 * size: Size of the memory region
1246 */
1247int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1248{
1249 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1250 uint64_t ddr_size = smem_get_ddr_size();
1251 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1252 uintptr_t pa_start_addr = PA(start);
1253
1254 /* Check for boundary conditions. */
1255 if ((UINT_MAX - start) < size)
1256 return -1;
1257
1258 /* Check if memory range is beyond the ddr range. */
1259 if (pa_start_addr < ddr_pa_start_addr ||
1260 pa_start_addr >= (ddr_pa_end_addr) ||
1261 (pa_start_addr + size) > ddr_pa_end_addr)
1262 return -1;
1263 else
1264 return 0;
1265}
1266
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001267BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001268
Mayank Groverdd080e82018-09-04 20:12:13 +05301269int getimage(void **image_buffer, uint32_t *imgsize,
1270 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301271{
Mayank Groverdd080e82018-09-04 20:12:13 +05301272 uint32_t loadedindex;
1273 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301274 imgname == NULL) {
1275 dprintf(CRITICAL, "getimage: invalid parameters\n");
1276 return -1;
1277 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301278 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1279 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301280 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301281 *image_buffer = info.images[loadedindex].image_buffer;
1282 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301283 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1284 info.images[loadedindex].name,
1285 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301286 return 0;
1287 }
1288 }
1289 return -1;
1290}
1291
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001292static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1293{
1294 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001295
1296#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001297#if IMAGE_VERIF_ALGO_SHA1
1298 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1299#else
1300 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1301#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001302#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001303
1304 /* Assume device is rooted at this time. */
1305 device.is_tampered = 1;
1306
1307 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1308
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001309#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301310 uint32_t bootstate;
1311 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301312 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301313 {
1314 ret = boot_verify_image((unsigned char *)bootimg_addr,
1315 bootimg_size, "/recovery", &bootstate);
1316 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001317 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301318 {
1319 ret = boot_verify_image((unsigned char *)bootimg_addr,
1320 bootimg_size, "/boot", &bootstate);
1321 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001322 boot_verify_print_state();
1323#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001324 ret = image_verify((unsigned char *)bootimg_addr,
1325 (unsigned char *)(bootimg_addr + bootimg_size),
1326 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001327 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001328#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001329 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1330
1331 if (ret)
1332 {
1333 /* Authorized kernel */
1334 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001335 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001336 }
1337
Amit Blay4aa292f2015-04-28 21:55:59 +03001338#ifdef MDTP_SUPPORT
1339 {
1340 /* Verify MDTP lock.
1341 * For boot & recovery partitions, use aboot's verification result.
1342 */
1343 mdtp_ext_partition_verification_t ext_partition;
1344 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1345 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1346 ext_partition.page_size = 0; /* Not needed since already validated */
1347 ext_partition.image_addr = 0; /* Not needed since already validated */
1348 ext_partition.image_size = 0; /* Not needed since already validated */
1349 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1350 mdtp_fwlock_verify_lock(&ext_partition);
1351 }
1352#endif /* MDTP_SUPPORT */
1353
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001354#if USE_PCOM_SECBOOT
1355 set_tamper_flag(device.is_tampered);
1356#endif
1357
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001358#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001359 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001360 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001361 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001362#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001363 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001364#if ENABLE_VB_ATTEST
1365 mdelay(DELAY_WAIT);
1366 shutdown_device();
1367#else
lijuanga40d6302015-07-20 20:10:13 +08001368 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001369#endif
lijuanga40d6302015-07-20 20:10:13 +08001370#else
1371 dprintf(CRITICAL,
1372 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1373 mdelay(5000);
1374#endif
1375
1376 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001377 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001378#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001379 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001380 wait_for_users_action();
1381#else
1382 dprintf(CRITICAL,
1383 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1384 mdelay(5000);
1385#endif
1386 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001387 default:
lijuanga40d6302015-07-20 20:10:13 +08001388 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001389 }
1390#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001391#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301392 if(device.is_tampered)
1393 {
1394 write_device_info_mmc(&device);
1395 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301396 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301397 #endif
1398 #ifdef ASSERT_ON_TAMPER
1399 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1400 ASSERT(0);
1401 #endif
1402 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001403#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001404}
1405
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301406static bool check_format_bit()
1407{
1408 bool ret = false;
1409 int index;
1410 uint64_t offset;
1411 struct boot_selection_info *in = NULL;
1412 char *buf = NULL;
1413
1414 index = partition_get_index("bootselect");
1415 if (index == INVALID_PTN)
1416 {
1417 dprintf(INFO, "Unable to locate /bootselect partition\n");
1418 return ret;
1419 }
1420 offset = partition_get_offset(index);
1421 if(!offset)
1422 {
1423 dprintf(INFO, "partition /bootselect doesn't exist\n");
1424 return ret;
1425 }
1426 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301427 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301428 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301429 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301430 {
1431 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1432 free(buf);
1433 return ret;
1434 }
1435 in = (struct boot_selection_info *) buf;
1436 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1437 (in->version == BOOTSELECT_VERSION)) {
1438 if ((in->state_info & BOOTSELECT_FORMAT) &&
1439 !(in->state_info & BOOTSELECT_FACTORY))
1440 ret = true;
1441 } else {
1442 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1443 in->signature, in->version);
1444 ASSERT(0);
1445 }
1446 free(buf);
1447 return ret;
1448}
1449
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001450void boot_verifier_init()
1451{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001452 uint32_t boot_state;
1453 /* Check if device unlock */
1454 if(device.is_unlocked)
1455 {
1456 boot_verify_send_event(DEV_UNLOCK);
1457 boot_verify_print_state();
1458 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1459 return;
1460 }
1461 else
1462 {
1463 boot_verify_send_event(BOOT_INIT);
1464 }
1465
1466 /* Initialize keystore */
1467 boot_state = boot_verify_keystore_init();
1468 if(boot_state == YELLOW)
1469 {
1470 boot_verify_print_state();
1471 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1472 }
1473}
1474
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301475/* Function to return recovery appended dtbo buffer info */
1476void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1477{
1478 *dtbo_size = recovery_dtbo_size;
1479 *dtbo_buf = recovery_dtbo_buf;
1480 return;
1481}
1482
Shashank Mittal23b8f422010-04-16 19:27:21 -07001483int boot_linux_from_mmc(void)
1484{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301485 boot_img_hdr *hdr = (void*) buf;
1486 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001487 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001488 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001489 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001490 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001491
Shashank Mittalcd98d472011-08-02 14:29:24 -07001492 unsigned char *image_addr = 0;
1493 unsigned kernel_actual;
1494 unsigned ramdisk_actual;
1495 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001496 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301497 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001498
Matthew Qin271927e2015-03-31 22:07:07 -04001499 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301500 unsigned dtb_image_size = 0;
1501 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001502 unsigned int out_len = 0;
1503 unsigned int out_avai_len = 0;
1504 unsigned char *out_addr = NULL;
1505 uint32_t dtb_offset = 0;
1506 unsigned char *kernel_start_addr = NULL;
1507 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001508 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301509 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001510 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301511#if VERIFIED_BOOT_2
1512 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301513 void *dtbo_image_buf = NULL;
1514 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301515 void *vbmeta_image_buf = NULL;
1516 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301517#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301518 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001519#if DEVICE_TREE
1520 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001521 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001522 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001523 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001524 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001525 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001526#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001527 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301528 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001529
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301530 if (check_format_bit())
1531 boot_into_recovery = 1;
1532
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001533 if (!boot_into_recovery) {
1534 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1535 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1536 if (rcode <= 0) {
1537 boot_into_ffbm = false;
1538 if (rcode < 0)
1539 dprintf(CRITICAL,"failed to get ffbm cookie");
1540 } else
1541 boot_into_ffbm = true;
1542 } else
1543 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301544 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001545 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1546 dprintf(INFO, "Unified boot method!\n");
1547 hdr = uhdr;
1548 goto unified_boot;
1549 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301550
1551 /* For a/b recovery image code is on boot partition.
1552 If we support multislot, always use boot partition. */
1553 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301554 ((!partition_multislot_is_supported()) ||
1555 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301556 ptn_name = "recovery";
1557 else
1558 ptn_name = "boot";
1559
1560 index = partition_get_index(ptn_name);
1561 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301562 image_size = partition_get_size(index);
1563 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301564 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1565 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001566 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301567
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001568 /* Set Lun for boot & recovery partitions */
1569 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001570
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301571 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001572 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1573 return -1;
1574 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001575
1576 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001577 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301578 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001579 }
1580
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001581 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301582
1583 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1584 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1585 return -1;
1586 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001587 page_size = hdr->page_size;
1588 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001589 }
1590
Matthew Qin49e51fa2015-02-09 10:40:45 +08001591 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1592 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301593 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001594
Matthew Qin49e51fa2015-02-09 10:40:45 +08001595 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301596#if VERIFIED_BOOT_2
1597 /* Create hole in start of image for VB salt to copy */
1598 image_addr += SALT_BUFF_OFFSET;
1599#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301600 memcpy(image_addr, (void *)buf, page_size);
1601
1602 /* ensure commandline is terminated */
1603 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001604
1605#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301606#ifndef OSVERSION_IN_BOOTIMAGE
1607 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301608#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301609 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301610#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301611
Parth Dixit4097b622016-03-15 14:42:27 +05301612 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301613 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + (uint64_t)dt_actual + page_size)) {
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301614 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1615 return -1;
1616 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301617 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001618#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301619 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual + (uint64_t)second_actual + page_size)) {
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301620 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1621 return -1;
1622 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301623 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001624#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301625 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001626
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301627#ifdef OSVERSION_IN_BOOTIMAGE
1628 /* If header version is ONE and booting into recovery,
1629 dtbo is appended with recovery image.
1630 Doing following:
1631 * Validating the recovery offset and size.
1632 * Extracting recovery dtbo to be used as dtbo.
1633 */
1634 if (boot_into_recovery &&
1635 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1636 {
1637 struct boot_img_hdr_v1 *hdr1 =
1638 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301639 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301640
Mayank Grovera1a83c72018-09-24 11:23:15 +05301641 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301642 if ((hdr1->header_size !=
1643 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1644 {
1645 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1646 return -1;
1647 }
1648
Mayank Grovera1a83c72018-09-24 11:23:15 +05301649 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1650 {
1651 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301652 MAX_SUPPORTED_DTBO_IMG_BUF);
1653 return -1;
1654 }
1655
Mayank Grovera1a83c72018-09-24 11:23:15 +05301656 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301657 {
1658 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1659 return -1;
1660 }
1661
Mayank Grovera1a83c72018-09-24 11:23:15 +05301662 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1663 dprintf(CRITICAL,
1664 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
1665 return -1;
1666 }
1667
1668 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301669 {
1670 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
1671 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
1672 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
1673 return -1;
1674 }
1675
1676 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05301677 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301678 imagesize_actual += recovery_dtbo_size;
1679
1680 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1681 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
1682 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
1683
1684 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301685
1686 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
1687 struct boot_img_hdr_v1 *hdr1 =
1688 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
1689 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
1690 (image_addr + sizeof(boot_img_hdr) +
1691 BOOT_IMAGE_HEADER_V2_OFFSET);
1692 unsigned int recovery_dtbo_actual = 0;
1693
lijuang0b17ba22019-09-04 14:20:47 +08001694 recovery_dtbo_actual =
1695 ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
1696 imagesize_actual += recovery_dtbo_actual;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301697
1698 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
1699
1700
1701 dtb_image_offset = page_size + patched_kernel_hdr_size +
1702 kernel_actual + ramdisk_actual + second_actual +
1703 recovery_dtbo_actual;
1704
1705 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1706 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
1707 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301708#endif
1709
Parth Dixit1375d9e2019-07-08 20:56:13 +05301710
Saranya Chidurab1aaf232018-11-19 15:47:50 +05301711 /* Validate the boot/recovery image size is within the bounds of partition size */
1712 if (imagesize_actual > image_size) {
1713 dprintf(CRITICAL, "Image size is greater than partition size.\n");
1714 return -1;
1715 }
1716
Matthew Qin49e51fa2015-02-09 10:40:45 +08001717#if VERIFIED_BOOT
1718 boot_verifier_init();
1719#endif
1720
Mayank Grover1ceaee22019-04-01 17:54:32 +05301721#if VERIFIED_BOOT_2
1722 /* read full partition if device is unlocked */
1723 if (device.is_unlocked)
1724 imagesize_actual = image_size;
1725#endif
1726
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301727 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001728 {
1729 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1730 return -1;
1731 }
1732
Matthew Qinbb7923d2015-02-09 10:56:09 +08001733 /*
1734 * Update loading flow of bootimage to support compressed/uncompressed
1735 * bootimage on both 64bit and 32bit platform.
1736 * 1. Load bootimage from emmc partition onto DDR.
1737 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1738 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1739 * platform accordingly.
1740 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1741 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301742 if (partition_multislot_is_supported())
1743 {
1744 current_active_slot = partition_find_active_slot();
1745 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1746 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1747 }
1748 else
1749 {
1750 dprintf(INFO, "Loading (%s) image (%d): start\n",
1751 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1752 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001753 bs_set_timestamp(BS_KERNEL_LOAD_START);
1754
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301755 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1756 {
1757 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1758 return -1;
1759 }
Kishor PK9e91b592017-05-24 12:14:55 +05301760 offset = page_size;
1761 /* Read image without signature and header*/
1762 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001763 {
1764 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1765 return -1;
1766 }
1767
Mayank Grover351a75e2017-05-30 20:06:08 +05301768 if (partition_multislot_is_supported())
1769 {
1770 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1771 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1772 }
1773 else
1774 {
1775 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001776 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1777
Mayank Grover351a75e2017-05-30 20:06:08 +05301778 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001779 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1780
1781 /* Authenticate Kernel */
1782 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1783 (int) target_use_signed_kernel(),
1784 device.is_unlocked,
1785 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301786#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05301787 /* if device is unlocked skip reading signature, as full partition is read */
1788 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05301789 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05301790 offset = imagesize_actual;
1791 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1792 {
1793 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1794 return -1;
1795 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001796
Mayank Grover1ceaee22019-04-01 17:54:32 +05301797 /* Read signature */
1798 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1799 {
1800 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1801 return -1;
1802 }
Monika Singh292b3e92018-03-17 22:40:23 +05301803 }
1804
Mayank Grover027a9412018-09-04 15:12:05 +05301805 /* load and validate dtbo partition */
1806 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
1807
Mayank Grover9df84c02018-08-30 15:46:35 +05301808 /* load vbmeta partition */
1809 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
1810
Monika Singh292b3e92018-03-17 22:40:23 +05301811 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05301812
1813 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05301814 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05301815 info.images[IMG_BOOT].imgsize = imagesize_actual;
1816 info.images[IMG_BOOT].name = ptn_name;
1817 ++info.num_loaded_images;
1818
1819 /* Pass loaded dtbo image */
1820 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05301821 info.images[IMG_DTBO].image_buffer =
1822 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05301823 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
1824 info.images[IMG_DTBO].name = "dtbo";
1825 ++info.num_loaded_images;
1826 }
1827
Mayank Grover9df84c02018-08-30 15:46:35 +05301828 /* Pass loaded vbmeta image */
1829 if (vbmeta_image_buf != NULL) {
1830 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
1831 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
1832 info.images[IMG_VBMETA].name = "vbmeta";
1833 ++info.num_loaded_images;
1834 }
1835
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301836 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05301837 info.multi_slot_boot = partition_multislot_is_supported();
1838 info.bootreason_alarm = boot_reason_alarm;
1839 info.bootinto_recovery = boot_into_recovery;
1840 status = load_image_and_auth(&info);
1841 if(status)
1842 return -1;
1843
1844 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05301845
1846 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05301847 if (vbmeta_image_buf != NULL) {
1848 free(vbmeta_image_buf);
1849 --info.num_loaded_images;
1850 }
Monika Singh292b3e92018-03-17 22:40:23 +05301851#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001852 /* Change the condition a little bit to include the test framework support.
1853 * We would never reach this point if device is in fastboot mode, even if we did
1854 * that means we are in test mode, so execute kernel authentication part for the
1855 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301856 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001857 {
1858 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301859 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001860 {
1861 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1862 return -1;
1863 }
1864
1865 /* Read signature */
1866 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1867 {
1868 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1869 return -1;
1870 }
1871
1872 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001873 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301874 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001875 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001876 } else {
1877 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1878 #ifdef TZ_SAVE_KERNEL_HASH
1879 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1880 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001881
1882#ifdef MDTP_SUPPORT
1883 {
1884 /* Verify MDTP lock.
1885 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1886 * since verification was skipped in aboot. The signature is not part of the loaded image.
1887 */
1888 mdtp_ext_partition_verification_t ext_partition;
1889 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1890 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1891 ext_partition.page_size = page_size;
1892 ext_partition.image_addr = (uint32)image_addr;
1893 ext_partition.image_size = imagesize_actual;
1894 ext_partition.sig_avail = FALSE;
1895 mdtp_fwlock_verify_lock(&ext_partition);
1896 }
1897#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001898 }
Monika Singh292b3e92018-03-17 22:40:23 +05301899#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001900
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001901#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001902 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001903 {
1904#if FBCON_DISPLAY_MSG
1905 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1906 wait_for_users_action();
1907#else
1908 dprintf(CRITICAL,
1909 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1910 mdelay(5000);
1911#endif
1912 }
1913#endif
1914
1915#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301916 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301917 {
1918 /* set boot and system versions. */
1919 set_os_version((unsigned char *)image_addr);
1920 // send root of trust
1921 if(!send_rot_command((uint32_t)device.is_unlocked))
1922 ASSERT(0);
1923 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301924#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001925 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001926 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1927 * If not, continue booting.
1928 */
1929 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1930 {
1931 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1932 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05301933#if VERIFIED_BOOT_2
1934 if (dtbo_image_sz)
1935 out_avai_len -= DTBO_IMG_BUF;
1936#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04001937 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001938 rc = decompress((unsigned char *)(image_addr + page_size),
1939 hdr->kernel_size, out_addr, out_avai_len,
1940 &dtb_offset, &out_len);
1941 if (rc)
1942 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001943 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001944 ASSERT(0);
1945 }
1946
Matthew Qin0b15b322015-05-19 05:20:54 -04001947 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001948 kptr = (struct kernel64_hdr *)out_addr;
1949 kernel_start_addr = out_addr;
1950 kernel_size = out_len;
1951 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001952 dprintf(INFO, "Uncpmpressed kernel in use\n");
1953 if (!strncmp((char*)(image_addr + page_size),
1954 PATCHED_KERNEL_MAGIC,
1955 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1956 dprintf(INFO, "Patched kernel detected\n");
1957 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1958 PATCHED_KERNEL_HEADER_SIZE);
1959 //The size of the kernel is stored at start of kernel image + 16
1960 //The dtb would start just after the kernel
1961 dtb_offset = *((uint32_t*)((unsigned char*)
1962 (image_addr + page_size +
1963 sizeof(PATCHED_KERNEL_MAGIC) -
1964 1)));
1965 //The actual kernel starts after the 20 byte header.
1966 kernel_start_addr = (unsigned char*)(image_addr +
1967 page_size + PATCHED_KERNEL_HEADER_SIZE);
1968 kernel_size = hdr->kernel_size;
1969 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1970 } else {
1971 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1972 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1973 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1974 kernel_size = hdr->kernel_size;
1975 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001976 }
1977
1978 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001979 * Update the kernel/ramdisk/tags address if the boot image header
1980 * has default values, these default values come from mkbootimg when
1981 * the boot image is flashed using fastboot flash:raw
1982 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001983 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001984
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001985 /* Get virtual addresses since the hdr saves physical addresses. */
1986 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1987 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1988 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001989
Matthew Qinbb7923d2015-02-09 10:56:09 +08001990 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001991 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001992 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301993 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1994 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1995 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001996 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301997 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001998 return -1;
1999 }
2000
2001#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302002 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2003 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002004 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302005 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002006 return -1;
2007 }
2008#endif
2009
Matthew Qin49e51fa2015-02-09 10:40:45 +08002010 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002011 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002012 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002013
Matthew Qin49e51fa2015-02-09 10:40:45 +08002014 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302015 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002016 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2017 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002018
Matthew Qin49e51fa2015-02-09 10:40:45 +08002019 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2020 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2021 return -1;
2022 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002023
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302024 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2025 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302026 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302027 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2028 return -1;
2029 }
2030
Matthew Qin49e51fa2015-02-09 10:40:45 +08002031 /* Find index of device tree within device tree table */
2032 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2033 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2034 return -1;
2035 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002036
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302037 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2038 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2039 return -1;
2040 }
2041
2042 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302043 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302044 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2045 return -1;
2046 }
2047
Matthew Qin271927e2015-03-31 22:07:07 -04002048 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2049 {
2050 unsigned int compressed_size = 0;
2051 out_addr += out_len;
2052 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002053 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002054 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2055 dt_entry.size, out_addr, out_avai_len,
2056 &compressed_size, &dtb_size);
2057 if (rc)
2058 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002059 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002060 ASSERT(0);
2061 }
2062
Matthew Qin0b15b322015-05-19 05:20:54 -04002063 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002064 best_match_dt_addr = out_addr;
2065 } else {
2066 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2067 dtb_size = dt_entry.size;
2068 }
2069
Matthew Qin49e51fa2015-02-09 10:40:45 +08002070 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302071 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2072 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002073 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302074 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002075 return -1;
2076 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002077
Matthew Qin271927e2015-03-31 22:07:07 -04002078 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002079 } else {
2080 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302081 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2082 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002083 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302084 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002085 return -1;
2086 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002087 /*
2088 * If appended dev tree is found, update the atags with
2089 * memory address to the DTB appended location on RAM.
2090 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302091 *
2092 * Make sure everything from scratch address is read before next step!
2093 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002094 */
2095 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302096 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2097
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302098#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302099 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2100
2101 image_buf = (void*)(image_addr);
2102 dtb_offset = dtb_image_offset;
2103 dtb_image_size = imagesize_actual;
2104 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302105#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302106
2107 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002108 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002109 if (!dtb) {
2110 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002111 return -1;
2112 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002113 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002114 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002115
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002116 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2117 target_load_ssd_keystore();
2118
Shashank Mittal23b8f422010-04-16 19:27:21 -07002119unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002120
Dima Zavin77e41f32013-03-06 16:10:43 -08002121 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002122 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002123 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2124
2125 return 0;
2126}
2127
Dima Zavin214cc642009-01-26 11:16:21 -08002128int boot_linux_from_flash(void)
2129{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302130 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002131 struct ptentry *ptn;
2132 struct ptable *ptable;
2133 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002134
Shashank Mittalcd98d472011-08-02 14:29:24 -07002135 unsigned char *image_addr = 0;
2136 unsigned kernel_actual;
2137 unsigned ramdisk_actual;
2138 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302139 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002140
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002141#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302142 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002143 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302144 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002145 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302146 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302147 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302148 unsigned int dtb_size = 0;
2149 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002150#endif
2151
David Ng183a7422009-12-07 14:55:21 -08002152 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302153 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002154 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2155 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2156 return -1;
2157 }
2158 goto continue_boot;
2159 }
2160
Dima Zavin214cc642009-01-26 11:16:21 -08002161 ptable = flash_get_ptable();
2162 if (ptable == NULL) {
2163 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2164 return -1;
2165 }
2166
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002167 if(!boot_into_recovery)
2168 {
2169 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002170
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002171 if (ptn == NULL) {
2172 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2173 return -1;
2174 }
2175 }
2176 else
2177 {
2178 ptn = ptable_find(ptable, "recovery");
2179 if (ptn == NULL) {
2180 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2181 return -1;
2182 }
Dima Zavin214cc642009-01-26 11:16:21 -08002183 }
2184
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302185 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002186 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002187 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2188 return -1;
2189 }
Dima Zavin214cc642009-01-26 11:16:21 -08002190
2191 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002192 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002193 return -1;
2194 }
2195
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002196 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002197 dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002198 return -1;
2199 }
2200
Kishor PK9e91b592017-05-24 12:14:55 +05302201 image_addr = (unsigned char *)target_get_scratch_address();
2202 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302203
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002204 /*
2205 * Update the kernel/ramdisk/tags address if the boot image header
2206 * has default values, these default values come from mkbootimg when
2207 * the boot image is flashed using fastboot flash:raw
2208 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002209 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002210
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002211 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002212 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2213 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2214 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2215
2216 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2217 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302218 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002219
Kishor PK9e91b592017-05-24 12:14:55 +05302220 /* ensure commandline is terminated */
2221 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2222
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002223 /* Check if the addresses in the header are valid. */
2224 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302225 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2226 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2227 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002228 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302229 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002230 return -1;
2231 }
2232
2233#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302234 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302235 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2236 return -1;
2237 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302238 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302239
Mayank Grovere559cec2017-10-17 15:12:03 +05302240 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2241 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302242 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302243 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302244 return -1;
2245 }
2246#else
2247
2248#ifndef OSVERSION_IN_BOOTIMAGE
2249 dt_size = hdr->dt_size;
2250#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302251 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302252 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + (uint64_t)dt_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302253 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2254 return -1;
2255 }
2256
Kishor PKd8ddcad2017-07-27 13:53:57 +05302257 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302258
Mayank Grovere559cec2017-10-17 15:12:03 +05302259 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2260 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302261 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302262 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302263 return -1;
2264 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002265#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002266
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302267 /* Read full boot.img from flash */
2268 dprintf(INFO, "Loading (%s) image (%d): start\n",
2269 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2270 bs_set_timestamp(BS_KERNEL_LOAD_START);
2271
2272 if (UINT_MAX - page_size < imagesize_actual)
2273 {
2274 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2275 return -1;
2276 }
2277
2278 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2279 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2280 {
2281 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2282 return -1;
2283 }
2284
2285 offset = page_size;
2286 /* Read image without signature and header */
2287 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2288 {
2289 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2290 return -1;
2291 }
2292
2293 dprintf(INFO, "Loading (%s) image (%d): done\n",
2294 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2295 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2296
Shashank Mittalcd98d472011-08-02 14:29:24 -07002297 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002298 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002299 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002300 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302301
Shashank Mittalcd98d472011-08-02 14:29:24 -07002302 /* Read signature */
2303 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2304 {
2305 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002306 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002307 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002308
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302309 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302310 }
2311 offset = page_size;
2312 if(hdr->second_size != 0) {
2313 if (UINT_MAX - offset < second_actual)
2314 {
2315 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2316 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302317 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302318 offset += second_actual;
2319 /* Second image loading not implemented. */
2320 ASSERT(0);
2321 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302322 /* Move kernel and ramdisk to correct address */
2323 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2324 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2325
2326#if DEVICE_TREE
2327 if(dt_size != 0) {
2328
2329 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2330
2331 table = (struct dt_table*) dt_table_offset;
2332
2333 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2334 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2335 return -1;
2336 }
2337
2338 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2339 goes beyound hdr->dt_size*/
2340 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2341 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2342 return -1;
2343 }
2344
2345 /* Find index of device tree within device tree table */
2346 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2347 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2348 return -1;
2349 }
2350
2351 /* Validate and Read device device tree in the "tags_add */
2352 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2353 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2354 {
2355 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2356 return -1;
2357 }
2358
2359 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2360 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2361 return -1;
2362 }
2363
2364 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2365 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2366 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2367 return -1;
2368 }
2369
2370 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2371 dtb_size = dt_entry.size;
2372 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302373
2374 } else {
2375 /* Validate the tags_addr */
2376 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2377 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2378 {
2379 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2380 return -1;
2381 }
2382 /*
2383 * If appended dev tree is found, update the atags with
2384 * memory address to the DTB appended location on RAM.
2385 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302386 *
2387 * Make sure everything from scratch address is read before next step!
2388 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302389 */
2390 void *dtb = NULL;
2391 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2392 if (!dtb) {
2393 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2394 return -1;
2395 }
2396 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302397#endif
2398 if(target_use_signed_kernel() && (!device.is_unlocked))
2399 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002400 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002401 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002402 {
2403 write_device_info_flash(&device);
2404 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302405#if USE_PCOM_SECBOOT
2406 set_tamper_flag(device.is_tampered);
2407#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002408 }
David Ng183a7422009-12-07 14:55:21 -08002409continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002410
Dima Zavin214cc642009-01-26 11:16:21 -08002411 /* TODO: create/pass atags to kernel */
2412
Ajay Dudanie28a6072011-07-01 13:59:46 -07002413 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002414 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002415 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2416
2417 return 0;
2418}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002419
Shashank Mittal162244e2011-08-08 19:01:25 -07002420void write_device_info_mmc(device_info *dev)
2421{
Shashank Mittal162244e2011-08-08 19:01:25 -07002422 unsigned long long ptn = 0;
2423 unsigned long long size;
2424 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002425 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002426 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302427 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002428
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002429 if (devinfo_present)
2430 index = partition_get_index("devinfo");
2431 else
2432 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002433
Shashank Mittal162244e2011-08-08 19:01:25 -07002434 ptn = partition_get_offset(index);
2435 if(ptn == 0)
2436 {
2437 return;
2438 }
2439
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002440 lun = partition_get_lun(index);
2441 mmc_set_lun(lun);
2442
Shashank Mittal162244e2011-08-08 19:01:25 -07002443 size = partition_get_size(index);
2444
Mayank Groverddd348d2018-01-23 14:07:09 +05302445 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2446 mmc_blocksize_mask);
2447 if (device_info_sz == UINT_MAX)
2448 {
2449 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2450 return;
2451 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002452
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002453 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302454 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002455 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302456 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002457 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002458 {
2459 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002460 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002461 }
2462}
2463
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002464void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002465{
Shashank Mittal162244e2011-08-08 19:01:25 -07002466 unsigned long long ptn = 0;
2467 unsigned long long size;
2468 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002469 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302470 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002471
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002472 if ((index = partition_get_index("devinfo")) < 0)
2473 {
2474 devinfo_present = false;
2475 index = partition_get_index("aboot");
2476 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002477
Shashank Mittal162244e2011-08-08 19:01:25 -07002478 ptn = partition_get_offset(index);
2479 if(ptn == 0)
2480 {
2481 return;
2482 }
2483
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002484 mmc_set_lun(partition_get_lun(index));
2485
Shashank Mittal162244e2011-08-08 19:01:25 -07002486 size = partition_get_size(index);
2487
Mayank Groverddd348d2018-01-23 14:07:09 +05302488 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2489 mmc_blocksize_mask);
2490 if (device_info_sz == UINT_MAX)
2491 {
2492 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2493 return;
2494 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002495
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002496 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302497 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002498 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302499 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002500 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002501 {
2502 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002503 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002504 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002505}
2506
2507void write_device_info_flash(device_info *dev)
2508{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002509 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002510 struct ptentry *ptn;
2511 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002512 if(info == NULL)
2513 {
2514 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2515 ASSERT(0);
2516 }
2517 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002518 ptable = flash_get_ptable();
2519 if (ptable == NULL)
2520 {
2521 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2522 return;
2523 }
2524
2525 ptn = ptable_find(ptable, "devinfo");
2526 if (ptn == NULL)
2527 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002528 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002529 return;
2530 }
2531
Mayank Groverdedbc892017-10-24 13:41:34 +05302532 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002533 memcpy(info, dev, sizeof(device_info));
2534
2535 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2536 {
2537 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2538 return;
2539 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002540 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002541}
2542
vijay kumarc65876c2015-04-24 13:29:16 +05302543static int read_allow_oem_unlock(device_info *dev)
2544{
vijay kumarc65876c2015-04-24 13:29:16 +05302545 unsigned offset;
2546 int index;
2547 unsigned long long ptn;
2548 unsigned long long ptn_size;
2549 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002550 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302551
vijay kumarca2e6812015-07-08 20:28:25 +05302552 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302553 if (index == INVALID_PTN)
2554 {
vijay kumarca2e6812015-07-08 20:28:25 +05302555 index = partition_get_index(frp_ptns[1]);
2556 if (index == INVALID_PTN)
2557 {
2558 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2559 return -1;
2560 }
vijay kumarc65876c2015-04-24 13:29:16 +05302561 }
2562
2563 ptn = partition_get_offset(index);
2564 ptn_size = partition_get_size(index);
2565 offset = ptn_size - blocksize;
2566
Mayank Grover48860402016-11-29 12:34:53 +05302567 /* Set Lun for partition */
2568 mmc_set_lun(partition_get_lun(index));
2569
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002570 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302571 {
2572 dprintf(CRITICAL, "Reading MMC failed\n");
2573 return -1;
2574 }
2575
2576 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2577 is_allow_unlock = buf[blocksize-1] & 0x01;
2578 return 0;
2579}
2580
2581static int write_allow_oem_unlock(bool allow_unlock)
2582{
vijay kumarc65876c2015-04-24 13:29:16 +05302583 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302584 int index;
2585 unsigned long long ptn;
2586 unsigned long long ptn_size;
2587 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002588 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302589
vijay kumarca2e6812015-07-08 20:28:25 +05302590 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302591 if (index == INVALID_PTN)
2592 {
vijay kumarca2e6812015-07-08 20:28:25 +05302593 index = partition_get_index(frp_ptns[1]);
2594 if (index == INVALID_PTN)
2595 {
2596 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2597 return -1;
2598 }
vijay kumarc65876c2015-04-24 13:29:16 +05302599 }
2600
2601 ptn = partition_get_offset(index);
2602 ptn_size = partition_get_size(index);
2603 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302604 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302605
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002606 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302607 {
2608 dprintf(CRITICAL, "Reading MMC failed\n");
2609 return -1;
2610 }
2611
2612 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2613 buf[blocksize-1] = allow_unlock;
2614 if (mmc_write(ptn + offset, blocksize, buf))
2615 {
2616 dprintf(CRITICAL, "Writing MMC failed\n");
2617 return -1;
2618 }
2619
2620 return 0;
2621}
2622
Shashank Mittal162244e2011-08-08 19:01:25 -07002623void read_device_info_flash(device_info *dev)
2624{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002625 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002626 struct ptentry *ptn;
2627 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002628 if(info == NULL)
2629 {
2630 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2631 ASSERT(0);
2632 }
2633 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002634 ptable = flash_get_ptable();
2635 if (ptable == NULL)
2636 {
2637 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2638 return;
2639 }
2640
2641 ptn = ptable_find(ptable, "devinfo");
2642 if (ptn == NULL)
2643 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002644 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002645 return;
2646 }
2647
2648 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2649 {
2650 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2651 return;
2652 }
2653
2654 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2655 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002656 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2657 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002658 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002659 write_device_info_flash(info);
2660 }
2661 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002662 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002663}
2664
2665void write_device_info(device_info *dev)
2666{
2667 if(target_is_emmc_boot())
2668 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002669 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2670 if(info == NULL)
2671 {
2672 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2673 ASSERT(0);
2674 }
2675 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002676 memcpy(info, dev, sizeof(struct device_info));
2677
2678#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302679 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302680 is_secure_boot_enable()) {
2681 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2682 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002683 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002684 else
2685 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002686#else
2687 write_device_info_mmc(info);
2688#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002689 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002690 }
2691 else
2692 {
2693 write_device_info_flash(dev);
2694 }
2695}
2696
Monika Singh94316462018-03-15 18:39:01 +05302697int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2698{
2699 if (!devinfo_present) {
2700 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2701 return -EINVAL;
2702 }
2703 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2704 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2705 __func__, loc, ARRAY_SIZE(device.rollback_index));
2706 ASSERT(0);
2707 }
2708
2709 *roll_back_index = device.rollback_index[loc];
2710 return 0;
2711}
2712
2713int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2714{
2715 if (!devinfo_present) {
2716 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2717 return -EINVAL;
2718 }
2719 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2720 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2721 __func__, loc, ARRAY_SIZE(device.rollback_index));
2722 ASSERT(0);
2723 }
2724
2725 device.rollback_index[loc] = roll_back_index;
2726 write_device_info(&device);
2727 return 0;
2728}
2729
Monika Singhb0fad822018-03-15 18:50:55 +05302730int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2731{
2732 if (!devinfo_present) {
2733 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2734 return -EINVAL;
2735 }
2736
2737 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2738 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2739 return -ENODEV;
2740 }
2741
2742 memcpy(device.user_public_key, user_key, user_key_size);
2743 device.user_public_key_length = user_key_size;
2744 write_device_info(&device);
2745 return 0;
2746}
2747
2748int erase_userkey()
2749{
2750 if (!devinfo_present) {
2751 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2752 return -EINVAL;
2753 }
2754 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2755 device.user_public_key_length = 0;
2756 write_device_info(&device);
2757 return 0;
2758}
2759
2760int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2761{
2762 if (!devinfo_present) {
2763 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2764 return -EINVAL;
2765 }
2766 *user_key = device.user_public_key;
2767 *user_key_size = device.user_public_key_length;
2768 return 0;
2769}
2770
Shashank Mittal162244e2011-08-08 19:01:25 -07002771void read_device_info(device_info *dev)
2772{
2773 if(target_is_emmc_boot())
2774 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002775 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2776 if(info == NULL)
2777 {
2778 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2779 ASSERT(0);
2780 }
2781 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002782
2783#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302784 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302785 is_secure_boot_enable()) {
2786 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2787 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002788 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002789 else
2790 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002791#else
2792 read_device_info_mmc(info);
2793#endif
2794
2795 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2796 {
2797 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002798 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002799 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302800#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302801 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302802 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302803#endif
lijuang511a2b52015-08-14 20:50:51 +08002804 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002805 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302806#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302807 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302808 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302809#endif
lijuang511a2b52015-08-14 20:50:51 +08002810 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002811 info->is_tampered = 0;
2812 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302813#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302814 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302815 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302816#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002817 write_device_info(info);
2818 }
2819 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002820 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002821 }
2822 else
2823 {
2824 read_device_info_flash(dev);
2825 }
2826}
2827
2828void reset_device_info()
2829{
2830 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002831 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002832 write_device_info(&device);
2833}
2834
2835void set_device_root()
2836{
2837 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002838 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002839 write_device_info(&device);
2840}
2841
lijuang4ece1e72015-08-14 21:02:36 +08002842/* set device unlock value
2843 * Must check FRP before call this function
2844 * Need to wipe data when unlock status changed
2845 * type 0: oem unlock
2846 * type 1: unlock critical
2847 * status 0: unlock as false
2848 * status 1: lock as true
2849 */
2850void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002851{
lijuang4ece1e72015-08-14 21:02:36 +08002852 if (type == UNLOCK)
2853 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302854#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302855 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302856 type == UNLOCK_CRITICAL)
2857 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302858#endif
lijuang4ece1e72015-08-14 21:02:36 +08002859 write_device_info(&device);
2860}
2861
2862static void set_device_unlock(int type, bool status)
2863{
2864 int is_unlocked = -1;
2865 char response[MAX_RSP_SIZE];
2866
2867 /* check device unlock status if it is as expected */
2868 if (type == UNLOCK)
2869 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302870#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302871 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302872 type == UNLOCK_CRITICAL)
2873 {
2874 is_unlocked = device.is_unlock_critical;
2875 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302876#endif
lijuang4ece1e72015-08-14 21:02:36 +08002877 if (is_unlocked == status) {
2878 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2879 fastboot_info(response);
2880 fastboot_okay("");
2881 return;
2882 }
2883
2884 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002885 if (status && !is_allow_unlock) {
2886 fastboot_fail("oem unlock is not allowed");
2887 return;
2888 }
lijuang21f12f52015-08-22 16:22:19 +08002889
lijuang4ece1e72015-08-14 21:02:36 +08002890#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002891 display_unlock_menu(type, status);
2892 fastboot_okay("");
2893 return;
lijuang4ece1e72015-08-14 21:02:36 +08002894#else
lijuang07c5d6e2018-04-23 18:32:13 +08002895 if (status && type == UNLOCK) {
2896 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2897 return;
lijuang21f12f52015-08-22 16:22:19 +08002898 }
lijuang07c5d6e2018-04-23 18:32:13 +08002899#endif
lijuang4ece1e72015-08-14 21:02:36 +08002900
2901 set_device_unlock_value(type, status);
2902
2903 /* wipe data */
2904 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302905 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002906 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2907 write_misc(0, &msg, sizeof(msg));
2908
2909 fastboot_okay("");
2910 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002911}
2912
lijuang511a2b52015-08-14 20:50:51 +08002913static bool critical_flash_allowed(const char * entry)
2914{
2915 uint32_t i = 0;
2916 if (entry == NULL)
2917 return false;
2918
2919 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2920 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2921 return true;
2922 }
2923 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002924}
2925
2926#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002927int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2928{
2929 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002930 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302931 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002932 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302933 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002934 unsigned int compressed_size = 0;
2935 unsigned int dtb_size = 0;
2936 unsigned int out_avai_len = 0;
2937 unsigned char *out_addr = NULL;
2938 unsigned char *best_match_dt_addr = NULL;
2939 int rc;
2940
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302941 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07002942
Parth Dixit4097b622016-03-15 14:42:27 +05302943#ifndef OSVERSION_IN_BOOTIMAGE
2944 dt_size = hdr->dt_size;
2945#endif
2946
2947 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002948 /* add kernel offset */
2949 dt_image_offset += page_size;
2950 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2951 dt_image_offset += n;
2952
2953 /* add ramdisk offset */
2954 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2955 dt_image_offset += n;
2956
2957 /* add second offset */
2958 if(hdr->second_size != 0) {
2959 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2960 dt_image_offset += n;
2961 }
2962
2963 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002964 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002965
Deepa Dinamani19648b42013-09-05 17:05:55 -07002966 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002967 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2968 return -1;
2969 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302970
2971 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2972 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302973 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302974 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2975 return -1;
2976 }
2977
Joel Kingaa335dc2013-06-03 16:11:08 -07002978 /* Find index of device tree within device tree table */
2979 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002980 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2981 return -1;
2982 }
2983
Matthew Qin271927e2015-03-31 22:07:07 -04002984 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2985 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2986 {
2987 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2988 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002989 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002990 rc = decompress(best_match_dt_addr,
2991 dt_entry.size, out_addr, out_avai_len,
2992 &compressed_size, &dtb_size);
2993 if (rc)
2994 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002995 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002996 ASSERT(0);
2997 }
2998
Matthew Qin0b15b322015-05-19 05:20:54 -04002999 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003000 best_match_dt_addr = out_addr;
3001 } else {
3002 dtb_size = dt_entry.size;
3003 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003004 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303005 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3006 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003007 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303008 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003009 return -1;
3010 }
3011
Amol Jadicb524072012-08-09 16:40:18 -07003012 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003013 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003014 } else
3015 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003016
3017 /* Everything looks fine. Return success. */
3018 return 0;
3019}
3020#endif
3021
Brian Swetland9c4c0752009-01-25 16:23:50 -08003022void cmd_boot(const char *arg, void *data, unsigned sz)
3023{
3024 unsigned kernel_actual;
3025 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303026 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003027 uint32_t image_actual;
3028 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303029 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003030 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003031 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003032 int ret = 0;
3033 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003034 unsigned int out_len = 0;
3035 unsigned int out_avai_len = 0;
3036 unsigned char *out_addr = NULL;
3037 uint32_t dtb_offset = 0;
3038 unsigned char *kernel_start_addr = NULL;
3039 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003040 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303041#if VERIFIED_BOOT_2
3042 void *dtbo_image_buf = NULL;
3043 uint32_t dtbo_image_sz = 0;
3044 void *vbmeta_image_buf = NULL;
3045 uint32_t vbmeta_image_sz = 0;
3046#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303047#if !VERIFIED_BOOT_2
3048 uint32_t sig_actual = 0;
3049 uint32_t sig_size = 0;
3050#ifdef MDTP_SUPPORT
3051 static bool is_mdtp_activated = 0;
3052#endif /* MDTP_SUPPORT */
3053#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003054
lijuang2008ff22016-03-07 17:56:27 +08003055#if FBCON_DISPLAY_MSG
3056 /* Exit keys' detection thread firstly */
3057 exit_menu_keys_detection();
3058#endif
3059
Monika Singh292b3e92018-03-17 22:40:23 +05303060#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003061 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003062 {
3063 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003064 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003065 }
3066#endif
3067
Brian Swetland9c4c0752009-01-25 16:23:50 -08003068 if (sz < sizeof(hdr)) {
3069 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003070 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003071 }
3072
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303073 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003074
3075 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003076 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003077
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003078 if(target_is_emmc_boot() && hdr->page_size) {
3079 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003080 page_mask = page_size - 1;
3081 }
3082
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003083 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3084 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303085 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003086#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303087#ifndef OSVERSION_IN_BOOTIMAGE
3088 dt_size = hdr->dt_size;
3089#endif
3090 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003091#endif
3092
3093 image_actual = ADD_OF(page_size, kernel_actual);
3094 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303095 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003096 image_actual = ADD_OF(image_actual, dt_actual);
3097
Kishor PK5134b332017-05-09 17:50:08 +05303098 /* Checking to prevent oob access in read_der_message_length */
3099 if (image_actual > sz) {
3100 fastboot_fail("bootimage header fields are invalid");
3101 goto boot_failed;
3102 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303103
Monika Singh292b3e92018-03-17 22:40:23 +05303104#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303105 /* Pass size of boot partition, as imgsize, to avoid
3106 read fewer bytes error */
3107 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303108
3109 /* load and validate dtbo partition */
3110 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3111
3112 /* load vbmeta partition */
3113 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3114
Monika Singh292b3e92018-03-17 22:40:23 +05303115 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303116
3117 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3118 info.images[IMG_BOOT].imgsize = image_actual;
3119 info.images[IMG_BOOT].name = "boot";
3120 ++info.num_loaded_images;
3121
3122 /* Pass loaded dtbo image */
3123 if (dtbo_image_buf != NULL) {
3124 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3125 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3126 info.images[IMG_DTBO].name = "dtbo";
3127 ++info.num_loaded_images;
3128 }
3129
3130 /* Pass loaded vbmeta image */
3131 if (vbmeta_image_buf != NULL) {
3132 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3133 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3134 info.images[IMG_VBMETA].name = "vbmeta";
3135 ++info.num_loaded_images;
3136 }
3137
Monika Singh292b3e92018-03-17 22:40:23 +05303138 info.multi_slot_boot = partition_multislot_is_supported();
3139 if (load_image_and_auth(&info))
3140 goto boot_failed;
3141 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303142
3143 /* Free the buffer allocated to vbmeta post verification */
3144 if (vbmeta_image_buf != NULL) {
3145 free(vbmeta_image_buf);
3146 --info.num_loaded_images;
3147 }
Monika Singh292b3e92018-03-17 22:40:23 +05303148#else
Kishor PK5134b332017-05-09 17:50:08 +05303149 sig_size = sz - image_actual;
3150
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303151 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303152 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303153 /* Calculate the signature length from boot image */
3154 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303155 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303156 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003157
Monika Singh292b3e92018-03-17 22:40:23 +05303158 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303159 fastboot_fail("bootimage header fields are invalid");
3160 goto boot_failed;
3161 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003162 }
3163
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003164 // Initialize boot state before trying to verify boot.img
3165#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003166 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303167#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003168 /* Handle overflow if the input image size is greater than
3169 * boot image buffer can hold
3170 */
Monika Singh292b3e92018-03-17 22:40:23 +05303171 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003172 {
3173 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003174 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003175 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003176
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003177 /* Verify the boot image
3178 * device & page_size are initialized in aboot_init
3179 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003180 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003181 /* Pass size excluding signature size, otherwise we would try to
3182 * access signature beyond its length
3183 */
Monika Singh292b3e92018-03-17 22:40:23 +05303184 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003185 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003186#ifdef MDTP_SUPPORT
3187 else
3188 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003189 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003190 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003191
3192 if (!is_mdtp_activated) {
3193 ext_partition.partition = MDTP_PARTITION_NONE;
3194 mdtp_fwlock_verify_lock(&ext_partition);
3195 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003196 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003197
Amir Kotzer7c768c02016-04-13 09:08:05 +03003198 /* If mdtp state cannot be validate, block fastboot boot*/
3199 if(mdtp_activated(&is_mdtp_activated)){
3200 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3201 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3202 goto boot_failed;
3203 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003204 if(is_mdtp_activated){
3205 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003206 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003207 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003208#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303209#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003210
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003211#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303212 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303213 {
3214 /* set boot and system versions. */
3215 set_os_version((unsigned char *)data);
3216 // send root of trust
3217 if(!send_rot_command((uint32_t)device.is_unlocked))
3218 ASSERT(0);
3219 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303220#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003221 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003222 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3223 * If not, continue booting.
3224 */
3225 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3226 {
3227 out_addr = (unsigned char *)target_get_scratch_address();
3228 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3229 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303230#if VERIFIED_BOOT_2
3231 if (dtbo_image_sz)
3232 out_avai_len -= DTBO_IMG_BUF;
3233#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003234 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003235 ret = decompress((unsigned char *)(ptr + page_size),
3236 hdr->kernel_size, out_addr, out_avai_len,
3237 &dtb_offset, &out_len);
3238 if (ret)
3239 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003240 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003241 ASSERT(0);
3242 }
3243
Matthew Qin0b15b322015-05-19 05:20:54 -04003244 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003245 kptr = (struct kernel64_hdr *)out_addr;
3246 kernel_start_addr = out_addr;
3247 kernel_size = out_len;
3248 } else {
3249 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3250 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3251 kernel_size = hdr->kernel_size;
3252 }
3253
3254 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003255 * Update the kernel/ramdisk/tags address if the boot image header
3256 * has default values, these default values come from mkbootimg when
3257 * the boot image is flashed using fastboot flash:raw
3258 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003259 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003260
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003261 /* Get virtual addresses since the hdr saves physical addresses. */
3262 hdr->kernel_addr = VA(hdr->kernel_addr);
3263 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3264 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003265
Matthew Qinbb7923d2015-02-09 10:56:09 +08003266 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003267 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003268 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303269 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3270 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3271 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003272 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303273 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003274 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003275 }
3276
Amol Jadicb524072012-08-09 16:40:18 -07003277#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003278 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003279 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003280 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003281
3282 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003283#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303284 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3285 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003286 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303287 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003288 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003289 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003290#endif
3291
3292 /* Load ramdisk & kernel */
3293 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003294 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003295
3296#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303297 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3298 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003299 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303300 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003301 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003302 }
3303
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003304 /*
3305 * If dtb is not found look for appended DTB in the kernel.
3306 * If appended dev tree is found, update the atags with
3307 * memory address to the DTB appended location on RAM.
3308 * Else update with the atags address in the kernel header
3309 */
3310 if (!dtb_copied) {
3311 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003312 dtb = dev_tree_appended((void*)(ptr + page_size),
3313 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003314 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003315 if (!dtb) {
3316 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003317 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003318 }
Amol Jadicb524072012-08-09 16:40:18 -07003319 }
3320#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003321
3322 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003323 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003324
Dima Zavin77e41f32013-03-06 16:10:43 -08003325 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003326 (const char*) hdr->cmdline, board_machtype(),
3327 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003328
3329 /* fastboot already stop, it's no need to show fastboot menu */
3330 return;
3331boot_failed:
3332#if FBCON_DISPLAY_MSG
3333 /* revert to fastboot menu if boot failed */
3334 display_fastboot_menu();
3335#endif
3336 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003337}
3338
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003339void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003340{
3341 struct ptentry *ptn;
3342 struct ptable *ptable;
3343
3344 ptable = flash_get_ptable();
3345 if (ptable == NULL) {
3346 fastboot_fail("partition table doesn't exist");
3347 return;
3348 }
3349
3350 ptn = ptable_find(ptable, arg);
3351 if (ptn == NULL) {
3352 fastboot_fail("unknown partition name");
3353 return;
3354 }
3355
Monika Singh292b3e92018-03-17 22:40:23 +05303356 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3357 dprintf(INFO, "erasing avb_custom_key\n");
3358 if (erase_userkey()) {
3359 fastboot_fail("Erasing avb_custom_key failed");
3360 } else {
3361 fastboot_okay("");
3362 }
3363 return;
3364 }
3365
Dima Zavin214cc642009-01-26 11:16:21 -08003366 if (flash_erase(ptn)) {
3367 fastboot_fail("failed to erase partition");
3368 return;
3369 }
3370 fastboot_okay("");
3371}
3372
Bikas Gurungd48bd242010-09-04 19:54:32 -07003373
3374void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3375{
3376 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003377 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003378 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003379 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303380 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003381
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003382#if VERIFIED_BOOT
3383 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3384 {
3385 if(!device.is_unlocked)
3386 {
3387 fastboot_fail("unlock device to erase keystore");
3388 return;
3389 }
3390 }
3391#endif
3392
Kinson Chikf1a43512011-07-14 11:28:39 -07003393 index = partition_get_index(arg);
3394 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003395 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003396
Monika Singhc4778b72018-05-16 11:16:42 +05303397 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3398 dprintf(INFO, "erasing avb_custom_key\n");
3399 if (erase_userkey()) {
3400 fastboot_fail("Erasing avb_custom_key failed");
3401 } else {
3402 fastboot_okay("");
3403 }
3404 return;
3405 }
3406
Kinson Chikf1a43512011-07-14 11:28:39 -07003407 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003408 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003409 return;
3410 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003411
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003412 lun = partition_get_lun(index);
3413 mmc_set_lun(lun);
3414
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003415 if (platform_boot_dev_isemmc())
3416 {
3417 if (mmc_erase_card(ptn, size)) {
3418 fastboot_fail("failed to erase partition\n");
3419 return;
3420 }
3421 } else {
3422 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3423 size = partition_get_size(index);
3424 if (size > DEFAULT_ERASE_SIZE)
3425 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003426
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003427 /* Simple inefficient version of erase. Just writing
3428 0 in first several blocks */
3429 if (mmc_write(ptn , size, (unsigned int *)out)) {
3430 fastboot_fail("failed to erase partition");
3431 return;
3432 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303433 /*Erase FDE metadata at the userdata footer*/
3434 if(!(strncmp(arg, "userdata", 8)))
3435 {
3436 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3437 memset((void *)footer, 0, FOOTER_SIZE);
3438
3439 size = partition_get_size(index);
3440
3441 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3442 fastboot_fail("failed to erase userdata footer");
3443 free(footer);
3444 return;
3445 }
3446 free(footer);
3447 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003448 }
Monika Singh292b3e92018-03-17 22:40:23 +05303449#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303450 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303451 !(strncmp(arg, "userdata", 8)) &&
3452 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003453 ASSERT(0);
3454#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003455 fastboot_okay("");
3456}
3457
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003458void cmd_erase(const char *arg, void *data, unsigned sz)
3459{
Monika Singh292b3e92018-03-17 22:40:23 +05303460#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003461 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003462 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003463 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003464 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003465 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003466 return;
3467 }
3468 }
3469#endif
3470
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003471 if(target_is_emmc_boot())
3472 cmd_erase_mmc(arg, data, sz);
3473 else
3474 cmd_erase_nand(arg, data, sz);
3475}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003476
Mayank Grover11ff9692018-01-11 11:54:49 +05303477/* Get the size from partiton name */
3478static void get_partition_size(const char *arg, char *response)
3479{
3480 uint64_t ptn = 0;
3481 uint64_t size;
3482 int index = INVALID_PTN;
3483
3484 index = partition_get_index(arg);
3485
3486 if (index == INVALID_PTN)
3487 {
3488 dprintf(CRITICAL, "Invalid partition index\n");
3489 return;
3490 }
3491
3492 ptn = partition_get_offset(index);
3493
3494 if(!ptn)
3495 {
3496 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3497 return;
3498 }
3499
3500 size = partition_get_size(index);
3501
3502 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3503 return;
3504}
3505
Mayank Grover52cd10a2018-03-15 12:57:54 +05303506/* Function to check partition type of a partition*/
3507static fs_signature_type
3508check_partition_fs_signature(const char *arg)
3509{
3510 fs_signature_type ret = NO_FS;
3511 int index;
3512 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303513 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3514 uint32_t sb_blk_offset = 0;
3515 char *sb_buffer = buffer;
3516
Mayank Grover52cd10a2018-03-15 12:57:54 +05303517 if (!sb_buffer)
3518 {
3519 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3520 goto out;
3521 }
3522
3523 /* Read super block */
3524 if ((index = partition_get_index(arg)) < 0)
3525 {
3526 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3527 goto out;
3528 }
3529 ptn = partition_get_offset(index);
3530 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303531 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3532
3533 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303534 (void *)sb_buffer, mmc_blocksize))
3535 {
3536 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3537 goto out;
3538 }
3539
Mayank Grover399826a2018-08-27 12:15:15 +05303540 if (sb_blk_offset == 0)
3541 sb_buffer += FS_SUPERBLOCK_OFFSET;
3542
3543 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303544 {
3545 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3546 ret = EXT_FS_SIGNATURE;
3547 }
Mayank Grover399826a2018-08-27 12:15:15 +05303548 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303549 {
3550 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3551 ret = EXT_F2FS_SIGNATURE;
3552 }
3553 else
3554 {
3555 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3556 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3557 ret = NO_FS;
3558 }
3559
3560out:
Mayank Grover399826a2018-08-27 12:15:15 +05303561 if(buffer)
3562 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303563 return ret;
3564}
3565
Mayank Groverd38fe012018-03-13 15:33:16 +05303566/* Function to get partition type */
3567static void get_partition_type(const char *arg, char *response)
3568{
3569 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303570 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303571
3572 if (arg == NULL ||
3573 response == NULL)
3574 {
3575 dprintf(CRITICAL, "Invalid input parameter\n");
3576 return;
3577 }
3578
3579 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303580 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303581
3582 /* Mark partiton type for known paritions only */
3583 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3584 {
3585 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3586 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303587 /* Check partition for FS signature */
3588 fs_signature = check_partition_fs_signature(arg);
3589 switch (fs_signature)
3590 {
3591 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303592 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303593 break;
3594 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303595 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303596 break;
3597 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303598 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303599 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303600 }
3601 }
3602 return;
3603}
3604
Mayank Grover11ff9692018-01-11 11:54:49 +05303605/*
3606 * Publish the partition type & size info
3607 * fastboot getvar will publish the required information.
3608 * fastboot getvar partition_size:<partition_name>: partition size in hex
3609 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3610 */
3611static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3612{
Mayank Groverd38fe012018-03-13 15:33:16 +05303613 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303614 static bool published = false;
3615 struct partition_entry *ptn_entry =
3616 partition_get_partition_entries();
3617 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3618
3619 for (i = 0; i < num_parts; i++) {
3620 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3621 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3622 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3623
Mayank Groverd38fe012018-03-13 15:33:16 +05303624 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303625 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303626 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3627 {
3628 dprintf(CRITICAL, "partition size name truncated\n");
3629 return;
3630 }
3631 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3632 {
3633 dprintf(CRITICAL, "partition type name truncated\n");
3634 return;
3635 }
3636
3637 if (!published)
3638 {
3639 /* publish partition size & type info */
3640 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3641 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3642 }
3643 }
3644 if (!published)
3645 published = true;
3646}
3647
3648
Ajay Dudani5c761132011-04-07 20:19:04 -07003649void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003650{
3651 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003652 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003653 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003654 char *token = NULL;
3655 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003656 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003657 uint8_t lun = 0;
3658 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303659 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003660
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003661 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003662 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003663 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003664 if(token)
3665 {
3666 lun = atoi(token);
3667 mmc_set_lun(lun);
3668 lun_set = true;
3669 }
3670
Mao Jinlong226f33a2014-07-04 17:24:10 +08003671 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003672 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003673 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3674 {
3675 if (!aboot_frp_unlock(pname, data, sz))
3676 {
3677 fastboot_info("FRP unlock successful");
3678 fastboot_okay("");
3679 }
3680 else
3681 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3682
3683 return;
3684 }
3685
Mao Jinlong226f33a2014-07-04 17:24:10 +08003686 if (!strcmp(pname, "partition"))
3687 {
3688 dprintf(INFO, "Attempt to write partition image.\n");
3689 if (write_partition(sz, (unsigned char *) data)) {
3690 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003691 return;
3692 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303693 /* Re-publish partition table */
3694 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303695
3696 /* Rescan partition table to ensure we have multislot support*/
3697 if (partition_scan_for_multislot())
3698 {
3699 current_active_slot = partition_find_active_slot();
3700 dprintf(INFO, "Multislot supported: Slot %s active",
3701 (SUFFIX_SLOT(current_active_slot)));
3702 }
3703 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003704 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003705 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003706 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003707#if VERIFIED_BOOT
3708 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3709 {
3710 if(!device.is_unlocked)
3711 {
3712 fastboot_fail("unlock device to flash keystore");
3713 return;
3714 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303715 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003716 {
3717 fastboot_fail("image is not a keystore file");
3718 return;
3719 }
3720 }
3721#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003722 index = partition_get_index(pname);
3723 ptn = partition_get_offset(index);
3724 if(ptn == 0) {
3725 fastboot_fail("partition table doesn't exist");
3726 return;
3727 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003728
Mayank Grover351a75e2017-05-30 20:06:08 +05303729 if (!strncmp(pname, "boot", strlen("boot"))
3730 || !strcmp(pname, "recovery"))
3731 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003732 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3733 fastboot_fail("image is not a boot image");
3734 return;
3735 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303736
3737 /* Reset multislot_partition attributes in case of flashing boot */
3738 if (partition_multislot_is_supported())
3739 {
3740 partition_reset_attributes(index);
3741 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003742 }
3743
3744 if(!lun_set)
3745 {
3746 lun = partition_get_lun(index);
3747 mmc_set_lun(lun);
3748 }
3749
3750 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303751 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003752 fastboot_fail("size too large");
3753 return;
3754 }
3755 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3756 fastboot_fail("flash write failure");
3757 return;
3758 }
Greg Grisco6e754772011-06-23 12:19:39 -07003759 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003760 }
3761 fastboot_okay("");
3762 return;
3763}
3764
Ajay Dudanide984792015-03-02 09:57:41 -08003765void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3766{
3767 int i, images;
3768 meta_header_t *meta_header;
3769 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303770 /*End of the image address*/
3771 uintptr_t data_end;
3772
3773 if( (UINT_MAX - sz) > (uintptr_t)data )
3774 data_end = (uintptr_t)data + sz;
3775 else
3776 {
3777 fastboot_fail("Cannot flash: image header corrupt");
3778 return;
3779 }
3780
3781 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3782 {
3783 fastboot_fail("Cannot flash: image header corrupt");
3784 return;
3785 }
Ajay Dudanide984792015-03-02 09:57:41 -08003786
lijuang8ee21882016-04-19 16:57:11 +08003787 /* If device is locked:
3788 * Forbid to flash image to avoid the device to bypass the image
3789 * which with "any" name other than bootloader. Because it maybe
3790 * a meta package of all partitions.
3791 */
Monika Singh292b3e92018-03-17 22:40:23 +05303792#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003793 if (target_build_variant_user()) {
3794 if (!device.is_unlocked) {
3795 fastboot_fail("Device is locked, meta image flashing is not allowed");
3796 return;
3797 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303798
Mayank Grover912eaa62017-10-26 12:08:53 +05303799 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303800 !device.is_unlock_critical)
3801 {
lijuang8ee21882016-04-19 16:57:11 +08003802 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3803 return;
3804 }
lijuang8ee21882016-04-19 16:57:11 +08003805 }
3806#endif
3807
Ajay Dudanide984792015-03-02 09:57:41 -08003808 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303809 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3810 {
3811 fastboot_fail("Cannot flash: image header corrupt");
3812 return;
3813 }
Ajay Dudanide984792015-03-02 09:57:41 -08003814 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3815
3816 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3817
3818 for (i=0; i<images; i++) {
3819
3820 if((img_header_entry[i].ptn_name == NULL) ||
3821 (img_header_entry[i].start_offset == 0) ||
3822 (img_header_entry[i].size == 0))
3823 break;
Kishor PK49831502017-04-21 17:55:43 +05303824 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3825 fastboot_fail("Integer overflow detected in start_offset of img");
3826 break;
3827 }
3828 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3829 fastboot_fail("Integer overflow detected in size of img");
3830 break;
3831 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303832 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3833 + img_header_entry[i].size) )
3834 {
3835 fastboot_fail("Cannot flash: image size mismatch");
3836 break;
3837 }
3838
Ajay Dudanide984792015-03-02 09:57:41 -08003839 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3840 (void *) data + img_header_entry[i].start_offset,
3841 img_header_entry[i].size);
3842 }
3843
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003844 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3845 {
3846 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3847 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3848 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3849 }
3850 else
3851 {
3852 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3853 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3854 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3855 }
3856
3857 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003858 fastboot_okay("");
3859 return;
3860}
3861
Ajay Dudani5c761132011-04-07 20:19:04 -07003862void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3863{
3864 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003865 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003866 uint32_t *fill_buf = NULL;
3867 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303868 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003869 sparse_header_t *sparse_header;
3870 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003871 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003872 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303873 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003874 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303875 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003876 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303877 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303878 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003879
Kinson Chikf1a43512011-07-14 11:28:39 -07003880 index = partition_get_index(arg);
3881 ptn = partition_get_offset(index);
3882 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003883 fastboot_fail("partition table doesn't exist");
3884 return;
3885 }
3886
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303887 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303888
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003889 lun = partition_get_lun(index);
3890 mmc_set_lun(lun);
3891
vijay kumar800255e2015-04-24 20:26:19 +05303892 if (sz < sizeof(sparse_header_t)) {
3893 fastboot_fail("size too low");
3894 return;
3895 }
3896
Ajay Dudani5c761132011-04-07 20:19:04 -07003897 /* Read and skip over sparse image header */
3898 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303899
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303900 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3901 fastboot_fail("Invalid block size\n");
3902 return;
3903 }
3904
vijay kumar1321f342015-03-27 12:13:42 +05303905 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003906 fastboot_fail("size too large");
3907 return;
3908 }
3909
vijay kumarde4fcf62015-04-23 13:05:49 +05303910 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303911
Parth Dixit64b1a482016-03-07 16:31:26 +05303912 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303913 fastboot_fail("buffer overreads occured due to invalid sparse header");
3914 return;
3915 }
3916
vijay kumarde4fcf62015-04-23 13:05:49 +05303917 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003918 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303919 fastboot_fail("sparse header size mismatch");
3920 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003921 }
3922
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003923 dprintf (SPEW, "=== Sparse Image Header ===\n");
3924 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3925 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3926 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3927 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3928 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3929 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3930 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3931 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003932
3933 /* Start processing chunks */
3934 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3935 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303936 /* Make sure the total image size does not exceed the partition size */
3937 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3938 fastboot_fail("size too large");
3939 return;
3940 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003941 /* Read and skip over chunk header */
3942 chunk_header = (chunk_header_t *) data;
3943 data += sizeof(chunk_header_t);
3944
Parth Dixit64b1a482016-03-07 16:31:26 +05303945 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303946 fastboot_fail("buffer overreads occured due to invalid sparse header");
3947 return;
3948 }
3949
Ajay Dudani5c761132011-04-07 20:19:04 -07003950 dprintf (SPEW, "=== Chunk Header ===\n");
3951 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3952 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3953 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3954
vijay kumar800255e2015-04-24 20:26:19 +05303955 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003956 {
vijay kumar800255e2015-04-24 20:26:19 +05303957 fastboot_fail("chunk header size mismatch");
3958 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003959 }
3960
wufeng.jiang813dc352015-06-02 23:02:46 -04003961 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3962
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303963 /* Make sure that the chunk size calculated from sparse image does not
3964 * exceed partition size
3965 */
3966 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3967 {
3968 fastboot_fail("Chunk data size exceeds partition size");
3969 return;
3970 }
3971
Ajay Dudani5c761132011-04-07 20:19:04 -07003972 switch (chunk_header->chunk_type)
3973 {
3974 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003975 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003976 chunk_data_sz))
3977 {
3978 fastboot_fail("Bogus chunk size for chunk type Raw");
3979 return;
3980 }
3981
Parth Dixit64b1a482016-03-07 16:31:26 +05303982 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303983 fastboot_fail("buffer overreads occured due to invalid sparse header");
3984 return;
3985 }
3986
wufeng.jiang813dc352015-06-02 23:02:46 -04003987 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3988 otherwise it will return in the line above
3989 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003990 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003991 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003992 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003993 {
3994 fastboot_fail("flash write failure");
3995 return;
3996 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303997 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3998 fastboot_fail("Bogus size for RAW chunk type");
3999 return;
4000 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004001 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004002 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004003 break;
4004
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004005 case CHUNK_TYPE_FILL:
4006 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4007 sizeof(uint32_t)))
4008 {
4009 fastboot_fail("Bogus chunk size for chunk type FILL");
4010 return;
4011 }
4012
Mayank Grover4f50bba2017-07-19 18:17:08 +05304013 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4014 /* Integer overflow detected */
4015 if (blk_sz_actual < sparse_header->blk_sz)
4016 {
4017 fastboot_fail("Invalid block size");
4018 return;
4019 }
4020
4021 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004022 if (!fill_buf)
4023 {
4024 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4025 return;
4026 }
4027
Parth Dixit64b1a482016-03-07 16:31:26 +05304028 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304029 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304030 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304031 return;
4032 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004033 fill_val = *(uint32_t *)data;
4034 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004035
4036 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4037 {
4038 fill_buf[i] = fill_val;
4039 }
4040
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304041 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4042 {
4043 fastboot_fail("bogus size for chunk FILL type");
4044 free(fill_buf);
4045 return;
4046 }
4047
wufeng.jiang813dc352015-06-02 23:02:46 -04004048 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004049 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304050 /* Make sure that the data written to partition does not exceed partition size */
4051 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4052 {
4053 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304054 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304055 return;
4056 }
4057
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004058 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4059 sparse_header->blk_sz,
4060 fill_buf))
4061 {
4062 fastboot_fail("flash write failure");
4063 free(fill_buf);
4064 return;
4065 }
4066
4067 total_blocks++;
4068 }
4069
4070 free(fill_buf);
4071 break;
4072
Ajay Dudani5c761132011-04-07 20:19:04 -07004073 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304074 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4075 fastboot_fail("bogus size for chunk DONT CARE type");
4076 return;
4077 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004078 total_blocks += chunk_header->chunk_sz;
4079 break;
4080
Ajay Dudani5c761132011-04-07 20:19:04 -07004081 case CHUNK_TYPE_CRC:
4082 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4083 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004084 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004085 return;
4086 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304087 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4088 fastboot_fail("bogus size for chunk CRC type");
4089 return;
4090 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004091 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304092 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304093 fastboot_fail("integer overflow occured");
4094 return;
4095 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004096 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304097 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304098 fastboot_fail("buffer overreads occured due to invalid sparse header");
4099 return;
4100 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004101 break;
4102
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004103 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004104 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004105 fastboot_fail("Unknown chunk type");
4106 return;
4107 }
4108 }
4109
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004110 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4111 total_blocks, sparse_header->total_blks);
4112
4113 if(total_blocks != sparse_header->total_blks)
4114 {
4115 fastboot_fail("sparse image write failure");
4116 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004117
4118 fastboot_okay("");
4119 return;
4120}
4121
4122void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4123{
4124 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004125 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004126
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004127#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004128 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4129 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004130 int ret=0;
4131 uint32 major_version=0;
4132 uint32 minor_version=0;
4133
4134 ret = scm_svc_version(&major_version,&minor_version);
4135 if(!ret)
4136 {
4137 if(major_version >= 2)
4138 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004139 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004140 {
4141 ret = encrypt_scm((uint32 **) &data, &sz);
4142 if (ret != 0) {
4143 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4144 return;
4145 }
4146
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004147 /* Protect only for SSD */
4148 if (!strcmp(arg, "ssd")) {
4149 ret = scm_protect_keystore((uint32 *) data, sz);
4150 if (ret != 0) {
4151 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4152 return;
4153 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004154 }
4155 }
4156 else
4157 {
4158 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4159 if(ret != 0)
4160 {
4161 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4162 return;
4163 }
4164 }
4165 }
4166 else
4167 {
4168 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4169 magic_number[1] == DECRYPT_MAGIC_1)
4170 {
4171 ret = decrypt_scm((uint32 **) &data, &sz);
4172 if (ret != 0) {
4173 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4174 return;
4175 }
4176 }
4177 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4178 magic_number[1] == ENCRYPT_MAGIC_1)
4179 {
4180 ret = encrypt_scm((uint32 **) &data, &sz);
4181 if (ret != 0) {
4182 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4183 return;
4184 }
4185 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004186 }
4187 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004188 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004189 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004190 dprintf(CRITICAL,"INVALID SVC Version\n");
4191 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004192 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004193#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004194
Monika Singh292b3e92018-03-17 22:40:23 +05304195#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004196 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004197 {
lijuang511a2b52015-08-14 20:50:51 +08004198 /* if device is locked:
4199 * common partition will not allow to be flashed
4200 * critical partition will allow to flash image.
4201 */
4202 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4203 fastboot_fail("Partition flashing is not allowed");
4204 return;
4205 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304206
lijuang511a2b52015-08-14 20:50:51 +08004207 /* if device critical is locked:
4208 * common partition will allow to be flashed
4209 * critical partition will not allow to flash image.
4210 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304211 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304212 !device.is_unlock_critical &&
4213 critical_flash_allowed(arg)) {
4214 fastboot_fail("Critical partition flashing is not allowed");
4215 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004216 }
4217 }
4218#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304219 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4220 dprintf(INFO, "flashing avb_custom_key\n");
4221 if (store_userkey(data, sz)) {
4222 fastboot_fail("Flashing avb_custom_key failed");
4223 } else {
4224 fastboot_okay("");
4225 }
4226 return;
4227 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004228
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004229 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004230 meta_header = (meta_header_t *) data;
4231 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004232 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004233 else if (meta_header->magic == META_HEADER_MAGIC)
4234 cmd_flash_meta_img(arg, data, sz);
4235 else
4236 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004237
4238#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304239 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304240 (!strncmp(arg, "system", 6)) &&
4241 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004242 // reset dm_verity mode to enforcing
4243 device.verity_mode = 1;
4244 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304245#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004246
Ajay Dudani5c761132011-04-07 20:19:04 -07004247 return;
4248}
4249
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004250void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4251{
4252 struct ptentry *sys_ptn;
4253 struct ptable *ptable;
4254
4255 ptable = flash_get_ptable();
4256 if (ptable == NULL) {
4257 fastboot_fail("partition table doesn't exist");
4258 return;
4259 }
4260
4261 sys_ptn = ptable_find(ptable, "system");
4262 if (sys_ptn == NULL) {
4263 fastboot_fail("system partition not found");
4264 return;
4265 }
4266
4267 sz = ROUND_TO_PAGE(sz, page_mask);
4268 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4269 fastboot_fail("update_ubi_vol failed");
4270 else
4271 fastboot_okay("");
4272}
4273
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004274void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004275{
4276 struct ptentry *ptn;
4277 struct ptable *ptable;
4278 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304279 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304280 unsigned bytes_to_round_page = 0;
4281 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004282
Kishor PK38ed93d2017-04-25 14:19:26 +05304283 if((uintptr_t)data > (UINT_MAX - sz)) {
4284 fastboot_fail("Cannot flash: image header corrupt");
4285 return;
4286 }
4287
Dima Zavin214cc642009-01-26 11:16:21 -08004288 ptable = flash_get_ptable();
4289 if (ptable == NULL) {
4290 fastboot_fail("partition table doesn't exist");
4291 return;
4292 }
4293
4294 ptn = ptable_find(ptable, arg);
4295 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004296 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4297 arg);
4298 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004299 return;
4300 }
4301
Monika Singh292b3e92018-03-17 22:40:23 +05304302 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4303 dprintf(INFO, "flashing avb_custom_key\n");
4304 if (store_userkey(data, sz)) {
4305 fastboot_fail("Flashing avb_custom_key failed");
4306 } else {
4307 fastboot_okay("");
4308 }
4309 return;
4310 }
4311
Dima Zavin214cc642009-01-26 11:16:21 -08004312 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304313 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4314 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4315 } else {
4316 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004317 return;
4318 }
4319 }
4320
Amol Jadi5c61a952012-05-04 17:05:35 -07004321 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004322 || !strcmp(ptn->name, "userdata")
4323 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004324 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004325 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004326 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304327 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304328 rounded_size = ROUNDUP(sz, page_size);
4329 bytes_to_round_page = rounded_size - sz;
4330 if (bytes_to_round_page) {
4331 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4332 fastboot_fail("Integer overflow detected");
4333 return;
4334 }
4335 if (((uintptr_t)data + sz + bytes_to_round_page) >
4336 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4337 fastboot_fail("Buffer size is not aligned to page_size");
4338 return;
4339 }
4340 else {
4341 memset(data + sz, 0, bytes_to_round_page);
4342 sz = rounded_size;
4343 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304344 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304345 }
4346
Kishor PK38ed93d2017-04-25 14:19:26 +05304347 /*Checking partition_size for the possible integer overflow */
4348 partition_size = validate_partition_size(ptn);
4349
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304350 if (sz > partition_size) {
4351 fastboot_fail("Image size too large");
4352 return;
4353 }
4354
Dima Zavin214cc642009-01-26 11:16:21 -08004355 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304356 if ((sz > UBI_EC_HDR_SIZE) &&
4357 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004358 if (flash_ubi_img(ptn, data, sz)) {
4359 fastboot_fail("flash write failure");
4360 return;
4361 }
4362 } else {
4363 if (flash_write(ptn, extra, data, sz)) {
4364 fastboot_fail("flash write failure");
4365 return;
4366 }
Dima Zavin214cc642009-01-26 11:16:21 -08004367 }
4368 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4369 fastboot_okay("");
4370}
4371
Kishor PK38ed93d2017-04-25 14:19:26 +05304372
4373static inline uint64_t validate_partition_size(struct ptentry *ptn)
4374{
4375 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4376 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4377 return ptn->length * flash_num_pages_per_blk() * page_size;
4378 }
4379 }
4380 return 0;
4381}
4382
4383
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004384void cmd_flash(const char *arg, void *data, unsigned sz)
4385{
4386 if(target_is_emmc_boot())
4387 cmd_flash_mmc(arg, data, sz);
4388 else
4389 cmd_flash_nand(arg, data, sz);
4390}
4391
Dima Zavin214cc642009-01-26 11:16:21 -08004392void cmd_continue(const char *arg, void *data, unsigned sz)
4393{
4394 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004395 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004396
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004397 if (target_is_emmc_boot())
4398 {
lijuanga40d6302015-07-20 20:10:13 +08004399#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004400 /* Exit keys' detection thread firstly */
4401 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004402#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004403 boot_linux_from_mmc();
4404 }
4405 else
4406 {
4407 boot_linux_from_flash();
4408 }
Dima Zavin214cc642009-01-26 11:16:21 -08004409}
4410
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004411void cmd_reboot(const char *arg, void *data, unsigned sz)
4412{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004413 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004414 fastboot_okay("");
4415 reboot_device(0);
4416}
4417
Mayank Grover351a75e2017-05-30 20:06:08 +05304418void cmd_set_active(const char *arg, void *data, unsigned sz)
4419{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304420 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304421 unsigned i,current_active_slot;
4422 const char *current_slot_suffix;
4423
4424 if (!partition_multislot_is_supported())
4425 {
4426 fastboot_fail("Command not supported");
4427 return;
4428 }
4429
4430 if (arg)
4431 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304432 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304433 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304434 {
4435 current_active_slot = partition_find_active_slot();
4436
4437 /* Check if trying to make curent slot active */
4438 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304439 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4440 (char *)suffix_delimiter, &sp);
4441
Mayank Grover5eb5f692017-07-19 20:16:04 +05304442 if (current_slot_suffix &&
4443 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304444 {
4445 fastboot_okay("Slot already set active");
4446 return;
4447 }
4448 else
4449 {
4450 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4451 {
4452 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304453 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4454 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304455 if (current_slot_suffix &&
4456 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304457 {
lijuang8b03e5f2019-07-12 18:16:19 +08004458 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304459 publish_getvar_multislot_vars();
4460 fastboot_okay("");
4461 return;
4462 }
4463 }
4464 }
4465 }
4466 }
4467 fastboot_fail("Invalid slot suffix.");
4468 return;
4469}
4470
Mayank Grover98c4c742019-04-25 17:21:37 +05304471#if DYNAMIC_PARTITION_SUPPORT
4472void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4473{
4474 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4475 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4476 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4477 fastboot_fail("Failed to update recovery command");
4478 return;
4479 }
4480 fastboot_okay("");
4481 reboot_device(REBOOT_MODE_UNKNOWN);
4482
4483 //shouldn't come here.
4484 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4485 return;
4486}
4487
4488void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4489{
4490 dprintf(INFO, "rebooting the device - recovery\n");
4491 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
4492 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4493 fastboot_fail("Failed to update recovery command");
4494 return;
4495 }
4496 fastboot_okay("");
4497 reboot_device(REBOOT_MODE_UNKNOWN);
4498
4499 //shouldn't come here.
4500 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4501 return;
4502}
4503#endif
4504
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004505void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4506{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004507 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004508 fastboot_okay("");
4509 reboot_device(FASTBOOT_MODE);
4510}
4511
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004512void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4513{
4514 dprintf(INFO, "Enabling charger screen check\n");
4515 device.charger_screen_enabled = 1;
4516 write_device_info(&device);
4517 fastboot_okay("");
4518}
4519
4520void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4521{
4522 dprintf(INFO, "Disabling charger screen check\n");
4523 device.charger_screen_enabled = 0;
4524 write_device_info(&device);
4525 fastboot_okay("");
4526}
4527
lijuanga25d8bb2015-09-16 13:11:52 +08004528void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4529{
4530 char *p = NULL;
4531 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304532 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004533
4534 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304535 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004536 if (p) {
4537 if (!strncmp(p, "0", 1)) {
4538 device.charger_screen_enabled = 0;
4539 } else if (!strncmp(p, "1", 1)) {
4540 device.charger_screen_enabled = 1;
4541 }
4542 }
4543 }
4544
4545 /* update charger_screen_enabled value for getvar
4546 * command
4547 */
4548 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4549 device.charger_screen_enabled);
4550
4551 write_device_info(&device);
4552 fastboot_okay("");
4553}
4554
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304555void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4556{
4557 dprintf(INFO, "Selecting display panel %s\n", arg);
4558 if (arg)
4559 strlcpy(device.display_panel, arg,
4560 sizeof(device.display_panel));
4561 write_device_info(&device);
4562 fastboot_okay("");
4563}
4564
Shashank Mittal162244e2011-08-08 19:01:25 -07004565void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4566{
lijuang4ece1e72015-08-14 21:02:36 +08004567 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304568}
4569
4570void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4571{
lijuang4ece1e72015-08-14 21:02:36 +08004572 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304573 if(!is_allow_unlock) {
4574 fastboot_fail("oem unlock is not allowed");
4575 return;
4576 }
4577
lijuang4ece1e72015-08-14 21:02:36 +08004578 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304579
lijuang4ece1e72015-08-14 21:02:36 +08004580 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304581 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304582 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304583 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4584 write_misc(0, &msg, sizeof(msg));
4585
4586 fastboot_okay("");
4587 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004588 }
4589 fastboot_okay("");
4590}
4591
Channagoud Kadabiad259832015-05-29 11:14:17 -07004592static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4593{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304594 int ret=1;
4595 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004596
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304597 /*
4598 Authentication method not implemented.
4599
4600 OEM to implement, authentication system which on successful validataion,
4601 calls write_allow_oem_unlock() with is_allow_unlock.
4602 */
4603#if 0
4604 authentication_success = oem_specific_auth_mthd();
4605#endif
4606
4607 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004608 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304609 is_allow_unlock = true;
4610 write_allow_oem_unlock(is_allow_unlock);
4611 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004612 }
4613 return ret;
4614}
4615
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004616void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4617{
lijuang4ece1e72015-08-14 21:02:36 +08004618 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004619}
4620
Shashank Mittala0032282011-08-26 14:50:11 -07004621void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4622{
lijuang511a2b52015-08-14 20:50:51 +08004623 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004624 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004625 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004626 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4627 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304628#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304629 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304630 {
4631 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4632 (device.is_unlock_critical ? "true" : "false"));
4633 fastboot_info(response);
4634 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304635#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004636 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004637 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304638 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4639 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004640 fastboot_okay("");
4641}
4642
lijuang511a2b52015-08-14 20:50:51 +08004643void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4644{
4645 char response[MAX_RSP_SIZE];
4646 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4647 fastboot_info(response);
4648 fastboot_okay("");
4649}
4650
4651void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4652{
lijuang4ece1e72015-08-14 21:02:36 +08004653 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004654}
4655
4656void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4657{
lijuang4ece1e72015-08-14 21:02:36 +08004658 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004659}
4660
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004661void cmd_preflash(const char *arg, void *data, unsigned sz)
4662{
4663 fastboot_okay("");
4664}
4665
Mao Flynn7b379f32015-04-20 00:28:30 +08004666static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304667
Mao Flynn7b379f32015-04-20 00:28:30 +08004668int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304669{
Mao Flynn7b379f32015-04-20 00:28:30 +08004670 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304671 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004672 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304673 return -1;
4674 return 0;
4675}
4676
Mao Flynn7b379f32015-04-20 00:28:30 +08004677int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004678{
4679 struct ptentry *ptn;
4680 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004681 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004682 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004683
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304684 ptable = flash_get_ptable();
4685 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004686 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4687 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304688 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004689
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304690 ptn = ptable_find(ptable, "splash");
4691 if (ptn == NULL) {
4692 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004693 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304694 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004695 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304696 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004697 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304698 }
4699
Mao Flynn7b379f32015-04-20 00:28:30 +08004700 header = (struct logo_img_header *)logo_header;
4701 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304702 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004703 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304704 }
4705
4706 fb_display = fbcon_display();
4707 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304708 if (header->type && (header->blocks != 0) &&
4709 (UINT_MAX >= header->blocks * 512) &&
4710 ((header->blocks * 512) <= (fb_display->width *
4711 fb_display->height * (fb_display->bpp / 8)))) {
4712 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08004713 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4714
4715 /* if the logo is full-screen size, remove "fbcon_clear()" */
4716 if ((header->width != fb_display->width)
4717 || (header->height != fb_display->height))
4718 fbcon_clear();
4719
4720 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4721 (uint32_t *)base,
4722 (header->blocks * 512))) {
4723 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4724 return -1;
4725 }
4726 fbcon_extract_to_screen(header, base);
4727 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004728 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004729
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004730 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4731 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004732 return -1;
4733 }
4734
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304735 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304736 uint32_t fb_size = ROUNDUP(fb_display->width *
4737 fb_display->height *
4738 (fb_display->bpp / 8), 4096);
4739 uint32_t splash_size = ((((header->width * header->height *
4740 fb_display->bpp/8) + 511) >> 9) << 9);
4741
4742 if (splash_size > fb_size) {
4743 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4744 return -1;
4745 }
4746
Mao Flynn7b379f32015-04-20 00:28:30 +08004747 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4748 (uint32_t *)base,
4749 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304750 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304751 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004752 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004753 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304754 }
4755
Mao Flynn7b379f32015-04-20 00:28:30 +08004756 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304757}
4758
Mao Flynn7b379f32015-04-20 00:28:30 +08004759int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304760{
4761 int index = INVALID_PTN;
4762 unsigned long long ptn = 0;
4763 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004764 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004765 uint32_t blocksize, realsize, readsize;
4766 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304767
4768 index = partition_get_index("splash");
4769 if (index == 0) {
4770 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004771 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304772 }
4773
4774 ptn = partition_get_offset(index);
4775 if (ptn == 0) {
4776 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004777 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304778 }
4779
Mao Flynn1893a7f2015-06-03 12:03:36 +08004780 mmc_set_lun(partition_get_lun(index));
4781
4782 blocksize = mmc_get_device_blocksize();
4783 if (blocksize == 0) {
4784 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4785 return -1;
4786 }
4787
4788 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004789 if (!fb_display)
4790 {
4791 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4792 return -1;
4793 }
4794
Mao Flynn1893a7f2015-06-03 12:03:36 +08004795 base = (uint8_t *) fb_display->base;
4796
Kishor PKee5c0a32018-03-06 16:49:46 +05304797 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304798 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004799 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304800 }
4801
Mao Flynn1893a7f2015-06-03 12:03:36 +08004802 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004803 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304804 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004805 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304806 }
4807
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304808 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304809 if (header->type && (header->blocks != 0) &&
4810 (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
4811 ((header->blocks * 512) <= (fb_display->width *
4812 fb_display->height * (fb_display->bpp / 8)))) {
4813 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08004814 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004815
Mao Flynn1893a7f2015-06-03 12:03:36 +08004816 realsize = header->blocks * 512;
4817 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4818
4819 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004820 if ((header->width != fb_display->width)
4821 || (header->height != fb_display->height))
4822 fbcon_clear();
4823
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304824 uint32_t fb_size = ROUNDUP(fb_display->width *
4825 fb_display->height *
4826 (fb_display->bpp / 8), 4096);
4827
4828 if (readsize > fb_size) {
4829 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4830 return -1;
4831 }
4832
Kishor PKee5c0a32018-03-06 16:49:46 +05304833 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004834 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4835 return -1;
4836 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004837
Mao Flynn1893a7f2015-06-03 12:03:36 +08004838 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4839 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304840
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004841 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4842 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004843 return -1;
4844 }
4845
4846 realsize = header->width * header->height * fb_display->bpp / 8;
4847 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4848
4849 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304850 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004851 fbcon_clear();
4852 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4853 return -1;
4854 }
4855 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304856 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004857 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4858 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4859 return -1;
4860 }
4861 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4862 }
4863 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304864 }
4865
Mao Flynn7b379f32015-04-20 00:28:30 +08004866 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304867}
4868
Mao Flynn7b379f32015-04-20 00:28:30 +08004869int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304870{
4871 if (target_is_emmc_boot()) {
4872 return splash_screen_mmc();
4873 } else {
4874 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004875 }
4876}
4877
Mayank Grover351a75e2017-05-30 20:06:08 +05304878void publish_getvar_multislot_vars()
4879{
4880 int i,count;
4881 static bool published = false;
4882 static char slot_count[MAX_RSP_SIZE];
4883 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4884 static char active_slot_suffix[MAX_RSP_SIZE];
4885 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4886 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4887 const char *tmp;
4888 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304889 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304890
4891 if (!published)
4892 {
4893 /* Update slot meta info */
4894 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4895 partition_get_partition_count());
4896 for(i=0; i<count; i++)
4897 {
4898 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304899 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4900 has_slot_pname[i]);
4901 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304902 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4903 }
4904
4905 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4906 {
4907 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304908 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304909 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4910 "slot-unbootable:%s", tmp);
4911 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4912 "slot-active:%s", tmp);
4913 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4914 "slot-success:%s", tmp);
4915 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4916 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304917 fastboot_publish(slot_info[i].slot_is_unbootable,
4918 slot_info[i].slot_is_unbootable_rsp);
4919 fastboot_publish(slot_info[i].slot_is_active,
4920 slot_info[i].slot_is_active_rsp);
4921 fastboot_publish(slot_info[i].slot_is_succesful,
4922 slot_info[i].slot_is_succesful_rsp);
4923 fastboot_publish(slot_info[i].slot_retry_count,
4924 slot_info[i].slot_retry_count_rsp);
4925 }
4926 fastboot_publish("current-slot", active_slot_suffix);
4927 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4928 fastboot_publish("slot-count", slot_count);
4929 published = true;
4930 }
4931
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304932 active_slt = partition_find_active_slot();
4933 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304934 {
4935 tmp = SUFFIX_SLOT(active_slt);
4936 tmp++; // to remove "_" from slot_suffix.
4937 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4938 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304939 else
4940 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4941
Mayank Grover351a75e2017-05-30 20:06:08 +05304942 /* Update partition meta information */
4943 partition_fill_slot_meta(slot_info);
4944 return;
4945}
4946
lijuang4ece1e72015-08-14 21:02:36 +08004947void get_product_name(unsigned char *buf)
4948{
4949 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4950 return;
4951}
4952
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304953#if PRODUCT_IOT
4954void get_bootloader_version_iot(unsigned char *buf)
4955{
4956 if (buf != NULL)
4957 {
4958 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4959 strlcat(buf, "-", MAX_VERSION_LEN);
4960 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4961 }
4962 return;
4963}
4964#endif
4965
lijuang4ece1e72015-08-14 21:02:36 +08004966void get_bootloader_version(unsigned char *buf)
4967{
4968 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4969 return;
4970}
4971
4972void get_baseband_version(unsigned char *buf)
4973{
4974 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4975 return;
4976}
4977
Monika Singh32a09f72018-03-14 13:08:29 +05304978bool is_device_locked_critical()
4979{
4980 return device.is_unlock_critical ? false:true;
4981}
4982
lijuang4ece1e72015-08-14 21:02:36 +08004983bool is_device_locked()
4984{
4985 return device.is_unlocked ? false:true;
4986}
4987
Monika Singh32a09f72018-03-14 13:08:29 +05304988bool is_verity_enforcing()
4989{
4990 return device.verity_mode ? true:false;
4991}
4992
Amol Jadi5edf3552013-07-23 14:15:34 -07004993/* register commands and variables for fastboot */
4994void aboot_fastboot_register_commands(void)
4995{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004996 int i;
lijuangf16461c2015-08-03 17:09:34 +08004997 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004998
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004999 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08005000 /* By default the enabled list is empty. */
5001 {"", NULL},
5002 /* move commands enclosed within the below ifndef to here
5003 * if they need to be enabled in user build.
5004 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005005#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005006 /* Register the following commands only for non-user builds */
5007 {"flash:", cmd_flash},
5008 {"erase:", cmd_erase},
5009 {"boot", cmd_boot},
5010 {"continue", cmd_continue},
5011 {"reboot", cmd_reboot},
5012 {"reboot-bootloader", cmd_reboot_bootloader},
5013 {"oem unlock", cmd_oem_unlock},
5014 {"oem unlock-go", cmd_oem_unlock_go},
5015 {"oem lock", cmd_oem_lock},
5016 {"flashing unlock", cmd_oem_unlock},
5017 {"flashing lock", cmd_oem_lock},
5018 {"flashing lock_critical", cmd_flashing_lock_critical},
5019 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5020 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5021 {"oem device-info", cmd_oem_devinfo},
5022 {"preflash", cmd_preflash},
5023 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5024 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005025 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005026 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305027 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305028#if DYNAMIC_PARTITION_SUPPORT
5029 {"reboot-fastboot",cmd_reboot_fastboot},
5030 {"reboot-recovery",cmd_reboot_recovery},
5031#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005032#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005033 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005034#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005035#endif
lijuang511a2b52015-08-14 20:50:51 +08005036 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005037
5038 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5039 for (i = 1; i < fastboot_cmds_count; i++)
5040 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5041
Amol Jadi5edf3552013-07-23 14:15:34 -07005042 /* publish variables and their values */
5043 fastboot_publish("product", TARGET(BOARD));
5044 fastboot_publish("kernel", "lk");
5045 fastboot_publish("serialno", sn_buf);
5046
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305047 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5048 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5049 fastboot_publish("hw-revision", soc_version_str);
5050
Amol Jadi5edf3552013-07-23 14:15:34 -07005051 /*
5052 * partition info is supported only for emmc partitions
5053 * Calling this for NAND prints some error messages which
5054 * is harmless but misleading. Avoid calling this for NAND
5055 * devices.
5056 */
5057 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305058 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005059
Mayank Grover351a75e2017-05-30 20:06:08 +05305060 if (partition_multislot_is_supported())
5061 publish_getvar_multislot_vars();
5062
Amol Jadi5edf3552013-07-23 14:15:34 -07005063 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005064#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005065 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5066 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005067#else
5068 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5069 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5070#endif
5071
Amol Jadi5edf3552013-07-23 14:15:34 -07005072 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005073 /* Is the charger screen check enabled */
5074 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5075 device.charger_screen_enabled);
5076 fastboot_publish("charger-screen-enabled",
5077 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08005078 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305079 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5080 device.display_panel);
5081 fastboot_publish("display-panel",
5082 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305083
5084 if (target_is_emmc_boot())
5085 {
5086 mmc_blocksize = mmc_get_device_blocksize();
5087 }
5088 else
5089 {
5090 mmc_blocksize = flash_block_size();
5091 }
5092 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5093 fastboot_publish("erase-block-size", (const char *) block_size_string);
5094 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305095#if PRODUCT_IOT
5096 get_bootloader_version_iot(&bootloader_version_string);
5097 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5098
5099 /* Version baseband is n/a for apq iot devices */
5100 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305101#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005102 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5103 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305104#endif
lijuangf16461c2015-08-03 17:09:34 +08005105 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305106 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005107 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5108 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5109 target_is_emmc_boot()? "eMMC":"UFS");
5110 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005111#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005112 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005113 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005114 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005115#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305116 if (target_dynamic_partition_supported())
5117 fastboot_publish("is-userspace", "no");
Amol Jadi5edf3552013-07-23 14:15:34 -07005118}
5119
Brian Swetland9c4c0752009-01-25 16:23:50 -08005120void aboot_init(const struct app_descriptor *app)
5121{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005122 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305123 int boot_err_type = 0;
5124 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07005125
lijuang39831732016-01-08 17:49:02 +08005126 /* Initialise wdog to catch early lk crashes */
5127#if WDOG_SUPPORT
5128 msm_wdog_init();
5129#endif
5130
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005131 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005132 if (target_is_emmc_boot())
5133 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005134 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005135 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305136 mmc_blocksize = mmc_get_device_blocksize();
5137 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005138 }
5139 else
5140 {
5141 page_size = flash_page_size();
5142 page_mask = page_size - 1;
5143 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005144 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5145
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005146 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05305147 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005148
Mayank Grover351a75e2017-05-30 20:06:08 +05305149 /* Detect multi-slot support */
5150 if (partition_multislot_is_supported())
5151 {
5152 boot_slot = partition_find_active_slot();
5153 if (boot_slot == INVALID)
5154 {
5155 boot_into_fastboot = true;
5156 dprintf(INFO, "Active Slot: (INVALID)\n");
5157 }
5158 else
5159 {
5160 /* Setting the state of system to boot active slot */
5161 partition_mark_active_slot(boot_slot);
5162 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5163 }
5164 }
5165
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005166 /* Display splash screen if enabled */
5167#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08005168#if NO_ALARM_DISPLAY
5169 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005170#endif
lijuang99c02d82015-02-13 19:04:34 +08005171 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07005172#if DISPLAY_HDMI_PRIMARY
5173 if (!strlen(device.display_panel))
5174 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5175 sizeof(device.display_panel));
5176#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005177#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07005178 /* Wait if the display shutdown is in progress */
5179 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005180 if (!pm_appsbl_display_init_done())
5181 target_display_init(device.display_panel);
5182 else
5183 display_image_on_screen();
5184#else
lijuang99c02d82015-02-13 19:04:34 +08005185 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005186#endif
lijuang99c02d82015-02-13 19:04:34 +08005187 dprintf(SPEW, "Display Init: Done\n");
5188#if NO_ALARM_DISPLAY
5189 }
5190#endif
5191#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005192
Greg Griscod6250552011-06-29 14:40:23 -07005193 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005194 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08005195
Dhaval Patel223ec952013-07-18 14:49:44 -07005196 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5197
Matthew Qindefd5562014-07-11 18:02:40 +08005198 /*
5199 * Check power off reason if user force reset,
5200 * if yes phone will do normal boot.
5201 */
5202 if (is_user_force_reset())
5203 goto normal_boot;
5204
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005205 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005206 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005207 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005208 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005209 reboot_device(EMERGENCY_DLOAD);
5210 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5211
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005212 boot_into_fastboot = true;
5213 }
5214 if (!boot_into_fastboot)
5215 {
5216 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5217 boot_into_recovery = 1;
5218 if (!boot_into_recovery &&
5219 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005220 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005221 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005222 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005223 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005224 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005225 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005226
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005227#if USE_PON_REBOOT_REG
5228 reboot_mode = check_hard_reboot_mode();
5229#else
Ajay Dudani77421292010-10-27 19:34:06 -07005230 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005231#endif
5232 if (reboot_mode == RECOVERY_MODE)
5233 {
Ajay Dudani77421292010-10-27 19:34:06 -07005234 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005235 }
5236 else if(reboot_mode == FASTBOOT_MODE)
5237 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005238 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005239 }
5240 else if(reboot_mode == ALARM_BOOT)
5241 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005242 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005243 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305244#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305245 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005246 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305247 if (reboot_mode == DM_VERITY_ENFORCING)
5248 {
5249 device.verity_mode = 1;
5250 write_device_info(&device);
5251 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005252#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305253 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005254#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305255 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005256#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305257 {
5258 device.verity_mode = 0;
5259 write_device_info(&device);
5260 }
5261 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5262 {
5263 if(send_delete_keys_to_tz())
5264 ASSERT(0);
5265 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005266 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305267#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005268
Matthew Qindefd5562014-07-11 18:02:40 +08005269normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005270 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005271 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005272 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005273 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005274 if(emmc_recovery_init())
5275 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5276 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005277 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005278 if((device.is_unlocked) || (device.is_tampered))
5279 {
5280 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305281 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005282 #endif
5283 #if USE_PCOM_SECBOOT
5284 set_tamper_flag(device.is_tampered);
5285 #endif
5286 }
Shashank Mittala0032282011-08-26 14:50:11 -07005287 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005288
Mayank Grover351a75e2017-05-30 20:06:08 +05305289retry_boot:
5290 /* Trying to boot active partition */
5291 if (partition_multislot_is_supported())
5292 {
5293 boot_slot = partition_find_boot_slot();
5294 partition_mark_active_slot(boot_slot);
5295 if (boot_slot == INVALID)
5296 goto fastboot;
5297 }
5298
5299 boot_err_type = boot_linux_from_mmc();
5300 switch (boot_err_type)
5301 {
5302 case ERR_INVALID_PAGE_SIZE:
5303 case ERR_DT_PARSE:
5304 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305305 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305306 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305307 {
5308 /*
5309 * Deactivate current slot, as it failed to
5310 * boot, and retry next slot.
5311 */
5312 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305313 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305314 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305315 else
5316 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305317 default:
5318 break;
5319 /* going to fastboot menu */
5320 }
Shashank Mittala0032282011-08-26 14:50:11 -07005321 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005322 else
5323 {
5324 recovery_init();
5325 #if USE_PCOM_SECBOOT
5326 if((device.is_unlocked) || (device.is_tampered))
5327 set_tamper_flag(device.is_tampered);
5328 #endif
5329 boot_linux_from_flash();
5330 }
5331 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5332 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005333 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005334
Mayank Grover351a75e2017-05-30 20:06:08 +05305335fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07005336 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005337
Amol Jadi5edf3552013-07-23 14:15:34 -07005338 /* register aboot specific fastboot commands */
5339 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005340
Amol Jadi5edf3552013-07-23 14:15:34 -07005341 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005342 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005343
5344 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305345#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005346 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305347#else
5348 /* Add salt buffer offset at start of image address to copy VB salt */
5349 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5350 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5351#endif
lijuang4ece1e72015-08-14 21:02:36 +08005352#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005353 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005354#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005355}
5356
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005357uint32_t get_page_size()
5358{
5359 return page_size;
5360}
5361
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005362/*
5363 * Calculated and save hash (SHA256) for non-signed boot image.
5364 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005365 * @param image_addr - Boot image address
5366 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005367 *
5368 * @return int - 0 on success, negative value on failure.
5369 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005370static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005371{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005372 unsigned int digest[8];
5373#if IMAGE_VERIF_ALGO_SHA1
5374 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5375#else
5376 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5377#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005378
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005379 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005380 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005381
5382 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005383 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005384
5385 return 0;
5386}
5387
Mayank Grover351a75e2017-05-30 20:06:08 +05305388
Brian Swetland9c4c0752009-01-25 16:23:50 -08005389APP_START(aboot)
5390 .init = aboot_init,
5391APP_END