blob: 07b56879d52aa897175d4e1f11731dd23c80117a [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Mayank Grover98c4c742019-04-25 17:21:37 +05305 * Copyright (c) 2009-2019, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Mayank Grover351a75e2017-05-30 20:06:08 +053051#include <ab_partition_parser.h>
Monika Singh292b3e92018-03-17 22:40:23 +053052#include <verifiedboot.h>
Greg Griscod6250552011-06-29 14:40:23 -070053#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070054#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070055#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070056#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030057#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070058#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070059#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070060#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053061#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080062#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053063#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080064#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080065#if USE_RPMB_FOR_DEVINFO
66#include <rpmb.h>
67#endif
Dima Zavin214cc642009-01-26 11:16:21 -080068
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070069#if ENABLE_WBC
70#include <pm_app_smbchg.h>
71#endif
72
Neeti Desai17379b82012-06-04 18:42:53 -070073#if DEVICE_TREE
74#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070076#endif
77
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053078#if WDOG_SUPPORT
79#include <wdog.h>
80#endif
81
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070082#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070083#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080084#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080085#include "bootimg.h"
86#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070087#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080088#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070089#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070091#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070092#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020093#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070094#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080095#include <menu_keys_detect.h>
96#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070097#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070098
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700100extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700101extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700102extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530103 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700105void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700106void write_device_info_mmc(device_info *dev);
107void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700108static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700109static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530110static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530111bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530112static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530113static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700114/* fastboot command function pointer */
115typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
Monika Singh292b3e92018-03-17 22:40:23 +0530116bool get_perm_attr_status();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700117
118struct fastboot_cmd_desc {
119 char * name;
120 fastboot_cmd_fn cb;
121};
122
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700123#define EXPAND(NAME) #NAME
124#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700125
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700126#define DISPLAY_PANEL_HDMI "hdmi"
127
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800128#ifdef MEMBASE
129#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
130#else
David Ng183a7422009-12-07 14:55:21 -0800131#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800132#endif
133
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700134#ifndef MEMSIZE
135#define MEMSIZE 1024*1024
136#endif
137
138#define MAX_TAGS_SIZE 1024
Kishor PKee5c0a32018-03-06 16:49:46 +0530139#define PLL_CODES_OFFSET 4096
Kun Liang2f1601a2013-08-12 16:29:54 +0800140/* make 4096 as default size to ensure EFS,EXT4's erasing */
141#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700142#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530143#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800144
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700145#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700146#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800147
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800148#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
149
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700150#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
151
Ameya Thakur10a33452016-06-13 14:24:26 -0700152//Size of the header that is used in case the boot image has
153//a uncompressed kernel + appended dtb
154#define PATCHED_KERNEL_HEADER_SIZE 20
155
156//String used to determine if the boot image has
157//a uncompressed kernel + appended dtb
158#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
159
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800160#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700161static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700162#else
David Ng183a7422009-12-07 14:55:21 -0800163static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700164#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530165static const char *dynamic_bootdev_cmdline =
166 " androidboot.boot_devices=soc/";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800167static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300168static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530169
170static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800171static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800172static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800173static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700174static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300175static const char *secondary_gpt_enable = " gpt";
Monika Singh292b3e92018-03-17 22:40:23 +0530176#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200177static const char *mdtp_activated_flag = " mdtp";
Monika Singh292b3e92018-03-17 22:40:23 +0530178#endif
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800179static const char *baseband_apq = " androidboot.baseband=apq";
180static const char *baseband_msm = " androidboot.baseband=msm";
181static const char *baseband_csfb = " androidboot.baseband=csfb";
182static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700183static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800184static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700185static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800186static const char *baseband_dsda = " androidboot.baseband=dsda";
187static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800188static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800189static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530190static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530191static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
192static const char *skip_ramfs = " skip_initramfs";
Mayank Grover466d6562018-05-10 14:52:20 +0530193
194#if HIBERNATION_SUPPORT
195static const char *resume = " resume=/dev/mmcblk0p";
196#endif
197
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530198#ifdef INIT_BIN_LE
199static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE;
200#else
Mayank Grover3804be72017-06-22 11:59:23 +0530201static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530202#endif
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530203
204#if VERITY_LE
205static const char *verity_dev = " root=/dev/dm-0";
206static const char *verity_system_part = " dm=\"system";
207static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
208#else
Mayank Grover5384ed82018-05-09 12:09:24 +0530209static const char *sys_path = " root=/dev/mmcblk0p";
lijuang83ef4b22018-08-23 11:01:55 +0800210
211#define MAX_DTBO_IDX_STR 64
212static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx=";
Parth Dixit1375d9e2019-07-08 20:56:13 +0530213
214#define MAX_DTB_IDX_STR MAX_DTBO_IDX_STR
215static const char *android_boot_dtb_idx = " androidboot.dtb_idx=";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530216#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800217
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700218#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700219static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700220static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530221static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700222//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700223
224struct verified_boot_verity_mode vbvm[] =
225{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700226#if ENABLE_VB_ATTEST
227 {false, "eio"},
228#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700229 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700230#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700231 {true, "enforcing"},
232};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700233struct verified_boot_state_name vbsn[] =
234{
235 {GREEN, "green"},
236 {ORANGE, "orange"},
237 {YELLOW,"yellow"},
238 {RED,"red" },
239};
240#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700241/*As per spec delay wait time before shutdown in Red state*/
242#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700243static unsigned page_size = 0;
244static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530245static unsigned mmc_blocksize = 0;
246static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700247static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
248static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530249static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800250static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700251static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700252bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530253static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530254static char *vbcmdline;
255static bootinfo info = {0};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530256static void *recovery_dtbo_buf = NULL;
257static uint32_t recovery_dtbo_size = 0;
258
Shashank Mittalcd98d472011-08-02 14:29:24 -0700259/* Assuming unauthorized kernel image by default */
260static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530261static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
262
vijay kumarc65876c2015-04-24 13:29:16 +0530263static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700264
vijay kumarca2e6812015-07-08 20:28:25 +0530265static char frp_ptns[2][8] = {"config","frp"};
266
lijuang511a2b52015-08-14 20:50:51 +0800267static const char *critical_flash_allowed_ptn[] = {
268 "aboot",
269 "rpm",
270 "tz",
271 "sbl",
272 "sdi",
273 "sbl1",
274 "xbl",
275 "hyp",
276 "pmic",
277 "bootloader",
278 "devinfo",
279 "partition"};
280
Dima Zavin42168f22009-01-30 11:52:22 -0800281struct atag_ptbl_entry
282{
283 char name[16];
284 unsigned offset;
285 unsigned size;
286 unsigned flags;
287};
288
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700289/*
290 * Partition info, required to be published
291 * for fastboot
292 */
293struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530294 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700295 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
296 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
297 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
298 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
299};
300
301/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530302 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700303 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530304#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530305#define EXT_STR "ext4"
306#define F2FS_STR "f2fs"
307
308#define FS_SUPERBLOCK_OFFSET 0x400
309#define EXT_MAGIC 0xEF53
310#define EXT_MAGIC_OFFSET_SB 0x38
311#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
312#define F2FS_MAGIC_OFFSET_SB 0x0
313
314typedef enum fs_signature_type {
315 EXT_FS_SIGNATURE = 1,
316 EXT_F2FS_SIGNATURE = 2,
317 NO_FS = -1
318} fs_signature_type;
319
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530320struct getvar_partition_info part_info[NUM_PARTITIONS];
321struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700322{
Mayank Grover49920212018-02-09 18:15:55 +0530323 { "system" , "partition-size:", "partition-type:", "", "ext4" },
324 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
325 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
326 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700327};
328
329char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700330char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800331char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700332char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530333char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530334char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530335char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530336#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530337
338/* For IOT we are using custom version */
339#define PRODUCT_IOT_VERSION "IOT001"
340char bootloader_version_string[MAX_RSP_SIZE];
341#endif
lijuang102dfa92015-10-09 18:31:03 +0800342
343#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800344char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800345char battery_soc_ok [MAX_RSP_SIZE];
346#endif
347
lijuangf16461c2015-08-03 17:09:34 +0800348char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700349
Greg Griscod2471ef2011-07-14 13:00:42 -0700350extern int emmc_recovery_init(void);
351
Kinson Chik0b1c8162011-08-31 16:31:57 -0700352#if NO_KEYPAD_DRIVER
353extern int fastboot_trigger(void);
354#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700355
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530356static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700357{
358 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700359#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800360 if (is_arm64)
361 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
362 else
363 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700364 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
365 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700366#endif
367}
368
Dima Zavin42168f22009-01-30 11:52:22 -0800369static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
370{
371 struct atag_ptbl_entry atag_ptn;
372
373 memcpy(atag_ptn.name, ptn->name, 16);
374 atag_ptn.name[15] = '\0';
375 atag_ptn.offset = ptn->start;
376 atag_ptn.size = ptn->length;
377 atag_ptn.flags = ptn->flags;
378 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
379 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
380}
Mayank Grover9df84c02018-08-30 15:46:35 +0530381#ifdef VERIFIED_BOOT_2
382void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
383{
384 int index = 0;
385 char *vbm_img_buf = NULL;
386 unsigned long long ptn = 0;
387 unsigned long long ptn_size = 0;
388
389 /* Immediately return if dtbo is not supported */
390 index = partition_get_index("vbmeta");
391 ptn = partition_get_offset(index);
392 if(!ptn)
393 {
394 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
395 return;
396 }
397
398 ptn_size = partition_get_size(index);
399 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
400 {
401 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
402 return;
403 }
404
405 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
406 if (!vbm_img_buf)
407 {
408 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
409 return;
410 }
411
412 mmc_set_lun(partition_get_lun(index));
413 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
414 {
415 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
416 free(vbm_img_buf);
417 return;
418 }
419
420 *vbmeta_image_buf = vbm_img_buf;
421 *vbmeta_image_sz = (uint32_t)ptn_size;
422 return;
423}
424#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800425
lijuang102dfa92015-10-09 18:31:03 +0800426#if CHECK_BAT_VOLTAGE
427void update_battery_status(void)
428{
429 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
430 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
431}
432#endif
433
Neeti Desaie245d492012-06-01 12:52:13 -0700434unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800435{
David Ng183a7422009-12-07 14:55:21 -0800436 int cmdline_len = 0;
437 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800438 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700439 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800440 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300441 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700442 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700443 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530444#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530445 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530446#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530447 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530448 int system_ptn_index = -1;
449 unsigned int lun = 0;
450 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530451#if VERITY_LE
452 int syspath_buflen = strlen(verity_dev)
453 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
454 + strlen(verity_params) + sizeof(int) + 2;
455#else
lijuang83ef4b22018-08-23 11:01:55 +0800456 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
457 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
458 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530459 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
460 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530461#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530462 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530463#if HIBERNATION_SUPPORT
464 int resume_buflen = strlen(resume) + sizeof(int) + 2;
465 char resume_buf[resume_buflen];
466 int swap_ptn_index = INVALID_PTN;
467#endif
468
Mayank Grover889be1b2017-09-12 20:12:23 +0530469#if VERIFIED_BOOT
470 uint32_t boot_state = RED;
471#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530472
473#if USE_LE_SYSTEMD
474 is_systemd_present=true;
475#endif
476
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700477#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530478 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530479 {
480 boot_state = boot_verify_get_state();
481 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530482#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700483
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200484#ifdef MDTP_SUPPORT
485 mdtp_activated(&is_mdtp_activated);
486#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800487
Brian Swetland9c4c0752009-01-25 16:23:50 -0800488 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800489 cmdline_len = strlen(cmdline);
490 have_cmdline = 1;
491 }
492 if (target_is_emmc_boot()) {
493 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700494 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530495 if (!boot_dev_buf) {
496 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
497 } else {
498 platform_boot_dev_cmdline(boot_dev_buf);
499#if USE_BOOTDEV_CMDLINE
500 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700501#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530502 if (target_dynamic_partition_supported()) {
503 cmdline_len += strlen(dynamic_bootdev_cmdline);
504 cmdline_len += strlen(boot_dev_buf);
505 }
506 }
David Ng183a7422009-12-07 14:55:21 -0800507 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800508
509 cmdline_len += strlen(usb_sn_cmdline);
510 cmdline_len += strlen(sn_buf);
511
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700512#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530513 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700514 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530515 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
516 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
517 {
518 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
519 ASSERT(0);
520 }
521 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
522 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700523 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530524#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700525
Monika Singh292b3e92018-03-17 22:40:23 +0530526
527 if (vbcmdline != NULL) {
528 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
529 strlen(vbcmdline));
530 cmdline_len += strlen(vbcmdline);
531 }
532
Pavel Nedev5614d222013-06-17 18:01:02 +0300533 if (boot_into_recovery && gpt_exists)
534 cmdline_len += strlen(secondary_gpt_enable);
535
Monika Singh292b3e92018-03-17 22:40:23 +0530536#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200537 if(is_mdtp_activated)
538 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530539#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300540 if (boot_into_ffbm) {
541 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530542
543 if(is_systemd_present)
544 cmdline_len += strlen(systemd_ffbm_mode);
545
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700546 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800547 /* reduce kernel console messages to speed-up boot */
548 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800549 } else if (boot_reason_alarm) {
550 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800551 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800552 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700553 pause_at_bootup = 1;
554 cmdline_len += strlen(battchg_pause);
555 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800556
Shashank Mittalcd98d472011-08-02 14:29:24 -0700557 if(target_use_signed_kernel() && auth_kernel_img) {
558 cmdline_len += strlen(auth_kernel);
559 }
560
Joonwoo Park61112782013-10-02 19:50:39 -0700561 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
562 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530563 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700564 have_target_boot_params = 1;
565 cmdline_len += strlen(target_boot_params);
566 }
567
Ajay Dudanid04110c2011-01-17 23:55:07 -0800568 /* Determine correct androidboot.baseband to use */
569 switch(target_baseband())
570 {
571 case BASEBAND_APQ:
572 cmdline_len += strlen(baseband_apq);
573 break;
574
575 case BASEBAND_MSM:
576 cmdline_len += strlen(baseband_msm);
577 break;
578
579 case BASEBAND_CSFB:
580 cmdline_len += strlen(baseband_csfb);
581 break;
582
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800583 case BASEBAND_SVLTE2A:
584 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800585 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700586
587 case BASEBAND_MDM:
588 cmdline_len += strlen(baseband_mdm);
589 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700590
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800591 case BASEBAND_MDM2:
592 cmdline_len += strlen(baseband_mdm2);
593 break;
594
Amol Jadi5c61a952012-05-04 17:05:35 -0700595 case BASEBAND_SGLTE:
596 cmdline_len += strlen(baseband_sglte);
597 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530598
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800599 case BASEBAND_SGLTE2:
600 cmdline_len += strlen(baseband_sglte2);
601 break;
602
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530603 case BASEBAND_DSDA:
604 cmdline_len += strlen(baseband_dsda);
605 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800606
607 case BASEBAND_DSDA2:
608 cmdline_len += strlen(baseband_dsda2);
609 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530610 case BASEBAND_APQ_NOWGR:
611 cmdline_len += strlen(baseband_apq_nowgr);
612 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800613 }
614
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300615#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800616 if (cmdline) {
617 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530618 target_display_panel_node(display_panel_buf,
619 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800620 strlen(display_panel_buf)) {
621 cmdline_len += strlen(display_panel_buf);
622 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700623 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300624#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700625
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800626 if (target_warm_boot()) {
627 warm_boot = true;
628 cmdline_len += strlen(warmboot_cmdline);
629 }
630
Mayank Grover5384ed82018-05-09 12:09:24 +0530631 if (target_uses_system_as_root() ||
632 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530633 {
634 current_active_slot = partition_find_active_slot();
635 cmdline_len += (strlen(androidboot_slot_suffix)+
636 strlen(SUFFIX_SLOT(current_active_slot)));
637
Mayank Grover3804be72017-06-22 11:59:23 +0530638 system_ptn_index = partition_get_index("system");
639 if (platform_boot_dev_isemmc())
640 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530641#if VERITY_LE
642 /*
643 Condition 4: Verity and A/B both enabled
644 Eventual command line looks like:
645 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
646 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
647 */
648 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
649 verity_dev,
650 verity_system_part, suffix_slot[current_active_slot],
651 verity_params, system_ptn_index + 1);
652#else
653 /*
654 Condition 5: A/B enabled, but verity disabled
655 Eventual command line looks like:
656 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
657 ... root=/dev/mmcblk0p<NN> ...
658 */
659 snprintf(syspath_buf, syspath_buflen, " %s%d",
660 sys_path, system_ptn_index + 1);
661#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530662 }
663 else
664 {
665 lun = partition_get_lun(system_ptn_index);
666 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
667 lun_char_base + lun,
668 partition_get_index_in_lun("system", lun));
669 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530670
Monika Singh292b3e92018-03-17 22:40:23 +0530671#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530672 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530673#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530674 }
675
676 if (target_uses_system_as_root() ||
677 partition_multislot_is_supported())
678 {
679 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530680
681 /* For dynamic partition, support skip skip_initramfs */
682 if (!target_dynamic_partition_supported() &&
683 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530684 cmdline_len += strlen(skip_ramfs);
685 }
686
Mayank Grover466d6562018-05-10 14:52:20 +0530687#if HIBERNATION_SUPPORT
688 if (platform_boot_dev_isemmc())
689 {
690 swap_ptn_index = partition_get_index("swap");
691 if (swap_ptn_index != INVALID_PTN)
692 {
693 snprintf(resume_buf, resume_buflen,
694 " %s%d", resume,
695 (swap_ptn_index + 1));
696 cmdline_len += strlen(resume_buf);
697 }
698 else
699 {
700 dprintf(INFO, "WARNING: swap partition not found\n");
701 }
702 }
703#endif
704
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800705#if TARGET_CMDLINE_SUPPORT
706 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
707 int target_cmd_line_len;
708 ASSERT(target_cmdline_buf);
709 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
710 cmdline_len += target_cmd_line_len;
711#endif
712
lijuang83ef4b22018-08-23 11:01:55 +0800713#if !VERITY_LE
714 dtbo_idx = get_dtbo_idx ();
715 if (dtbo_idx != INVALID_PTN) {
716 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
717 android_boot_dtbo_idx, dtbo_idx);
718 cmdline_len += strlen (dtbo_idx_str);
719 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530720
721 dtb_idx = get_dtb_idx ();
722 if (dtb_idx != INVALID_PTN) {
723 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
724 android_boot_dtb_idx, dtb_idx);
725 cmdline_len += strlen (dtb_idx_str);
726 }
lijuang83ef4b22018-08-23 11:01:55 +0800727#endif
728
David Ng183a7422009-12-07 14:55:21 -0800729 if (cmdline_len > 0) {
730 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800731 unsigned char *dst;
732
733 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
734 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530735 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800736 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700737
Amol Jadi168b7712012-03-06 16:15:00 -0800738 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800739 if (have_cmdline) {
740 src = cmdline;
741 while ((*dst++ = *src++));
742 }
743 if (target_is_emmc_boot()) {
744 src = emmc_cmdline;
745 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700746 have_cmdline = 1;
747 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800748#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700749 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530750 if (have_cmdline &&
751 boot_dev_buf) {
752 --dst;
753 while ((*dst++ = *src++));
754 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700755#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530756 /* Dynamic partition append boot_devices */
757 if (target_dynamic_partition_supported() &&
758 boot_dev_buf) {
759 src = dynamic_bootdev_cmdline;
760 if (have_cmdline) --dst;
761 while ((*dst++ = *src++));
762 src = boot_dev_buf;
763 if (have_cmdline) --dst;
764 while ((*dst++ = *src++));
765 }
David Ngf773dde2010-07-26 19:55:08 -0700766 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800767
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700768#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530769 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700770 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530771 src = verified_state;
772 if(have_cmdline) --dst;
773 have_cmdline = 1;
774 while ((*dst++ = *src++));
775 src = vbsn[boot_state].name;
776 if(have_cmdline) --dst;
777 while ((*dst++ = *src++));
778
779 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
780 {
781 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
782 ASSERT(0);
783 }
784 src = verity_mode;
785 if(have_cmdline) --dst;
786 while ((*dst++ = *src++));
787 src = vbvm[device.verity_mode].name;
788 if(have_cmdline) -- dst;
789 while ((*dst++ = *src++));
790 src = keymaster_v1;
791 if(have_cmdline) --dst;
792 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700793 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530794#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530795
796 if (vbcmdline != NULL) {
797 src = vbcmdline;
798 if (have_cmdline) --dst;
799 while ((*dst++ = *src++));
800 }
801
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800802 src = usb_sn_cmdline;
803 if (have_cmdline) --dst;
804 have_cmdline = 1;
805 while ((*dst++ = *src++));
806 src = sn_buf;
807 if (have_cmdline) --dst;
808 have_cmdline = 1;
809 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800810 if (warm_boot) {
811 if (have_cmdline) --dst;
812 src = warmboot_cmdline;
813 while ((*dst++ = *src++));
814 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800815
Pavel Nedev5614d222013-06-17 18:01:02 +0300816 if (boot_into_recovery && gpt_exists) {
817 src = secondary_gpt_enable;
818 if (have_cmdline) --dst;
819 while ((*dst++ = *src++));
820 }
Monika Singh292b3e92018-03-17 22:40:23 +0530821#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200822 if (is_mdtp_activated) {
823 src = mdtp_activated_flag;
824 if (have_cmdline) --dst;
825 while ((*dst++ = *src++));
826 }
Monika Singh292b3e92018-03-17 22:40:23 +0530827#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300828 if (boot_into_ffbm) {
829 src = androidboot_mode;
830 if (have_cmdline) --dst;
831 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700832 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300833 if (have_cmdline) --dst;
834 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530835
836 if(is_systemd_present) {
837 src = systemd_ffbm_mode;
838 if (have_cmdline) --dst;
839 while ((*dst++ = *src++));
840 }
841
Pavel Nedev898298c2013-02-27 12:36:09 -0800842 src = loglevel;
843 if (have_cmdline) --dst;
844 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800845 } else if (boot_reason_alarm) {
846 src = alarmboot_cmdline;
847 if (have_cmdline) --dst;
848 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300849 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700850 src = battchg_pause;
851 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800852 while ((*dst++ = *src++));
853 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800854
Shashank Mittalcd98d472011-08-02 14:29:24 -0700855 if(target_use_signed_kernel() && auth_kernel_img) {
856 src = auth_kernel;
857 if (have_cmdline) --dst;
858 while ((*dst++ = *src++));
859 }
860
Ajay Dudanid04110c2011-01-17 23:55:07 -0800861 switch(target_baseband())
862 {
863 case BASEBAND_APQ:
864 src = baseband_apq;
865 if (have_cmdline) --dst;
866 while ((*dst++ = *src++));
867 break;
868
869 case BASEBAND_MSM:
870 src = baseband_msm;
871 if (have_cmdline) --dst;
872 while ((*dst++ = *src++));
873 break;
874
875 case BASEBAND_CSFB:
876 src = baseband_csfb;
877 if (have_cmdline) --dst;
878 while ((*dst++ = *src++));
879 break;
880
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800881 case BASEBAND_SVLTE2A:
882 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800883 if (have_cmdline) --dst;
884 while ((*dst++ = *src++));
885 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700886
887 case BASEBAND_MDM:
888 src = baseband_mdm;
889 if (have_cmdline) --dst;
890 while ((*dst++ = *src++));
891 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700892
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800893 case BASEBAND_MDM2:
894 src = baseband_mdm2;
895 if (have_cmdline) --dst;
896 while ((*dst++ = *src++));
897 break;
898
Amol Jadi5c61a952012-05-04 17:05:35 -0700899 case BASEBAND_SGLTE:
900 src = baseband_sglte;
901 if (have_cmdline) --dst;
902 while ((*dst++ = *src++));
903 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530904
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800905 case BASEBAND_SGLTE2:
906 src = baseband_sglte2;
907 if (have_cmdline) --dst;
908 while ((*dst++ = *src++));
909 break;
910
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530911 case BASEBAND_DSDA:
912 src = baseband_dsda;
913 if (have_cmdline) --dst;
914 while ((*dst++ = *src++));
915 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800916
917 case BASEBAND_DSDA2:
918 src = baseband_dsda2;
919 if (have_cmdline) --dst;
920 while ((*dst++ = *src++));
921 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530922 case BASEBAND_APQ_NOWGR:
923 src = baseband_apq_nowgr;
924 if (have_cmdline) --dst;
925 while ((*dst++ = *src++));
926 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800927 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700928
929 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700930 src = display_panel_buf;
931 if (have_cmdline) --dst;
932 while ((*dst++ = *src++));
933 }
Joonwoo Park61112782013-10-02 19:50:39 -0700934
935 if (have_target_boot_params) {
936 if (have_cmdline) --dst;
937 src = target_boot_params;
938 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530939 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700940 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800941
Mayank Grover351a75e2017-05-30 20:06:08 +0530942 if (partition_multislot_is_supported() && have_cmdline)
943 {
944 src = androidboot_slot_suffix;
945 --dst;
946 while ((*dst++ = *src++));
947 --dst;
948 src = SUFFIX_SLOT(current_active_slot);
949 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530950 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530951
Mayank Grover351a75e2017-05-30 20:06:08 +0530952
Mayank Grover5384ed82018-05-09 12:09:24 +0530953 /*
954 * System-As-Root behaviour, system.img should contain both
955 * system content and ramdisk content, and should be mounted at
956 * root(a/b).
957 * Apending skip_ramfs for non a/b builds which use, system as root.
958 */
959 if ((target_uses_system_as_root() ||
960 partition_multislot_is_supported()) &&
961 have_cmdline)
962 {
Mayank Groverb716edf2019-05-08 16:06:55 +0530963 if (!target_dynamic_partition_supported() &&
964 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +0530965 {
966 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +0530967 --dst;
968 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530969 }
970
971 src = sys_path_cmdline;
972 --dst;
973 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530974
Monika Singh292b3e92018-03-17 22:40:23 +0530975#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +0530976 src = syspath_buf;
977 --dst;
978 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +0530979#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530980 }
981
Mayank Grover466d6562018-05-10 14:52:20 +0530982#if HIBERNATION_SUPPORT
983 if (swap_ptn_index != INVALID_PTN)
984 {
985 src = resume_buf;
986 --dst;
987 while ((*dst++ = *src++));
988 }
989#endif
990
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800991#if TARGET_CMDLINE_SUPPORT
992 if (target_cmdline_buf && target_cmd_line_len)
993 {
994 if (have_cmdline) --dst;
995 src = target_cmdline_buf;
996 while((*dst++ = *src++));
997 free(target_cmdline_buf);
998 }
999#endif
lijuang83ef4b22018-08-23 11:01:55 +08001000
1001#if !VERITY_LE
1002 if (dtbo_idx != INVALID_PTN) {
1003 src = dtbo_idx_str;
1004 --dst;
1005 while ((*dst++ = *src++));
1006 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301007
1008 if (dtb_idx != INVALID_PTN) {
1009 src = dtb_idx_str;
1010 --dst;
1011 while ((*dst++ = *src++));
1012 }
lijuang83ef4b22018-08-23 11:01:55 +08001013#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001014 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001015
1016
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001017 if (boot_dev_buf)
1018 free(boot_dev_buf);
1019
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001020 if (cmdline_final)
1021 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1022 else
1023 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001024 return cmdline_final;
1025}
1026
1027unsigned *atag_core(unsigned *ptr)
1028{
1029 /* CORE */
1030 *ptr++ = 2;
1031 *ptr++ = 0x54410001;
1032
1033 return ptr;
1034
1035}
1036
1037unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1038 unsigned ramdisk_size)
1039{
1040 if (ramdisk_size) {
1041 *ptr++ = 4;
1042 *ptr++ = 0x54420005;
1043 *ptr++ = (unsigned)ramdisk;
1044 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001045 }
1046
Neeti Desaie245d492012-06-01 12:52:13 -07001047 return ptr;
1048}
1049
1050unsigned *atag_ptable(unsigned **ptr_addr)
1051{
1052 int i;
1053 struct ptable *ptable;
1054
1055 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001056 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1057 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001058 *(*ptr_addr)++ = 0x4d534d70;
1059 for (i = 0; i < ptable->count; ++i)
1060 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1061 }
1062
1063 return (*ptr_addr);
1064}
1065
1066unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1067{
1068 int cmdline_length = 0;
1069 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001070 char *dest;
1071
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001072 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001073 n = (cmdline_length + 4) & (~3);
1074
1075 *ptr++ = (n / 4) + 2;
1076 *ptr++ = 0x54410009;
1077 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001078 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001079 ptr += (n / 4);
1080
1081 return ptr;
1082}
1083
1084unsigned *atag_end(unsigned *ptr)
1085{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001086 /* END */
1087 *ptr++ = 0;
1088 *ptr++ = 0;
1089
Neeti Desaie245d492012-06-01 12:52:13 -07001090 return ptr;
1091}
1092
1093void generate_atags(unsigned *ptr, const char *cmdline,
1094 void *ramdisk, unsigned ramdisk_size)
1095{
vijay kumar21a37452015-12-29 16:23:21 +05301096 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001097 ptr = atag_core(ptr);
1098 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1099 ptr = target_atag_mem(ptr);
1100
1101 /* Skip NAND partition ATAGS for eMMC boot */
1102 if (!target_is_emmc_boot()){
1103 ptr = atag_ptable(&ptr);
1104 }
1105
vijay kumar21a37452015-12-29 16:23:21 +05301106 /*
1107 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1108 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1109 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301110 if (!cmdline)
1111 return;
1112
vijay kumar21a37452015-12-29 16:23:21 +05301113 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1114 ptr = atag_cmdline(ptr, cmdline);
1115 ptr = atag_end(ptr);
1116 }
1117 else {
1118 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1119 ASSERT(0);
1120 }
Neeti Desaie245d492012-06-01 12:52:13 -07001121}
1122
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001123typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001124void boot_linux(void *kernel, unsigned *tags,
1125 const char *cmdline, unsigned machtype,
1126 void *ramdisk, unsigned ramdisk_size)
1127{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001128 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001129#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001130 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001131#endif
1132
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001133 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001134 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301135 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001136
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301137 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001138
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001139 final_cmdline = update_cmdline((const char*)cmdline);
1140
Neeti Desai17379b82012-06-04 18:42:53 -07001141#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001142 dprintf(INFO, "Updating device tree: start\n");
1143
Neeti Desai17379b82012-06-04 18:42:53 -07001144 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301145 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001146 if(ret)
1147 {
1148 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1149 ASSERT(0);
1150 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001151 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001152#else
Neeti Desaie245d492012-06-01 12:52:13 -07001153 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001154 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001155#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001156
Monika Singh0ec6db82019-07-18 16:57:58 +05301157#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301158 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301159 {
1160 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001161#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001162#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301163 display_bootverify_menu(DISPLAY_MENU_EIO);
1164 wait_for_users_action();
1165 if(!pwr_key_is_pressed)
1166 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001167#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301168 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001169#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301170 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001171#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301172 dprintf(CRITICAL,
1173 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1174 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001175#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301176 }
lijuangbdd9bb42016-03-01 18:22:17 +08001177 }
lijuangbdd9bb42016-03-01 18:22:17 +08001178#endif
1179
1180#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001181 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001182 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001183 {
1184 dprintf(INFO, "Failed to write protect dev info\n");
1185 ASSERT(0);
1186 }
1187#endif
1188
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001189 /* Turn off splash screen if enabled */
1190#if DISPLAY_SPLASH_SCREEN
1191 target_display_shutdown();
1192#endif
1193
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001194 /* Perform target specific cleanup */
1195 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301196 free_verified_boot_resource(&info);
1197 if (final_cmdline)
1198 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001199
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001200 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301201 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001202
1203 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001204
Amol Jadi4421e652011-06-16 15:00:48 -07001205 /* do any platform specific cleanup before kernel entry */
1206 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001207
Brian Swetland9c4c0752009-01-25 16:23:50 -08001208 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001209
Amol Jadi504f9fe2012-08-16 13:56:48 -07001210#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001211 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001212#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001213 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001214
1215 if (IS_ARM64(kptr))
1216 /* Jump to a 64bit kernel */
1217 scm_elexec_call((paddr_t)kernel, tags_phys);
1218 else
1219 /* Jump to a 32bit kernel */
1220 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001221}
1222
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001223/* Function to check if the memory address range falls within the aboot
1224 * boundaries.
1225 * start: Start of the memory region
1226 * size: Size of the memory region
1227 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301228int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001229{
1230 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001231 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001232 return -1;
1233
1234 /* Check for memory overlap. */
1235 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1236 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001237 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001238 return 0;
1239 else
1240 return -1;
1241}
1242
Mayank Grovere559cec2017-10-17 15:12:03 +05301243/* Function to check if the memory address range falls beyond ddr region.
1244 * start: Start of the memory region
1245 * size: Size of the memory region
1246 */
1247int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1248{
1249 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1250 uint64_t ddr_size = smem_get_ddr_size();
1251 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1252 uintptr_t pa_start_addr = PA(start);
1253
1254 /* Check for boundary conditions. */
1255 if ((UINT_MAX - start) < size)
1256 return -1;
1257
1258 /* Check if memory range is beyond the ddr range. */
1259 if (pa_start_addr < ddr_pa_start_addr ||
1260 pa_start_addr >= (ddr_pa_end_addr) ||
1261 (pa_start_addr + size) > ddr_pa_end_addr)
1262 return -1;
1263 else
1264 return 0;
1265}
1266
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001267BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001268
Mayank Groverdd080e82018-09-04 20:12:13 +05301269int getimage(void **image_buffer, uint32_t *imgsize,
1270 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301271{
Mayank Groverdd080e82018-09-04 20:12:13 +05301272 uint32_t loadedindex;
1273 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301274 imgname == NULL) {
1275 dprintf(CRITICAL, "getimage: invalid parameters\n");
1276 return -1;
1277 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301278 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1279 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301280 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301281 *image_buffer = info.images[loadedindex].image_buffer;
1282 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301283 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1284 info.images[loadedindex].name,
1285 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301286 return 0;
1287 }
1288 }
1289 return -1;
1290}
1291
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001292static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1293{
1294 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001295
1296#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001297#if IMAGE_VERIF_ALGO_SHA1
1298 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1299#else
1300 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1301#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001302#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001303
1304 /* Assume device is rooted at this time. */
1305 device.is_tampered = 1;
1306
1307 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1308
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001309#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301310 uint32_t bootstate;
1311 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301312 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301313 {
1314 ret = boot_verify_image((unsigned char *)bootimg_addr,
1315 bootimg_size, "/recovery", &bootstate);
1316 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001317 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301318 {
1319 ret = boot_verify_image((unsigned char *)bootimg_addr,
1320 bootimg_size, "/boot", &bootstate);
1321 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001322 boot_verify_print_state();
1323#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001324 ret = image_verify((unsigned char *)bootimg_addr,
1325 (unsigned char *)(bootimg_addr + bootimg_size),
1326 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001327 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001328#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001329 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1330
1331 if (ret)
1332 {
1333 /* Authorized kernel */
1334 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001335 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001336 }
1337
Amit Blay4aa292f2015-04-28 21:55:59 +03001338#ifdef MDTP_SUPPORT
1339 {
1340 /* Verify MDTP lock.
1341 * For boot & recovery partitions, use aboot's verification result.
1342 */
1343 mdtp_ext_partition_verification_t ext_partition;
1344 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1345 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1346 ext_partition.page_size = 0; /* Not needed since already validated */
1347 ext_partition.image_addr = 0; /* Not needed since already validated */
1348 ext_partition.image_size = 0; /* Not needed since already validated */
1349 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1350 mdtp_fwlock_verify_lock(&ext_partition);
1351 }
1352#endif /* MDTP_SUPPORT */
1353
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001354#if USE_PCOM_SECBOOT
1355 set_tamper_flag(device.is_tampered);
1356#endif
1357
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001358#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001359 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001360 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001361 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001362#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001363 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001364#if ENABLE_VB_ATTEST
1365 mdelay(DELAY_WAIT);
1366 shutdown_device();
1367#else
lijuanga40d6302015-07-20 20:10:13 +08001368 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001369#endif
lijuanga40d6302015-07-20 20:10:13 +08001370#else
1371 dprintf(CRITICAL,
1372 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1373 mdelay(5000);
1374#endif
1375
1376 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001377 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001378#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001379 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001380 wait_for_users_action();
1381#else
1382 dprintf(CRITICAL,
1383 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1384 mdelay(5000);
1385#endif
1386 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001387 default:
lijuanga40d6302015-07-20 20:10:13 +08001388 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001389 }
1390#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001391#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301392 if(device.is_tampered)
1393 {
1394 write_device_info_mmc(&device);
1395 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301396 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301397 #endif
1398 #ifdef ASSERT_ON_TAMPER
1399 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1400 ASSERT(0);
1401 #endif
1402 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001403#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001404}
1405
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301406static bool check_format_bit()
1407{
1408 bool ret = false;
1409 int index;
1410 uint64_t offset;
1411 struct boot_selection_info *in = NULL;
1412 char *buf = NULL;
1413
1414 index = partition_get_index("bootselect");
1415 if (index == INVALID_PTN)
1416 {
1417 dprintf(INFO, "Unable to locate /bootselect partition\n");
1418 return ret;
1419 }
1420 offset = partition_get_offset(index);
1421 if(!offset)
1422 {
1423 dprintf(INFO, "partition /bootselect doesn't exist\n");
1424 return ret;
1425 }
1426 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301427 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301428 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301429 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301430 {
1431 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1432 free(buf);
1433 return ret;
1434 }
1435 in = (struct boot_selection_info *) buf;
1436 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1437 (in->version == BOOTSELECT_VERSION)) {
1438 if ((in->state_info & BOOTSELECT_FORMAT) &&
1439 !(in->state_info & BOOTSELECT_FACTORY))
1440 ret = true;
1441 } else {
1442 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1443 in->signature, in->version);
1444 ASSERT(0);
1445 }
1446 free(buf);
1447 return ret;
1448}
1449
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001450void boot_verifier_init()
1451{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001452 uint32_t boot_state;
1453 /* Check if device unlock */
1454 if(device.is_unlocked)
1455 {
1456 boot_verify_send_event(DEV_UNLOCK);
1457 boot_verify_print_state();
1458 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1459 return;
1460 }
1461 else
1462 {
1463 boot_verify_send_event(BOOT_INIT);
1464 }
1465
1466 /* Initialize keystore */
1467 boot_state = boot_verify_keystore_init();
1468 if(boot_state == YELLOW)
1469 {
1470 boot_verify_print_state();
1471 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1472 }
1473}
1474
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301475/* Function to return recovery appended dtbo buffer info */
1476void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1477{
1478 *dtbo_size = recovery_dtbo_size;
1479 *dtbo_buf = recovery_dtbo_buf;
1480 return;
1481}
1482
Shashank Mittal23b8f422010-04-16 19:27:21 -07001483int boot_linux_from_mmc(void)
1484{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301485 boot_img_hdr *hdr = (void*) buf;
1486 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001487 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001488 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001489 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001490 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001491
Shashank Mittalcd98d472011-08-02 14:29:24 -07001492 unsigned char *image_addr = 0;
1493 unsigned kernel_actual;
1494 unsigned ramdisk_actual;
1495 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001496 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301497 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001498
Matthew Qin271927e2015-03-31 22:07:07 -04001499 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301500 unsigned dtb_image_size = 0;
1501 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001502 unsigned int out_len = 0;
1503 unsigned int out_avai_len = 0;
1504 unsigned char *out_addr = NULL;
1505 uint32_t dtb_offset = 0;
1506 unsigned char *kernel_start_addr = NULL;
1507 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001508 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301509 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001510 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301511#if VERIFIED_BOOT_2
1512 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301513 void *dtbo_image_buf = NULL;
1514 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301515 void *vbmeta_image_buf = NULL;
1516 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301517#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301518 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001519#if DEVICE_TREE
1520 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001521 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001522 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001523 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001524 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001525 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001526#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001527 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301528 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001529
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301530 if (check_format_bit())
1531 boot_into_recovery = 1;
1532
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001533 if (!boot_into_recovery) {
1534 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1535 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1536 if (rcode <= 0) {
1537 boot_into_ffbm = false;
1538 if (rcode < 0)
1539 dprintf(CRITICAL,"failed to get ffbm cookie");
1540 } else
1541 boot_into_ffbm = true;
1542 } else
1543 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301544 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001545 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1546 dprintf(INFO, "Unified boot method!\n");
1547 hdr = uhdr;
1548 goto unified_boot;
1549 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301550
1551 /* For a/b recovery image code is on boot partition.
1552 If we support multislot, always use boot partition. */
1553 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301554 ((!partition_multislot_is_supported()) ||
1555 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301556 ptn_name = "recovery";
1557 else
1558 ptn_name = "boot";
1559
1560 index = partition_get_index(ptn_name);
1561 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301562 image_size = partition_get_size(index);
1563 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301564 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1565 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001566 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301567
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001568 /* Set Lun for boot & recovery partitions */
1569 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001570
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301571 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001572 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1573 return -1;
1574 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001575
1576 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001577 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301578 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001579 }
1580
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001581 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301582
1583 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1584 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1585 return -1;
1586 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001587 page_size = hdr->page_size;
1588 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001589 }
1590
Matthew Qin49e51fa2015-02-09 10:40:45 +08001591 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1592 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301593 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001594
Matthew Qin49e51fa2015-02-09 10:40:45 +08001595 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301596#if VERIFIED_BOOT_2
1597 /* Create hole in start of image for VB salt to copy */
1598 image_addr += SALT_BUFF_OFFSET;
1599#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301600 memcpy(image_addr, (void *)buf, page_size);
1601
1602 /* ensure commandline is terminated */
1603 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001604
1605#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301606#ifndef OSVERSION_IN_BOOTIMAGE
1607 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301608#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301609 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301610#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301611
Parth Dixit4097b622016-03-15 14:42:27 +05301612 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301613 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + (uint64_t)dt_actual + page_size)) {
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301614 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1615 return -1;
1616 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301617 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001618#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301619 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual + (uint64_t)second_actual + page_size)) {
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301620 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1621 return -1;
1622 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301623 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001624#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301625 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001626
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301627#ifdef OSVERSION_IN_BOOTIMAGE
1628 /* If header version is ONE and booting into recovery,
1629 dtbo is appended with recovery image.
1630 Doing following:
1631 * Validating the recovery offset and size.
1632 * Extracting recovery dtbo to be used as dtbo.
1633 */
1634 if (boot_into_recovery &&
1635 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1636 {
1637 struct boot_img_hdr_v1 *hdr1 =
1638 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301639 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301640
Mayank Grovera1a83c72018-09-24 11:23:15 +05301641 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301642 if ((hdr1->header_size !=
1643 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1644 {
1645 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1646 return -1;
1647 }
1648
Mayank Grovera1a83c72018-09-24 11:23:15 +05301649 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1650 {
1651 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301652 MAX_SUPPORTED_DTBO_IMG_BUF);
1653 return -1;
1654 }
1655
Mayank Grovera1a83c72018-09-24 11:23:15 +05301656 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301657 {
1658 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1659 return -1;
1660 }
1661
Mayank Grovera1a83c72018-09-24 11:23:15 +05301662 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1663 dprintf(CRITICAL,
1664 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
1665 return -1;
1666 }
1667
1668 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301669 {
1670 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
1671 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
1672 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
1673 return -1;
1674 }
1675
1676 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05301677 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301678 imagesize_actual += recovery_dtbo_size;
1679
1680 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1681 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
1682 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
1683
1684 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301685
1686 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
1687 struct boot_img_hdr_v1 *hdr1 =
1688 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
1689 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
1690 (image_addr + sizeof(boot_img_hdr) +
1691 BOOT_IMAGE_HEADER_V2_OFFSET);
1692 unsigned int recovery_dtbo_actual = 0;
1693
1694 imagesize_actual += ROUND_TO_PAGE(hdr1->recovery_dtbo_size,
1695 page_mask);
1696
1697 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
1698
1699
1700 dtb_image_offset = page_size + patched_kernel_hdr_size +
1701 kernel_actual + ramdisk_actual + second_actual +
1702 recovery_dtbo_actual;
1703
1704 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1705 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
1706 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301707#endif
1708
Parth Dixit1375d9e2019-07-08 20:56:13 +05301709
Saranya Chidurab1aaf232018-11-19 15:47:50 +05301710 /* Validate the boot/recovery image size is within the bounds of partition size */
1711 if (imagesize_actual > image_size) {
1712 dprintf(CRITICAL, "Image size is greater than partition size.\n");
1713 return -1;
1714 }
1715
Matthew Qin49e51fa2015-02-09 10:40:45 +08001716#if VERIFIED_BOOT
1717 boot_verifier_init();
1718#endif
1719
Mayank Grover1ceaee22019-04-01 17:54:32 +05301720#if VERIFIED_BOOT_2
1721 /* read full partition if device is unlocked */
1722 if (device.is_unlocked)
1723 imagesize_actual = image_size;
1724#endif
1725
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301726 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001727 {
1728 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1729 return -1;
1730 }
1731
Matthew Qinbb7923d2015-02-09 10:56:09 +08001732 /*
1733 * Update loading flow of bootimage to support compressed/uncompressed
1734 * bootimage on both 64bit and 32bit platform.
1735 * 1. Load bootimage from emmc partition onto DDR.
1736 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1737 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1738 * platform accordingly.
1739 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1740 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301741 if (partition_multislot_is_supported())
1742 {
1743 current_active_slot = partition_find_active_slot();
1744 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1745 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1746 }
1747 else
1748 {
1749 dprintf(INFO, "Loading (%s) image (%d): start\n",
1750 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1751 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001752 bs_set_timestamp(BS_KERNEL_LOAD_START);
1753
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301754 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1755 {
1756 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1757 return -1;
1758 }
Kishor PK9e91b592017-05-24 12:14:55 +05301759 offset = page_size;
1760 /* Read image without signature and header*/
1761 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001762 {
1763 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1764 return -1;
1765 }
1766
Mayank Grover351a75e2017-05-30 20:06:08 +05301767 if (partition_multislot_is_supported())
1768 {
1769 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1770 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1771 }
1772 else
1773 {
1774 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001775 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1776
Mayank Grover351a75e2017-05-30 20:06:08 +05301777 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001778 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1779
1780 /* Authenticate Kernel */
1781 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1782 (int) target_use_signed_kernel(),
1783 device.is_unlocked,
1784 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301785#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05301786 /* if device is unlocked skip reading signature, as full partition is read */
1787 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05301788 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05301789 offset = imagesize_actual;
1790 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1791 {
1792 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1793 return -1;
1794 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001795
Mayank Grover1ceaee22019-04-01 17:54:32 +05301796 /* Read signature */
1797 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1798 {
1799 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1800 return -1;
1801 }
Monika Singh292b3e92018-03-17 22:40:23 +05301802 }
1803
Mayank Grover027a9412018-09-04 15:12:05 +05301804 /* load and validate dtbo partition */
1805 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
1806
Mayank Grover9df84c02018-08-30 15:46:35 +05301807 /* load vbmeta partition */
1808 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
1809
Monika Singh292b3e92018-03-17 22:40:23 +05301810 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05301811
1812 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05301813 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05301814 info.images[IMG_BOOT].imgsize = imagesize_actual;
1815 info.images[IMG_BOOT].name = ptn_name;
1816 ++info.num_loaded_images;
1817
1818 /* Pass loaded dtbo image */
1819 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05301820 info.images[IMG_DTBO].image_buffer =
1821 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05301822 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
1823 info.images[IMG_DTBO].name = "dtbo";
1824 ++info.num_loaded_images;
1825 }
1826
Mayank Grover9df84c02018-08-30 15:46:35 +05301827 /* Pass loaded vbmeta image */
1828 if (vbmeta_image_buf != NULL) {
1829 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
1830 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
1831 info.images[IMG_VBMETA].name = "vbmeta";
1832 ++info.num_loaded_images;
1833 }
1834
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301835 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05301836 info.multi_slot_boot = partition_multislot_is_supported();
1837 info.bootreason_alarm = boot_reason_alarm;
1838 info.bootinto_recovery = boot_into_recovery;
1839 status = load_image_and_auth(&info);
1840 if(status)
1841 return -1;
1842
1843 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05301844
1845 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05301846 if (vbmeta_image_buf != NULL) {
1847 free(vbmeta_image_buf);
1848 --info.num_loaded_images;
1849 }
Monika Singh292b3e92018-03-17 22:40:23 +05301850#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001851 /* Change the condition a little bit to include the test framework support.
1852 * We would never reach this point if device is in fastboot mode, even if we did
1853 * that means we are in test mode, so execute kernel authentication part for the
1854 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301855 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001856 {
1857 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301858 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001859 {
1860 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1861 return -1;
1862 }
1863
1864 /* Read signature */
1865 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1866 {
1867 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1868 return -1;
1869 }
1870
1871 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001872 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301873 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001874 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001875 } else {
1876 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1877 #ifdef TZ_SAVE_KERNEL_HASH
1878 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1879 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001880
1881#ifdef MDTP_SUPPORT
1882 {
1883 /* Verify MDTP lock.
1884 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1885 * since verification was skipped in aboot. The signature is not part of the loaded image.
1886 */
1887 mdtp_ext_partition_verification_t ext_partition;
1888 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1889 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1890 ext_partition.page_size = page_size;
1891 ext_partition.image_addr = (uint32)image_addr;
1892 ext_partition.image_size = imagesize_actual;
1893 ext_partition.sig_avail = FALSE;
1894 mdtp_fwlock_verify_lock(&ext_partition);
1895 }
1896#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001897 }
Monika Singh292b3e92018-03-17 22:40:23 +05301898#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001899
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001900#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001901 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001902 {
1903#if FBCON_DISPLAY_MSG
1904 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1905 wait_for_users_action();
1906#else
1907 dprintf(CRITICAL,
1908 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1909 mdelay(5000);
1910#endif
1911 }
1912#endif
1913
1914#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301915 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301916 {
1917 /* set boot and system versions. */
1918 set_os_version((unsigned char *)image_addr);
1919 // send root of trust
1920 if(!send_rot_command((uint32_t)device.is_unlocked))
1921 ASSERT(0);
1922 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301923#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001924 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001925 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1926 * If not, continue booting.
1927 */
1928 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1929 {
1930 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1931 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05301932#if VERIFIED_BOOT_2
1933 if (dtbo_image_sz)
1934 out_avai_len -= DTBO_IMG_BUF;
1935#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04001936 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001937 rc = decompress((unsigned char *)(image_addr + page_size),
1938 hdr->kernel_size, out_addr, out_avai_len,
1939 &dtb_offset, &out_len);
1940 if (rc)
1941 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001942 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001943 ASSERT(0);
1944 }
1945
Matthew Qin0b15b322015-05-19 05:20:54 -04001946 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001947 kptr = (struct kernel64_hdr *)out_addr;
1948 kernel_start_addr = out_addr;
1949 kernel_size = out_len;
1950 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001951 dprintf(INFO, "Uncpmpressed kernel in use\n");
1952 if (!strncmp((char*)(image_addr + page_size),
1953 PATCHED_KERNEL_MAGIC,
1954 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1955 dprintf(INFO, "Patched kernel detected\n");
1956 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1957 PATCHED_KERNEL_HEADER_SIZE);
1958 //The size of the kernel is stored at start of kernel image + 16
1959 //The dtb would start just after the kernel
1960 dtb_offset = *((uint32_t*)((unsigned char*)
1961 (image_addr + page_size +
1962 sizeof(PATCHED_KERNEL_MAGIC) -
1963 1)));
1964 //The actual kernel starts after the 20 byte header.
1965 kernel_start_addr = (unsigned char*)(image_addr +
1966 page_size + PATCHED_KERNEL_HEADER_SIZE);
1967 kernel_size = hdr->kernel_size;
1968 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1969 } else {
1970 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1971 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1972 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1973 kernel_size = hdr->kernel_size;
1974 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001975 }
1976
1977 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001978 * Update the kernel/ramdisk/tags address if the boot image header
1979 * has default values, these default values come from mkbootimg when
1980 * the boot image is flashed using fastboot flash:raw
1981 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001982 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001983
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001984 /* Get virtual addresses since the hdr saves physical addresses. */
1985 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1986 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1987 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001988
Matthew Qinbb7923d2015-02-09 10:56:09 +08001989 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001990 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001991 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301992 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1993 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1994 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001995 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301996 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001997 return -1;
1998 }
1999
2000#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302001 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2002 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002003 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302004 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002005 return -1;
2006 }
2007#endif
2008
Matthew Qin49e51fa2015-02-09 10:40:45 +08002009 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002010 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002011 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002012
Matthew Qin49e51fa2015-02-09 10:40:45 +08002013 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302014 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002015 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2016 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002017
Matthew Qin49e51fa2015-02-09 10:40:45 +08002018 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2019 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2020 return -1;
2021 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002022
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302023 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2024 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302025 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302026 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2027 return -1;
2028 }
2029
Matthew Qin49e51fa2015-02-09 10:40:45 +08002030 /* Find index of device tree within device tree table */
2031 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2032 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2033 return -1;
2034 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002035
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302036 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2037 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2038 return -1;
2039 }
2040
2041 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302042 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302043 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2044 return -1;
2045 }
2046
Matthew Qin271927e2015-03-31 22:07:07 -04002047 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2048 {
2049 unsigned int compressed_size = 0;
2050 out_addr += out_len;
2051 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002052 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002053 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2054 dt_entry.size, out_addr, out_avai_len,
2055 &compressed_size, &dtb_size);
2056 if (rc)
2057 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002058 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002059 ASSERT(0);
2060 }
2061
Matthew Qin0b15b322015-05-19 05:20:54 -04002062 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002063 best_match_dt_addr = out_addr;
2064 } else {
2065 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2066 dtb_size = dt_entry.size;
2067 }
2068
Matthew Qin49e51fa2015-02-09 10:40:45 +08002069 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302070 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2071 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002072 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302073 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002074 return -1;
2075 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002076
Matthew Qin271927e2015-03-31 22:07:07 -04002077 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002078 } else {
2079 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302080 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2081 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002082 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302083 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002084 return -1;
2085 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002086 /*
2087 * If appended dev tree is found, update the atags with
2088 * memory address to the DTB appended location on RAM.
2089 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302090 *
2091 * Make sure everything from scratch address is read before next step!
2092 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002093 */
2094 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302095 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2096
2097 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2098
2099 image_buf = (void*)(image_addr);
2100 dtb_offset = dtb_image_offset;
2101 dtb_image_size = imagesize_actual;
2102 }
2103
2104 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002105 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002106 if (!dtb) {
2107 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002108 return -1;
2109 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002110 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002111 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002112
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002113 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2114 target_load_ssd_keystore();
2115
Shashank Mittal23b8f422010-04-16 19:27:21 -07002116unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002117
Dima Zavin77e41f32013-03-06 16:10:43 -08002118 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002119 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002120 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2121
2122 return 0;
2123}
2124
Dima Zavin214cc642009-01-26 11:16:21 -08002125int boot_linux_from_flash(void)
2126{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302127 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002128 struct ptentry *ptn;
2129 struct ptable *ptable;
2130 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002131
Shashank Mittalcd98d472011-08-02 14:29:24 -07002132 unsigned char *image_addr = 0;
2133 unsigned kernel_actual;
2134 unsigned ramdisk_actual;
2135 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302136 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002137
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002138#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302139 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002140 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302141 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002142 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302143 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302144 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302145 unsigned int dtb_size = 0;
2146 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002147#endif
2148
David Ng183a7422009-12-07 14:55:21 -08002149 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302150 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002151 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2152 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2153 return -1;
2154 }
2155 goto continue_boot;
2156 }
2157
Dima Zavin214cc642009-01-26 11:16:21 -08002158 ptable = flash_get_ptable();
2159 if (ptable == NULL) {
2160 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2161 return -1;
2162 }
2163
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002164 if(!boot_into_recovery)
2165 {
2166 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002167
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002168 if (ptn == NULL) {
2169 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2170 return -1;
2171 }
2172 }
2173 else
2174 {
2175 ptn = ptable_find(ptable, "recovery");
2176 if (ptn == NULL) {
2177 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2178 return -1;
2179 }
Dima Zavin214cc642009-01-26 11:16:21 -08002180 }
2181
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302182 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002183 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002184 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2185 return -1;
2186 }
Dima Zavin214cc642009-01-26 11:16:21 -08002187
2188 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002189 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002190 return -1;
2191 }
2192
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002193 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002194 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 -08002195 return -1;
2196 }
2197
Kishor PK9e91b592017-05-24 12:14:55 +05302198 image_addr = (unsigned char *)target_get_scratch_address();
2199 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302200
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002201 /*
2202 * Update the kernel/ramdisk/tags address if the boot image header
2203 * has default values, these default values come from mkbootimg when
2204 * the boot image is flashed using fastboot flash:raw
2205 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002206 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002207
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002208 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002209 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2210 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2211 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2212
2213 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2214 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302215 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002216
Kishor PK9e91b592017-05-24 12:14:55 +05302217 /* ensure commandline is terminated */
2218 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2219
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002220 /* Check if the addresses in the header are valid. */
2221 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302222 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2223 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2224 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002225 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302226 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002227 return -1;
2228 }
2229
2230#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302231 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302232 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2233 return -1;
2234 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302235 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302236
Mayank Grovere559cec2017-10-17 15:12:03 +05302237 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2238 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302239 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302240 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302241 return -1;
2242 }
2243#else
2244
2245#ifndef OSVERSION_IN_BOOTIMAGE
2246 dt_size = hdr->dt_size;
2247#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302248 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302249 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 +05302250 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2251 return -1;
2252 }
2253
Kishor PKd8ddcad2017-07-27 13:53:57 +05302254 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302255
Mayank Grovere559cec2017-10-17 15:12:03 +05302256 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2257 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302258 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302259 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302260 return -1;
2261 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002262#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002263
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302264 /* Read full boot.img from flash */
2265 dprintf(INFO, "Loading (%s) image (%d): start\n",
2266 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2267 bs_set_timestamp(BS_KERNEL_LOAD_START);
2268
2269 if (UINT_MAX - page_size < imagesize_actual)
2270 {
2271 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2272 return -1;
2273 }
2274
2275 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2276 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2277 {
2278 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2279 return -1;
2280 }
2281
2282 offset = page_size;
2283 /* Read image without signature and header */
2284 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2285 {
2286 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2287 return -1;
2288 }
2289
2290 dprintf(INFO, "Loading (%s) image (%d): done\n",
2291 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2292 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2293
Shashank Mittalcd98d472011-08-02 14:29:24 -07002294 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002295 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002296 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002297 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302298
Shashank Mittalcd98d472011-08-02 14:29:24 -07002299 /* Read signature */
2300 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2301 {
2302 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002303 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002304 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002305
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302306 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302307 }
2308 offset = page_size;
2309 if(hdr->second_size != 0) {
2310 if (UINT_MAX - offset < second_actual)
2311 {
2312 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2313 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302314 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302315 offset += second_actual;
2316 /* Second image loading not implemented. */
2317 ASSERT(0);
2318 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302319 /* Move kernel and ramdisk to correct address */
2320 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2321 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2322
2323#if DEVICE_TREE
2324 if(dt_size != 0) {
2325
2326 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2327
2328 table = (struct dt_table*) dt_table_offset;
2329
2330 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2331 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2332 return -1;
2333 }
2334
2335 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2336 goes beyound hdr->dt_size*/
2337 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2338 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2339 return -1;
2340 }
2341
2342 /* Find index of device tree within device tree table */
2343 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2344 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2345 return -1;
2346 }
2347
2348 /* Validate and Read device device tree in the "tags_add */
2349 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2350 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2351 {
2352 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2353 return -1;
2354 }
2355
2356 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2357 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2358 return -1;
2359 }
2360
2361 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2362 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2363 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2364 return -1;
2365 }
2366
2367 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2368 dtb_size = dt_entry.size;
2369 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302370
2371 } else {
2372 /* Validate the tags_addr */
2373 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2374 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2375 {
2376 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2377 return -1;
2378 }
2379 /*
2380 * If appended dev tree is found, update the atags with
2381 * memory address to the DTB appended location on RAM.
2382 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302383 *
2384 * Make sure everything from scratch address is read before next step!
2385 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302386 */
2387 void *dtb = NULL;
2388 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2389 if (!dtb) {
2390 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2391 return -1;
2392 }
2393 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302394#endif
2395 if(target_use_signed_kernel() && (!device.is_unlocked))
2396 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002397 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002398 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002399 {
2400 write_device_info_flash(&device);
2401 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302402#if USE_PCOM_SECBOOT
2403 set_tamper_flag(device.is_tampered);
2404#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002405 }
David Ng183a7422009-12-07 14:55:21 -08002406continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002407
Dima Zavin214cc642009-01-26 11:16:21 -08002408 /* TODO: create/pass atags to kernel */
2409
Ajay Dudanie28a6072011-07-01 13:59:46 -07002410 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002411 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002412 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2413
2414 return 0;
2415}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002416
Shashank Mittal162244e2011-08-08 19:01:25 -07002417void write_device_info_mmc(device_info *dev)
2418{
Shashank Mittal162244e2011-08-08 19:01:25 -07002419 unsigned long long ptn = 0;
2420 unsigned long long size;
2421 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002422 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002423 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302424 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002425
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002426 if (devinfo_present)
2427 index = partition_get_index("devinfo");
2428 else
2429 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002430
Shashank Mittal162244e2011-08-08 19:01:25 -07002431 ptn = partition_get_offset(index);
2432 if(ptn == 0)
2433 {
2434 return;
2435 }
2436
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002437 lun = partition_get_lun(index);
2438 mmc_set_lun(lun);
2439
Shashank Mittal162244e2011-08-08 19:01:25 -07002440 size = partition_get_size(index);
2441
Mayank Groverddd348d2018-01-23 14:07:09 +05302442 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2443 mmc_blocksize_mask);
2444 if (device_info_sz == UINT_MAX)
2445 {
2446 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2447 return;
2448 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002449
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002450 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302451 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002452 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302453 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002454 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002455 {
2456 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002457 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002458 }
2459}
2460
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002461void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002462{
Shashank Mittal162244e2011-08-08 19:01:25 -07002463 unsigned long long ptn = 0;
2464 unsigned long long size;
2465 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002466 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302467 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002468
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002469 if ((index = partition_get_index("devinfo")) < 0)
2470 {
2471 devinfo_present = false;
2472 index = partition_get_index("aboot");
2473 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002474
Shashank Mittal162244e2011-08-08 19:01:25 -07002475 ptn = partition_get_offset(index);
2476 if(ptn == 0)
2477 {
2478 return;
2479 }
2480
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002481 mmc_set_lun(partition_get_lun(index));
2482
Shashank Mittal162244e2011-08-08 19:01:25 -07002483 size = partition_get_size(index);
2484
Mayank Groverddd348d2018-01-23 14:07:09 +05302485 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2486 mmc_blocksize_mask);
2487 if (device_info_sz == UINT_MAX)
2488 {
2489 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2490 return;
2491 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002492
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002493 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302494 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002495 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302496 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002497 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002498 {
2499 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002500 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002501 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002502}
2503
2504void write_device_info_flash(device_info *dev)
2505{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002506 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002507 struct ptentry *ptn;
2508 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002509 if(info == NULL)
2510 {
2511 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2512 ASSERT(0);
2513 }
2514 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002515 ptable = flash_get_ptable();
2516 if (ptable == NULL)
2517 {
2518 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2519 return;
2520 }
2521
2522 ptn = ptable_find(ptable, "devinfo");
2523 if (ptn == NULL)
2524 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002525 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002526 return;
2527 }
2528
Mayank Groverdedbc892017-10-24 13:41:34 +05302529 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002530 memcpy(info, dev, sizeof(device_info));
2531
2532 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2533 {
2534 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2535 return;
2536 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002537 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002538}
2539
vijay kumarc65876c2015-04-24 13:29:16 +05302540static int read_allow_oem_unlock(device_info *dev)
2541{
vijay kumarc65876c2015-04-24 13:29:16 +05302542 unsigned offset;
2543 int index;
2544 unsigned long long ptn;
2545 unsigned long long ptn_size;
2546 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002547 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302548
vijay kumarca2e6812015-07-08 20:28:25 +05302549 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302550 if (index == INVALID_PTN)
2551 {
vijay kumarca2e6812015-07-08 20:28:25 +05302552 index = partition_get_index(frp_ptns[1]);
2553 if (index == INVALID_PTN)
2554 {
2555 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2556 return -1;
2557 }
vijay kumarc65876c2015-04-24 13:29:16 +05302558 }
2559
2560 ptn = partition_get_offset(index);
2561 ptn_size = partition_get_size(index);
2562 offset = ptn_size - blocksize;
2563
Mayank Grover48860402016-11-29 12:34:53 +05302564 /* Set Lun for partition */
2565 mmc_set_lun(partition_get_lun(index));
2566
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002567 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302568 {
2569 dprintf(CRITICAL, "Reading MMC failed\n");
2570 return -1;
2571 }
2572
2573 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2574 is_allow_unlock = buf[blocksize-1] & 0x01;
2575 return 0;
2576}
2577
2578static int write_allow_oem_unlock(bool allow_unlock)
2579{
vijay kumarc65876c2015-04-24 13:29:16 +05302580 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302581 int index;
2582 unsigned long long ptn;
2583 unsigned long long ptn_size;
2584 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002585 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302586
vijay kumarca2e6812015-07-08 20:28:25 +05302587 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302588 if (index == INVALID_PTN)
2589 {
vijay kumarca2e6812015-07-08 20:28:25 +05302590 index = partition_get_index(frp_ptns[1]);
2591 if (index == INVALID_PTN)
2592 {
2593 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2594 return -1;
2595 }
vijay kumarc65876c2015-04-24 13:29:16 +05302596 }
2597
2598 ptn = partition_get_offset(index);
2599 ptn_size = partition_get_size(index);
2600 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302601 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302602
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002603 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302604 {
2605 dprintf(CRITICAL, "Reading MMC failed\n");
2606 return -1;
2607 }
2608
2609 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2610 buf[blocksize-1] = allow_unlock;
2611 if (mmc_write(ptn + offset, blocksize, buf))
2612 {
2613 dprintf(CRITICAL, "Writing MMC failed\n");
2614 return -1;
2615 }
2616
2617 return 0;
2618}
2619
Shashank Mittal162244e2011-08-08 19:01:25 -07002620void read_device_info_flash(device_info *dev)
2621{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002622 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002623 struct ptentry *ptn;
2624 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002625 if(info == NULL)
2626 {
2627 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2628 ASSERT(0);
2629 }
2630 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002631 ptable = flash_get_ptable();
2632 if (ptable == NULL)
2633 {
2634 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2635 return;
2636 }
2637
2638 ptn = ptable_find(ptable, "devinfo");
2639 if (ptn == NULL)
2640 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002641 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002642 return;
2643 }
2644
2645 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2646 {
2647 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2648 return;
2649 }
2650
2651 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2652 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002653 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2654 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002655 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002656 write_device_info_flash(info);
2657 }
2658 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002659 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002660}
2661
2662void write_device_info(device_info *dev)
2663{
2664 if(target_is_emmc_boot())
2665 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002666 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2667 if(info == NULL)
2668 {
2669 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2670 ASSERT(0);
2671 }
2672 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002673 memcpy(info, dev, sizeof(struct device_info));
2674
2675#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302676 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302677 is_secure_boot_enable()) {
2678 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2679 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002680 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002681 else
2682 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002683#else
2684 write_device_info_mmc(info);
2685#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002686 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002687 }
2688 else
2689 {
2690 write_device_info_flash(dev);
2691 }
2692}
2693
Monika Singh94316462018-03-15 18:39:01 +05302694int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2695{
2696 if (!devinfo_present) {
2697 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2698 return -EINVAL;
2699 }
2700 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2701 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2702 __func__, loc, ARRAY_SIZE(device.rollback_index));
2703 ASSERT(0);
2704 }
2705
2706 *roll_back_index = device.rollback_index[loc];
2707 return 0;
2708}
2709
2710int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2711{
2712 if (!devinfo_present) {
2713 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2714 return -EINVAL;
2715 }
2716 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2717 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2718 __func__, loc, ARRAY_SIZE(device.rollback_index));
2719 ASSERT(0);
2720 }
2721
2722 device.rollback_index[loc] = roll_back_index;
2723 write_device_info(&device);
2724 return 0;
2725}
2726
Monika Singhb0fad822018-03-15 18:50:55 +05302727int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2728{
2729 if (!devinfo_present) {
2730 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2731 return -EINVAL;
2732 }
2733
2734 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2735 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2736 return -ENODEV;
2737 }
2738
2739 memcpy(device.user_public_key, user_key, user_key_size);
2740 device.user_public_key_length = user_key_size;
2741 write_device_info(&device);
2742 return 0;
2743}
2744
2745int erase_userkey()
2746{
2747 if (!devinfo_present) {
2748 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2749 return -EINVAL;
2750 }
2751 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2752 device.user_public_key_length = 0;
2753 write_device_info(&device);
2754 return 0;
2755}
2756
2757int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2758{
2759 if (!devinfo_present) {
2760 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2761 return -EINVAL;
2762 }
2763 *user_key = device.user_public_key;
2764 *user_key_size = device.user_public_key_length;
2765 return 0;
2766}
2767
Shashank Mittal162244e2011-08-08 19:01:25 -07002768void read_device_info(device_info *dev)
2769{
2770 if(target_is_emmc_boot())
2771 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002772 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2773 if(info == NULL)
2774 {
2775 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2776 ASSERT(0);
2777 }
2778 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002779
2780#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302781 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302782 is_secure_boot_enable()) {
2783 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2784 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002785 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002786 else
2787 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002788#else
2789 read_device_info_mmc(info);
2790#endif
2791
2792 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2793 {
2794 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002795 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002796 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302797#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302798 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302799 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302800#endif
lijuang511a2b52015-08-14 20:50:51 +08002801 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002802 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302803#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302804 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302805 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302806#endif
lijuang511a2b52015-08-14 20:50:51 +08002807 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002808 info->is_tampered = 0;
2809 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302810#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302811 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302812 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302813#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002814 write_device_info(info);
2815 }
2816 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002817 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002818 }
2819 else
2820 {
2821 read_device_info_flash(dev);
2822 }
2823}
2824
2825void reset_device_info()
2826{
2827 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002828 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002829 write_device_info(&device);
2830}
2831
2832void set_device_root()
2833{
2834 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002835 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002836 write_device_info(&device);
2837}
2838
lijuang4ece1e72015-08-14 21:02:36 +08002839/* set device unlock value
2840 * Must check FRP before call this function
2841 * Need to wipe data when unlock status changed
2842 * type 0: oem unlock
2843 * type 1: unlock critical
2844 * status 0: unlock as false
2845 * status 1: lock as true
2846 */
2847void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002848{
lijuang4ece1e72015-08-14 21:02:36 +08002849 if (type == UNLOCK)
2850 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302851#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302852 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302853 type == UNLOCK_CRITICAL)
2854 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302855#endif
lijuang4ece1e72015-08-14 21:02:36 +08002856 write_device_info(&device);
2857}
2858
2859static void set_device_unlock(int type, bool status)
2860{
2861 int is_unlocked = -1;
2862 char response[MAX_RSP_SIZE];
2863
2864 /* check device unlock status if it is as expected */
2865 if (type == UNLOCK)
2866 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302867#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302868 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302869 type == UNLOCK_CRITICAL)
2870 {
2871 is_unlocked = device.is_unlock_critical;
2872 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302873#endif
lijuang4ece1e72015-08-14 21:02:36 +08002874 if (is_unlocked == status) {
2875 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2876 fastboot_info(response);
2877 fastboot_okay("");
2878 return;
2879 }
2880
2881 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002882 if (status && !is_allow_unlock) {
2883 fastboot_fail("oem unlock is not allowed");
2884 return;
2885 }
lijuang21f12f52015-08-22 16:22:19 +08002886
lijuang4ece1e72015-08-14 21:02:36 +08002887#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002888 display_unlock_menu(type, status);
2889 fastboot_okay("");
2890 return;
lijuang4ece1e72015-08-14 21:02:36 +08002891#else
lijuang07c5d6e2018-04-23 18:32:13 +08002892 if (status && type == UNLOCK) {
2893 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2894 return;
lijuang21f12f52015-08-22 16:22:19 +08002895 }
lijuang07c5d6e2018-04-23 18:32:13 +08002896#endif
lijuang4ece1e72015-08-14 21:02:36 +08002897
2898 set_device_unlock_value(type, status);
2899
2900 /* wipe data */
2901 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302902 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002903 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2904 write_misc(0, &msg, sizeof(msg));
2905
2906 fastboot_okay("");
2907 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002908}
2909
lijuang511a2b52015-08-14 20:50:51 +08002910static bool critical_flash_allowed(const char * entry)
2911{
2912 uint32_t i = 0;
2913 if (entry == NULL)
2914 return false;
2915
2916 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2917 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2918 return true;
2919 }
2920 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002921}
2922
2923#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002924int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2925{
2926 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002927 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302928 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002929 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302930 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002931 unsigned int compressed_size = 0;
2932 unsigned int dtb_size = 0;
2933 unsigned int out_avai_len = 0;
2934 unsigned char *out_addr = NULL;
2935 unsigned char *best_match_dt_addr = NULL;
2936 int rc;
2937
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302938 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07002939
Parth Dixit4097b622016-03-15 14:42:27 +05302940#ifndef OSVERSION_IN_BOOTIMAGE
2941 dt_size = hdr->dt_size;
2942#endif
2943
2944 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002945 /* add kernel offset */
2946 dt_image_offset += page_size;
2947 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2948 dt_image_offset += n;
2949
2950 /* add ramdisk offset */
2951 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2952 dt_image_offset += n;
2953
2954 /* add second offset */
2955 if(hdr->second_size != 0) {
2956 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2957 dt_image_offset += n;
2958 }
2959
2960 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002961 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002962
Deepa Dinamani19648b42013-09-05 17:05:55 -07002963 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002964 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2965 return -1;
2966 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302967
2968 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2969 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302970 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302971 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2972 return -1;
2973 }
2974
Joel Kingaa335dc2013-06-03 16:11:08 -07002975 /* Find index of device tree within device tree table */
2976 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002977 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2978 return -1;
2979 }
2980
Matthew Qin271927e2015-03-31 22:07:07 -04002981 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2982 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2983 {
2984 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2985 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002986 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002987 rc = decompress(best_match_dt_addr,
2988 dt_entry.size, out_addr, out_avai_len,
2989 &compressed_size, &dtb_size);
2990 if (rc)
2991 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002992 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002993 ASSERT(0);
2994 }
2995
Matthew Qin0b15b322015-05-19 05:20:54 -04002996 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002997 best_match_dt_addr = out_addr;
2998 } else {
2999 dtb_size = dt_entry.size;
3000 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003001 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303002 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3003 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003004 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303005 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003006 return -1;
3007 }
3008
Amol Jadicb524072012-08-09 16:40:18 -07003009 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003010 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003011 } else
3012 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003013
3014 /* Everything looks fine. Return success. */
3015 return 0;
3016}
3017#endif
3018
Brian Swetland9c4c0752009-01-25 16:23:50 -08003019void cmd_boot(const char *arg, void *data, unsigned sz)
3020{
3021 unsigned kernel_actual;
3022 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303023 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003024 uint32_t image_actual;
3025 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303026 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003027 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003028 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003029 int ret = 0;
3030 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003031 unsigned int out_len = 0;
3032 unsigned int out_avai_len = 0;
3033 unsigned char *out_addr = NULL;
3034 uint32_t dtb_offset = 0;
3035 unsigned char *kernel_start_addr = NULL;
3036 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003037 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303038#if VERIFIED_BOOT_2
3039 void *dtbo_image_buf = NULL;
3040 uint32_t dtbo_image_sz = 0;
3041 void *vbmeta_image_buf = NULL;
3042 uint32_t vbmeta_image_sz = 0;
3043#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303044#if !VERIFIED_BOOT_2
3045 uint32_t sig_actual = 0;
3046 uint32_t sig_size = 0;
3047#ifdef MDTP_SUPPORT
3048 static bool is_mdtp_activated = 0;
3049#endif /* MDTP_SUPPORT */
3050#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003051
lijuang2008ff22016-03-07 17:56:27 +08003052#if FBCON_DISPLAY_MSG
3053 /* Exit keys' detection thread firstly */
3054 exit_menu_keys_detection();
3055#endif
3056
Monika Singh292b3e92018-03-17 22:40:23 +05303057#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003058 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003059 {
3060 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003061 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003062 }
3063#endif
3064
Brian Swetland9c4c0752009-01-25 16:23:50 -08003065 if (sz < sizeof(hdr)) {
3066 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003067 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003068 }
3069
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303070 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003071
3072 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003073 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003074
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003075 if(target_is_emmc_boot() && hdr->page_size) {
3076 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003077 page_mask = page_size - 1;
3078 }
3079
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003080 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3081 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303082 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003083#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303084#ifndef OSVERSION_IN_BOOTIMAGE
3085 dt_size = hdr->dt_size;
3086#endif
3087 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003088#endif
3089
3090 image_actual = ADD_OF(page_size, kernel_actual);
3091 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303092 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003093 image_actual = ADD_OF(image_actual, dt_actual);
3094
Kishor PK5134b332017-05-09 17:50:08 +05303095 /* Checking to prevent oob access in read_der_message_length */
3096 if (image_actual > sz) {
3097 fastboot_fail("bootimage header fields are invalid");
3098 goto boot_failed;
3099 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303100
Monika Singh292b3e92018-03-17 22:40:23 +05303101#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303102 /* Pass size of boot partition, as imgsize, to avoid
3103 read fewer bytes error */
3104 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303105
3106 /* load and validate dtbo partition */
3107 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3108
3109 /* load vbmeta partition */
3110 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3111
Monika Singh292b3e92018-03-17 22:40:23 +05303112 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303113
3114 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3115 info.images[IMG_BOOT].imgsize = image_actual;
3116 info.images[IMG_BOOT].name = "boot";
3117 ++info.num_loaded_images;
3118
3119 /* Pass loaded dtbo image */
3120 if (dtbo_image_buf != NULL) {
3121 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3122 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3123 info.images[IMG_DTBO].name = "dtbo";
3124 ++info.num_loaded_images;
3125 }
3126
3127 /* Pass loaded vbmeta image */
3128 if (vbmeta_image_buf != NULL) {
3129 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3130 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3131 info.images[IMG_VBMETA].name = "vbmeta";
3132 ++info.num_loaded_images;
3133 }
3134
Monika Singh292b3e92018-03-17 22:40:23 +05303135 info.multi_slot_boot = partition_multislot_is_supported();
3136 if (load_image_and_auth(&info))
3137 goto boot_failed;
3138 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303139
3140 /* Free the buffer allocated to vbmeta post verification */
3141 if (vbmeta_image_buf != NULL) {
3142 free(vbmeta_image_buf);
3143 --info.num_loaded_images;
3144 }
Monika Singh292b3e92018-03-17 22:40:23 +05303145#else
Kishor PK5134b332017-05-09 17:50:08 +05303146 sig_size = sz - image_actual;
3147
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303148 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303149 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303150 /* Calculate the signature length from boot image */
3151 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303152 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303153 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003154
Monika Singh292b3e92018-03-17 22:40:23 +05303155 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303156 fastboot_fail("bootimage header fields are invalid");
3157 goto boot_failed;
3158 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003159 }
3160
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003161 // Initialize boot state before trying to verify boot.img
3162#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003163 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303164#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003165 /* Handle overflow if the input image size is greater than
3166 * boot image buffer can hold
3167 */
Monika Singh292b3e92018-03-17 22:40:23 +05303168 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003169 {
3170 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003171 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003172 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003173
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003174 /* Verify the boot image
3175 * device & page_size are initialized in aboot_init
3176 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003177 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003178 /* Pass size excluding signature size, otherwise we would try to
3179 * access signature beyond its length
3180 */
Monika Singh292b3e92018-03-17 22:40:23 +05303181 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003182 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003183#ifdef MDTP_SUPPORT
3184 else
3185 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003186 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003187 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003188
3189 if (!is_mdtp_activated) {
3190 ext_partition.partition = MDTP_PARTITION_NONE;
3191 mdtp_fwlock_verify_lock(&ext_partition);
3192 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003193 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003194
Amir Kotzer7c768c02016-04-13 09:08:05 +03003195 /* If mdtp state cannot be validate, block fastboot boot*/
3196 if(mdtp_activated(&is_mdtp_activated)){
3197 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3198 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3199 goto boot_failed;
3200 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003201 if(is_mdtp_activated){
3202 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003203 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003204 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003205#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303206#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003207
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003208#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303209 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303210 {
3211 /* set boot and system versions. */
3212 set_os_version((unsigned char *)data);
3213 // send root of trust
3214 if(!send_rot_command((uint32_t)device.is_unlocked))
3215 ASSERT(0);
3216 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303217#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003218 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003219 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3220 * If not, continue booting.
3221 */
3222 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3223 {
3224 out_addr = (unsigned char *)target_get_scratch_address();
3225 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3226 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303227#if VERIFIED_BOOT_2
3228 if (dtbo_image_sz)
3229 out_avai_len -= DTBO_IMG_BUF;
3230#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003231 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003232 ret = decompress((unsigned char *)(ptr + page_size),
3233 hdr->kernel_size, out_addr, out_avai_len,
3234 &dtb_offset, &out_len);
3235 if (ret)
3236 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003237 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003238 ASSERT(0);
3239 }
3240
Matthew Qin0b15b322015-05-19 05:20:54 -04003241 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003242 kptr = (struct kernel64_hdr *)out_addr;
3243 kernel_start_addr = out_addr;
3244 kernel_size = out_len;
3245 } else {
3246 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3247 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3248 kernel_size = hdr->kernel_size;
3249 }
3250
3251 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003252 * Update the kernel/ramdisk/tags address if the boot image header
3253 * has default values, these default values come from mkbootimg when
3254 * the boot image is flashed using fastboot flash:raw
3255 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003256 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003257
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003258 /* Get virtual addresses since the hdr saves physical addresses. */
3259 hdr->kernel_addr = VA(hdr->kernel_addr);
3260 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3261 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003262
Matthew Qinbb7923d2015-02-09 10:56:09 +08003263 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003264 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003265 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303266 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3267 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3268 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003269 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303270 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003271 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003272 }
3273
Amol Jadicb524072012-08-09 16:40:18 -07003274#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003275 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003276 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003277 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003278
3279 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003280#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303281 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3282 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003283 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303284 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003285 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003286 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003287#endif
3288
3289 /* Load ramdisk & kernel */
3290 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003291 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003292
3293#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303294 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3295 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003296 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303297 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003298 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003299 }
3300
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003301 /*
3302 * If dtb is not found look for appended DTB in the kernel.
3303 * If appended dev tree is found, update the atags with
3304 * memory address to the DTB appended location on RAM.
3305 * Else update with the atags address in the kernel header
3306 */
3307 if (!dtb_copied) {
3308 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003309 dtb = dev_tree_appended((void*)(ptr + page_size),
3310 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003311 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003312 if (!dtb) {
3313 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003314 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003315 }
Amol Jadicb524072012-08-09 16:40:18 -07003316 }
3317#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003318
3319 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003320 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003321
Dima Zavin77e41f32013-03-06 16:10:43 -08003322 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003323 (const char*) hdr->cmdline, board_machtype(),
3324 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003325
3326 /* fastboot already stop, it's no need to show fastboot menu */
3327 return;
3328boot_failed:
3329#if FBCON_DISPLAY_MSG
3330 /* revert to fastboot menu if boot failed */
3331 display_fastboot_menu();
3332#endif
3333 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003334}
3335
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003336void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003337{
3338 struct ptentry *ptn;
3339 struct ptable *ptable;
3340
3341 ptable = flash_get_ptable();
3342 if (ptable == NULL) {
3343 fastboot_fail("partition table doesn't exist");
3344 return;
3345 }
3346
3347 ptn = ptable_find(ptable, arg);
3348 if (ptn == NULL) {
3349 fastboot_fail("unknown partition name");
3350 return;
3351 }
3352
Monika Singh292b3e92018-03-17 22:40:23 +05303353 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3354 dprintf(INFO, "erasing avb_custom_key\n");
3355 if (erase_userkey()) {
3356 fastboot_fail("Erasing avb_custom_key failed");
3357 } else {
3358 fastboot_okay("");
3359 }
3360 return;
3361 }
3362
Dima Zavin214cc642009-01-26 11:16:21 -08003363 if (flash_erase(ptn)) {
3364 fastboot_fail("failed to erase partition");
3365 return;
3366 }
3367 fastboot_okay("");
3368}
3369
Bikas Gurungd48bd242010-09-04 19:54:32 -07003370
3371void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3372{
3373 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003374 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003375 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003376 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303377 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003378
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003379#if VERIFIED_BOOT
3380 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3381 {
3382 if(!device.is_unlocked)
3383 {
3384 fastboot_fail("unlock device to erase keystore");
3385 return;
3386 }
3387 }
3388#endif
3389
Kinson Chikf1a43512011-07-14 11:28:39 -07003390 index = partition_get_index(arg);
3391 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003392 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003393
Monika Singhc4778b72018-05-16 11:16:42 +05303394 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3395 dprintf(INFO, "erasing avb_custom_key\n");
3396 if (erase_userkey()) {
3397 fastboot_fail("Erasing avb_custom_key failed");
3398 } else {
3399 fastboot_okay("");
3400 }
3401 return;
3402 }
3403
Kinson Chikf1a43512011-07-14 11:28:39 -07003404 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003405 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003406 return;
3407 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003408
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003409 lun = partition_get_lun(index);
3410 mmc_set_lun(lun);
3411
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003412 if (platform_boot_dev_isemmc())
3413 {
3414 if (mmc_erase_card(ptn, size)) {
3415 fastboot_fail("failed to erase partition\n");
3416 return;
3417 }
3418 } else {
3419 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3420 size = partition_get_size(index);
3421 if (size > DEFAULT_ERASE_SIZE)
3422 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003423
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003424 /* Simple inefficient version of erase. Just writing
3425 0 in first several blocks */
3426 if (mmc_write(ptn , size, (unsigned int *)out)) {
3427 fastboot_fail("failed to erase partition");
3428 return;
3429 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303430 /*Erase FDE metadata at the userdata footer*/
3431 if(!(strncmp(arg, "userdata", 8)))
3432 {
3433 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3434 memset((void *)footer, 0, FOOTER_SIZE);
3435
3436 size = partition_get_size(index);
3437
3438 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3439 fastboot_fail("failed to erase userdata footer");
3440 free(footer);
3441 return;
3442 }
3443 free(footer);
3444 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003445 }
Monika Singh292b3e92018-03-17 22:40:23 +05303446#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303447 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303448 !(strncmp(arg, "userdata", 8)) &&
3449 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003450 ASSERT(0);
3451#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003452 fastboot_okay("");
3453}
3454
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003455void cmd_erase(const char *arg, void *data, unsigned sz)
3456{
Monika Singh292b3e92018-03-17 22:40:23 +05303457#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003458 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003459 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003460 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003461 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003462 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003463 return;
3464 }
3465 }
3466#endif
3467
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003468 if(target_is_emmc_boot())
3469 cmd_erase_mmc(arg, data, sz);
3470 else
3471 cmd_erase_nand(arg, data, sz);
3472}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003473
Mayank Grover11ff9692018-01-11 11:54:49 +05303474/* Get the size from partiton name */
3475static void get_partition_size(const char *arg, char *response)
3476{
3477 uint64_t ptn = 0;
3478 uint64_t size;
3479 int index = INVALID_PTN;
3480
3481 index = partition_get_index(arg);
3482
3483 if (index == INVALID_PTN)
3484 {
3485 dprintf(CRITICAL, "Invalid partition index\n");
3486 return;
3487 }
3488
3489 ptn = partition_get_offset(index);
3490
3491 if(!ptn)
3492 {
3493 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3494 return;
3495 }
3496
3497 size = partition_get_size(index);
3498
3499 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3500 return;
3501}
3502
Mayank Grover52cd10a2018-03-15 12:57:54 +05303503/* Function to check partition type of a partition*/
3504static fs_signature_type
3505check_partition_fs_signature(const char *arg)
3506{
3507 fs_signature_type ret = NO_FS;
3508 int index;
3509 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303510 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3511 uint32_t sb_blk_offset = 0;
3512 char *sb_buffer = buffer;
3513
Mayank Grover52cd10a2018-03-15 12:57:54 +05303514 if (!sb_buffer)
3515 {
3516 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3517 goto out;
3518 }
3519
3520 /* Read super block */
3521 if ((index = partition_get_index(arg)) < 0)
3522 {
3523 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3524 goto out;
3525 }
3526 ptn = partition_get_offset(index);
3527 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303528 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3529
3530 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303531 (void *)sb_buffer, mmc_blocksize))
3532 {
3533 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3534 goto out;
3535 }
3536
Mayank Grover399826a2018-08-27 12:15:15 +05303537 if (sb_blk_offset == 0)
3538 sb_buffer += FS_SUPERBLOCK_OFFSET;
3539
3540 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303541 {
3542 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3543 ret = EXT_FS_SIGNATURE;
3544 }
Mayank Grover399826a2018-08-27 12:15:15 +05303545 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303546 {
3547 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3548 ret = EXT_F2FS_SIGNATURE;
3549 }
3550 else
3551 {
3552 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3553 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3554 ret = NO_FS;
3555 }
3556
3557out:
Mayank Grover399826a2018-08-27 12:15:15 +05303558 if(buffer)
3559 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303560 return ret;
3561}
3562
Mayank Groverd38fe012018-03-13 15:33:16 +05303563/* Function to get partition type */
3564static void get_partition_type(const char *arg, char *response)
3565{
3566 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303567 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303568
3569 if (arg == NULL ||
3570 response == NULL)
3571 {
3572 dprintf(CRITICAL, "Invalid input parameter\n");
3573 return;
3574 }
3575
3576 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303577 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303578
3579 /* Mark partiton type for known paritions only */
3580 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3581 {
3582 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3583 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303584 /* Check partition for FS signature */
3585 fs_signature = check_partition_fs_signature(arg);
3586 switch (fs_signature)
3587 {
3588 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303589 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303590 break;
3591 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303592 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303593 break;
3594 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303595 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303596 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303597 }
3598 }
3599 return;
3600}
3601
Mayank Grover11ff9692018-01-11 11:54:49 +05303602/*
3603 * Publish the partition type & size info
3604 * fastboot getvar will publish the required information.
3605 * fastboot getvar partition_size:<partition_name>: partition size in hex
3606 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3607 */
3608static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3609{
Mayank Groverd38fe012018-03-13 15:33:16 +05303610 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303611 static bool published = false;
3612 struct partition_entry *ptn_entry =
3613 partition_get_partition_entries();
3614 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3615
3616 for (i = 0; i < num_parts; i++) {
3617 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3618 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3619 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3620
Mayank Groverd38fe012018-03-13 15:33:16 +05303621 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303622 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303623 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3624 {
3625 dprintf(CRITICAL, "partition size name truncated\n");
3626 return;
3627 }
3628 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3629 {
3630 dprintf(CRITICAL, "partition type name truncated\n");
3631 return;
3632 }
3633
3634 if (!published)
3635 {
3636 /* publish partition size & type info */
3637 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3638 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3639 }
3640 }
3641 if (!published)
3642 published = true;
3643}
3644
3645
Ajay Dudani5c761132011-04-07 20:19:04 -07003646void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003647{
3648 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003649 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003650 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003651 char *token = NULL;
3652 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003653 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003654 uint8_t lun = 0;
3655 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303656 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003657
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003658 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003659 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003660 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003661 if(token)
3662 {
3663 lun = atoi(token);
3664 mmc_set_lun(lun);
3665 lun_set = true;
3666 }
3667
Mao Jinlong226f33a2014-07-04 17:24:10 +08003668 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003669 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003670 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3671 {
3672 if (!aboot_frp_unlock(pname, data, sz))
3673 {
3674 fastboot_info("FRP unlock successful");
3675 fastboot_okay("");
3676 }
3677 else
3678 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3679
3680 return;
3681 }
3682
Mao Jinlong226f33a2014-07-04 17:24:10 +08003683 if (!strcmp(pname, "partition"))
3684 {
3685 dprintf(INFO, "Attempt to write partition image.\n");
3686 if (write_partition(sz, (unsigned char *) data)) {
3687 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003688 return;
3689 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303690 /* Re-publish partition table */
3691 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303692
3693 /* Rescan partition table to ensure we have multislot support*/
3694 if (partition_scan_for_multislot())
3695 {
3696 current_active_slot = partition_find_active_slot();
3697 dprintf(INFO, "Multislot supported: Slot %s active",
3698 (SUFFIX_SLOT(current_active_slot)));
3699 }
3700 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003701 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003702 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003703 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003704#if VERIFIED_BOOT
3705 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3706 {
3707 if(!device.is_unlocked)
3708 {
3709 fastboot_fail("unlock device to flash keystore");
3710 return;
3711 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303712 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003713 {
3714 fastboot_fail("image is not a keystore file");
3715 return;
3716 }
3717 }
3718#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003719 index = partition_get_index(pname);
3720 ptn = partition_get_offset(index);
3721 if(ptn == 0) {
3722 fastboot_fail("partition table doesn't exist");
3723 return;
3724 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003725
Mayank Grover351a75e2017-05-30 20:06:08 +05303726 if (!strncmp(pname, "boot", strlen("boot"))
3727 || !strcmp(pname, "recovery"))
3728 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003729 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3730 fastboot_fail("image is not a boot image");
3731 return;
3732 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303733
3734 /* Reset multislot_partition attributes in case of flashing boot */
3735 if (partition_multislot_is_supported())
3736 {
3737 partition_reset_attributes(index);
3738 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003739 }
3740
3741 if(!lun_set)
3742 {
3743 lun = partition_get_lun(index);
3744 mmc_set_lun(lun);
3745 }
3746
3747 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303748 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003749 fastboot_fail("size too large");
3750 return;
3751 }
3752 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3753 fastboot_fail("flash write failure");
3754 return;
3755 }
Greg Grisco6e754772011-06-23 12:19:39 -07003756 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003757 }
3758 fastboot_okay("");
3759 return;
3760}
3761
Ajay Dudanide984792015-03-02 09:57:41 -08003762void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3763{
3764 int i, images;
3765 meta_header_t *meta_header;
3766 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303767 /*End of the image address*/
3768 uintptr_t data_end;
3769
3770 if( (UINT_MAX - sz) > (uintptr_t)data )
3771 data_end = (uintptr_t)data + sz;
3772 else
3773 {
3774 fastboot_fail("Cannot flash: image header corrupt");
3775 return;
3776 }
3777
3778 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3779 {
3780 fastboot_fail("Cannot flash: image header corrupt");
3781 return;
3782 }
Ajay Dudanide984792015-03-02 09:57:41 -08003783
lijuang8ee21882016-04-19 16:57:11 +08003784 /* If device is locked:
3785 * Forbid to flash image to avoid the device to bypass the image
3786 * which with "any" name other than bootloader. Because it maybe
3787 * a meta package of all partitions.
3788 */
Monika Singh292b3e92018-03-17 22:40:23 +05303789#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003790 if (target_build_variant_user()) {
3791 if (!device.is_unlocked) {
3792 fastboot_fail("Device is locked, meta image flashing is not allowed");
3793 return;
3794 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303795
Mayank Grover912eaa62017-10-26 12:08:53 +05303796 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303797 !device.is_unlock_critical)
3798 {
lijuang8ee21882016-04-19 16:57:11 +08003799 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3800 return;
3801 }
lijuang8ee21882016-04-19 16:57:11 +08003802 }
3803#endif
3804
Ajay Dudanide984792015-03-02 09:57:41 -08003805 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303806 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3807 {
3808 fastboot_fail("Cannot flash: image header corrupt");
3809 return;
3810 }
Ajay Dudanide984792015-03-02 09:57:41 -08003811 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3812
3813 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3814
3815 for (i=0; i<images; i++) {
3816
3817 if((img_header_entry[i].ptn_name == NULL) ||
3818 (img_header_entry[i].start_offset == 0) ||
3819 (img_header_entry[i].size == 0))
3820 break;
Kishor PK49831502017-04-21 17:55:43 +05303821 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3822 fastboot_fail("Integer overflow detected in start_offset of img");
3823 break;
3824 }
3825 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3826 fastboot_fail("Integer overflow detected in size of img");
3827 break;
3828 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303829 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3830 + img_header_entry[i].size) )
3831 {
3832 fastboot_fail("Cannot flash: image size mismatch");
3833 break;
3834 }
3835
Ajay Dudanide984792015-03-02 09:57:41 -08003836 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3837 (void *) data + img_header_entry[i].start_offset,
3838 img_header_entry[i].size);
3839 }
3840
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003841 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3842 {
3843 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3844 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3845 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3846 }
3847 else
3848 {
3849 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3850 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3851 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3852 }
3853
3854 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003855 fastboot_okay("");
3856 return;
3857}
3858
Ajay Dudani5c761132011-04-07 20:19:04 -07003859void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3860{
3861 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003862 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003863 uint32_t *fill_buf = NULL;
3864 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303865 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003866 sparse_header_t *sparse_header;
3867 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003868 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003869 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303870 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003871 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303872 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003873 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303874 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303875 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003876
Kinson Chikf1a43512011-07-14 11:28:39 -07003877 index = partition_get_index(arg);
3878 ptn = partition_get_offset(index);
3879 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003880 fastboot_fail("partition table doesn't exist");
3881 return;
3882 }
3883
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303884 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303885
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003886 lun = partition_get_lun(index);
3887 mmc_set_lun(lun);
3888
vijay kumar800255e2015-04-24 20:26:19 +05303889 if (sz < sizeof(sparse_header_t)) {
3890 fastboot_fail("size too low");
3891 return;
3892 }
3893
Ajay Dudani5c761132011-04-07 20:19:04 -07003894 /* Read and skip over sparse image header */
3895 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303896
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303897 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3898 fastboot_fail("Invalid block size\n");
3899 return;
3900 }
3901
vijay kumar1321f342015-03-27 12:13:42 +05303902 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003903 fastboot_fail("size too large");
3904 return;
3905 }
3906
vijay kumarde4fcf62015-04-23 13:05:49 +05303907 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303908
Parth Dixit64b1a482016-03-07 16:31:26 +05303909 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303910 fastboot_fail("buffer overreads occured due to invalid sparse header");
3911 return;
3912 }
3913
vijay kumarde4fcf62015-04-23 13:05:49 +05303914 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003915 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303916 fastboot_fail("sparse header size mismatch");
3917 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003918 }
3919
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003920 dprintf (SPEW, "=== Sparse Image Header ===\n");
3921 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3922 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3923 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3924 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3925 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3926 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3927 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3928 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003929
3930 /* Start processing chunks */
3931 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3932 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303933 /* Make sure the total image size does not exceed the partition size */
3934 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3935 fastboot_fail("size too large");
3936 return;
3937 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003938 /* Read and skip over chunk header */
3939 chunk_header = (chunk_header_t *) data;
3940 data += sizeof(chunk_header_t);
3941
Parth Dixit64b1a482016-03-07 16:31:26 +05303942 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303943 fastboot_fail("buffer overreads occured due to invalid sparse header");
3944 return;
3945 }
3946
Ajay Dudani5c761132011-04-07 20:19:04 -07003947 dprintf (SPEW, "=== Chunk Header ===\n");
3948 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3949 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3950 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3951
vijay kumar800255e2015-04-24 20:26:19 +05303952 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003953 {
vijay kumar800255e2015-04-24 20:26:19 +05303954 fastboot_fail("chunk header size mismatch");
3955 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003956 }
3957
wufeng.jiang813dc352015-06-02 23:02:46 -04003958 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3959
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303960 /* Make sure that the chunk size calculated from sparse image does not
3961 * exceed partition size
3962 */
3963 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3964 {
3965 fastboot_fail("Chunk data size exceeds partition size");
3966 return;
3967 }
3968
Ajay Dudani5c761132011-04-07 20:19:04 -07003969 switch (chunk_header->chunk_type)
3970 {
3971 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003972 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003973 chunk_data_sz))
3974 {
3975 fastboot_fail("Bogus chunk size for chunk type Raw");
3976 return;
3977 }
3978
Parth Dixit64b1a482016-03-07 16:31:26 +05303979 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303980 fastboot_fail("buffer overreads occured due to invalid sparse header");
3981 return;
3982 }
3983
wufeng.jiang813dc352015-06-02 23:02:46 -04003984 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3985 otherwise it will return in the line above
3986 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003987 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003988 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003989 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003990 {
3991 fastboot_fail("flash write failure");
3992 return;
3993 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303994 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3995 fastboot_fail("Bogus size for RAW chunk type");
3996 return;
3997 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003998 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003999 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004000 break;
4001
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004002 case CHUNK_TYPE_FILL:
4003 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4004 sizeof(uint32_t)))
4005 {
4006 fastboot_fail("Bogus chunk size for chunk type FILL");
4007 return;
4008 }
4009
Mayank Grover4f50bba2017-07-19 18:17:08 +05304010 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4011 /* Integer overflow detected */
4012 if (blk_sz_actual < sparse_header->blk_sz)
4013 {
4014 fastboot_fail("Invalid block size");
4015 return;
4016 }
4017
4018 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004019 if (!fill_buf)
4020 {
4021 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4022 return;
4023 }
4024
Parth Dixit64b1a482016-03-07 16:31:26 +05304025 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304026 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304027 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304028 return;
4029 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004030 fill_val = *(uint32_t *)data;
4031 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004032
4033 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4034 {
4035 fill_buf[i] = fill_val;
4036 }
4037
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304038 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4039 {
4040 fastboot_fail("bogus size for chunk FILL type");
4041 free(fill_buf);
4042 return;
4043 }
4044
wufeng.jiang813dc352015-06-02 23:02:46 -04004045 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004046 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304047 /* Make sure that the data written to partition does not exceed partition size */
4048 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4049 {
4050 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304051 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304052 return;
4053 }
4054
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004055 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4056 sparse_header->blk_sz,
4057 fill_buf))
4058 {
4059 fastboot_fail("flash write failure");
4060 free(fill_buf);
4061 return;
4062 }
4063
4064 total_blocks++;
4065 }
4066
4067 free(fill_buf);
4068 break;
4069
Ajay Dudani5c761132011-04-07 20:19:04 -07004070 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304071 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4072 fastboot_fail("bogus size for chunk DONT CARE type");
4073 return;
4074 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004075 total_blocks += chunk_header->chunk_sz;
4076 break;
4077
Ajay Dudani5c761132011-04-07 20:19:04 -07004078 case CHUNK_TYPE_CRC:
4079 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4080 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004081 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004082 return;
4083 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304084 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4085 fastboot_fail("bogus size for chunk CRC type");
4086 return;
4087 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004088 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304089 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304090 fastboot_fail("integer overflow occured");
4091 return;
4092 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004093 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304094 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304095 fastboot_fail("buffer overreads occured due to invalid sparse header");
4096 return;
4097 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004098 break;
4099
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004100 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004101 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004102 fastboot_fail("Unknown chunk type");
4103 return;
4104 }
4105 }
4106
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004107 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4108 total_blocks, sparse_header->total_blks);
4109
4110 if(total_blocks != sparse_header->total_blks)
4111 {
4112 fastboot_fail("sparse image write failure");
4113 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004114
4115 fastboot_okay("");
4116 return;
4117}
4118
4119void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4120{
4121 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004122 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004123
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004124#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004125 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4126 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004127 int ret=0;
4128 uint32 major_version=0;
4129 uint32 minor_version=0;
4130
4131 ret = scm_svc_version(&major_version,&minor_version);
4132 if(!ret)
4133 {
4134 if(major_version >= 2)
4135 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004136 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004137 {
4138 ret = encrypt_scm((uint32 **) &data, &sz);
4139 if (ret != 0) {
4140 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4141 return;
4142 }
4143
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004144 /* Protect only for SSD */
4145 if (!strcmp(arg, "ssd")) {
4146 ret = scm_protect_keystore((uint32 *) data, sz);
4147 if (ret != 0) {
4148 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4149 return;
4150 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004151 }
4152 }
4153 else
4154 {
4155 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4156 if(ret != 0)
4157 {
4158 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4159 return;
4160 }
4161 }
4162 }
4163 else
4164 {
4165 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4166 magic_number[1] == DECRYPT_MAGIC_1)
4167 {
4168 ret = decrypt_scm((uint32 **) &data, &sz);
4169 if (ret != 0) {
4170 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4171 return;
4172 }
4173 }
4174 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4175 magic_number[1] == ENCRYPT_MAGIC_1)
4176 {
4177 ret = encrypt_scm((uint32 **) &data, &sz);
4178 if (ret != 0) {
4179 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4180 return;
4181 }
4182 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004183 }
4184 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004185 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004186 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004187 dprintf(CRITICAL,"INVALID SVC Version\n");
4188 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004189 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004190#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004191
Monika Singh292b3e92018-03-17 22:40:23 +05304192#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004193 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004194 {
lijuang511a2b52015-08-14 20:50:51 +08004195 /* if device is locked:
4196 * common partition will not allow to be flashed
4197 * critical partition will allow to flash image.
4198 */
4199 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4200 fastboot_fail("Partition flashing is not allowed");
4201 return;
4202 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304203
lijuang511a2b52015-08-14 20:50:51 +08004204 /* if device critical is locked:
4205 * common partition will allow to be flashed
4206 * critical partition will not allow to flash image.
4207 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304208 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304209 !device.is_unlock_critical &&
4210 critical_flash_allowed(arg)) {
4211 fastboot_fail("Critical partition flashing is not allowed");
4212 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004213 }
4214 }
4215#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304216 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4217 dprintf(INFO, "flashing avb_custom_key\n");
4218 if (store_userkey(data, sz)) {
4219 fastboot_fail("Flashing avb_custom_key failed");
4220 } else {
4221 fastboot_okay("");
4222 }
4223 return;
4224 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004225
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004226 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004227 meta_header = (meta_header_t *) data;
4228 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004229 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004230 else if (meta_header->magic == META_HEADER_MAGIC)
4231 cmd_flash_meta_img(arg, data, sz);
4232 else
4233 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004234
4235#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304236 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304237 (!strncmp(arg, "system", 6)) &&
4238 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004239 // reset dm_verity mode to enforcing
4240 device.verity_mode = 1;
4241 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304242#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004243
Ajay Dudani5c761132011-04-07 20:19:04 -07004244 return;
4245}
4246
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004247void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4248{
4249 struct ptentry *sys_ptn;
4250 struct ptable *ptable;
4251
4252 ptable = flash_get_ptable();
4253 if (ptable == NULL) {
4254 fastboot_fail("partition table doesn't exist");
4255 return;
4256 }
4257
4258 sys_ptn = ptable_find(ptable, "system");
4259 if (sys_ptn == NULL) {
4260 fastboot_fail("system partition not found");
4261 return;
4262 }
4263
4264 sz = ROUND_TO_PAGE(sz, page_mask);
4265 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4266 fastboot_fail("update_ubi_vol failed");
4267 else
4268 fastboot_okay("");
4269}
4270
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004271void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004272{
4273 struct ptentry *ptn;
4274 struct ptable *ptable;
4275 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304276 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304277 unsigned bytes_to_round_page = 0;
4278 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004279
Kishor PK38ed93d2017-04-25 14:19:26 +05304280 if((uintptr_t)data > (UINT_MAX - sz)) {
4281 fastboot_fail("Cannot flash: image header corrupt");
4282 return;
4283 }
4284
Dima Zavin214cc642009-01-26 11:16:21 -08004285 ptable = flash_get_ptable();
4286 if (ptable == NULL) {
4287 fastboot_fail("partition table doesn't exist");
4288 return;
4289 }
4290
4291 ptn = ptable_find(ptable, arg);
4292 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004293 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4294 arg);
4295 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004296 return;
4297 }
4298
Monika Singh292b3e92018-03-17 22:40:23 +05304299 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4300 dprintf(INFO, "flashing avb_custom_key\n");
4301 if (store_userkey(data, sz)) {
4302 fastboot_fail("Flashing avb_custom_key failed");
4303 } else {
4304 fastboot_okay("");
4305 }
4306 return;
4307 }
4308
Dima Zavin214cc642009-01-26 11:16:21 -08004309 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304310 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4311 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4312 } else {
4313 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004314 return;
4315 }
4316 }
4317
Amol Jadi5c61a952012-05-04 17:05:35 -07004318 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004319 || !strcmp(ptn->name, "userdata")
4320 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004321 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004322 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004323 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304324 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304325 rounded_size = ROUNDUP(sz, page_size);
4326 bytes_to_round_page = rounded_size - sz;
4327 if (bytes_to_round_page) {
4328 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4329 fastboot_fail("Integer overflow detected");
4330 return;
4331 }
4332 if (((uintptr_t)data + sz + bytes_to_round_page) >
4333 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4334 fastboot_fail("Buffer size is not aligned to page_size");
4335 return;
4336 }
4337 else {
4338 memset(data + sz, 0, bytes_to_round_page);
4339 sz = rounded_size;
4340 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304341 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304342 }
4343
Kishor PK38ed93d2017-04-25 14:19:26 +05304344 /*Checking partition_size for the possible integer overflow */
4345 partition_size = validate_partition_size(ptn);
4346
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304347 if (sz > partition_size) {
4348 fastboot_fail("Image size too large");
4349 return;
4350 }
4351
Dima Zavin214cc642009-01-26 11:16:21 -08004352 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304353 if ((sz > UBI_EC_HDR_SIZE) &&
4354 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004355 if (flash_ubi_img(ptn, data, sz)) {
4356 fastboot_fail("flash write failure");
4357 return;
4358 }
4359 } else {
4360 if (flash_write(ptn, extra, data, sz)) {
4361 fastboot_fail("flash write failure");
4362 return;
4363 }
Dima Zavin214cc642009-01-26 11:16:21 -08004364 }
4365 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4366 fastboot_okay("");
4367}
4368
Kishor PK38ed93d2017-04-25 14:19:26 +05304369
4370static inline uint64_t validate_partition_size(struct ptentry *ptn)
4371{
4372 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4373 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4374 return ptn->length * flash_num_pages_per_blk() * page_size;
4375 }
4376 }
4377 return 0;
4378}
4379
4380
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004381void cmd_flash(const char *arg, void *data, unsigned sz)
4382{
4383 if(target_is_emmc_boot())
4384 cmd_flash_mmc(arg, data, sz);
4385 else
4386 cmd_flash_nand(arg, data, sz);
4387}
4388
Dima Zavin214cc642009-01-26 11:16:21 -08004389void cmd_continue(const char *arg, void *data, unsigned sz)
4390{
4391 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004392 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004393
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004394 if (target_is_emmc_boot())
4395 {
lijuanga40d6302015-07-20 20:10:13 +08004396#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004397 /* Exit keys' detection thread firstly */
4398 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004399#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004400 boot_linux_from_mmc();
4401 }
4402 else
4403 {
4404 boot_linux_from_flash();
4405 }
Dima Zavin214cc642009-01-26 11:16:21 -08004406}
4407
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004408void cmd_reboot(const char *arg, void *data, unsigned sz)
4409{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004410 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004411 fastboot_okay("");
4412 reboot_device(0);
4413}
4414
Mayank Grover351a75e2017-05-30 20:06:08 +05304415void cmd_set_active(const char *arg, void *data, unsigned sz)
4416{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304417 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304418 unsigned i,current_active_slot;
4419 const char *current_slot_suffix;
4420
4421 if (!partition_multislot_is_supported())
4422 {
4423 fastboot_fail("Command not supported");
4424 return;
4425 }
4426
4427 if (arg)
4428 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304429 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304430 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304431 {
4432 current_active_slot = partition_find_active_slot();
4433
4434 /* Check if trying to make curent slot active */
4435 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304436 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4437 (char *)suffix_delimiter, &sp);
4438
Mayank Grover5eb5f692017-07-19 20:16:04 +05304439 if (current_slot_suffix &&
4440 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304441 {
4442 fastboot_okay("Slot already set active");
4443 return;
4444 }
4445 else
4446 {
4447 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4448 {
4449 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304450 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4451 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304452 if (current_slot_suffix &&
4453 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304454 {
lijuang8b03e5f2019-07-12 18:16:19 +08004455 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304456 publish_getvar_multislot_vars();
4457 fastboot_okay("");
4458 return;
4459 }
4460 }
4461 }
4462 }
4463 }
4464 fastboot_fail("Invalid slot suffix.");
4465 return;
4466}
4467
Mayank Grover98c4c742019-04-25 17:21:37 +05304468#if DYNAMIC_PARTITION_SUPPORT
4469void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4470{
4471 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4472 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4473 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4474 fastboot_fail("Failed to update recovery command");
4475 return;
4476 }
4477 fastboot_okay("");
4478 reboot_device(REBOOT_MODE_UNKNOWN);
4479
4480 //shouldn't come here.
4481 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4482 return;
4483}
4484
4485void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4486{
4487 dprintf(INFO, "rebooting the device - recovery\n");
4488 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
4489 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4490 fastboot_fail("Failed to update recovery command");
4491 return;
4492 }
4493 fastboot_okay("");
4494 reboot_device(REBOOT_MODE_UNKNOWN);
4495
4496 //shouldn't come here.
4497 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4498 return;
4499}
4500#endif
4501
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004502void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4503{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004504 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004505 fastboot_okay("");
4506 reboot_device(FASTBOOT_MODE);
4507}
4508
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004509void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4510{
4511 dprintf(INFO, "Enabling charger screen check\n");
4512 device.charger_screen_enabled = 1;
4513 write_device_info(&device);
4514 fastboot_okay("");
4515}
4516
4517void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4518{
4519 dprintf(INFO, "Disabling charger screen check\n");
4520 device.charger_screen_enabled = 0;
4521 write_device_info(&device);
4522 fastboot_okay("");
4523}
4524
lijuanga25d8bb2015-09-16 13:11:52 +08004525void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4526{
4527 char *p = NULL;
4528 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304529 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004530
4531 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304532 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004533 if (p) {
4534 if (!strncmp(p, "0", 1)) {
4535 device.charger_screen_enabled = 0;
4536 } else if (!strncmp(p, "1", 1)) {
4537 device.charger_screen_enabled = 1;
4538 }
4539 }
4540 }
4541
4542 /* update charger_screen_enabled value for getvar
4543 * command
4544 */
4545 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4546 device.charger_screen_enabled);
4547
4548 write_device_info(&device);
4549 fastboot_okay("");
4550}
4551
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304552void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4553{
4554 dprintf(INFO, "Selecting display panel %s\n", arg);
4555 if (arg)
4556 strlcpy(device.display_panel, arg,
4557 sizeof(device.display_panel));
4558 write_device_info(&device);
4559 fastboot_okay("");
4560}
4561
Shashank Mittal162244e2011-08-08 19:01:25 -07004562void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4563{
lijuang4ece1e72015-08-14 21:02:36 +08004564 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304565}
4566
4567void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4568{
lijuang4ece1e72015-08-14 21:02:36 +08004569 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304570 if(!is_allow_unlock) {
4571 fastboot_fail("oem unlock is not allowed");
4572 return;
4573 }
4574
lijuang4ece1e72015-08-14 21:02:36 +08004575 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304576
lijuang4ece1e72015-08-14 21:02:36 +08004577 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304578 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304579 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304580 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4581 write_misc(0, &msg, sizeof(msg));
4582
4583 fastboot_okay("");
4584 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004585 }
4586 fastboot_okay("");
4587}
4588
Channagoud Kadabiad259832015-05-29 11:14:17 -07004589static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4590{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304591 int ret=1;
4592 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004593
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304594 /*
4595 Authentication method not implemented.
4596
4597 OEM to implement, authentication system which on successful validataion,
4598 calls write_allow_oem_unlock() with is_allow_unlock.
4599 */
4600#if 0
4601 authentication_success = oem_specific_auth_mthd();
4602#endif
4603
4604 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004605 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304606 is_allow_unlock = true;
4607 write_allow_oem_unlock(is_allow_unlock);
4608 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004609 }
4610 return ret;
4611}
4612
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004613void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4614{
lijuang4ece1e72015-08-14 21:02:36 +08004615 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004616}
4617
Shashank Mittala0032282011-08-26 14:50:11 -07004618void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4619{
lijuang511a2b52015-08-14 20:50:51 +08004620 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004621 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004622 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004623 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4624 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304625#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304626 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304627 {
4628 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4629 (device.is_unlock_critical ? "true" : "false"));
4630 fastboot_info(response);
4631 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304632#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004633 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004634 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304635 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4636 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004637 fastboot_okay("");
4638}
4639
lijuang511a2b52015-08-14 20:50:51 +08004640void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4641{
4642 char response[MAX_RSP_SIZE];
4643 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4644 fastboot_info(response);
4645 fastboot_okay("");
4646}
4647
4648void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4649{
lijuang4ece1e72015-08-14 21:02:36 +08004650 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004651}
4652
4653void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4654{
lijuang4ece1e72015-08-14 21:02:36 +08004655 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004656}
4657
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004658void cmd_preflash(const char *arg, void *data, unsigned sz)
4659{
4660 fastboot_okay("");
4661}
4662
Mao Flynn7b379f32015-04-20 00:28:30 +08004663static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304664
Mao Flynn7b379f32015-04-20 00:28:30 +08004665int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304666{
Mao Flynn7b379f32015-04-20 00:28:30 +08004667 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304668 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004669 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304670 return -1;
4671 return 0;
4672}
4673
Mao Flynn7b379f32015-04-20 00:28:30 +08004674int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004675{
4676 struct ptentry *ptn;
4677 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004678 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004679 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004680
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304681 ptable = flash_get_ptable();
4682 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004683 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4684 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304685 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004686
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304687 ptn = ptable_find(ptable, "splash");
4688 if (ptn == NULL) {
4689 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004690 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304691 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004692 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304693 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004694 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304695 }
4696
Mao Flynn7b379f32015-04-20 00:28:30 +08004697 header = (struct logo_img_header *)logo_header;
4698 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304699 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004700 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304701 }
4702
4703 fb_display = fbcon_display();
4704 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304705 if (header->type && (header->blocks != 0) &&
4706 (UINT_MAX >= header->blocks * 512) &&
4707 ((header->blocks * 512) <= (fb_display->width *
4708 fb_display->height * (fb_display->bpp / 8)))) {
4709 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08004710 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4711
4712 /* if the logo is full-screen size, remove "fbcon_clear()" */
4713 if ((header->width != fb_display->width)
4714 || (header->height != fb_display->height))
4715 fbcon_clear();
4716
4717 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4718 (uint32_t *)base,
4719 (header->blocks * 512))) {
4720 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4721 return -1;
4722 }
4723 fbcon_extract_to_screen(header, base);
4724 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004725 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004726
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004727 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4728 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004729 return -1;
4730 }
4731
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304732 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304733 uint32_t fb_size = ROUNDUP(fb_display->width *
4734 fb_display->height *
4735 (fb_display->bpp / 8), 4096);
4736 uint32_t splash_size = ((((header->width * header->height *
4737 fb_display->bpp/8) + 511) >> 9) << 9);
4738
4739 if (splash_size > fb_size) {
4740 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4741 return -1;
4742 }
4743
Mao Flynn7b379f32015-04-20 00:28:30 +08004744 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4745 (uint32_t *)base,
4746 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304747 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304748 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004749 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004750 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304751 }
4752
Mao Flynn7b379f32015-04-20 00:28:30 +08004753 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304754}
4755
Mao Flynn7b379f32015-04-20 00:28:30 +08004756int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304757{
4758 int index = INVALID_PTN;
4759 unsigned long long ptn = 0;
4760 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004761 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004762 uint32_t blocksize, realsize, readsize;
4763 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304764
4765 index = partition_get_index("splash");
4766 if (index == 0) {
4767 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004768 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304769 }
4770
4771 ptn = partition_get_offset(index);
4772 if (ptn == 0) {
4773 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004774 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304775 }
4776
Mao Flynn1893a7f2015-06-03 12:03:36 +08004777 mmc_set_lun(partition_get_lun(index));
4778
4779 blocksize = mmc_get_device_blocksize();
4780 if (blocksize == 0) {
4781 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4782 return -1;
4783 }
4784
4785 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004786 if (!fb_display)
4787 {
4788 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4789 return -1;
4790 }
4791
Mao Flynn1893a7f2015-06-03 12:03:36 +08004792 base = (uint8_t *) fb_display->base;
4793
Kishor PKee5c0a32018-03-06 16:49:46 +05304794 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304795 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004796 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304797 }
4798
Mao Flynn1893a7f2015-06-03 12:03:36 +08004799 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004800 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304801 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004802 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304803 }
4804
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304805 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304806 if (header->type && (header->blocks != 0) &&
4807 (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
4808 ((header->blocks * 512) <= (fb_display->width *
4809 fb_display->height * (fb_display->bpp / 8)))) {
4810 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08004811 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004812
Mao Flynn1893a7f2015-06-03 12:03:36 +08004813 realsize = header->blocks * 512;
4814 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4815
4816 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004817 if ((header->width != fb_display->width)
4818 || (header->height != fb_display->height))
4819 fbcon_clear();
4820
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304821 uint32_t fb_size = ROUNDUP(fb_display->width *
4822 fb_display->height *
4823 (fb_display->bpp / 8), 4096);
4824
4825 if (readsize > fb_size) {
4826 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4827 return -1;
4828 }
4829
Kishor PKee5c0a32018-03-06 16:49:46 +05304830 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004831 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4832 return -1;
4833 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004834
Mao Flynn1893a7f2015-06-03 12:03:36 +08004835 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4836 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304837
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004838 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4839 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004840 return -1;
4841 }
4842
4843 realsize = header->width * header->height * fb_display->bpp / 8;
4844 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4845
4846 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304847 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004848 fbcon_clear();
4849 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4850 return -1;
4851 }
4852 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304853 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004854 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4855 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4856 return -1;
4857 }
4858 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4859 }
4860 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304861 }
4862
Mao Flynn7b379f32015-04-20 00:28:30 +08004863 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304864}
4865
Mao Flynn7b379f32015-04-20 00:28:30 +08004866int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304867{
4868 if (target_is_emmc_boot()) {
4869 return splash_screen_mmc();
4870 } else {
4871 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004872 }
4873}
4874
Mayank Grover351a75e2017-05-30 20:06:08 +05304875void publish_getvar_multislot_vars()
4876{
4877 int i,count;
4878 static bool published = false;
4879 static char slot_count[MAX_RSP_SIZE];
4880 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4881 static char active_slot_suffix[MAX_RSP_SIZE];
4882 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4883 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4884 const char *tmp;
4885 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304886 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304887
4888 if (!published)
4889 {
4890 /* Update slot meta info */
4891 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4892 partition_get_partition_count());
4893 for(i=0; i<count; i++)
4894 {
4895 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304896 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4897 has_slot_pname[i]);
4898 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304899 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4900 }
4901
4902 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4903 {
4904 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304905 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304906 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4907 "slot-unbootable:%s", tmp);
4908 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4909 "slot-active:%s", tmp);
4910 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4911 "slot-success:%s", tmp);
4912 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4913 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304914 fastboot_publish(slot_info[i].slot_is_unbootable,
4915 slot_info[i].slot_is_unbootable_rsp);
4916 fastboot_publish(slot_info[i].slot_is_active,
4917 slot_info[i].slot_is_active_rsp);
4918 fastboot_publish(slot_info[i].slot_is_succesful,
4919 slot_info[i].slot_is_succesful_rsp);
4920 fastboot_publish(slot_info[i].slot_retry_count,
4921 slot_info[i].slot_retry_count_rsp);
4922 }
4923 fastboot_publish("current-slot", active_slot_suffix);
4924 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4925 fastboot_publish("slot-count", slot_count);
4926 published = true;
4927 }
4928
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304929 active_slt = partition_find_active_slot();
4930 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304931 {
4932 tmp = SUFFIX_SLOT(active_slt);
4933 tmp++; // to remove "_" from slot_suffix.
4934 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4935 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304936 else
4937 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4938
Mayank Grover351a75e2017-05-30 20:06:08 +05304939 /* Update partition meta information */
4940 partition_fill_slot_meta(slot_info);
4941 return;
4942}
4943
lijuang4ece1e72015-08-14 21:02:36 +08004944void get_product_name(unsigned char *buf)
4945{
4946 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4947 return;
4948}
4949
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304950#if PRODUCT_IOT
4951void get_bootloader_version_iot(unsigned char *buf)
4952{
4953 if (buf != NULL)
4954 {
4955 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4956 strlcat(buf, "-", MAX_VERSION_LEN);
4957 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4958 }
4959 return;
4960}
4961#endif
4962
lijuang4ece1e72015-08-14 21:02:36 +08004963void get_bootloader_version(unsigned char *buf)
4964{
4965 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4966 return;
4967}
4968
4969void get_baseband_version(unsigned char *buf)
4970{
4971 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4972 return;
4973}
4974
Monika Singh32a09f72018-03-14 13:08:29 +05304975bool is_device_locked_critical()
4976{
4977 return device.is_unlock_critical ? false:true;
4978}
4979
lijuang4ece1e72015-08-14 21:02:36 +08004980bool is_device_locked()
4981{
4982 return device.is_unlocked ? false:true;
4983}
4984
Monika Singh32a09f72018-03-14 13:08:29 +05304985bool is_verity_enforcing()
4986{
4987 return device.verity_mode ? true:false;
4988}
4989
Amol Jadi5edf3552013-07-23 14:15:34 -07004990/* register commands and variables for fastboot */
4991void aboot_fastboot_register_commands(void)
4992{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004993 int i;
lijuangf16461c2015-08-03 17:09:34 +08004994 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004995
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004996 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004997 /* By default the enabled list is empty. */
4998 {"", NULL},
4999 /* move commands enclosed within the below ifndef to here
5000 * if they need to be enabled in user build.
5001 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005002#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005003 /* Register the following commands only for non-user builds */
5004 {"flash:", cmd_flash},
5005 {"erase:", cmd_erase},
5006 {"boot", cmd_boot},
5007 {"continue", cmd_continue},
5008 {"reboot", cmd_reboot},
5009 {"reboot-bootloader", cmd_reboot_bootloader},
5010 {"oem unlock", cmd_oem_unlock},
5011 {"oem unlock-go", cmd_oem_unlock_go},
5012 {"oem lock", cmd_oem_lock},
5013 {"flashing unlock", cmd_oem_unlock},
5014 {"flashing lock", cmd_oem_lock},
5015 {"flashing lock_critical", cmd_flashing_lock_critical},
5016 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5017 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5018 {"oem device-info", cmd_oem_devinfo},
5019 {"preflash", cmd_preflash},
5020 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5021 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005022 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005023 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305024 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305025#if DYNAMIC_PARTITION_SUPPORT
5026 {"reboot-fastboot",cmd_reboot_fastboot},
5027 {"reboot-recovery",cmd_reboot_recovery},
5028#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005029#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005030 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005031#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005032#endif
lijuang511a2b52015-08-14 20:50:51 +08005033 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005034
5035 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5036 for (i = 1; i < fastboot_cmds_count; i++)
5037 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5038
Amol Jadi5edf3552013-07-23 14:15:34 -07005039 /* publish variables and their values */
5040 fastboot_publish("product", TARGET(BOARD));
5041 fastboot_publish("kernel", "lk");
5042 fastboot_publish("serialno", sn_buf);
5043
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305044 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5045 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5046 fastboot_publish("hw-revision", soc_version_str);
5047
Amol Jadi5edf3552013-07-23 14:15:34 -07005048 /*
5049 * partition info is supported only for emmc partitions
5050 * Calling this for NAND prints some error messages which
5051 * is harmless but misleading. Avoid calling this for NAND
5052 * devices.
5053 */
5054 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305055 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005056
Mayank Grover351a75e2017-05-30 20:06:08 +05305057 if (partition_multislot_is_supported())
5058 publish_getvar_multislot_vars();
5059
Amol Jadi5edf3552013-07-23 14:15:34 -07005060 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005061#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005062 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5063 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005064#else
5065 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5066 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5067#endif
5068
Amol Jadi5edf3552013-07-23 14:15:34 -07005069 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005070 /* Is the charger screen check enabled */
5071 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5072 device.charger_screen_enabled);
5073 fastboot_publish("charger-screen-enabled",
5074 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08005075 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305076 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5077 device.display_panel);
5078 fastboot_publish("display-panel",
5079 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305080
5081 if (target_is_emmc_boot())
5082 {
5083 mmc_blocksize = mmc_get_device_blocksize();
5084 }
5085 else
5086 {
5087 mmc_blocksize = flash_block_size();
5088 }
5089 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5090 fastboot_publish("erase-block-size", (const char *) block_size_string);
5091 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305092#if PRODUCT_IOT
5093 get_bootloader_version_iot(&bootloader_version_string);
5094 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5095
5096 /* Version baseband is n/a for apq iot devices */
5097 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305098#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005099 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5100 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305101#endif
lijuangf16461c2015-08-03 17:09:34 +08005102 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305103 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005104 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5105 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5106 target_is_emmc_boot()? "eMMC":"UFS");
5107 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005108#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005109 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005110 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005111 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005112#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305113 if (target_dynamic_partition_supported())
5114 fastboot_publish("is-userspace", "no");
Amol Jadi5edf3552013-07-23 14:15:34 -07005115}
5116
Brian Swetland9c4c0752009-01-25 16:23:50 -08005117void aboot_init(const struct app_descriptor *app)
5118{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005119 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305120 int boot_err_type = 0;
5121 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07005122
lijuang39831732016-01-08 17:49:02 +08005123 /* Initialise wdog to catch early lk crashes */
5124#if WDOG_SUPPORT
5125 msm_wdog_init();
5126#endif
5127
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005128 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005129 if (target_is_emmc_boot())
5130 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005131 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005132 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305133 mmc_blocksize = mmc_get_device_blocksize();
5134 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005135 }
5136 else
5137 {
5138 page_size = flash_page_size();
5139 page_mask = page_size - 1;
5140 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005141 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5142
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005143 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05305144 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005145
Mayank Grover351a75e2017-05-30 20:06:08 +05305146 /* Detect multi-slot support */
5147 if (partition_multislot_is_supported())
5148 {
5149 boot_slot = partition_find_active_slot();
5150 if (boot_slot == INVALID)
5151 {
5152 boot_into_fastboot = true;
5153 dprintf(INFO, "Active Slot: (INVALID)\n");
5154 }
5155 else
5156 {
5157 /* Setting the state of system to boot active slot */
5158 partition_mark_active_slot(boot_slot);
5159 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5160 }
5161 }
5162
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005163 /* Display splash screen if enabled */
5164#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08005165#if NO_ALARM_DISPLAY
5166 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005167#endif
lijuang99c02d82015-02-13 19:04:34 +08005168 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07005169#if DISPLAY_HDMI_PRIMARY
5170 if (!strlen(device.display_panel))
5171 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5172 sizeof(device.display_panel));
5173#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005174#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07005175 /* Wait if the display shutdown is in progress */
5176 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005177 if (!pm_appsbl_display_init_done())
5178 target_display_init(device.display_panel);
5179 else
5180 display_image_on_screen();
5181#else
lijuang99c02d82015-02-13 19:04:34 +08005182 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005183#endif
lijuang99c02d82015-02-13 19:04:34 +08005184 dprintf(SPEW, "Display Init: Done\n");
5185#if NO_ALARM_DISPLAY
5186 }
5187#endif
5188#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005189
Greg Griscod6250552011-06-29 14:40:23 -07005190 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005191 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08005192
Dhaval Patel223ec952013-07-18 14:49:44 -07005193 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5194
Matthew Qindefd5562014-07-11 18:02:40 +08005195 /*
5196 * Check power off reason if user force reset,
5197 * if yes phone will do normal boot.
5198 */
5199 if (is_user_force_reset())
5200 goto normal_boot;
5201
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005202 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005203 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005204 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005205 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005206 reboot_device(EMERGENCY_DLOAD);
5207 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5208
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005209 boot_into_fastboot = true;
5210 }
5211 if (!boot_into_fastboot)
5212 {
5213 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5214 boot_into_recovery = 1;
5215 if (!boot_into_recovery &&
5216 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005217 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005218 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005219 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005220 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005221 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005222 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005223
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005224#if USE_PON_REBOOT_REG
5225 reboot_mode = check_hard_reboot_mode();
5226#else
Ajay Dudani77421292010-10-27 19:34:06 -07005227 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005228#endif
5229 if (reboot_mode == RECOVERY_MODE)
5230 {
Ajay Dudani77421292010-10-27 19:34:06 -07005231 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005232 }
5233 else if(reboot_mode == FASTBOOT_MODE)
5234 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005235 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005236 }
5237 else if(reboot_mode == ALARM_BOOT)
5238 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005239 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005240 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305241#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305242 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005243 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305244 if (reboot_mode == DM_VERITY_ENFORCING)
5245 {
5246 device.verity_mode = 1;
5247 write_device_info(&device);
5248 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005249#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305250 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005251#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305252 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005253#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305254 {
5255 device.verity_mode = 0;
5256 write_device_info(&device);
5257 }
5258 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5259 {
5260 if(send_delete_keys_to_tz())
5261 ASSERT(0);
5262 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005263 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305264#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005265
Matthew Qindefd5562014-07-11 18:02:40 +08005266normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005267 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005268 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005269 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005270 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005271 if(emmc_recovery_init())
5272 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5273 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005274 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005275 if((device.is_unlocked) || (device.is_tampered))
5276 {
5277 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305278 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005279 #endif
5280 #if USE_PCOM_SECBOOT
5281 set_tamper_flag(device.is_tampered);
5282 #endif
5283 }
Shashank Mittala0032282011-08-26 14:50:11 -07005284 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005285
Mayank Grover351a75e2017-05-30 20:06:08 +05305286retry_boot:
5287 /* Trying to boot active partition */
5288 if (partition_multislot_is_supported())
5289 {
5290 boot_slot = partition_find_boot_slot();
5291 partition_mark_active_slot(boot_slot);
5292 if (boot_slot == INVALID)
5293 goto fastboot;
5294 }
5295
5296 boot_err_type = boot_linux_from_mmc();
5297 switch (boot_err_type)
5298 {
5299 case ERR_INVALID_PAGE_SIZE:
5300 case ERR_DT_PARSE:
5301 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305302 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305303 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305304 {
5305 /*
5306 * Deactivate current slot, as it failed to
5307 * boot, and retry next slot.
5308 */
5309 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305310 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305311 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305312 else
5313 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305314 default:
5315 break;
5316 /* going to fastboot menu */
5317 }
Shashank Mittala0032282011-08-26 14:50:11 -07005318 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005319 else
5320 {
5321 recovery_init();
5322 #if USE_PCOM_SECBOOT
5323 if((device.is_unlocked) || (device.is_tampered))
5324 set_tamper_flag(device.is_tampered);
5325 #endif
5326 boot_linux_from_flash();
5327 }
5328 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5329 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005330 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005331
Mayank Grover351a75e2017-05-30 20:06:08 +05305332fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07005333 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005334
Amol Jadi5edf3552013-07-23 14:15:34 -07005335 /* register aboot specific fastboot commands */
5336 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005337
Amol Jadi5edf3552013-07-23 14:15:34 -07005338 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005339 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005340
5341 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305342#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005343 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305344#else
5345 /* Add salt buffer offset at start of image address to copy VB salt */
5346 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5347 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5348#endif
lijuang4ece1e72015-08-14 21:02:36 +08005349#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005350 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005351#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005352}
5353
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005354uint32_t get_page_size()
5355{
5356 return page_size;
5357}
5358
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005359/*
5360 * Calculated and save hash (SHA256) for non-signed boot image.
5361 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005362 * @param image_addr - Boot image address
5363 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005364 *
5365 * @return int - 0 on success, negative value on failure.
5366 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005367static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005368{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005369 unsigned int digest[8];
5370#if IMAGE_VERIF_ALGO_SHA1
5371 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5372#else
5373 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5374#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005375
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005376 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005377 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005378
5379 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005380 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005381
5382 return 0;
5383}
5384
Mayank Grover351a75e2017-05-30 20:06:08 +05305385
Brian Swetland9c4c0752009-01-25 16:23:50 -08005386APP_START(aboot)
5387 .init = aboot_init,
5388APP_END