blob: 018f01aa0a9cb9ce73d4f1eb1042d92b2f01b46d [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Mayank Grover98c4c742019-04-25 17:21:37 +05305 * Copyright (c) 2009-2019, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Mayank Grover351a75e2017-05-30 20:06:08 +053051#include <ab_partition_parser.h>
Monika Singh292b3e92018-03-17 22:40:23 +053052#include <verifiedboot.h>
Greg Griscod6250552011-06-29 14:40:23 -070053#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070054#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070055#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070056#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030057#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070058#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070059#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070060#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053061#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080062#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053063#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080064#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080065#if USE_RPMB_FOR_DEVINFO
66#include <rpmb.h>
67#endif
Dima Zavin214cc642009-01-26 11:16:21 -080068
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070069#if ENABLE_WBC
Umang Chheda4c140de2019-12-19 14:30:38 +053070#include <pm_smbchg_common.h>
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070071#endif
72
Neeti Desai17379b82012-06-04 18:42:53 -070073#if DEVICE_TREE
74#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070076#endif
77
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053078#if WDOG_SUPPORT
79#include <wdog.h>
80#endif
81
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070082#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070083#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080084#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080085#include "bootimg.h"
86#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070087#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080088#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070089#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070091#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070092#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020093#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070094#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080095#include <menu_keys_detect.h>
96#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070097#include "fastboot_test.h"
tracychui3b4abb72020-06-15 10:50:49 +080098/*[TracyChui]Add memory_config property 20200615 start */
99#include <mmu.h>
100/*[TracyChui]Add memory_config property 20200615 end */
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700101
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700102extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700103extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700104extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700105extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530106 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700107
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700108void *info_buf;
tracychuid184b912020-06-05 17:31:38 +0800109/*[20200605][TracyChui] Implement get Serial Number start*/
110#if defined(ENABLE_PRODINFO_ACCESS)
111void *prodinfo_buf;
112#endif
113/*[20200605][TracyChui] Implement get Serial Number end*/
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700114void write_device_info_mmc(device_info *dev);
115void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700116static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700117static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530118static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530119bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530120static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530121static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700122/* fastboot command function pointer */
123typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
Monika Singh292b3e92018-03-17 22:40:23 +0530124bool get_perm_attr_status();
tracychuid184b912020-06-05 17:31:38 +0800125/*[20200605][TracyChui] Implement get Serial Number start*/
126#if defined(ENABLE_PRODINFO_ACCESS)
127void write_prod_info(prod_info *dev);
128#endif
129/*[20200605][TracyChui] Implement get Serial Number end */
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700130
131struct fastboot_cmd_desc {
132 char * name;
133 fastboot_cmd_fn cb;
134};
135
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700136#define EXPAND(NAME) #NAME
137#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700138
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700139#define DISPLAY_PANEL_HDMI "hdmi"
140
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800141#ifdef MEMBASE
142#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
143#else
David Ng183a7422009-12-07 14:55:21 -0800144#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800145#endif
146
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700147#ifndef MEMSIZE
148#define MEMSIZE 1024*1024
149#endif
150
151#define MAX_TAGS_SIZE 1024
Kishor PKee5c0a32018-03-06 16:49:46 +0530152#define PLL_CODES_OFFSET 4096
Kun Liang2f1601a2013-08-12 16:29:54 +0800153/* make 4096 as default size to ensure EFS,EXT4's erasing */
154#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700155#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530156#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800157
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700158#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700159#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800160
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800161#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
162
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700163#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
164
Ameya Thakur10a33452016-06-13 14:24:26 -0700165//Size of the header that is used in case the boot image has
166//a uncompressed kernel + appended dtb
167#define PATCHED_KERNEL_HEADER_SIZE 20
168
169//String used to determine if the boot image has
170//a uncompressed kernel + appended dtb
171#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
172
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800173#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700174static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700175#else
David Ng183a7422009-12-07 14:55:21 -0800176static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700177#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530178static const char *dynamic_bootdev_cmdline =
179 " androidboot.boot_devices=soc/";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800180static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300181static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530182
183static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800184static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800185static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800186static const char *battchg_pause = " androidboot.mode=charger";
tracychuid184b912020-06-05 17:31:38 +0800187/*[20200605][TracyChui] Implement get Serial Number start */
188#if defined(ENABLE_PRODINFO_ACCESS)
189static const char *cust_sn_cmdline = " androidboot.customer_serialno=";
190static const char *factory_sn_cmdline = " androidboot.factory_serialno=";
191static const char *UsbAdbEnable = " androidboot.adb_enable=1";
192#endif
193/*[20200605][TracyChui] Implement get Serial Number end*/
Shashank Mittalcd98d472011-08-02 14:29:24 -0700194static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300195static const char *secondary_gpt_enable = " gpt";
Monika Singh292b3e92018-03-17 22:40:23 +0530196#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200197static const char *mdtp_activated_flag = " mdtp";
Monika Singh292b3e92018-03-17 22:40:23 +0530198#endif
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800199static const char *baseband_apq = " androidboot.baseband=apq";
200static const char *baseband_msm = " androidboot.baseband=msm";
201static const char *baseband_csfb = " androidboot.baseband=csfb";
202static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700203static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800204static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700205static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800206static const char *baseband_dsda = " androidboot.baseband=dsda";
207static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800208static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800209static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530210static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530211static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
212static const char *skip_ramfs = " skip_initramfs";
tracychui3b4abb72020-06-15 10:50:49 +0800213/* [TracyChui]Add memory_config property 20200615 start */
214static const char *memory_config_3G = " androidboot.memory_config=32GB,3GB";
215static const char *memory_config_4G = " androidboot.memory_config=64GB,4GB";
216/* [TracyChui]Add memory_config property 20200615 end */
Mayank Grover466d6562018-05-10 14:52:20 +0530217
218#if HIBERNATION_SUPPORT
219static const char *resume = " resume=/dev/mmcblk0p";
220#endif
221
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530222#ifdef INIT_BIN_LE
223static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE;
224#else
Mayank Grover3804be72017-06-22 11:59:23 +0530225static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530226#endif
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530227
228#if VERITY_LE
229static const char *verity_dev = " root=/dev/dm-0";
230static const char *verity_system_part = " dm=\"system";
231static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
232#else
Mayank Grover5384ed82018-05-09 12:09:24 +0530233static const char *sys_path = " root=/dev/mmcblk0p";
lijuang83ef4b22018-08-23 11:01:55 +0800234
235#define MAX_DTBO_IDX_STR 64
236static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx=";
Parth Dixit1375d9e2019-07-08 20:56:13 +0530237
238#define MAX_DTB_IDX_STR MAX_DTBO_IDX_STR
239static const char *android_boot_dtb_idx = " androidboot.dtb_idx=";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530240#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800241
michaellincb058f12020-04-24 15:05:10 +0800242//<20200424-michaellin, Add PCBA stage to system properties
243#include <platform/gpio.h>
244static const char *PCBA_STAGE_0 = " androidboot.pcbastage=EP0";
245static const char *PCBA_STAGE_1 = " androidboot.pcbastage=EP1";
246static const char *PCBA_STAGE_2 = " androidboot.pcbastage=EP2";
247static const char *PCBA_STAGE_3 = " androidboot.pcbastage=FP";
248static const char *PCBA_STAGE_4 = " androidboot.pcbastage=MP";
michaellin52b647a2020-05-05 14:58:47 +0800249static const char *PCBA_STAGE_5 = " androidboot.pcbastage=MP-8903MB_001";
michaellincb058f12020-04-24 15:05:10 +0800250static const char *PCBA_STAGE_F = " androidboot.pcbastage=Reserved";
251//>20200424-michaellin
252
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700253#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700254static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700255static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530256static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700257//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700258
259struct verified_boot_verity_mode vbvm[] =
260{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700261#if ENABLE_VB_ATTEST
262 {false, "eio"},
263#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700264 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700265#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700266 {true, "enforcing"},
267};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700268struct verified_boot_state_name vbsn[] =
269{
270 {GREEN, "green"},
271 {ORANGE, "orange"},
272 {YELLOW,"yellow"},
273 {RED,"red" },
274};
275#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700276/*As per spec delay wait time before shutdown in Red state*/
277#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700278static unsigned page_size = 0;
279static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530280static unsigned mmc_blocksize = 0;
281static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700282static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
283static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530284static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800285static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700286static bool devinfo_present = true;
tracychuid184b912020-06-05 17:31:38 +0800287/*[20200605][TracyChui] Implement get Serial Number start */
288#if defined(ENABLE_PRODINFO_ACCESS)
289static bool prodinfo_present = true;
290#endif
291/*[20200605][TracyChui] Implement get Serial Number end*/
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700292bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530293static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530294static char *vbcmdline;
295static bootinfo info = {0};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530296static void *recovery_dtbo_buf = NULL;
297static uint32_t recovery_dtbo_size = 0;
298
Shashank Mittalcd98d472011-08-02 14:29:24 -0700299/* Assuming unauthorized kernel image by default */
300static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530301static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
tracychuid184b912020-06-05 17:31:38 +0800302/*[20200605][TracyChui] Implement get Serial Number start*/
303#if defined(ENABLE_PRODINFO_ACCESS)
304static prod_info prod = {PRODINFO_MAGIC, {0}, {0}, 0};
305#endif
306/*[20200605][TracyChui] Implement get Serial Number end*/
Monika Singh292b3e92018-03-17 22:40:23 +0530307
jhchen7a504d12020-04-24 15:45:57 +0800308/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
309#ifdef ENABLE_FUSE_CHECK
310static const char *fuse_blown = " androidboot.oem.color=red";
311static const char *fuse_not_blown = " androidboot.oem.color=brown";
312#endif
313/*[Arima_8910][jhchen] 20181031 end*/
314
315/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
316#ifdef ENABLE_FUSE_CHECK
317 int is_fused = 0;
318#endif
319/*[Arima_8910][jhchen] 20181031 end*/
320
vijay kumarc65876c2015-04-24 13:29:16 +0530321static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700322
vijay kumarca2e6812015-07-08 20:28:25 +0530323static char frp_ptns[2][8] = {"config","frp"};
324
lijuang511a2b52015-08-14 20:50:51 +0800325static const char *critical_flash_allowed_ptn[] = {
326 "aboot",
327 "rpm",
328 "tz",
329 "sbl",
330 "sdi",
331 "sbl1",
332 "xbl",
333 "hyp",
334 "pmic",
335 "bootloader",
336 "devinfo",
337 "partition"};
338
Dima Zavin42168f22009-01-30 11:52:22 -0800339struct atag_ptbl_entry
340{
341 char name[16];
342 unsigned offset;
343 unsigned size;
344 unsigned flags;
345};
346
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700347/*
348 * Partition info, required to be published
349 * for fastboot
350 */
351struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530352 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700353 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
354 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
355 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
356 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
357};
358
359/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530360 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700361 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530362#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530363#define EXT_STR "ext4"
364#define F2FS_STR "f2fs"
365
366#define FS_SUPERBLOCK_OFFSET 0x400
367#define EXT_MAGIC 0xEF53
368#define EXT_MAGIC_OFFSET_SB 0x38
369#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
370#define F2FS_MAGIC_OFFSET_SB 0x0
371
372typedef enum fs_signature_type {
373 EXT_FS_SIGNATURE = 1,
374 EXT_F2FS_SIGNATURE = 2,
375 NO_FS = -1
376} fs_signature_type;
377
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530378struct getvar_partition_info part_info[NUM_PARTITIONS];
379struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700380{
Mayank Grover49920212018-02-09 18:15:55 +0530381 { "system" , "partition-size:", "partition-type:", "", "ext4" },
382 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
383 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
384 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700385};
386
387char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700388char charger_screen_enabled[MAX_RSP_SIZE];
tracychuid184b912020-06-05 17:31:38 +0800389/*[20200605][TracyChui] Implement get Serial Number start*/
390#if defined(ENABLE_PRODINFO_ACCESS)
391char cust_sn_buf[PRODINFO_MAX_SSN_LEN + 1];
392char factory_sn_buf[PRODINFO_MAX_SSN_LEN + 1];
393char AdbEnable[MAX_RSP_SIZE];
394#endif
395#if defined(ENABLE_PRODINFO_ACCESS)
396char sn_buf[PRODINFO_MAX_ISN_LEN + 1];
397#else
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800398char sn_buf[13];
tracychuid184b912020-06-05 17:31:38 +0800399#endif
400/*[20200605][TracyChui] Implement get Serial Number end*/
Dhaval Patel223ec952013-07-18 14:49:44 -0700401char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530402char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530403char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530404char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530405#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530406
407/* For IOT we are using custom version */
408#define PRODUCT_IOT_VERSION "IOT001"
409char bootloader_version_string[MAX_RSP_SIZE];
410#endif
lijuang102dfa92015-10-09 18:31:03 +0800411
412#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800413char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800414char battery_soc_ok [MAX_RSP_SIZE];
415#endif
416
lijuangf16461c2015-08-03 17:09:34 +0800417char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700418
Greg Griscod2471ef2011-07-14 13:00:42 -0700419extern int emmc_recovery_init(void);
420
Kinson Chik0b1c8162011-08-31 16:31:57 -0700421#if NO_KEYPAD_DRIVER
422extern int fastboot_trigger(void);
423#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700424
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530425static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700426{
427 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700428#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800429 if (is_arm64)
430 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
431 else
432 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700433 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
434 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700435#endif
436}
437
Dima Zavin42168f22009-01-30 11:52:22 -0800438static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
439{
440 struct atag_ptbl_entry atag_ptn;
441
442 memcpy(atag_ptn.name, ptn->name, 16);
443 atag_ptn.name[15] = '\0';
444 atag_ptn.offset = ptn->start;
445 atag_ptn.size = ptn->length;
446 atag_ptn.flags = ptn->flags;
447 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
448 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
449}
Mayank Grover9df84c02018-08-30 15:46:35 +0530450#ifdef VERIFIED_BOOT_2
451void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
452{
453 int index = 0;
454 char *vbm_img_buf = NULL;
455 unsigned long long ptn = 0;
456 unsigned long long ptn_size = 0;
457
458 /* Immediately return if dtbo is not supported */
459 index = partition_get_index("vbmeta");
460 ptn = partition_get_offset(index);
461 if(!ptn)
462 {
463 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
464 return;
465 }
466
467 ptn_size = partition_get_size(index);
468 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
469 {
470 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
471 return;
472 }
473
474 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
475 if (!vbm_img_buf)
476 {
477 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
478 return;
479 }
480
481 mmc_set_lun(partition_get_lun(index));
482 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
483 {
484 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
485 free(vbm_img_buf);
486 return;
487 }
488
489 *vbmeta_image_buf = vbm_img_buf;
490 *vbmeta_image_sz = (uint32_t)ptn_size;
491 return;
492}
493#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800494
lijuang102dfa92015-10-09 18:31:03 +0800495#if CHECK_BAT_VOLTAGE
496void update_battery_status(void)
497{
498 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
499 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
500}
501#endif
502
michaellincb058f12020-04-24 15:05:10 +0800503//<20200424-michaellin, Add PCBA stage to system properties
504uint32_t GetPcbaVariant(void)
505{
506 uint8_t GPIO_97=10;
507 uint8_t GPIO_98=10;
508 uint8_t GPIO_99=10;
509
510 uint8_t pcba_stage = 0;
511
512 // Config GPIO
513 gpio_tlmm_config(97, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
514 gpio_tlmm_config(98, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
515 gpio_tlmm_config(99, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
516
517 /* Wait for the gpio config to take effect - debounce time */
518 thread_sleep(10);
519
520 /* Get status of GPIO */
521 GPIO_97 = gpio_status(97);
522 GPIO_98 = gpio_status(98);
523 GPIO_99 = gpio_status(99);
524
525 pcba_stage = (GPIO_99 << 2) + (GPIO_98 << 1) + GPIO_97;
526 dprintf(ALWAYS, "pcba_stage status: %u\n", pcba_stage);
michaellin52b647a2020-05-05 14:58:47 +0800527 // GPIO_99 GPIO_98 GPIO_97
michaellincb058f12020-04-24 15:05:10 +0800528 // 0 0 0 EP0
529 // 0 0 1 EP1
530 // 0 1 0 EP2
michaellin52b647a2020-05-05 14:58:47 +0800531 // 0 1 1 FP(8901MB-007)/PP
532 // 1 0 0 MP(8901MB-008)
533 // 1 0 1 MP-8903MB_001
michaellincb058f12020-04-24 15:05:10 +0800534 return pcba_stage;
535}
536//>20200424-michaellin
537
Neeti Desaie245d492012-06-01 12:52:13 -0700538unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800539{
David Ng183a7422009-12-07 14:55:21 -0800540 int cmdline_len = 0;
541 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800542 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700543 int pause_at_bootup = 0;
tracychuid184b912020-06-05 17:31:38 +0800544/*[20200605][TracyChui] Implement get Serial Number start*/
545#if defined(ENABLE_PRODINFO_ACCESS)
546 int AdbEnable = 0;
547#endif
548/*[20200605][TracyChui] Implement get Serial Number end*/
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800549 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300550 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700551 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700552 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530553#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530554 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530555#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530556 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530557 int system_ptn_index = -1;
558 unsigned int lun = 0;
559 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530560#if VERITY_LE
561 int syspath_buflen = strlen(verity_dev)
562 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
563 + strlen(verity_params) + sizeof(int) + 2;
564#else
lijuang83ef4b22018-08-23 11:01:55 +0800565 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
566 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
567 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530568 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
569 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530570#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530571 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530572#if HIBERNATION_SUPPORT
573 int resume_buflen = strlen(resume) + sizeof(int) + 2;
574 char resume_buf[resume_buflen];
575 int swap_ptn_index = INVALID_PTN;
576#endif
577
Mayank Grover889be1b2017-09-12 20:12:23 +0530578#if VERIFIED_BOOT
579 uint32_t boot_state = RED;
580#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530581
582#if USE_LE_SYSTEMD
583 is_systemd_present=true;
584#endif
585
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700586#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530587 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530588 {
589 boot_state = boot_verify_get_state();
590 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530591#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700592
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200593#ifdef MDTP_SUPPORT
594 mdtp_activated(&is_mdtp_activated);
595#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800596
Brian Swetland9c4c0752009-01-25 16:23:50 -0800597 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800598 cmdline_len = strlen(cmdline);
599 have_cmdline = 1;
600 }
601 if (target_is_emmc_boot()) {
602 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700603 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530604 if (!boot_dev_buf) {
605 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
606 } else {
607 platform_boot_dev_cmdline(boot_dev_buf);
608#if USE_BOOTDEV_CMDLINE
609 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700610#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530611 if (target_dynamic_partition_supported()) {
612 cmdline_len += strlen(dynamic_bootdev_cmdline);
613 cmdline_len += strlen(boot_dev_buf);
614 }
615 }
David Ng183a7422009-12-07 14:55:21 -0800616 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800617
618 cmdline_len += strlen(usb_sn_cmdline);
619 cmdline_len += strlen(sn_buf);
tracychuid184b912020-06-05 17:31:38 +0800620/*[20200605][TracyChui] Implement get Serial Number start */
621#if defined(ENABLE_PRODINFO_ACCESS)
622 cmdline_len += strlen(cust_sn_cmdline);
623 cmdline_len += strlen(cust_sn_buf);
624 cmdline_len += strlen(factory_sn_cmdline);
625 cmdline_len += strlen(factory_sn_buf);
626#endif
627/*[20200605][TracyChui] Implement get Serial Number end */
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800628
tracychui3b4abb72020-06-15 10:50:49 +0800629
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700630#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530631 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700632 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530633 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
634 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
635 {
636 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
637 ASSERT(0);
638 }
639 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
640 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700641 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530642#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700643
Monika Singh292b3e92018-03-17 22:40:23 +0530644
645 if (vbcmdline != NULL) {
646 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
647 strlen(vbcmdline));
648 cmdline_len += strlen(vbcmdline);
649 }
650
Pavel Nedev5614d222013-06-17 18:01:02 +0300651 if (boot_into_recovery && gpt_exists)
652 cmdline_len += strlen(secondary_gpt_enable);
653
Monika Singh292b3e92018-03-17 22:40:23 +0530654#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200655 if(is_mdtp_activated)
656 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530657#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300658 if (boot_into_ffbm) {
659 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530660
661 if(is_systemd_present)
662 cmdline_len += strlen(systemd_ffbm_mode);
663
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700664 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800665 /* reduce kernel console messages to speed-up boot */
666 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800667 } else if (boot_reason_alarm) {
668 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800669 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800670 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700671 pause_at_bootup = 1;
672 cmdline_len += strlen(battchg_pause);
673 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800674
tracychuid184b912020-06-05 17:31:38 +0800675/*[20200605][TracyChui] Implement get Serial Number start*/
676#if defined(ENABLE_PRODINFO_ACCESS)
677 if (prod.is_adb_enabled) {
678 dprintf(CRITICAL, "Device will enable adb\n");
679
680 prod.is_adb_enabled = 0;
681 write_prod_info(&prod);
682
683 AdbEnable = 1;
684 cmdline_len += strlen(UsbAdbEnable);
685 }
686#endif
687/*[20200605][TracyChui] Implement get Serial Number end*/
688
Shashank Mittalcd98d472011-08-02 14:29:24 -0700689 if(target_use_signed_kernel() && auth_kernel_img) {
690 cmdline_len += strlen(auth_kernel);
691 }
692
Joonwoo Park61112782013-10-02 19:50:39 -0700693 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
694 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530695 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700696 have_target_boot_params = 1;
697 cmdline_len += strlen(target_boot_params);
698 }
699
Ajay Dudanid04110c2011-01-17 23:55:07 -0800700 /* Determine correct androidboot.baseband to use */
701 switch(target_baseband())
702 {
703 case BASEBAND_APQ:
704 cmdline_len += strlen(baseband_apq);
705 break;
706
707 case BASEBAND_MSM:
708 cmdline_len += strlen(baseband_msm);
709 break;
710
711 case BASEBAND_CSFB:
712 cmdline_len += strlen(baseband_csfb);
713 break;
714
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800715 case BASEBAND_SVLTE2A:
716 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800717 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700718
719 case BASEBAND_MDM:
720 cmdline_len += strlen(baseband_mdm);
721 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700722
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800723 case BASEBAND_MDM2:
724 cmdline_len += strlen(baseband_mdm2);
725 break;
726
Amol Jadi5c61a952012-05-04 17:05:35 -0700727 case BASEBAND_SGLTE:
728 cmdline_len += strlen(baseband_sglte);
729 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530730
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800731 case BASEBAND_SGLTE2:
732 cmdline_len += strlen(baseband_sglte2);
733 break;
734
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530735 case BASEBAND_DSDA:
736 cmdline_len += strlen(baseband_dsda);
737 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800738
739 case BASEBAND_DSDA2:
740 cmdline_len += strlen(baseband_dsda2);
741 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530742 case BASEBAND_APQ_NOWGR:
743 cmdline_len += strlen(baseband_apq_nowgr);
744 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800745 }
746
michaellincb058f12020-04-24 15:05:10 +0800747 //<20200424-michaellin, Add PCBA stage to system properties
748 switch(GetPcbaVariant())
749 {
750 case 0: // EP0
751 cmdline_len += strlen(PCBA_STAGE_0);
752 break;
753
754 case 1: // EP1
755 cmdline_len += strlen(PCBA_STAGE_1);
756 break;
757
758 case 2: // EP2
759 cmdline_len += strlen(PCBA_STAGE_2);
760 break;
761
michaellin52b647a2020-05-05 14:58:47 +0800762 case 3: // FP(8901MB-007)/PP
michaellincb058f12020-04-24 15:05:10 +0800763 cmdline_len += strlen(PCBA_STAGE_3);
764 break;
765
michaellin52b647a2020-05-05 14:58:47 +0800766 case 4: // MP(8901MB-008)
michaellincb058f12020-04-24 15:05:10 +0800767 cmdline_len += strlen(PCBA_STAGE_4);
tracychui3b4abb72020-06-15 10:50:49 +0800768 break;
michaellincb058f12020-04-24 15:05:10 +0800769
michaellin52b647a2020-05-05 14:58:47 +0800770 case 5: // MP-8903MB_001
771 cmdline_len += strlen(PCBA_STAGE_5);
tracychui3b4abb72020-06-15 10:50:49 +0800772 break;
michaellin52b647a2020-05-05 14:58:47 +0800773
michaellincb058f12020-04-24 15:05:10 +0800774 default:// Reserved
775 cmdline_len += strlen(PCBA_STAGE_F);
776 break;
777 }
778 //>20200424-michaellin
779
tracychui3b4abb72020-06-15 10:50:49 +0800780 /* [TracyChui]Add memory_config property 20200615 start */
781 if(smem_get_ddr_size() == MEM_3GB )
782 {
783 cmdline_len += strlen(memory_config_3G);
784 }
785 else
786 {
787 cmdline_len += strlen(memory_config_4G);
788 }
789 /* [TracyChui]Add memory_config property 20200615 end */
790
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300791#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800792 if (cmdline) {
793 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530794 target_display_panel_node(display_panel_buf,
795 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800796 strlen(display_panel_buf)) {
797 cmdline_len += strlen(display_panel_buf);
798 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700799 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300800#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700801
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800802 if (target_warm_boot()) {
803 warm_boot = true;
804 cmdline_len += strlen(warmboot_cmdline);
805 }
806
Mayank Grover5384ed82018-05-09 12:09:24 +0530807 if (target_uses_system_as_root() ||
808 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530809 {
810 current_active_slot = partition_find_active_slot();
811 cmdline_len += (strlen(androidboot_slot_suffix)+
812 strlen(SUFFIX_SLOT(current_active_slot)));
813
Mayank Grover3804be72017-06-22 11:59:23 +0530814 system_ptn_index = partition_get_index("system");
815 if (platform_boot_dev_isemmc())
816 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530817#if VERITY_LE
818 /*
819 Condition 4: Verity and A/B both enabled
820 Eventual command line looks like:
821 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
822 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
823 */
824 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
825 verity_dev,
826 verity_system_part, suffix_slot[current_active_slot],
827 verity_params, system_ptn_index + 1);
828#else
829 /*
830 Condition 5: A/B enabled, but verity disabled
831 Eventual command line looks like:
832 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
833 ... root=/dev/mmcblk0p<NN> ...
834 */
835 snprintf(syspath_buf, syspath_buflen, " %s%d",
836 sys_path, system_ptn_index + 1);
837#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530838 }
839 else
840 {
841 lun = partition_get_lun(system_ptn_index);
842 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
843 lun_char_base + lun,
844 partition_get_index_in_lun("system", lun));
845 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530846
Monika Singh292b3e92018-03-17 22:40:23 +0530847#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530848 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530849#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530850 }
851
852 if (target_uses_system_as_root() ||
853 partition_multislot_is_supported())
854 {
855 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530856
857 /* For dynamic partition, support skip skip_initramfs */
858 if (!target_dynamic_partition_supported() &&
859 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530860 cmdline_len += strlen(skip_ramfs);
861 }
862
Mayank Grover466d6562018-05-10 14:52:20 +0530863#if HIBERNATION_SUPPORT
864 if (platform_boot_dev_isemmc())
865 {
866 swap_ptn_index = partition_get_index("swap");
867 if (swap_ptn_index != INVALID_PTN)
868 {
869 snprintf(resume_buf, resume_buflen,
870 " %s%d", resume,
871 (swap_ptn_index + 1));
872 cmdline_len += strlen(resume_buf);
873 }
874 else
875 {
876 dprintf(INFO, "WARNING: swap partition not found\n");
877 }
878 }
879#endif
880
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800881#if TARGET_CMDLINE_SUPPORT
882 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
883 int target_cmd_line_len;
884 ASSERT(target_cmdline_buf);
885 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
886 cmdline_len += target_cmd_line_len;
887#endif
888
lijuang83ef4b22018-08-23 11:01:55 +0800889#if !VERITY_LE
890 dtbo_idx = get_dtbo_idx ();
891 if (dtbo_idx != INVALID_PTN) {
892 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
893 android_boot_dtbo_idx, dtbo_idx);
894 cmdline_len += strlen (dtbo_idx_str);
895 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530896
897 dtb_idx = get_dtb_idx ();
898 if (dtb_idx != INVALID_PTN) {
899 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
900 android_boot_dtb_idx, dtb_idx);
901 cmdline_len += strlen (dtb_idx_str);
902 }
lijuang83ef4b22018-08-23 11:01:55 +0800903#endif
904
jhchen7a504d12020-04-24 15:45:57 +0800905/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
906#ifdef ENABLE_FUSE_CHECK
907 {
908 int hw_key_status = readl(0xA01D0);
909 is_fused = (hw_key_status & 0x2) >> 1;
910 dprintf(CRITICAL, "hw_key_status = 0x%x, is_fused=%d\n", hw_key_status, is_fused);
911 if (is_fused) {
912 cmdline_len += strlen(fuse_blown);
913 } else {
914 cmdline_len += strlen(fuse_not_blown);
915 }
916 }
917#endif
918/*[Arima_8910][jhchen] 20181031 end*/
919
David Ng183a7422009-12-07 14:55:21 -0800920 if (cmdline_len > 0) {
921 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800922 unsigned char *dst;
923
924 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
925 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530926 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800927 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700928
Amol Jadi168b7712012-03-06 16:15:00 -0800929 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800930 if (have_cmdline) {
931 src = cmdline;
932 while ((*dst++ = *src++));
933 }
934 if (target_is_emmc_boot()) {
935 src = emmc_cmdline;
936 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700937 have_cmdline = 1;
938 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800939#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700940 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530941 if (have_cmdline &&
942 boot_dev_buf) {
943 --dst;
944 while ((*dst++ = *src++));
945 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700946#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530947 /* Dynamic partition append boot_devices */
948 if (target_dynamic_partition_supported() &&
949 boot_dev_buf) {
950 src = dynamic_bootdev_cmdline;
951 if (have_cmdline) --dst;
952 while ((*dst++ = *src++));
953 src = boot_dev_buf;
954 if (have_cmdline) --dst;
955 while ((*dst++ = *src++));
956 }
David Ngf773dde2010-07-26 19:55:08 -0700957 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800958
michaellincb058f12020-04-24 15:05:10 +0800959 //<20200424-michaellin, Add PCBA stage to system properties
960 switch(GetPcbaVariant())
961 {
962 case 0:
963 src = PCBA_STAGE_0;
964 if (have_cmdline) --dst;
965 while ((*dst++ = *src++));
966 break;
967 case 1:
968 src = PCBA_STAGE_1;
969 if (have_cmdline) --dst;
970 while ((*dst++ = *src++));
971 break;
972 case 2:
973 src = PCBA_STAGE_2;
974 if (have_cmdline) --dst;
975 while ((*dst++ = *src++));
976 break;
977 case 3:
978 src = PCBA_STAGE_3;
979 if (have_cmdline) --dst;
980 while ((*dst++ = *src++));
981 break;
982 case 4:
983 src = PCBA_STAGE_4;
984 if (have_cmdline) --dst;
985 while ((*dst++ = *src++));
986 break;
michaellin52b647a2020-05-05 14:58:47 +0800987 case 5:
988 src = PCBA_STAGE_5;
989 if (have_cmdline) --dst;
990 while ((*dst++ = *src++));
991 break;
michaellincb058f12020-04-24 15:05:10 +0800992 default:
993 src = PCBA_STAGE_F;
994 if (have_cmdline) --dst;
995 while ((*dst++ = *src++));
996 break;
997 }
998 //>20200424-michaellin
999
tracychui3b4abb72020-06-15 10:50:49 +08001000 /* [TracyChui]Add memory_config property 20200615 start */
1001 if(smem_get_ddr_size() == MEM_3GB )
1002 {
1003 src = memory_config_3G;
1004 if (have_cmdline) --dst;
1005 while ((*dst++ = *src++));
1006 }
1007 else
1008 {
1009 src = memory_config_4G;
1010 if (have_cmdline) --dst;
1011 while ((*dst++ = *src++));
1012 }
1013 /* [TracyChui]Add memory_config property 20200615 end */
1014
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -07001015#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05301016 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -07001017 {
Mayank Grover889be1b2017-09-12 20:12:23 +05301018 src = verified_state;
1019 if(have_cmdline) --dst;
1020 have_cmdline = 1;
1021 while ((*dst++ = *src++));
1022 src = vbsn[boot_state].name;
1023 if(have_cmdline) --dst;
1024 while ((*dst++ = *src++));
1025
1026 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
1027 {
1028 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
1029 ASSERT(0);
1030 }
1031 src = verity_mode;
1032 if(have_cmdline) --dst;
1033 while ((*dst++ = *src++));
1034 src = vbvm[device.verity_mode].name;
1035 if(have_cmdline) -- dst;
1036 while ((*dst++ = *src++));
1037 src = keymaster_v1;
1038 if(have_cmdline) --dst;
1039 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -07001040 }
Parth Dixitddbc7352015-10-18 03:13:31 +05301041#endif
Monika Singh292b3e92018-03-17 22:40:23 +05301042
1043 if (vbcmdline != NULL) {
1044 src = vbcmdline;
1045 if (have_cmdline) --dst;
1046 while ((*dst++ = *src++));
1047 }
1048
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001049 src = usb_sn_cmdline;
1050 if (have_cmdline) --dst;
1051 have_cmdline = 1;
1052 while ((*dst++ = *src++));
1053 src = sn_buf;
1054 if (have_cmdline) --dst;
1055 have_cmdline = 1;
1056 while ((*dst++ = *src++));
tracychuid184b912020-06-05 17:31:38 +08001057
1058 /* [20200605][TracyChui] Implement get Serial Number */
1059 #if defined(ENABLE_PRODINFO_ACCESS)
1060 src = cust_sn_cmdline;
1061 if (have_cmdline) --dst;
1062 have_cmdline = 1;
1063 while ((*dst++ = *src++));
1064 src = cust_sn_buf;
1065 if (have_cmdline) --dst;
1066 have_cmdline = 1;
1067 while ((*dst++ = *src++));
1068 src = factory_sn_cmdline;
1069 if (have_cmdline) --dst;
1070 have_cmdline = 1;
1071 while ((*dst++ = *src++));
1072 src = factory_sn_buf;
1073 if (have_cmdline) --dst;
1074 have_cmdline = 1;
1075 while ((*dst++ = *src++));
1076 #endif
1077 /*[20200605][TracyChui] Implement get Serial Number end */
Hanumant Singh8e1ac232014-01-29 13:41:51 -08001078 if (warm_boot) {
1079 if (have_cmdline) --dst;
1080 src = warmboot_cmdline;
1081 while ((*dst++ = *src++));
1082 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001083
Pavel Nedev5614d222013-06-17 18:01:02 +03001084 if (boot_into_recovery && gpt_exists) {
1085 src = secondary_gpt_enable;
1086 if (have_cmdline) --dst;
1087 while ((*dst++ = *src++));
1088 }
Monika Singh292b3e92018-03-17 22:40:23 +05301089#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +02001090 if (is_mdtp_activated) {
1091 src = mdtp_activated_flag;
1092 if (have_cmdline) --dst;
1093 while ((*dst++ = *src++));
1094 }
Monika Singh292b3e92018-03-17 22:40:23 +05301095#endif
Pavel Nedev328ac822013-04-05 15:25:11 +03001096 if (boot_into_ffbm) {
1097 src = androidboot_mode;
1098 if (have_cmdline) --dst;
1099 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001100 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +03001101 if (have_cmdline) --dst;
1102 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +05301103
1104 if(is_systemd_present) {
1105 src = systemd_ffbm_mode;
1106 if (have_cmdline) --dst;
1107 while ((*dst++ = *src++));
1108 }
1109
Pavel Nedev898298c2013-02-27 12:36:09 -08001110 src = loglevel;
1111 if (have_cmdline) --dst;
1112 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +08001113 } else if (boot_reason_alarm) {
1114 src = alarmboot_cmdline;
1115 if (have_cmdline) --dst;
1116 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +03001117 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -07001118 src = battchg_pause;
1119 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -08001120 while ((*dst++ = *src++));
1121 }
Ajay Dudanid04110c2011-01-17 23:55:07 -08001122
tracychuid184b912020-06-05 17:31:38 +08001123/*[20200605][TracyChui] Implement get Serial Number start*/
1124#if defined(ENABLE_PRODINFO_ACCESS)
1125 if (AdbEnable) {
1126 src = UsbAdbEnable;
1127 if (have_cmdline) --dst;
1128 while ((*dst++ = *src++));
1129 }
1130#endif
1131/*[20200605][TracyChui] Implement get Serial Number end*/
1132
Shashank Mittalcd98d472011-08-02 14:29:24 -07001133 if(target_use_signed_kernel() && auth_kernel_img) {
1134 src = auth_kernel;
1135 if (have_cmdline) --dst;
1136 while ((*dst++ = *src++));
1137 }
1138
Ajay Dudanid04110c2011-01-17 23:55:07 -08001139 switch(target_baseband())
1140 {
1141 case BASEBAND_APQ:
1142 src = baseband_apq;
1143 if (have_cmdline) --dst;
1144 while ((*dst++ = *src++));
1145 break;
1146
1147 case BASEBAND_MSM:
1148 src = baseband_msm;
1149 if (have_cmdline) --dst;
1150 while ((*dst++ = *src++));
1151 break;
1152
1153 case BASEBAND_CSFB:
1154 src = baseband_csfb;
1155 if (have_cmdline) --dst;
1156 while ((*dst++ = *src++));
1157 break;
1158
Ajay Dudani6cff85e2011-02-04 16:02:16 -08001159 case BASEBAND_SVLTE2A:
1160 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001161 if (have_cmdline) --dst;
1162 while ((*dst++ = *src++));
1163 break;
Ajay Dudani403bc492011-09-30 16:17:21 -07001164
1165 case BASEBAND_MDM:
1166 src = baseband_mdm;
1167 if (have_cmdline) --dst;
1168 while ((*dst++ = *src++));
1169 break;
Amol Jadi5c61a952012-05-04 17:05:35 -07001170
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -08001171 case BASEBAND_MDM2:
1172 src = baseband_mdm2;
1173 if (have_cmdline) --dst;
1174 while ((*dst++ = *src++));
1175 break;
1176
Amol Jadi5c61a952012-05-04 17:05:35 -07001177 case BASEBAND_SGLTE:
1178 src = baseband_sglte;
1179 if (have_cmdline) --dst;
1180 while ((*dst++ = *src++));
1181 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301182
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -08001183 case BASEBAND_SGLTE2:
1184 src = baseband_sglte2;
1185 if (have_cmdline) --dst;
1186 while ((*dst++ = *src++));
1187 break;
1188
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301189 case BASEBAND_DSDA:
1190 src = baseband_dsda;
1191 if (have_cmdline) --dst;
1192 while ((*dst++ = *src++));
1193 break;
Amol Jadi2a15a272013-01-22 12:03:36 -08001194
1195 case BASEBAND_DSDA2:
1196 src = baseband_dsda2;
1197 if (have_cmdline) --dst;
1198 while ((*dst++ = *src++));
1199 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +05301200 case BASEBAND_APQ_NOWGR:
1201 src = baseband_apq_nowgr;
1202 if (have_cmdline) --dst;
1203 while ((*dst++ = *src++));
1204 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001205 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001206
1207 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -07001208 src = display_panel_buf;
1209 if (have_cmdline) --dst;
1210 while ((*dst++ = *src++));
1211 }
Joonwoo Park61112782013-10-02 19:50:39 -07001212
1213 if (have_target_boot_params) {
1214 if (have_cmdline) --dst;
1215 src = target_boot_params;
1216 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +05301217 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -07001218 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001219
Mayank Grover351a75e2017-05-30 20:06:08 +05301220 if (partition_multislot_is_supported() && have_cmdline)
1221 {
1222 src = androidboot_slot_suffix;
1223 --dst;
1224 while ((*dst++ = *src++));
1225 --dst;
1226 src = SUFFIX_SLOT(current_active_slot);
1227 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301228 }
Mayank Grover351a75e2017-05-30 20:06:08 +05301229
Mayank Grover351a75e2017-05-30 20:06:08 +05301230
Mayank Grover5384ed82018-05-09 12:09:24 +05301231 /*
1232 * System-As-Root behaviour, system.img should contain both
1233 * system content and ramdisk content, and should be mounted at
1234 * root(a/b).
1235 * Apending skip_ramfs for non a/b builds which use, system as root.
1236 */
1237 if ((target_uses_system_as_root() ||
1238 partition_multislot_is_supported()) &&
1239 have_cmdline)
1240 {
Mayank Groverb716edf2019-05-08 16:06:55 +05301241 if (!target_dynamic_partition_supported() &&
1242 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +05301243 {
1244 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +05301245 --dst;
1246 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301247 }
1248
1249 src = sys_path_cmdline;
1250 --dst;
1251 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +05301252
Monika Singh292b3e92018-03-17 22:40:23 +05301253#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +05301254 src = syspath_buf;
1255 --dst;
1256 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +05301257#endif
Mayank Grover351a75e2017-05-30 20:06:08 +05301258 }
1259
Mayank Grover466d6562018-05-10 14:52:20 +05301260#if HIBERNATION_SUPPORT
1261 if (swap_ptn_index != INVALID_PTN)
1262 {
1263 src = resume_buf;
1264 --dst;
1265 while ((*dst++ = *src++));
1266 }
1267#endif
1268
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001269#if TARGET_CMDLINE_SUPPORT
1270 if (target_cmdline_buf && target_cmd_line_len)
1271 {
1272 if (have_cmdline) --dst;
1273 src = target_cmdline_buf;
1274 while((*dst++ = *src++));
1275 free(target_cmdline_buf);
1276 }
1277#endif
lijuang83ef4b22018-08-23 11:01:55 +08001278
1279#if !VERITY_LE
1280 if (dtbo_idx != INVALID_PTN) {
1281 src = dtbo_idx_str;
1282 --dst;
1283 while ((*dst++ = *src++));
1284 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301285
1286 if (dtb_idx != INVALID_PTN) {
1287 src = dtb_idx_str;
1288 --dst;
1289 while ((*dst++ = *src++));
1290 }
lijuang83ef4b22018-08-23 11:01:55 +08001291#endif
jhchen7a504d12020-04-24 15:45:57 +08001292
1293/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
1294#ifdef ENABLE_FUSE_CHECK
1295 if (is_fused) {
1296 src = fuse_blown;
1297 if (have_cmdline) --dst;
1298 while ((*dst++ = *src++));
1299 } else {
1300 src = fuse_not_blown;
1301 if (have_cmdline) --dst;
1302 while ((*dst++ = *src++));
1303 }
1304#endif
1305/*[Arima_8910][jhchen] 20181031 end*/
Neeti Desaie245d492012-06-01 12:52:13 -07001306 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001307
1308
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001309 if (boot_dev_buf)
1310 free(boot_dev_buf);
1311
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001312 if (cmdline_final)
1313 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1314 else
1315 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001316 return cmdline_final;
1317}
1318
1319unsigned *atag_core(unsigned *ptr)
1320{
1321 /* CORE */
1322 *ptr++ = 2;
1323 *ptr++ = 0x54410001;
1324
1325 return ptr;
1326
1327}
1328
1329unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1330 unsigned ramdisk_size)
1331{
1332 if (ramdisk_size) {
1333 *ptr++ = 4;
1334 *ptr++ = 0x54420005;
1335 *ptr++ = (unsigned)ramdisk;
1336 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001337 }
1338
Neeti Desaie245d492012-06-01 12:52:13 -07001339 return ptr;
1340}
1341
1342unsigned *atag_ptable(unsigned **ptr_addr)
1343{
1344 int i;
1345 struct ptable *ptable;
1346
1347 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001348 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1349 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001350 *(*ptr_addr)++ = 0x4d534d70;
1351 for (i = 0; i < ptable->count; ++i)
1352 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1353 }
1354
1355 return (*ptr_addr);
1356}
1357
1358unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1359{
1360 int cmdline_length = 0;
1361 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001362 char *dest;
1363
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001364 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001365 n = (cmdline_length + 4) & (~3);
1366
1367 *ptr++ = (n / 4) + 2;
1368 *ptr++ = 0x54410009;
1369 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001370 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001371 ptr += (n / 4);
1372
1373 return ptr;
1374}
1375
1376unsigned *atag_end(unsigned *ptr)
1377{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001378 /* END */
1379 *ptr++ = 0;
1380 *ptr++ = 0;
1381
Neeti Desaie245d492012-06-01 12:52:13 -07001382 return ptr;
1383}
1384
1385void generate_atags(unsigned *ptr, const char *cmdline,
1386 void *ramdisk, unsigned ramdisk_size)
1387{
vijay kumar21a37452015-12-29 16:23:21 +05301388 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001389 ptr = atag_core(ptr);
1390 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1391 ptr = target_atag_mem(ptr);
1392
1393 /* Skip NAND partition ATAGS for eMMC boot */
1394 if (!target_is_emmc_boot()){
1395 ptr = atag_ptable(&ptr);
1396 }
1397
vijay kumar21a37452015-12-29 16:23:21 +05301398 /*
1399 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1400 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1401 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301402 if (!cmdline)
1403 return;
1404
vijay kumar21a37452015-12-29 16:23:21 +05301405 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1406 ptr = atag_cmdline(ptr, cmdline);
1407 ptr = atag_end(ptr);
1408 }
1409 else {
1410 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1411 ASSERT(0);
1412 }
Neeti Desaie245d492012-06-01 12:52:13 -07001413}
1414
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001415typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001416void boot_linux(void *kernel, unsigned *tags,
1417 const char *cmdline, unsigned machtype,
1418 void *ramdisk, unsigned ramdisk_size)
1419{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001420 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001421#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001422 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001423#endif
1424
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001425 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001426 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301427 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001428
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301429 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001430
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001431 final_cmdline = update_cmdline((const char*)cmdline);
1432
Neeti Desai17379b82012-06-04 18:42:53 -07001433#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001434 dprintf(INFO, "Updating device tree: start\n");
1435
Neeti Desai17379b82012-06-04 18:42:53 -07001436 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301437 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001438 if(ret)
1439 {
1440 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1441 ASSERT(0);
1442 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001443 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001444#else
Neeti Desaie245d492012-06-01 12:52:13 -07001445 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001446 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001447#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001448
Monika Singh0ec6db82019-07-18 16:57:58 +05301449#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301450 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301451 {
1452 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001453#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001454#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301455 display_bootverify_menu(DISPLAY_MENU_EIO);
1456 wait_for_users_action();
1457 if(!pwr_key_is_pressed)
1458 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001459#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301460 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001461#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301462 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001463#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301464 dprintf(CRITICAL,
1465 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1466 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001467#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301468 }
lijuangbdd9bb42016-03-01 18:22:17 +08001469 }
lijuangbdd9bb42016-03-01 18:22:17 +08001470#endif
1471
1472#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001473 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001474 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001475 {
1476 dprintf(INFO, "Failed to write protect dev info\n");
1477 ASSERT(0);
1478 }
1479#endif
1480
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001481 /* Turn off splash screen if enabled */
1482#if DISPLAY_SPLASH_SCREEN
1483 target_display_shutdown();
1484#endif
1485
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001486 /* Perform target specific cleanup */
1487 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301488 free_verified_boot_resource(&info);
1489 if (final_cmdline)
1490 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001491
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001492 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301493 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001494
1495 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001496
Amol Jadi4421e652011-06-16 15:00:48 -07001497 /* do any platform specific cleanup before kernel entry */
1498 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001499
Brian Swetland9c4c0752009-01-25 16:23:50 -08001500 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001501
Amol Jadi504f9fe2012-08-16 13:56:48 -07001502#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001503 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001504#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001505 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001506
1507 if (IS_ARM64(kptr))
1508 /* Jump to a 64bit kernel */
1509 scm_elexec_call((paddr_t)kernel, tags_phys);
1510 else
1511 /* Jump to a 32bit kernel */
1512 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001513}
1514
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001515/* Function to check if the memory address range falls within the aboot
1516 * boundaries.
1517 * start: Start of the memory region
1518 * size: Size of the memory region
1519 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301520int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001521{
1522 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001523 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001524 return -1;
1525
1526 /* Check for memory overlap. */
1527 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1528 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001529 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001530 return 0;
1531 else
1532 return -1;
1533}
1534
Mayank Grovere559cec2017-10-17 15:12:03 +05301535/* Function to check if the memory address range falls beyond ddr region.
1536 * start: Start of the memory region
1537 * size: Size of the memory region
1538 */
1539int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1540{
1541 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1542 uint64_t ddr_size = smem_get_ddr_size();
1543 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1544 uintptr_t pa_start_addr = PA(start);
1545
1546 /* Check for boundary conditions. */
1547 if ((UINT_MAX - start) < size)
1548 return -1;
1549
1550 /* Check if memory range is beyond the ddr range. */
1551 if (pa_start_addr < ddr_pa_start_addr ||
1552 pa_start_addr >= (ddr_pa_end_addr) ||
1553 (pa_start_addr + size) > ddr_pa_end_addr)
1554 return -1;
1555 else
1556 return 0;
1557}
1558
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001559BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001560
Mayank Groverdd080e82018-09-04 20:12:13 +05301561int getimage(void **image_buffer, uint32_t *imgsize,
1562 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301563{
Mayank Groverdd080e82018-09-04 20:12:13 +05301564 uint32_t loadedindex;
1565 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301566 imgname == NULL) {
1567 dprintf(CRITICAL, "getimage: invalid parameters\n");
1568 return -1;
1569 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301570 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1571 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301572 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301573 *image_buffer = info.images[loadedindex].image_buffer;
1574 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301575 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1576 info.images[loadedindex].name,
1577 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301578 return 0;
1579 }
1580 }
1581 return -1;
1582}
1583
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001584static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1585{
1586 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001587
1588#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001589#if IMAGE_VERIF_ALGO_SHA1
1590 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1591#else
1592 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1593#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001594#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001595
1596 /* Assume device is rooted at this time. */
1597 device.is_tampered = 1;
1598
1599 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1600
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001601#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301602 uint32_t bootstate;
1603 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301604 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301605 {
1606 ret = boot_verify_image((unsigned char *)bootimg_addr,
1607 bootimg_size, "/recovery", &bootstate);
1608 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001609 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301610 {
1611 ret = boot_verify_image((unsigned char *)bootimg_addr,
1612 bootimg_size, "/boot", &bootstate);
1613 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001614 boot_verify_print_state();
1615#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001616 ret = image_verify((unsigned char *)bootimg_addr,
1617 (unsigned char *)(bootimg_addr + bootimg_size),
1618 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001619 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001620#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001621 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1622
1623 if (ret)
1624 {
1625 /* Authorized kernel */
1626 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001627 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001628 }
1629
Amit Blay4aa292f2015-04-28 21:55:59 +03001630#ifdef MDTP_SUPPORT
1631 {
1632 /* Verify MDTP lock.
1633 * For boot & recovery partitions, use aboot's verification result.
1634 */
1635 mdtp_ext_partition_verification_t ext_partition;
1636 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1637 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1638 ext_partition.page_size = 0; /* Not needed since already validated */
1639 ext_partition.image_addr = 0; /* Not needed since already validated */
1640 ext_partition.image_size = 0; /* Not needed since already validated */
1641 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1642 mdtp_fwlock_verify_lock(&ext_partition);
1643 }
1644#endif /* MDTP_SUPPORT */
1645
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001646#if USE_PCOM_SECBOOT
1647 set_tamper_flag(device.is_tampered);
1648#endif
1649
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001650#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001651 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001652 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001653 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001654#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001655 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001656#if ENABLE_VB_ATTEST
1657 mdelay(DELAY_WAIT);
1658 shutdown_device();
1659#else
lijuanga40d6302015-07-20 20:10:13 +08001660 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001661#endif
lijuanga40d6302015-07-20 20:10:13 +08001662#else
1663 dprintf(CRITICAL,
1664 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1665 mdelay(5000);
1666#endif
1667
1668 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001669 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001670#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001671 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001672 wait_for_users_action();
1673#else
1674 dprintf(CRITICAL,
1675 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1676 mdelay(5000);
1677#endif
1678 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001679 default:
lijuanga40d6302015-07-20 20:10:13 +08001680 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001681 }
1682#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001683#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301684 if(device.is_tampered)
1685 {
1686 write_device_info_mmc(&device);
1687 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301688 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301689 #endif
1690 #ifdef ASSERT_ON_TAMPER
1691 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1692 ASSERT(0);
1693 #endif
1694 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001695#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001696}
1697
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301698static bool check_format_bit()
1699{
1700 bool ret = false;
1701 int index;
1702 uint64_t offset;
1703 struct boot_selection_info *in = NULL;
1704 char *buf = NULL;
1705
1706 index = partition_get_index("bootselect");
1707 if (index == INVALID_PTN)
1708 {
1709 dprintf(INFO, "Unable to locate /bootselect partition\n");
1710 return ret;
1711 }
1712 offset = partition_get_offset(index);
1713 if(!offset)
1714 {
1715 dprintf(INFO, "partition /bootselect doesn't exist\n");
1716 return ret;
1717 }
1718 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301719 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301720 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301721 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301722 {
1723 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1724 free(buf);
1725 return ret;
1726 }
1727 in = (struct boot_selection_info *) buf;
1728 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1729 (in->version == BOOTSELECT_VERSION)) {
1730 if ((in->state_info & BOOTSELECT_FORMAT) &&
1731 !(in->state_info & BOOTSELECT_FACTORY))
1732 ret = true;
1733 } else {
1734 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1735 in->signature, in->version);
1736 ASSERT(0);
1737 }
1738 free(buf);
1739 return ret;
1740}
1741
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001742void boot_verifier_init()
1743{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001744 uint32_t boot_state;
1745 /* Check if device unlock */
1746 if(device.is_unlocked)
1747 {
1748 boot_verify_send_event(DEV_UNLOCK);
1749 boot_verify_print_state();
1750 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1751 return;
1752 }
1753 else
1754 {
1755 boot_verify_send_event(BOOT_INIT);
1756 }
1757
1758 /* Initialize keystore */
1759 boot_state = boot_verify_keystore_init();
1760 if(boot_state == YELLOW)
1761 {
1762 boot_verify_print_state();
1763 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1764 }
1765}
1766
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301767/* Function to return recovery appended dtbo buffer info */
1768void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1769{
1770 *dtbo_size = recovery_dtbo_size;
1771 *dtbo_buf = recovery_dtbo_buf;
1772 return;
1773}
1774
Shashank Mittal23b8f422010-04-16 19:27:21 -07001775int boot_linux_from_mmc(void)
1776{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301777 boot_img_hdr *hdr = (void*) buf;
1778 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001779 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001780 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001781 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001782 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001783
Shashank Mittalcd98d472011-08-02 14:29:24 -07001784 unsigned char *image_addr = 0;
1785 unsigned kernel_actual;
1786 unsigned ramdisk_actual;
1787 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001788 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301789 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001790
Matthew Qin271927e2015-03-31 22:07:07 -04001791 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301792 unsigned dtb_image_size = 0;
1793 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001794 unsigned int out_len = 0;
1795 unsigned int out_avai_len = 0;
1796 unsigned char *out_addr = NULL;
1797 uint32_t dtb_offset = 0;
1798 unsigned char *kernel_start_addr = NULL;
1799 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001800 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301801 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001802 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301803#if VERIFIED_BOOT_2
1804 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301805 void *dtbo_image_buf = NULL;
1806 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301807 void *vbmeta_image_buf = NULL;
1808 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301809#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301810 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001811#if DEVICE_TREE
1812 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001813 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001814 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001815 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001816 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001817 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001818#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001819 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301820 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001821
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301822 if (check_format_bit())
1823 boot_into_recovery = 1;
1824
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001825 if (!boot_into_recovery) {
1826 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1827 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1828 if (rcode <= 0) {
1829 boot_into_ffbm = false;
1830 if (rcode < 0)
1831 dprintf(CRITICAL,"failed to get ffbm cookie");
1832 } else
1833 boot_into_ffbm = true;
1834 } else
1835 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301836 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001837 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1838 dprintf(INFO, "Unified boot method!\n");
1839 hdr = uhdr;
1840 goto unified_boot;
1841 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301842
1843 /* For a/b recovery image code is on boot partition.
1844 If we support multislot, always use boot partition. */
1845 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301846 ((!partition_multislot_is_supported()) ||
1847 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301848 ptn_name = "recovery";
1849 else
1850 ptn_name = "boot";
1851
1852 index = partition_get_index(ptn_name);
1853 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301854 image_size = partition_get_size(index);
1855 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301856 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1857 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001858 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301859
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001860 /* Set Lun for boot & recovery partitions */
1861 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001862
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301863 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001864 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1865 return -1;
1866 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001867
1868 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001869 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301870 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001871 }
1872
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001873 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301874
1875 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1876 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1877 return -1;
1878 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001879 page_size = hdr->page_size;
1880 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001881 }
1882
Matthew Qin49e51fa2015-02-09 10:40:45 +08001883 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1884 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301885 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001886
Matthew Qin49e51fa2015-02-09 10:40:45 +08001887 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301888#if VERIFIED_BOOT_2
1889 /* Create hole in start of image for VB salt to copy */
1890 image_addr += SALT_BUFF_OFFSET;
1891#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301892 memcpy(image_addr, (void *)buf, page_size);
1893
1894 /* ensure commandline is terminated */
1895 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001896
1897#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301898#ifndef OSVERSION_IN_BOOTIMAGE
1899 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301900#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301901 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301902#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301903
Parth Dixit4097b622016-03-15 14:42:27 +05301904 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301905 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 +05301906 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1907 return -1;
1908 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301909 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001910#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301911 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 +05301912 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1913 return -1;
1914 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301915 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001916#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301917 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001918
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301919#ifdef OSVERSION_IN_BOOTIMAGE
1920 /* If header version is ONE and booting into recovery,
1921 dtbo is appended with recovery image.
1922 Doing following:
1923 * Validating the recovery offset and size.
1924 * Extracting recovery dtbo to be used as dtbo.
1925 */
1926 if (boot_into_recovery &&
1927 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1928 {
1929 struct boot_img_hdr_v1 *hdr1 =
1930 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301931 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301932
Mayank Grovera1a83c72018-09-24 11:23:15 +05301933 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301934 if ((hdr1->header_size !=
1935 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1936 {
1937 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1938 return -1;
1939 }
1940
Mayank Grovera1a83c72018-09-24 11:23:15 +05301941 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1942 {
1943 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301944 MAX_SUPPORTED_DTBO_IMG_BUF);
1945 return -1;
1946 }
1947
Mayank Grovera1a83c72018-09-24 11:23:15 +05301948 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301949 {
1950 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1951 return -1;
1952 }
1953
Mayank Grovera1a83c72018-09-24 11:23:15 +05301954 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1955 dprintf(CRITICAL,
1956 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
1957 return -1;
1958 }
1959
1960 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301961 {
1962 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
1963 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
1964 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
1965 return -1;
1966 }
1967
1968 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05301969 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301970 imagesize_actual += recovery_dtbo_size;
1971
1972 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1973 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
1974 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
1975
1976 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301977
1978 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
1979 struct boot_img_hdr_v1 *hdr1 =
1980 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
1981 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
1982 (image_addr + sizeof(boot_img_hdr) +
1983 BOOT_IMAGE_HEADER_V2_OFFSET);
1984 unsigned int recovery_dtbo_actual = 0;
1985
lijuang0b17ba22019-09-04 14:20:47 +08001986 recovery_dtbo_actual =
1987 ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
1988 imagesize_actual += recovery_dtbo_actual;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301989
1990 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
1991
1992
1993 dtb_image_offset = page_size + patched_kernel_hdr_size +
1994 kernel_actual + ramdisk_actual + second_actual +
1995 recovery_dtbo_actual;
1996
1997 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1998 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
1999 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302000#endif
2001
Parth Dixit1375d9e2019-07-08 20:56:13 +05302002
Saranya Chidurab1aaf232018-11-19 15:47:50 +05302003 /* Validate the boot/recovery image size is within the bounds of partition size */
2004 if (imagesize_actual > image_size) {
2005 dprintf(CRITICAL, "Image size is greater than partition size.\n");
2006 return -1;
2007 }
2008
Matthew Qin49e51fa2015-02-09 10:40:45 +08002009#if VERIFIED_BOOT
2010 boot_verifier_init();
2011#endif
2012
Mayank Grover1ceaee22019-04-01 17:54:32 +05302013#if VERIFIED_BOOT_2
2014 /* read full partition if device is unlocked */
2015 if (device.is_unlocked)
2016 imagesize_actual = image_size;
2017#endif
2018
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05302019 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002020 {
2021 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
2022 return -1;
2023 }
2024
Matthew Qinbb7923d2015-02-09 10:56:09 +08002025 /*
2026 * Update loading flow of bootimage to support compressed/uncompressed
2027 * bootimage on both 64bit and 32bit platform.
2028 * 1. Load bootimage from emmc partition onto DDR.
2029 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
2030 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
2031 * platform accordingly.
2032 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
2033 */
Mayank Grover351a75e2017-05-30 20:06:08 +05302034 if (partition_multislot_is_supported())
2035 {
2036 current_active_slot = partition_find_active_slot();
2037 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
2038 imagesize_actual, SUFFIX_SLOT(current_active_slot));
2039 }
2040 else
2041 {
2042 dprintf(INFO, "Loading (%s) image (%d): start\n",
2043 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2044 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002045 bs_set_timestamp(BS_KERNEL_LOAD_START);
2046
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05302047 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
2048 {
2049 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
2050 return -1;
2051 }
Kishor PK9e91b592017-05-24 12:14:55 +05302052 offset = page_size;
2053 /* Read image without signature and header*/
2054 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002055 {
2056 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2057 return -1;
2058 }
2059
Mayank Grover351a75e2017-05-30 20:06:08 +05302060 if (partition_multislot_is_supported())
2061 {
2062 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
2063 imagesize_actual, SUFFIX_SLOT(current_active_slot));
2064 }
2065 else
2066 {
2067 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07002068 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2069
Mayank Grover351a75e2017-05-30 20:06:08 +05302070 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002071 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2072
2073 /* Authenticate Kernel */
2074 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
2075 (int) target_use_signed_kernel(),
2076 device.is_unlocked,
2077 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05302078#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05302079 /* if device is unlocked skip reading signature, as full partition is read */
2080 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05302081 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05302082 offset = imagesize_actual;
2083 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
2084 {
2085 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
2086 return -1;
2087 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002088
Mayank Grover1ceaee22019-04-01 17:54:32 +05302089 /* Read signature */
2090 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
2091 {
2092 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
2093 return -1;
2094 }
Monika Singh292b3e92018-03-17 22:40:23 +05302095 }
2096
Mayank Grover027a9412018-09-04 15:12:05 +05302097 /* load and validate dtbo partition */
2098 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
2099
Mayank Grover9df84c02018-08-30 15:46:35 +05302100 /* load vbmeta partition */
2101 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
2102
Monika Singh292b3e92018-03-17 22:40:23 +05302103 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05302104
2105 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05302106 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05302107 info.images[IMG_BOOT].imgsize = imagesize_actual;
2108 info.images[IMG_BOOT].name = ptn_name;
2109 ++info.num_loaded_images;
2110
2111 /* Pass loaded dtbo image */
2112 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05302113 info.images[IMG_DTBO].image_buffer =
2114 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05302115 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
2116 info.images[IMG_DTBO].name = "dtbo";
2117 ++info.num_loaded_images;
2118 }
2119
Mayank Grover9df84c02018-08-30 15:46:35 +05302120 /* Pass loaded vbmeta image */
2121 if (vbmeta_image_buf != NULL) {
2122 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
2123 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
2124 info.images[IMG_VBMETA].name = "vbmeta";
2125 ++info.num_loaded_images;
2126 }
2127
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302128 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05302129 info.multi_slot_boot = partition_multislot_is_supported();
2130 info.bootreason_alarm = boot_reason_alarm;
2131 info.bootinto_recovery = boot_into_recovery;
2132 status = load_image_and_auth(&info);
2133 if(status)
2134 return -1;
2135
2136 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05302137
2138 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05302139 if (vbmeta_image_buf != NULL) {
2140 free(vbmeta_image_buf);
2141 --info.num_loaded_images;
2142 }
Monika Singh292b3e92018-03-17 22:40:23 +05302143#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002144 /* Change the condition a little bit to include the test framework support.
2145 * We would never reach this point if device is in fastboot mode, even if we did
2146 * that means we are in test mode, so execute kernel authentication part for the
2147 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302148 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08002149 {
2150 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05302151 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002152 {
2153 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
2154 return -1;
2155 }
2156
2157 /* Read signature */
2158 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
2159 {
2160 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
2161 return -1;
2162 }
2163
2164 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002165 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302166 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002167 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08002168 } else {
2169 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
2170 #ifdef TZ_SAVE_KERNEL_HASH
2171 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
2172 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03002173
2174#ifdef MDTP_SUPPORT
2175 {
2176 /* Verify MDTP lock.
2177 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
2178 * since verification was skipped in aboot. The signature is not part of the loaded image.
2179 */
2180 mdtp_ext_partition_verification_t ext_partition;
2181 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
2182 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
2183 ext_partition.page_size = page_size;
2184 ext_partition.image_addr = (uint32)image_addr;
2185 ext_partition.image_size = imagesize_actual;
2186 ext_partition.sig_avail = FALSE;
2187 mdtp_fwlock_verify_lock(&ext_partition);
2188 }
2189#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002190 }
Monika Singh292b3e92018-03-17 22:40:23 +05302191#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002192
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07002193#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08002194 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02002195 {
2196#if FBCON_DISPLAY_MSG
2197 display_bootverify_menu(DISPLAY_MENU_ORANGE);
2198 wait_for_users_action();
2199#else
2200 dprintf(CRITICAL,
2201 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
2202 mdelay(5000);
2203#endif
2204 }
2205#endif
2206
2207#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302208 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302209 {
2210 /* set boot and system versions. */
2211 set_os_version((unsigned char *)image_addr);
2212 // send root of trust
2213 if(!send_rot_command((uint32_t)device.is_unlocked))
2214 ASSERT(0);
2215 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302216#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002217 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002218 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2219 * If not, continue booting.
2220 */
2221 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
2222 {
2223 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
2224 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05302225#if VERIFIED_BOOT_2
2226 if (dtbo_image_sz)
2227 out_avai_len -= DTBO_IMG_BUF;
2228#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04002229 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002230 rc = decompress((unsigned char *)(image_addr + page_size),
2231 hdr->kernel_size, out_addr, out_avai_len,
2232 &dtb_offset, &out_len);
2233 if (rc)
2234 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002235 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002236 ASSERT(0);
2237 }
2238
Matthew Qin0b15b322015-05-19 05:20:54 -04002239 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002240 kptr = (struct kernel64_hdr *)out_addr;
2241 kernel_start_addr = out_addr;
2242 kernel_size = out_len;
2243 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07002244 dprintf(INFO, "Uncpmpressed kernel in use\n");
2245 if (!strncmp((char*)(image_addr + page_size),
2246 PATCHED_KERNEL_MAGIC,
2247 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
2248 dprintf(INFO, "Patched kernel detected\n");
2249 kptr = (struct kernel64_hdr *)(image_addr + page_size +
2250 PATCHED_KERNEL_HEADER_SIZE);
2251 //The size of the kernel is stored at start of kernel image + 16
2252 //The dtb would start just after the kernel
2253 dtb_offset = *((uint32_t*)((unsigned char*)
2254 (image_addr + page_size +
2255 sizeof(PATCHED_KERNEL_MAGIC) -
2256 1)));
2257 //The actual kernel starts after the 20 byte header.
2258 kernel_start_addr = (unsigned char*)(image_addr +
2259 page_size + PATCHED_KERNEL_HEADER_SIZE);
2260 kernel_size = hdr->kernel_size;
2261 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
2262 } else {
2263 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
2264 kptr = (struct kernel64_hdr *)(image_addr + page_size);
2265 kernel_start_addr = (unsigned char *)(image_addr + page_size);
2266 kernel_size = hdr->kernel_size;
2267 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08002268 }
2269
2270 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002271 * Update the kernel/ramdisk/tags address if the boot image header
2272 * has default values, these default values come from mkbootimg when
2273 * the boot image is flashed using fastboot flash:raw
2274 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002275 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002276
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002277 /* Get virtual addresses since the hdr saves physical addresses. */
2278 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2279 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2280 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002281
Matthew Qinbb7923d2015-02-09 10:56:09 +08002282 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002283 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002284 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302285 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2286 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2287 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002288 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302289 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002290 return -1;
2291 }
2292
2293#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302294 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2295 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002296 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302297 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002298 return -1;
2299 }
2300#endif
2301
Matthew Qin49e51fa2015-02-09 10:40:45 +08002302 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002303 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002304 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002305
Matthew Qin49e51fa2015-02-09 10:40:45 +08002306 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302307 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002308 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2309 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002310
Matthew Qin49e51fa2015-02-09 10:40:45 +08002311 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2312 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2313 return -1;
2314 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002315
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302316 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2317 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302318 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302319 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2320 return -1;
2321 }
2322
Matthew Qin49e51fa2015-02-09 10:40:45 +08002323 /* Find index of device tree within device tree table */
2324 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2325 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2326 return -1;
2327 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002328
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302329 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2330 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2331 return -1;
2332 }
2333
2334 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302335 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302336 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2337 return -1;
2338 }
2339
Matthew Qin271927e2015-03-31 22:07:07 -04002340 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2341 {
2342 unsigned int compressed_size = 0;
2343 out_addr += out_len;
2344 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002345 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002346 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2347 dt_entry.size, out_addr, out_avai_len,
2348 &compressed_size, &dtb_size);
2349 if (rc)
2350 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002351 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002352 ASSERT(0);
2353 }
2354
Matthew Qin0b15b322015-05-19 05:20:54 -04002355 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002356 best_match_dt_addr = out_addr;
2357 } else {
2358 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2359 dtb_size = dt_entry.size;
2360 }
2361
Matthew Qin49e51fa2015-02-09 10:40:45 +08002362 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302363 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2364 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002365 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302366 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002367 return -1;
2368 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002369
Matthew Qin271927e2015-03-31 22:07:07 -04002370 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002371 } else {
2372 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302373 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2374 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002375 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302376 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002377 return -1;
2378 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002379 /*
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.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002386 */
2387 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302388 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2389
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302390#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302391 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2392
2393 image_buf = (void*)(image_addr);
2394 dtb_offset = dtb_image_offset;
2395 dtb_image_size = imagesize_actual;
2396 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302397#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302398
2399 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002400 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002401 if (!dtb) {
2402 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002403 return -1;
2404 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002405 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002406 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002407
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002408 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2409 target_load_ssd_keystore();
2410
Shashank Mittal23b8f422010-04-16 19:27:21 -07002411unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002412
Dima Zavin77e41f32013-03-06 16:10:43 -08002413 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002414 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002415 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2416
2417 return 0;
2418}
2419
Dima Zavin214cc642009-01-26 11:16:21 -08002420int boot_linux_from_flash(void)
2421{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302422 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002423 struct ptentry *ptn;
2424 struct ptable *ptable;
2425 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002426
Shashank Mittalcd98d472011-08-02 14:29:24 -07002427 unsigned char *image_addr = 0;
2428 unsigned kernel_actual;
2429 unsigned ramdisk_actual;
2430 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302431 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002432
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002433#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302434 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002435 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302436 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002437 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302438 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302439 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302440 unsigned int dtb_size = 0;
2441 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002442#endif
2443
David Ng183a7422009-12-07 14:55:21 -08002444 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302445 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002446 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2447 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2448 return -1;
2449 }
2450 goto continue_boot;
2451 }
2452
Dima Zavin214cc642009-01-26 11:16:21 -08002453 ptable = flash_get_ptable();
2454 if (ptable == NULL) {
2455 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2456 return -1;
2457 }
2458
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002459 if(!boot_into_recovery)
2460 {
2461 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002462
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002463 if (ptn == NULL) {
2464 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2465 return -1;
2466 }
2467 }
2468 else
2469 {
2470 ptn = ptable_find(ptable, "recovery");
2471 if (ptn == NULL) {
2472 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2473 return -1;
2474 }
Dima Zavin214cc642009-01-26 11:16:21 -08002475 }
2476
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302477 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002478 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002479 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2480 return -1;
2481 }
Dima Zavin214cc642009-01-26 11:16:21 -08002482
2483 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002484 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002485 return -1;
2486 }
2487
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002488 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002489 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 -08002490 return -1;
2491 }
2492
Kishor PK9e91b592017-05-24 12:14:55 +05302493 image_addr = (unsigned char *)target_get_scratch_address();
2494 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302495
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002496 /*
2497 * Update the kernel/ramdisk/tags address if the boot image header
2498 * has default values, these default values come from mkbootimg when
2499 * the boot image is flashed using fastboot flash:raw
2500 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002501 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002502
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002503 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002504 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2505 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2506 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2507
2508 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2509 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302510 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002511
Kishor PK9e91b592017-05-24 12:14:55 +05302512 /* ensure commandline is terminated */
2513 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2514
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002515 /* Check if the addresses in the header are valid. */
2516 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302517 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2518 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2519 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002520 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302521 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002522 return -1;
2523 }
2524
2525#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302526 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302527 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2528 return -1;
2529 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302530 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302531
Mayank Grovere559cec2017-10-17 15:12:03 +05302532 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2533 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302534 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302535 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302536 return -1;
2537 }
2538#else
2539
2540#ifndef OSVERSION_IN_BOOTIMAGE
2541 dt_size = hdr->dt_size;
2542#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302543 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302544 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 +05302545 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2546 return -1;
2547 }
2548
Kishor PKd8ddcad2017-07-27 13:53:57 +05302549 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302550
Mayank Grovere559cec2017-10-17 15:12:03 +05302551 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2552 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302553 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302554 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302555 return -1;
2556 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002557#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002558
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302559 /* Read full boot.img from flash */
2560 dprintf(INFO, "Loading (%s) image (%d): start\n",
2561 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2562 bs_set_timestamp(BS_KERNEL_LOAD_START);
2563
2564 if (UINT_MAX - page_size < imagesize_actual)
2565 {
2566 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2567 return -1;
2568 }
2569
2570 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2571 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2572 {
2573 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2574 return -1;
2575 }
2576
2577 offset = page_size;
2578 /* Read image without signature and header */
2579 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2580 {
2581 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2582 return -1;
2583 }
2584
2585 dprintf(INFO, "Loading (%s) image (%d): done\n",
2586 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2587 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2588
Shashank Mittalcd98d472011-08-02 14:29:24 -07002589 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002590 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002591 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002592 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302593
Shashank Mittalcd98d472011-08-02 14:29:24 -07002594 /* Read signature */
2595 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2596 {
2597 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002598 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002599 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002600
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302601 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302602 }
2603 offset = page_size;
2604 if(hdr->second_size != 0) {
2605 if (UINT_MAX - offset < second_actual)
2606 {
2607 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2608 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302609 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302610 offset += second_actual;
2611 /* Second image loading not implemented. */
2612 ASSERT(0);
2613 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302614 /* Move kernel and ramdisk to correct address */
2615 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2616 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2617
2618#if DEVICE_TREE
2619 if(dt_size != 0) {
2620
2621 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2622
2623 table = (struct dt_table*) dt_table_offset;
2624
2625 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2626 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2627 return -1;
2628 }
2629
2630 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2631 goes beyound hdr->dt_size*/
2632 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2633 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2634 return -1;
2635 }
2636
2637 /* Find index of device tree within device tree table */
2638 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2639 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2640 return -1;
2641 }
2642
2643 /* Validate and Read device device tree in the "tags_add */
2644 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2645 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2646 {
2647 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2648 return -1;
2649 }
2650
2651 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2652 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2653 return -1;
2654 }
2655
2656 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2657 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2658 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2659 return -1;
2660 }
2661
2662 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2663 dtb_size = dt_entry.size;
2664 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302665
2666 } else {
2667 /* Validate the tags_addr */
2668 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2669 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2670 {
2671 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2672 return -1;
2673 }
2674 /*
2675 * If appended dev tree is found, update the atags with
2676 * memory address to the DTB appended location on RAM.
2677 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302678 *
2679 * Make sure everything from scratch address is read before next step!
2680 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302681 */
2682 void *dtb = NULL;
2683 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2684 if (!dtb) {
2685 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2686 return -1;
2687 }
2688 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302689#endif
2690 if(target_use_signed_kernel() && (!device.is_unlocked))
2691 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002692 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002693 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002694 {
2695 write_device_info_flash(&device);
2696 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302697#if USE_PCOM_SECBOOT
2698 set_tamper_flag(device.is_tampered);
2699#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002700 }
David Ng183a7422009-12-07 14:55:21 -08002701continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002702
Dima Zavin214cc642009-01-26 11:16:21 -08002703 /* TODO: create/pass atags to kernel */
2704
Ajay Dudanie28a6072011-07-01 13:59:46 -07002705 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002706 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002707 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2708
2709 return 0;
2710}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002711
Shashank Mittal162244e2011-08-08 19:01:25 -07002712void write_device_info_mmc(device_info *dev)
2713{
Shashank Mittal162244e2011-08-08 19:01:25 -07002714 unsigned long long ptn = 0;
2715 unsigned long long size;
2716 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002717 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002718 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302719 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002720
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002721 if (devinfo_present)
2722 index = partition_get_index("devinfo");
2723 else
2724 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002725
Shashank Mittal162244e2011-08-08 19:01:25 -07002726 ptn = partition_get_offset(index);
2727 if(ptn == 0)
2728 {
2729 return;
2730 }
2731
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002732 lun = partition_get_lun(index);
2733 mmc_set_lun(lun);
2734
Shashank Mittal162244e2011-08-08 19:01:25 -07002735 size = partition_get_size(index);
2736
Mayank Groverddd348d2018-01-23 14:07:09 +05302737 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2738 mmc_blocksize_mask);
2739 if (device_info_sz == UINT_MAX)
2740 {
2741 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2742 return;
2743 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002744
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002745 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302746 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002747 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302748 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002749 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002750 {
2751 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002752 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002753 }
2754}
2755
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002756void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002757{
Shashank Mittal162244e2011-08-08 19:01:25 -07002758 unsigned long long ptn = 0;
2759 unsigned long long size;
2760 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002761 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302762 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002763
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002764 if ((index = partition_get_index("devinfo")) < 0)
2765 {
2766 devinfo_present = false;
2767 index = partition_get_index("aboot");
2768 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002769
Shashank Mittal162244e2011-08-08 19:01:25 -07002770 ptn = partition_get_offset(index);
2771 if(ptn == 0)
2772 {
2773 return;
2774 }
2775
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002776 mmc_set_lun(partition_get_lun(index));
2777
Shashank Mittal162244e2011-08-08 19:01:25 -07002778 size = partition_get_size(index);
2779
Mayank Groverddd348d2018-01-23 14:07:09 +05302780 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2781 mmc_blocksize_mask);
2782 if (device_info_sz == UINT_MAX)
2783 {
2784 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2785 return;
2786 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002787
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002788 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302789 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002790 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302791 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002792 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002793 {
2794 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002795 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002796 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002797}
2798
tracychuid184b912020-06-05 17:31:38 +08002799/*[20200605][TracyChui] Implement get Serial Number start*/
2800#if defined(ENABLE_PRODINFO_ACCESS)
2801void write_prod_info_mmc(prod_info *dev)
2802{
2803 unsigned long long ptn = 0;
2804 unsigned long long size;
2805 int index = INVALID_PTN;
2806 uint32_t blocksize;
2807 uint8_t lun = 0;
2808 uint32_t ret = 0;
2809
2810 if (prodinfo_present)
2811 index = partition_get_index("prodinfo");
2812 else
2813 index = partition_get_index("aboot");
2814
2815 ptn = partition_get_offset(index);
2816 if(ptn == 0)
2817 {
2818 return;
2819 }
2820
2821 lun = partition_get_lun(index);
2822 mmc_set_lun(lun);
2823
2824 size = partition_get_size(index);
2825
2826 blocksize = mmc_get_device_blocksize();
2827
2828 if (prodinfo_present)
2829 ret = mmc_write(ptn, blocksize, (void *)prodinfo_buf);
2830 else
2831 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)prodinfo_buf);
2832 if (ret)
2833 {
2834 dprintf(CRITICAL, "ERROR: Cannot write prod info\n");
2835 ASSERT(0);
2836 }
2837}
2838
2839void read_prod_info_mmc(struct prod_info *info)
2840{
2841 unsigned long long ptn = 0;
2842 unsigned long long size;
2843 int index = INVALID_PTN;
2844 uint32_t blocksize;
2845 uint32_t ret = 0;
2846
2847 if ((index = partition_get_index("prodinfo")) < 0)
2848 {
2849 prodinfo_present = false;
2850 index = partition_get_index("aboot");
2851 }
2852
2853 ptn = partition_get_offset(index);
2854 if(ptn == 0)
2855 {
2856 return;
2857 }
2858
2859 mmc_set_lun(partition_get_lun(index));
2860
2861 size = partition_get_size(index);
2862
2863 blocksize = mmc_get_device_blocksize();
2864
2865 if (prodinfo_present)
2866 ret = mmc_read(ptn, (void *)prodinfo_buf, blocksize);
2867 else
2868 ret = mmc_read((ptn + size - blocksize), (void *)prodinfo_buf, blocksize);
2869 if (ret)
2870 {
2871 dprintf(CRITICAL, "ERROR: Cannot read prod info\n");
2872 ASSERT(0);
2873 }
2874}
2875#endif
2876/*[20200605][TracyChui] Implement get Serial Number end*/
2877
Shashank Mittal162244e2011-08-08 19:01:25 -07002878void write_device_info_flash(device_info *dev)
2879{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002880 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002881 struct ptentry *ptn;
2882 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002883 if(info == NULL)
2884 {
2885 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2886 ASSERT(0);
2887 }
2888 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002889 ptable = flash_get_ptable();
2890 if (ptable == NULL)
2891 {
2892 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2893 return;
2894 }
2895
2896 ptn = ptable_find(ptable, "devinfo");
2897 if (ptn == NULL)
2898 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002899 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002900 return;
2901 }
2902
Mayank Groverdedbc892017-10-24 13:41:34 +05302903 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002904 memcpy(info, dev, sizeof(device_info));
2905
2906 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2907 {
2908 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2909 return;
2910 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002911 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002912}
2913
vijay kumarc65876c2015-04-24 13:29:16 +05302914static int read_allow_oem_unlock(device_info *dev)
2915{
vijay kumarc65876c2015-04-24 13:29:16 +05302916 unsigned offset;
2917 int index;
2918 unsigned long long ptn;
2919 unsigned long long ptn_size;
2920 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002921 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302922
vijay kumarca2e6812015-07-08 20:28:25 +05302923 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302924 if (index == INVALID_PTN)
2925 {
vijay kumarca2e6812015-07-08 20:28:25 +05302926 index = partition_get_index(frp_ptns[1]);
2927 if (index == INVALID_PTN)
2928 {
2929 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2930 return -1;
2931 }
vijay kumarc65876c2015-04-24 13:29:16 +05302932 }
2933
2934 ptn = partition_get_offset(index);
2935 ptn_size = partition_get_size(index);
2936 offset = ptn_size - blocksize;
2937
Mayank Grover48860402016-11-29 12:34:53 +05302938 /* Set Lun for partition */
2939 mmc_set_lun(partition_get_lun(index));
2940
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002941 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302942 {
2943 dprintf(CRITICAL, "Reading MMC failed\n");
2944 return -1;
2945 }
2946
2947 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2948 is_allow_unlock = buf[blocksize-1] & 0x01;
2949 return 0;
2950}
2951
2952static int write_allow_oem_unlock(bool allow_unlock)
2953{
vijay kumarc65876c2015-04-24 13:29:16 +05302954 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302955 int index;
2956 unsigned long long ptn;
2957 unsigned long long ptn_size;
2958 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002959 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302960
vijay kumarca2e6812015-07-08 20:28:25 +05302961 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302962 if (index == INVALID_PTN)
2963 {
vijay kumarca2e6812015-07-08 20:28:25 +05302964 index = partition_get_index(frp_ptns[1]);
2965 if (index == INVALID_PTN)
2966 {
2967 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2968 return -1;
2969 }
vijay kumarc65876c2015-04-24 13:29:16 +05302970 }
2971
2972 ptn = partition_get_offset(index);
2973 ptn_size = partition_get_size(index);
2974 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302975 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302976
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002977 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302978 {
2979 dprintf(CRITICAL, "Reading MMC failed\n");
2980 return -1;
2981 }
2982
2983 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2984 buf[blocksize-1] = allow_unlock;
2985 if (mmc_write(ptn + offset, blocksize, buf))
2986 {
2987 dprintf(CRITICAL, "Writing MMC failed\n");
2988 return -1;
2989 }
2990
2991 return 0;
2992}
2993
Shashank Mittal162244e2011-08-08 19:01:25 -07002994void read_device_info_flash(device_info *dev)
2995{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002996 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002997 struct ptentry *ptn;
2998 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002999 if(info == NULL)
3000 {
3001 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
3002 ASSERT(0);
3003 }
3004 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07003005 ptable = flash_get_ptable();
3006 if (ptable == NULL)
3007 {
3008 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3009 return;
3010 }
3011
3012 ptn = ptable_find(ptable, "devinfo");
3013 if (ptn == NULL)
3014 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07003015 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07003016 return;
3017 }
3018
3019 if (flash_read(ptn, 0, (void *)info_buf, page_size))
3020 {
3021 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
3022 return;
3023 }
3024
3025 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
3026 {
Shashank Mittal162244e2011-08-08 19:01:25 -07003027 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
3028 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07003029 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07003030 write_device_info_flash(info);
3031 }
3032 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003033 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003034}
3035
3036void write_device_info(device_info *dev)
3037{
3038 if(target_is_emmc_boot())
3039 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003040 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3041 if(info == NULL)
3042 {
3043 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
3044 ASSERT(0);
3045 }
3046 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003047 memcpy(info, dev, sizeof(struct device_info));
3048
3049#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05303050 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303051 is_secure_boot_enable()) {
3052 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
3053 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07003054 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07003055 else
3056 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003057#else
3058 write_device_info_mmc(info);
3059#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003060 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003061 }
3062 else
3063 {
3064 write_device_info_flash(dev);
3065 }
3066}
3067
Monika Singh94316462018-03-15 18:39:01 +05303068int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
3069{
3070 if (!devinfo_present) {
3071 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3072 return -EINVAL;
3073 }
3074 if (loc >= ARRAY_SIZE(device.rollback_index)) {
3075 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
3076 __func__, loc, ARRAY_SIZE(device.rollback_index));
3077 ASSERT(0);
3078 }
3079
3080 *roll_back_index = device.rollback_index[loc];
3081 return 0;
3082}
3083
3084int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
3085{
3086 if (!devinfo_present) {
3087 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3088 return -EINVAL;
3089 }
3090 if (loc >= ARRAY_SIZE(device.rollback_index)) {
3091 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
3092 __func__, loc, ARRAY_SIZE(device.rollback_index));
3093 ASSERT(0);
3094 }
3095
3096 device.rollback_index[loc] = roll_back_index;
3097 write_device_info(&device);
3098 return 0;
3099}
3100
Monika Singhb0fad822018-03-15 18:50:55 +05303101int store_userkey(uint8_t *user_key, uint32_t user_key_size)
3102{
3103 if (!devinfo_present) {
3104 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3105 return -EINVAL;
3106 }
3107
3108 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
3109 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
3110 return -ENODEV;
3111 }
3112
3113 memcpy(device.user_public_key, user_key, user_key_size);
3114 device.user_public_key_length = user_key_size;
3115 write_device_info(&device);
3116 return 0;
3117}
3118
3119int erase_userkey()
3120{
3121 if (!devinfo_present) {
3122 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3123 return -EINVAL;
3124 }
3125 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
3126 device.user_public_key_length = 0;
3127 write_device_info(&device);
3128 return 0;
3129}
3130
3131int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
3132{
3133 if (!devinfo_present) {
3134 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3135 return -EINVAL;
3136 }
3137 *user_key = device.user_public_key;
3138 *user_key_size = device.user_public_key_length;
3139 return 0;
3140}
3141
Shashank Mittal162244e2011-08-08 19:01:25 -07003142void read_device_info(device_info *dev)
3143{
3144 if(target_is_emmc_boot())
3145 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003146 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3147 if(info == NULL)
3148 {
3149 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
3150 ASSERT(0);
3151 }
3152 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003153
3154#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05303155 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303156 is_secure_boot_enable()) {
3157 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
3158 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07003159 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07003160 else
3161 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003162#else
3163 read_device_info_mmc(info);
3164#endif
3165
3166 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
3167 {
3168 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08003169 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07003170 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05303171#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303172 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303173 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05303174#endif
lijuang511a2b52015-08-14 20:50:51 +08003175 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07003176 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05303177#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303178 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303179 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05303180#endif
lijuang511a2b52015-08-14 20:50:51 +08003181 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003182 info->is_tampered = 0;
3183 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05303184#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303185 if (VB_M <= target_get_vb_version())
Monika Singh1c71a1c2019-12-03 12:12:48 +05303186 {
Mayank Grover889be1b2017-09-12 20:12:23 +05303187 info->verity_mode = 1; //enforcing by default
Monika Singh1c71a1c2019-12-03 12:12:48 +05303188 info->user_public_key_length = 0;
3189 memset(info->rollback_index, 0, sizeof(info->rollback_index));
3190 memset(info->user_public_key, 0, sizeof(info->user_public_key));
3191 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303192#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003193 write_device_info(info);
3194 }
jessicatseng1b9da342020-04-22 11:13:20 +08003195
3196//<2020/04/22-JessicaTseng, Enable off-charging
3197 info->charger_screen_enabled = 1;
3198 write_device_info(info);
3199//>2020/04/22-JessicaTseng
3200
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003201 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003202 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003203 }
3204 else
3205 {
3206 read_device_info_flash(dev);
3207 }
3208}
tracychuid184b912020-06-05 17:31:38 +08003209/*[20200605][TracyChui] Implement get Serial Number start*/
3210#if defined(ENABLE_PRODINFO_ACCESS)
3211void write_prod_info(prod_info *dev)
3212{
3213 if(target_is_emmc_boot())
3214 {
3215 struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3216 if(info == NULL)
3217 {
3218 dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n");
3219 ASSERT(0);
3220 }
3221 prodinfo_buf = info;
3222 memcpy(info, dev, sizeof(struct prod_info));
3223
3224 write_prod_info_mmc(info);
3225 free(info);
3226 }
3227}
3228
3229void read_prod_info(prod_info *dev)
3230{
3231 if(target_is_emmc_boot())
3232 {
3233 struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3234 if(info == NULL)
3235 {
3236 dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n");
3237 ASSERT(0);
3238 }
3239 prodinfo_buf = info;
3240
3241 read_prod_info_mmc(info);
3242
3243 if (memcmp(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE))
3244 {
3245 memcpy(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE);
3246 memcpy(info->isn, "No_Serial_Number", PRODINFO_MAX_ISN_LEN);
3247 memcpy(info->ssn, "No_Custer_Serial_Number", PRODINFO_MAX_SSN_LEN);
3248 info->is_adb_enabled = 0;
3249 write_prod_info(info);
3250 }
3251 memcpy(dev, info, sizeof(prod_info));
3252 free(info);
3253 }
3254}
3255#endif
3256/*[20200605][TracyChui] Implement get Serial Number end*/
Shashank Mittal162244e2011-08-08 19:01:25 -07003257
3258void reset_device_info()
3259{
3260 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003261 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07003262 write_device_info(&device);
3263}
3264
3265void set_device_root()
3266{
3267 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003268 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07003269 write_device_info(&device);
3270}
3271
lijuang4ece1e72015-08-14 21:02:36 +08003272/* set device unlock value
3273 * Must check FRP before call this function
3274 * Need to wipe data when unlock status changed
3275 * type 0: oem unlock
3276 * type 1: unlock critical
3277 * status 0: unlock as false
3278 * status 1: lock as true
3279 */
3280void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08003281{
lijuang4ece1e72015-08-14 21:02:36 +08003282 if (type == UNLOCK)
3283 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05303284#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303285 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303286 type == UNLOCK_CRITICAL)
3287 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05303288#endif
lijuang4ece1e72015-08-14 21:02:36 +08003289 write_device_info(&device);
3290}
3291
3292static void set_device_unlock(int type, bool status)
3293{
3294 int is_unlocked = -1;
3295 char response[MAX_RSP_SIZE];
3296
3297 /* check device unlock status if it is as expected */
3298 if (type == UNLOCK)
3299 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05303300#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303301 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303302 type == UNLOCK_CRITICAL)
3303 {
3304 is_unlocked = device.is_unlock_critical;
3305 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303306#endif
lijuang4ece1e72015-08-14 21:02:36 +08003307 if (is_unlocked == status) {
3308 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
3309 fastboot_info(response);
3310 fastboot_okay("");
3311 return;
3312 }
3313
3314 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08003315 if (status && !is_allow_unlock) {
3316 fastboot_fail("oem unlock is not allowed");
3317 return;
3318 }
lijuang21f12f52015-08-22 16:22:19 +08003319
ericlin8ef2adf2020-04-29 14:14:24 +08003320 //<2018/01/09-EricLin, To remove display_unlock_menu for quiet mode.
3321 if(target_build_variant_user()){
lijuang4ece1e72015-08-14 21:02:36 +08003322#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08003323 display_unlock_menu(type, status);
3324 fastboot_okay("");
3325 return;
lijuang4ece1e72015-08-14 21:02:36 +08003326#else
lijuang07c5d6e2018-04-23 18:32:13 +08003327 if (status && type == UNLOCK) {
3328 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
3329 return;
lijuang21f12f52015-08-22 16:22:19 +08003330 }
lijuang07c5d6e2018-04-23 18:32:13 +08003331#endif
ericlin8ef2adf2020-04-29 14:14:24 +08003332 }
3333 //<2018/01/09-EricLin
lijuang4ece1e72015-08-14 21:02:36 +08003334
3335 set_device_unlock_value(type, status);
3336
3337 /* wipe data */
3338 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303339 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08003340 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3341 write_misc(0, &msg, sizeof(msg));
3342
3343 fastboot_okay("");
ericlin8ef2adf2020-04-29 14:14:24 +08003344 //<2018/10/16-EricLin, To remove reset after oem lock/unlock.
3345 //reboot_device(RECOVERY_MODE);
3346 //<2018/10/16-EricLin
lijuang21f12f52015-08-22 16:22:19 +08003347}
3348
tracychui8fd71dc2020-06-11 10:17:13 +08003349/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start*/
3350#if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK)
3351static void set_device_unlock_skip_ui_check(int type, bool status)
3352{
3353 int is_unlocked = -1;
3354 char response[MAX_RSP_SIZE];
3355
3356 /* check device unlock status if it is as expected */
3357 if (type == UNLOCK)
3358 is_unlocked = device.is_unlocked;
3359#if VERIFIED_BOOT || VERIFIED_BOOT_2
3360 if(VB_M <= target_get_vb_version() &&
3361 type == UNLOCK_CRITICAL)
3362 {
3363 is_unlocked = device.is_unlock_critical;
3364 }
3365#endif
3366 if (is_unlocked == status) {
3367 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
3368 fastboot_info(response);
3369 fastboot_okay("");
3370 return;
3371 }
3372
3373 set_device_unlock_value(type, status);
3374 fastboot_okay("");
3375}
3376#endif
tracychui3b4abb72020-06-15 10:50:49 +08003377/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */
tracychui8fd71dc2020-06-11 10:17:13 +08003378
lijuang511a2b52015-08-14 20:50:51 +08003379static bool critical_flash_allowed(const char * entry)
3380{
3381 uint32_t i = 0;
3382 if (entry == NULL)
3383 return false;
3384
3385 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
3386 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
3387 return true;
3388 }
3389 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04003390}
3391
3392#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07003393int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
3394{
3395 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003396 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05303397 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07003398 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05303399 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003400 unsigned int compressed_size = 0;
3401 unsigned int dtb_size = 0;
3402 unsigned int out_avai_len = 0;
3403 unsigned char *out_addr = NULL;
3404 unsigned char *best_match_dt_addr = NULL;
3405 int rc;
3406
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303407 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07003408
Parth Dixit4097b622016-03-15 14:42:27 +05303409#ifndef OSVERSION_IN_BOOTIMAGE
3410 dt_size = hdr->dt_size;
3411#endif
3412
3413 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003414 /* add kernel offset */
3415 dt_image_offset += page_size;
3416 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3417 dt_image_offset += n;
3418
3419 /* add ramdisk offset */
3420 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
3421 dt_image_offset += n;
3422
3423 /* add second offset */
3424 if(hdr->second_size != 0) {
3425 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
3426 dt_image_offset += n;
3427 }
3428
3429 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07003430 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07003431
Deepa Dinamani19648b42013-09-05 17:05:55 -07003432 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003433 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
3434 return -1;
3435 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303436
3437 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
3438 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05303439 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303440 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
3441 return -1;
3442 }
3443
Joel Kingaa335dc2013-06-03 16:11:08 -07003444 /* Find index of device tree within device tree table */
3445 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07003446 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
3447 return -1;
3448 }
3449
Matthew Qin271927e2015-03-31 22:07:07 -04003450 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
3451 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
3452 {
3453 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
3454 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04003455 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003456 rc = decompress(best_match_dt_addr,
3457 dt_entry.size, out_addr, out_avai_len,
3458 &compressed_size, &dtb_size);
3459 if (rc)
3460 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003461 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003462 ASSERT(0);
3463 }
3464
Matthew Qin0b15b322015-05-19 05:20:54 -04003465 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003466 best_match_dt_addr = out_addr;
3467 } else {
3468 dtb_size = dt_entry.size;
3469 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003470 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303471 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3472 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003473 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303474 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003475 return -1;
3476 }
3477
Amol Jadicb524072012-08-09 16:40:18 -07003478 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003479 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003480 } else
3481 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003482
3483 /* Everything looks fine. Return success. */
3484 return 0;
3485}
3486#endif
3487
Brian Swetland9c4c0752009-01-25 16:23:50 -08003488void cmd_boot(const char *arg, void *data, unsigned sz)
3489{
3490 unsigned kernel_actual;
3491 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303492 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003493 uint32_t image_actual;
3494 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303495 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003496 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003497 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003498 int ret = 0;
3499 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003500 unsigned int out_len = 0;
3501 unsigned int out_avai_len = 0;
3502 unsigned char *out_addr = NULL;
3503 uint32_t dtb_offset = 0;
3504 unsigned char *kernel_start_addr = NULL;
3505 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003506 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303507#if VERIFIED_BOOT_2
3508 void *dtbo_image_buf = NULL;
3509 uint32_t dtbo_image_sz = 0;
3510 void *vbmeta_image_buf = NULL;
3511 uint32_t vbmeta_image_sz = 0;
3512#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303513#if !VERIFIED_BOOT_2
3514 uint32_t sig_actual = 0;
3515 uint32_t sig_size = 0;
3516#ifdef MDTP_SUPPORT
3517 static bool is_mdtp_activated = 0;
3518#endif /* MDTP_SUPPORT */
3519#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003520
lijuang2008ff22016-03-07 17:56:27 +08003521#if FBCON_DISPLAY_MSG
3522 /* Exit keys' detection thread firstly */
3523 exit_menu_keys_detection();
3524#endif
3525
Monika Singh292b3e92018-03-17 22:40:23 +05303526#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003527 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003528 {
3529 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003530 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003531 }
3532#endif
3533
Brian Swetland9c4c0752009-01-25 16:23:50 -08003534 if (sz < sizeof(hdr)) {
3535 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003536 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003537 }
3538
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303539 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003540
3541 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003542 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003543
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003544 if(target_is_emmc_boot() && hdr->page_size) {
3545 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003546 page_mask = page_size - 1;
3547 }
3548
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003549 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3550 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303551 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003552#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303553#ifndef OSVERSION_IN_BOOTIMAGE
3554 dt_size = hdr->dt_size;
3555#endif
3556 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003557#endif
3558
3559 image_actual = ADD_OF(page_size, kernel_actual);
3560 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303561 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003562 image_actual = ADD_OF(image_actual, dt_actual);
3563
Kishor PK5134b332017-05-09 17:50:08 +05303564 /* Checking to prevent oob access in read_der_message_length */
3565 if (image_actual > sz) {
3566 fastboot_fail("bootimage header fields are invalid");
3567 goto boot_failed;
3568 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303569
Monika Singh292b3e92018-03-17 22:40:23 +05303570#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303571 /* Pass size of boot partition, as imgsize, to avoid
3572 read fewer bytes error */
3573 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303574
3575 /* load and validate dtbo partition */
3576 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3577
3578 /* load vbmeta partition */
3579 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3580
Monika Singh292b3e92018-03-17 22:40:23 +05303581 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303582
3583 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3584 info.images[IMG_BOOT].imgsize = image_actual;
3585 info.images[IMG_BOOT].name = "boot";
3586 ++info.num_loaded_images;
3587
3588 /* Pass loaded dtbo image */
3589 if (dtbo_image_buf != NULL) {
3590 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3591 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3592 info.images[IMG_DTBO].name = "dtbo";
3593 ++info.num_loaded_images;
3594 }
3595
3596 /* Pass loaded vbmeta image */
3597 if (vbmeta_image_buf != NULL) {
3598 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3599 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3600 info.images[IMG_VBMETA].name = "vbmeta";
3601 ++info.num_loaded_images;
3602 }
3603
Monika Singh292b3e92018-03-17 22:40:23 +05303604 info.multi_slot_boot = partition_multislot_is_supported();
3605 if (load_image_and_auth(&info))
3606 goto boot_failed;
3607 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303608
3609 /* Free the buffer allocated to vbmeta post verification */
3610 if (vbmeta_image_buf != NULL) {
3611 free(vbmeta_image_buf);
3612 --info.num_loaded_images;
3613 }
Monika Singh292b3e92018-03-17 22:40:23 +05303614#else
Kishor PK5134b332017-05-09 17:50:08 +05303615 sig_size = sz - image_actual;
3616
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303617 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303618 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303619 /* Calculate the signature length from boot image */
3620 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303621 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303622 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003623
Monika Singh292b3e92018-03-17 22:40:23 +05303624 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303625 fastboot_fail("bootimage header fields are invalid");
3626 goto boot_failed;
3627 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003628 }
3629
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003630 // Initialize boot state before trying to verify boot.img
3631#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003632 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303633#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003634 /* Handle overflow if the input image size is greater than
3635 * boot image buffer can hold
3636 */
Monika Singh292b3e92018-03-17 22:40:23 +05303637 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003638 {
3639 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003640 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003641 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003642
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003643 /* Verify the boot image
3644 * device & page_size are initialized in aboot_init
3645 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003646 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003647 /* Pass size excluding signature size, otherwise we would try to
3648 * access signature beyond its length
3649 */
Monika Singh292b3e92018-03-17 22:40:23 +05303650 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003651 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003652#ifdef MDTP_SUPPORT
3653 else
3654 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003655 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003656 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003657
3658 if (!is_mdtp_activated) {
3659 ext_partition.partition = MDTP_PARTITION_NONE;
3660 mdtp_fwlock_verify_lock(&ext_partition);
3661 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003662 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003663
Amir Kotzer7c768c02016-04-13 09:08:05 +03003664 /* If mdtp state cannot be validate, block fastboot boot*/
3665 if(mdtp_activated(&is_mdtp_activated)){
3666 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3667 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3668 goto boot_failed;
3669 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003670 if(is_mdtp_activated){
3671 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003672 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003673 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003674#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303675#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003676
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003677#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303678 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303679 {
3680 /* set boot and system versions. */
3681 set_os_version((unsigned char *)data);
3682 // send root of trust
3683 if(!send_rot_command((uint32_t)device.is_unlocked))
3684 ASSERT(0);
3685 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303686#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003687 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003688 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3689 * If not, continue booting.
3690 */
3691 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3692 {
3693 out_addr = (unsigned char *)target_get_scratch_address();
3694 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3695 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303696#if VERIFIED_BOOT_2
3697 if (dtbo_image_sz)
3698 out_avai_len -= DTBO_IMG_BUF;
3699#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003700 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003701 ret = decompress((unsigned char *)(ptr + page_size),
3702 hdr->kernel_size, out_addr, out_avai_len,
3703 &dtb_offset, &out_len);
3704 if (ret)
3705 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003706 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003707 ASSERT(0);
3708 }
3709
Matthew Qin0b15b322015-05-19 05:20:54 -04003710 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003711 kptr = (struct kernel64_hdr *)out_addr;
3712 kernel_start_addr = out_addr;
3713 kernel_size = out_len;
3714 } else {
3715 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3716 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3717 kernel_size = hdr->kernel_size;
3718 }
3719
3720 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003721 * Update the kernel/ramdisk/tags address if the boot image header
3722 * has default values, these default values come from mkbootimg when
3723 * the boot image is flashed using fastboot flash:raw
3724 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003725 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003726
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003727 /* Get virtual addresses since the hdr saves physical addresses. */
3728 hdr->kernel_addr = VA(hdr->kernel_addr);
3729 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3730 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003731
Matthew Qinbb7923d2015-02-09 10:56:09 +08003732 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003733 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003734 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303735 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3736 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3737 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003738 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303739 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003740 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003741 }
3742
Amol Jadicb524072012-08-09 16:40:18 -07003743#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003744 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003745 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003746 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003747
3748 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003749#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303750 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3751 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003752 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303753 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003754 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003755 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003756#endif
3757
3758 /* Load ramdisk & kernel */
3759 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003760 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003761
3762#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303763 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3764 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003765 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303766 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003767 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003768 }
3769
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003770 /*
3771 * If dtb is not found look for appended DTB in the kernel.
3772 * If appended dev tree is found, update the atags with
3773 * memory address to the DTB appended location on RAM.
3774 * Else update with the atags address in the kernel header
3775 */
3776 if (!dtb_copied) {
3777 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003778 dtb = dev_tree_appended((void*)(ptr + page_size),
3779 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003780 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003781 if (!dtb) {
3782 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003783 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003784 }
Amol Jadicb524072012-08-09 16:40:18 -07003785 }
3786#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003787
3788 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003789 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003790
Dima Zavin77e41f32013-03-06 16:10:43 -08003791 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003792 (const char*) hdr->cmdline, board_machtype(),
3793 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003794
3795 /* fastboot already stop, it's no need to show fastboot menu */
3796 return;
3797boot_failed:
3798#if FBCON_DISPLAY_MSG
3799 /* revert to fastboot menu if boot failed */
3800 display_fastboot_menu();
3801#endif
3802 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003803}
3804
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003805void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003806{
3807 struct ptentry *ptn;
3808 struct ptable *ptable;
3809
3810 ptable = flash_get_ptable();
3811 if (ptable == NULL) {
3812 fastboot_fail("partition table doesn't exist");
3813 return;
3814 }
3815
3816 ptn = ptable_find(ptable, arg);
3817 if (ptn == NULL) {
3818 fastboot_fail("unknown partition name");
3819 return;
3820 }
3821
Monika Singh292b3e92018-03-17 22:40:23 +05303822 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3823 dprintf(INFO, "erasing avb_custom_key\n");
3824 if (erase_userkey()) {
3825 fastboot_fail("Erasing avb_custom_key failed");
3826 } else {
3827 fastboot_okay("");
3828 }
3829 return;
3830 }
3831
Dima Zavin214cc642009-01-26 11:16:21 -08003832 if (flash_erase(ptn)) {
3833 fastboot_fail("failed to erase partition");
3834 return;
3835 }
3836 fastboot_okay("");
3837}
3838
Bikas Gurungd48bd242010-09-04 19:54:32 -07003839
3840void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3841{
3842 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003843 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003844 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003845 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303846 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003847
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003848#if VERIFIED_BOOT
3849 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3850 {
3851 if(!device.is_unlocked)
3852 {
3853 fastboot_fail("unlock device to erase keystore");
3854 return;
3855 }
3856 }
3857#endif
3858
Kinson Chikf1a43512011-07-14 11:28:39 -07003859 index = partition_get_index(arg);
3860 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003861 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003862
Monika Singhc4778b72018-05-16 11:16:42 +05303863 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3864 dprintf(INFO, "erasing avb_custom_key\n");
3865 if (erase_userkey()) {
3866 fastboot_fail("Erasing avb_custom_key failed");
3867 } else {
3868 fastboot_okay("");
3869 }
3870 return;
3871 }
3872
Kinson Chikf1a43512011-07-14 11:28:39 -07003873 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003874 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003875 return;
3876 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003877
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003878 lun = partition_get_lun(index);
3879 mmc_set_lun(lun);
3880
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003881 if (platform_boot_dev_isemmc())
3882 {
3883 if (mmc_erase_card(ptn, size)) {
3884 fastboot_fail("failed to erase partition\n");
3885 return;
3886 }
3887 } else {
3888 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3889 size = partition_get_size(index);
3890 if (size > DEFAULT_ERASE_SIZE)
3891 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003892
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003893 /* Simple inefficient version of erase. Just writing
3894 0 in first several blocks */
3895 if (mmc_write(ptn , size, (unsigned int *)out)) {
3896 fastboot_fail("failed to erase partition");
3897 return;
3898 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303899 /*Erase FDE metadata at the userdata footer*/
3900 if(!(strncmp(arg, "userdata", 8)))
3901 {
3902 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3903 memset((void *)footer, 0, FOOTER_SIZE);
3904
3905 size = partition_get_size(index);
3906
3907 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3908 fastboot_fail("failed to erase userdata footer");
3909 free(footer);
3910 return;
3911 }
3912 free(footer);
3913 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003914 }
Monika Singh292b3e92018-03-17 22:40:23 +05303915#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303916 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303917 !(strncmp(arg, "userdata", 8)) &&
3918 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003919 ASSERT(0);
3920#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003921 fastboot_okay("");
3922}
3923
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003924void cmd_erase(const char *arg, void *data, unsigned sz)
3925{
Monika Singh292b3e92018-03-17 22:40:23 +05303926#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003927 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003928 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003929 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003930 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003931 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003932 return;
3933 }
3934 }
3935#endif
3936
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003937 if(target_is_emmc_boot())
3938 cmd_erase_mmc(arg, data, sz);
3939 else
3940 cmd_erase_nand(arg, data, sz);
3941}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003942
Mayank Grover11ff9692018-01-11 11:54:49 +05303943/* Get the size from partiton name */
3944static void get_partition_size(const char *arg, char *response)
3945{
3946 uint64_t ptn = 0;
3947 uint64_t size;
3948 int index = INVALID_PTN;
3949
3950 index = partition_get_index(arg);
3951
3952 if (index == INVALID_PTN)
3953 {
3954 dprintf(CRITICAL, "Invalid partition index\n");
3955 return;
3956 }
3957
3958 ptn = partition_get_offset(index);
3959
3960 if(!ptn)
3961 {
3962 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3963 return;
3964 }
3965
3966 size = partition_get_size(index);
3967
3968 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3969 return;
3970}
3971
Mayank Grover52cd10a2018-03-15 12:57:54 +05303972/* Function to check partition type of a partition*/
3973static fs_signature_type
3974check_partition_fs_signature(const char *arg)
3975{
3976 fs_signature_type ret = NO_FS;
3977 int index;
3978 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303979 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3980 uint32_t sb_blk_offset = 0;
3981 char *sb_buffer = buffer;
3982
Mayank Grover52cd10a2018-03-15 12:57:54 +05303983 if (!sb_buffer)
3984 {
3985 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3986 goto out;
3987 }
3988
3989 /* Read super block */
3990 if ((index = partition_get_index(arg)) < 0)
3991 {
3992 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3993 goto out;
3994 }
3995 ptn = partition_get_offset(index);
3996 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303997 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3998
3999 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05304000 (void *)sb_buffer, mmc_blocksize))
4001 {
4002 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
4003 goto out;
4004 }
4005
Mayank Grover399826a2018-08-27 12:15:15 +05304006 if (sb_blk_offset == 0)
4007 sb_buffer += FS_SUPERBLOCK_OFFSET;
4008
4009 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05304010 {
4011 dprintf(SPEW, "%s() Found EXT FS\n", arg);
4012 ret = EXT_FS_SIGNATURE;
4013 }
Mayank Grover399826a2018-08-27 12:15:15 +05304014 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05304015 {
4016 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
4017 ret = EXT_F2FS_SIGNATURE;
4018 }
4019 else
4020 {
4021 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
4022 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
4023 ret = NO_FS;
4024 }
4025
4026out:
Mayank Grover399826a2018-08-27 12:15:15 +05304027 if(buffer)
4028 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304029 return ret;
4030}
4031
Mayank Groverd38fe012018-03-13 15:33:16 +05304032/* Function to get partition type */
4033static void get_partition_type(const char *arg, char *response)
4034{
4035 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05304036 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05304037
4038 if (arg == NULL ||
4039 response == NULL)
4040 {
4041 dprintf(CRITICAL, "Invalid input parameter\n");
4042 return;
4043 }
4044
4045 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05304046 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05304047
4048 /* Mark partiton type for known paritions only */
4049 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
4050 {
4051 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
4052 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05304053 /* Check partition for FS signature */
4054 fs_signature = check_partition_fs_signature(arg);
4055 switch (fs_signature)
4056 {
4057 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05304058 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304059 break;
4060 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05304061 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304062 break;
4063 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05304064 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304065 }
Mayank Groverd38fe012018-03-13 15:33:16 +05304066 }
4067 }
4068 return;
4069}
4070
Mayank Grover11ff9692018-01-11 11:54:49 +05304071/*
4072 * Publish the partition type & size info
4073 * fastboot getvar will publish the required information.
4074 * fastboot getvar partition_size:<partition_name>: partition size in hex
4075 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
4076 */
4077static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
4078{
Mayank Groverd38fe012018-03-13 15:33:16 +05304079 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05304080 static bool published = false;
4081 struct partition_entry *ptn_entry =
4082 partition_get_partition_entries();
4083 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
4084
4085 for (i = 0; i < num_parts; i++) {
4086 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
4087 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
4088 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
4089
Mayank Groverd38fe012018-03-13 15:33:16 +05304090 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05304091 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05304092 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4093 {
4094 dprintf(CRITICAL, "partition size name truncated\n");
4095 return;
4096 }
4097 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4098 {
4099 dprintf(CRITICAL, "partition type name truncated\n");
4100 return;
4101 }
4102
4103 if (!published)
4104 {
4105 /* publish partition size & type info */
4106 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
4107 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
4108 }
4109 }
4110 if (!published)
4111 published = true;
4112}
4113
4114
Ajay Dudani5c761132011-04-07 20:19:04 -07004115void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07004116{
4117 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07004118 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07004119 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004120 char *token = NULL;
4121 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004122 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004123 uint8_t lun = 0;
4124 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05304125 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07004126
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004127 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004128 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004129 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004130 if(token)
4131 {
4132 lun = atoi(token);
4133 mmc_set_lun(lun);
4134 lun_set = true;
4135 }
4136
Mao Jinlong226f33a2014-07-04 17:24:10 +08004137 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07004138 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07004139 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
4140 {
4141 if (!aboot_frp_unlock(pname, data, sz))
4142 {
4143 fastboot_info("FRP unlock successful");
4144 fastboot_okay("");
4145 }
4146 else
4147 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
4148
4149 return;
4150 }
4151
Mao Jinlong226f33a2014-07-04 17:24:10 +08004152 if (!strcmp(pname, "partition"))
4153 {
4154 dprintf(INFO, "Attempt to write partition image.\n");
4155 if (write_partition(sz, (unsigned char *) data)) {
4156 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07004157 return;
4158 }
Mayank Grover11ff9692018-01-11 11:54:49 +05304159 /* Re-publish partition table */
4160 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05304161
4162 /* Rescan partition table to ensure we have multislot support*/
4163 if (partition_scan_for_multislot())
4164 {
4165 current_active_slot = partition_find_active_slot();
4166 dprintf(INFO, "Multislot supported: Slot %s active",
4167 (SUFFIX_SLOT(current_active_slot)));
4168 }
4169 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07004170 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08004171 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004172 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004173#if VERIFIED_BOOT
4174 if(!strcmp(pname, KEYSTORE_PTN_NAME))
4175 {
4176 if(!device.is_unlocked)
4177 {
4178 fastboot_fail("unlock device to flash keystore");
4179 return;
4180 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05304181 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004182 {
4183 fastboot_fail("image is not a keystore file");
4184 return;
4185 }
4186 }
4187#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08004188 index = partition_get_index(pname);
4189 ptn = partition_get_offset(index);
4190 if(ptn == 0) {
4191 fastboot_fail("partition table doesn't exist");
4192 return;
4193 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004194
Mayank Grover351a75e2017-05-30 20:06:08 +05304195 if (!strncmp(pname, "boot", strlen("boot"))
4196 || !strcmp(pname, "recovery"))
4197 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08004198 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
4199 fastboot_fail("image is not a boot image");
4200 return;
4201 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304202
4203 /* Reset multislot_partition attributes in case of flashing boot */
4204 if (partition_multislot_is_supported())
4205 {
4206 partition_reset_attributes(index);
4207 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08004208 }
4209
4210 if(!lun_set)
4211 {
4212 lun = partition_get_lun(index);
4213 mmc_set_lun(lun);
4214 }
4215
4216 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304217 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08004218 fastboot_fail("size too large");
4219 return;
4220 }
4221 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
4222 fastboot_fail("flash write failure");
4223 return;
4224 }
Greg Grisco6e754772011-06-23 12:19:39 -07004225 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07004226 }
4227 fastboot_okay("");
4228 return;
4229}
4230
Ajay Dudanide984792015-03-02 09:57:41 -08004231void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
4232{
4233 int i, images;
4234 meta_header_t *meta_header;
4235 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05304236 /*End of the image address*/
4237 uintptr_t data_end;
4238
4239 if( (UINT_MAX - sz) > (uintptr_t)data )
4240 data_end = (uintptr_t)data + sz;
4241 else
4242 {
4243 fastboot_fail("Cannot flash: image header corrupt");
4244 return;
4245 }
4246
4247 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
4248 {
4249 fastboot_fail("Cannot flash: image header corrupt");
4250 return;
4251 }
Ajay Dudanide984792015-03-02 09:57:41 -08004252
lijuang8ee21882016-04-19 16:57:11 +08004253 /* If device is locked:
4254 * Forbid to flash image to avoid the device to bypass the image
4255 * which with "any" name other than bootloader. Because it maybe
4256 * a meta package of all partitions.
4257 */
Monika Singh292b3e92018-03-17 22:40:23 +05304258#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08004259 if (target_build_variant_user()) {
4260 if (!device.is_unlocked) {
4261 fastboot_fail("Device is locked, meta image flashing is not allowed");
4262 return;
4263 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304264
Mayank Grover912eaa62017-10-26 12:08:53 +05304265 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304266 !device.is_unlock_critical)
4267 {
lijuang8ee21882016-04-19 16:57:11 +08004268 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
4269 return;
4270 }
lijuang8ee21882016-04-19 16:57:11 +08004271 }
4272#endif
4273
Ajay Dudanide984792015-03-02 09:57:41 -08004274 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05304275 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
4276 {
4277 fastboot_fail("Cannot flash: image header corrupt");
4278 return;
4279 }
Ajay Dudanide984792015-03-02 09:57:41 -08004280 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
4281
4282 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
4283
4284 for (i=0; i<images; i++) {
4285
4286 if((img_header_entry[i].ptn_name == NULL) ||
4287 (img_header_entry[i].start_offset == 0) ||
4288 (img_header_entry[i].size == 0))
4289 break;
Kishor PK49831502017-04-21 17:55:43 +05304290 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
4291 fastboot_fail("Integer overflow detected in start_offset of img");
4292 break;
4293 }
4294 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
4295 fastboot_fail("Integer overflow detected in size of img");
4296 break;
4297 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05304298 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
4299 + img_header_entry[i].size) )
4300 {
4301 fastboot_fail("Cannot flash: image size mismatch");
4302 break;
4303 }
4304
Ajay Dudanide984792015-03-02 09:57:41 -08004305 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
4306 (void *) data + img_header_entry[i].start_offset,
4307 img_header_entry[i].size);
4308 }
4309
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004310 if (!strncmp(arg, "bootloader", strlen("bootloader")))
4311 {
4312 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
4313 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
4314 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
4315 }
4316 else
4317 {
4318 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
4319 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
4320 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
4321 }
4322
4323 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08004324 fastboot_okay("");
4325 return;
4326}
4327
Ajay Dudani5c761132011-04-07 20:19:04 -07004328void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
4329{
4330 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04004331 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004332 uint32_t *fill_buf = NULL;
4333 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05304334 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004335 sparse_header_t *sparse_header;
4336 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07004337 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004338 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304339 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07004340 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05304341 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004342 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05304343 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05304344 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004345
Kinson Chikf1a43512011-07-14 11:28:39 -07004346 index = partition_get_index(arg);
4347 ptn = partition_get_offset(index);
4348 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07004349 fastboot_fail("partition table doesn't exist");
4350 return;
4351 }
4352
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304353 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304354
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004355 lun = partition_get_lun(index);
4356 mmc_set_lun(lun);
4357
vijay kumar800255e2015-04-24 20:26:19 +05304358 if (sz < sizeof(sparse_header_t)) {
4359 fastboot_fail("size too low");
4360 return;
4361 }
4362
Ajay Dudani5c761132011-04-07 20:19:04 -07004363 /* Read and skip over sparse image header */
4364 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05304365
Mayank Grover48bd9bb2017-07-19 12:04:16 +05304366 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
4367 fastboot_fail("Invalid block size\n");
4368 return;
4369 }
4370
vijay kumar1321f342015-03-27 12:13:42 +05304371 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08004372 fastboot_fail("size too large");
4373 return;
4374 }
4375
vijay kumarde4fcf62015-04-23 13:05:49 +05304376 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05304377
Parth Dixit64b1a482016-03-07 16:31:26 +05304378 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304379 fastboot_fail("buffer overreads occured due to invalid sparse header");
4380 return;
4381 }
4382
vijay kumarde4fcf62015-04-23 13:05:49 +05304383 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004384 {
vijay kumarde4fcf62015-04-23 13:05:49 +05304385 fastboot_fail("sparse header size mismatch");
4386 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004387 }
4388
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004389 dprintf (SPEW, "=== Sparse Image Header ===\n");
4390 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
4391 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
4392 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
4393 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
4394 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
4395 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
4396 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
4397 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07004398
4399 /* Start processing chunks */
4400 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
4401 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304402 /* Make sure the total image size does not exceed the partition size */
4403 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
4404 fastboot_fail("size too large");
4405 return;
4406 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004407 /* Read and skip over chunk header */
4408 chunk_header = (chunk_header_t *) data;
4409 data += sizeof(chunk_header_t);
4410
Parth Dixit64b1a482016-03-07 16:31:26 +05304411 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304412 fastboot_fail("buffer overreads occured due to invalid sparse header");
4413 return;
4414 }
4415
Ajay Dudani5c761132011-04-07 20:19:04 -07004416 dprintf (SPEW, "=== Chunk Header ===\n");
4417 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
4418 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
4419 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
4420
vijay kumar800255e2015-04-24 20:26:19 +05304421 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004422 {
vijay kumar800255e2015-04-24 20:26:19 +05304423 fastboot_fail("chunk header size mismatch");
4424 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004425 }
4426
wufeng.jiang813dc352015-06-02 23:02:46 -04004427 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
4428
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304429 /* Make sure that the chunk size calculated from sparse image does not
4430 * exceed partition size
4431 */
4432 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
4433 {
4434 fastboot_fail("Chunk data size exceeds partition size");
4435 return;
4436 }
4437
Ajay Dudani5c761132011-04-07 20:19:04 -07004438 switch (chunk_header->chunk_type)
4439 {
4440 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04004441 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07004442 chunk_data_sz))
4443 {
4444 fastboot_fail("Bogus chunk size for chunk type Raw");
4445 return;
4446 }
4447
Parth Dixit64b1a482016-03-07 16:31:26 +05304448 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304449 fastboot_fail("buffer overreads occured due to invalid sparse header");
4450 return;
4451 }
4452
wufeng.jiang813dc352015-06-02 23:02:46 -04004453 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
4454 otherwise it will return in the line above
4455 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07004456 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04004457 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07004458 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07004459 {
4460 fastboot_fail("flash write failure");
4461 return;
4462 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304463 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4464 fastboot_fail("Bogus size for RAW chunk type");
4465 return;
4466 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004467 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004468 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004469 break;
4470
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004471 case CHUNK_TYPE_FILL:
4472 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4473 sizeof(uint32_t)))
4474 {
4475 fastboot_fail("Bogus chunk size for chunk type FILL");
4476 return;
4477 }
4478
Mayank Grover4f50bba2017-07-19 18:17:08 +05304479 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4480 /* Integer overflow detected */
4481 if (blk_sz_actual < sparse_header->blk_sz)
4482 {
4483 fastboot_fail("Invalid block size");
4484 return;
4485 }
4486
4487 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004488 if (!fill_buf)
4489 {
4490 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4491 return;
4492 }
4493
Parth Dixit64b1a482016-03-07 16:31:26 +05304494 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304495 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304496 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304497 return;
4498 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004499 fill_val = *(uint32_t *)data;
4500 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004501
4502 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4503 {
4504 fill_buf[i] = fill_val;
4505 }
4506
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304507 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4508 {
4509 fastboot_fail("bogus size for chunk FILL type");
4510 free(fill_buf);
4511 return;
4512 }
4513
wufeng.jiang813dc352015-06-02 23:02:46 -04004514 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004515 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304516 /* Make sure that the data written to partition does not exceed partition size */
4517 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4518 {
4519 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304520 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304521 return;
4522 }
4523
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004524 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4525 sparse_header->blk_sz,
4526 fill_buf))
4527 {
4528 fastboot_fail("flash write failure");
4529 free(fill_buf);
4530 return;
4531 }
4532
4533 total_blocks++;
4534 }
4535
4536 free(fill_buf);
4537 break;
4538
Ajay Dudani5c761132011-04-07 20:19:04 -07004539 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304540 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4541 fastboot_fail("bogus size for chunk DONT CARE type");
4542 return;
4543 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004544 total_blocks += chunk_header->chunk_sz;
4545 break;
4546
Ajay Dudani5c761132011-04-07 20:19:04 -07004547 case CHUNK_TYPE_CRC:
4548 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4549 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004550 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004551 return;
4552 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304553 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4554 fastboot_fail("bogus size for chunk CRC type");
4555 return;
4556 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004557 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304558 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304559 fastboot_fail("integer overflow occured");
4560 return;
4561 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004562 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304563 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304564 fastboot_fail("buffer overreads occured due to invalid sparse header");
4565 return;
4566 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004567 break;
4568
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004569 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004570 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004571 fastboot_fail("Unknown chunk type");
4572 return;
4573 }
4574 }
4575
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004576 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4577 total_blocks, sparse_header->total_blks);
4578
4579 if(total_blocks != sparse_header->total_blks)
4580 {
4581 fastboot_fail("sparse image write failure");
4582 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004583
4584 fastboot_okay("");
4585 return;
4586}
4587
4588void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4589{
4590 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004591 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004592
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004593#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004594 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4595 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004596 int ret=0;
4597 uint32 major_version=0;
4598 uint32 minor_version=0;
4599
4600 ret = scm_svc_version(&major_version,&minor_version);
4601 if(!ret)
4602 {
4603 if(major_version >= 2)
4604 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004605 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004606 {
4607 ret = encrypt_scm((uint32 **) &data, &sz);
4608 if (ret != 0) {
4609 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4610 return;
4611 }
4612
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004613 /* Protect only for SSD */
4614 if (!strcmp(arg, "ssd")) {
4615 ret = scm_protect_keystore((uint32 *) data, sz);
4616 if (ret != 0) {
4617 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4618 return;
4619 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004620 }
4621 }
4622 else
4623 {
4624 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4625 if(ret != 0)
4626 {
4627 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4628 return;
4629 }
4630 }
4631 }
4632 else
4633 {
4634 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4635 magic_number[1] == DECRYPT_MAGIC_1)
4636 {
4637 ret = decrypt_scm((uint32 **) &data, &sz);
4638 if (ret != 0) {
4639 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4640 return;
4641 }
4642 }
4643 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4644 magic_number[1] == ENCRYPT_MAGIC_1)
4645 {
4646 ret = encrypt_scm((uint32 **) &data, &sz);
4647 if (ret != 0) {
4648 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4649 return;
4650 }
4651 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004652 }
4653 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004654 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004655 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004656 dprintf(CRITICAL,"INVALID SVC Version\n");
4657 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004658 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004659#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004660
Monika Singh292b3e92018-03-17 22:40:23 +05304661#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004662 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004663 {
lijuang511a2b52015-08-14 20:50:51 +08004664 /* if device is locked:
4665 * common partition will not allow to be flashed
4666 * critical partition will allow to flash image.
4667 */
4668 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4669 fastboot_fail("Partition flashing is not allowed");
4670 return;
4671 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304672
lijuang511a2b52015-08-14 20:50:51 +08004673 /* if device critical is locked:
4674 * common partition will allow to be flashed
4675 * critical partition will not allow to flash image.
4676 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304677 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304678 !device.is_unlock_critical &&
4679 critical_flash_allowed(arg)) {
4680 fastboot_fail("Critical partition flashing is not allowed");
4681 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004682 }
4683 }
4684#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304685 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4686 dprintf(INFO, "flashing avb_custom_key\n");
4687 if (store_userkey(data, sz)) {
4688 fastboot_fail("Flashing avb_custom_key failed");
4689 } else {
4690 fastboot_okay("");
4691 }
4692 return;
4693 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004694
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004695 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004696 meta_header = (meta_header_t *) data;
4697 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004698 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004699 else if (meta_header->magic == META_HEADER_MAGIC)
4700 cmd_flash_meta_img(arg, data, sz);
4701 else
4702 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004703
4704#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304705 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304706 (!strncmp(arg, "system", 6)) &&
4707 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004708 // reset dm_verity mode to enforcing
4709 device.verity_mode = 1;
4710 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304711#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004712
Ajay Dudani5c761132011-04-07 20:19:04 -07004713 return;
4714}
4715
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004716void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4717{
4718 struct ptentry *sys_ptn;
4719 struct ptable *ptable;
4720
4721 ptable = flash_get_ptable();
4722 if (ptable == NULL) {
4723 fastboot_fail("partition table doesn't exist");
4724 return;
4725 }
4726
4727 sys_ptn = ptable_find(ptable, "system");
4728 if (sys_ptn == NULL) {
4729 fastboot_fail("system partition not found");
4730 return;
4731 }
4732
4733 sz = ROUND_TO_PAGE(sz, page_mask);
4734 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4735 fastboot_fail("update_ubi_vol failed");
4736 else
4737 fastboot_okay("");
4738}
4739
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004740void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004741{
4742 struct ptentry *ptn;
4743 struct ptable *ptable;
4744 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304745 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304746 unsigned bytes_to_round_page = 0;
4747 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004748
Kishor PK38ed93d2017-04-25 14:19:26 +05304749 if((uintptr_t)data > (UINT_MAX - sz)) {
4750 fastboot_fail("Cannot flash: image header corrupt");
4751 return;
4752 }
4753
Dima Zavin214cc642009-01-26 11:16:21 -08004754 ptable = flash_get_ptable();
4755 if (ptable == NULL) {
4756 fastboot_fail("partition table doesn't exist");
4757 return;
4758 }
4759
4760 ptn = ptable_find(ptable, arg);
4761 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004762 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4763 arg);
4764 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004765 return;
4766 }
4767
Monika Singh292b3e92018-03-17 22:40:23 +05304768 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4769 dprintf(INFO, "flashing avb_custom_key\n");
4770 if (store_userkey(data, sz)) {
4771 fastboot_fail("Flashing avb_custom_key failed");
4772 } else {
4773 fastboot_okay("");
4774 }
4775 return;
4776 }
4777
Dima Zavin214cc642009-01-26 11:16:21 -08004778 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304779 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4780 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4781 } else {
4782 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004783 return;
4784 }
4785 }
4786
Amol Jadi5c61a952012-05-04 17:05:35 -07004787 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004788 || !strcmp(ptn->name, "userdata")
4789 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004790 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004791 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004792 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304793 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304794 rounded_size = ROUNDUP(sz, page_size);
4795 bytes_to_round_page = rounded_size - sz;
4796 if (bytes_to_round_page) {
4797 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4798 fastboot_fail("Integer overflow detected");
4799 return;
4800 }
4801 if (((uintptr_t)data + sz + bytes_to_round_page) >
4802 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4803 fastboot_fail("Buffer size is not aligned to page_size");
4804 return;
4805 }
4806 else {
4807 memset(data + sz, 0, bytes_to_round_page);
4808 sz = rounded_size;
4809 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304810 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304811 }
4812
Kishor PK38ed93d2017-04-25 14:19:26 +05304813 /*Checking partition_size for the possible integer overflow */
4814 partition_size = validate_partition_size(ptn);
4815
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304816 if (sz > partition_size) {
4817 fastboot_fail("Image size too large");
4818 return;
4819 }
4820
Dima Zavin214cc642009-01-26 11:16:21 -08004821 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304822 if ((sz > UBI_EC_HDR_SIZE) &&
4823 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004824 if (flash_ubi_img(ptn, data, sz)) {
4825 fastboot_fail("flash write failure");
4826 return;
4827 }
4828 } else {
4829 if (flash_write(ptn, extra, data, sz)) {
4830 fastboot_fail("flash write failure");
4831 return;
4832 }
Dima Zavin214cc642009-01-26 11:16:21 -08004833 }
4834 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4835 fastboot_okay("");
4836}
4837
Kishor PK38ed93d2017-04-25 14:19:26 +05304838
4839static inline uint64_t validate_partition_size(struct ptentry *ptn)
4840{
4841 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4842 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4843 return ptn->length * flash_num_pages_per_blk() * page_size;
4844 }
4845 }
4846 return 0;
4847}
4848
4849
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004850void cmd_flash(const char *arg, void *data, unsigned sz)
4851{
4852 if(target_is_emmc_boot())
4853 cmd_flash_mmc(arg, data, sz);
4854 else
4855 cmd_flash_nand(arg, data, sz);
4856}
4857
Dima Zavin214cc642009-01-26 11:16:21 -08004858void cmd_continue(const char *arg, void *data, unsigned sz)
4859{
4860 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004861 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004862
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004863 if (target_is_emmc_boot())
4864 {
lijuanga40d6302015-07-20 20:10:13 +08004865#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004866 /* Exit keys' detection thread firstly */
4867 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004868#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004869 boot_linux_from_mmc();
4870 }
4871 else
4872 {
4873 boot_linux_from_flash();
4874 }
Dima Zavin214cc642009-01-26 11:16:21 -08004875}
4876
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004877void cmd_reboot(const char *arg, void *data, unsigned sz)
4878{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004879 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004880 fastboot_okay("");
4881 reboot_device(0);
4882}
4883
Mayank Grover351a75e2017-05-30 20:06:08 +05304884void cmd_set_active(const char *arg, void *data, unsigned sz)
4885{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304886 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304887 unsigned i,current_active_slot;
4888 const char *current_slot_suffix;
4889
4890 if (!partition_multislot_is_supported())
4891 {
4892 fastboot_fail("Command not supported");
4893 return;
4894 }
4895
4896 if (arg)
4897 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304898 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304899 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304900 {
4901 current_active_slot = partition_find_active_slot();
4902
4903 /* Check if trying to make curent slot active */
4904 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304905 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4906 (char *)suffix_delimiter, &sp);
4907
Mayank Grover5eb5f692017-07-19 20:16:04 +05304908 if (current_slot_suffix &&
4909 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304910 {
4911 fastboot_okay("Slot already set active");
4912 return;
4913 }
4914 else
4915 {
4916 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4917 {
4918 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304919 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4920 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304921 if (current_slot_suffix &&
4922 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304923 {
lijuang8b03e5f2019-07-12 18:16:19 +08004924 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304925 publish_getvar_multislot_vars();
4926 fastboot_okay("");
4927 return;
4928 }
4929 }
4930 }
4931 }
4932 }
4933 fastboot_fail("Invalid slot suffix.");
4934 return;
4935}
4936
Mayank Grover98c4c742019-04-25 17:21:37 +05304937#if DYNAMIC_PARTITION_SUPPORT
4938void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4939{
4940 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4941 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4942 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4943 fastboot_fail("Failed to update recovery command");
4944 return;
4945 }
4946 fastboot_okay("");
4947 reboot_device(REBOOT_MODE_UNKNOWN);
4948
4949 //shouldn't come here.
4950 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4951 return;
4952}
4953
4954void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4955{
4956 dprintf(INFO, "rebooting the device - recovery\n");
4957 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
4958 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4959 fastboot_fail("Failed to update recovery command");
4960 return;
4961 }
4962 fastboot_okay("");
4963 reboot_device(REBOOT_MODE_UNKNOWN);
4964
4965 //shouldn't come here.
4966 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4967 return;
4968}
4969#endif
4970
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004971void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4972{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004973 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004974 fastboot_okay("");
4975 reboot_device(FASTBOOT_MODE);
4976}
4977
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004978void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4979{
4980 dprintf(INFO, "Enabling charger screen check\n");
4981 device.charger_screen_enabled = 1;
4982 write_device_info(&device);
4983 fastboot_okay("");
4984}
4985
4986void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4987{
4988 dprintf(INFO, "Disabling charger screen check\n");
4989 device.charger_screen_enabled = 0;
4990 write_device_info(&device);
4991 fastboot_okay("");
4992}
4993
tracychuid184b912020-06-05 17:31:38 +08004994/*[20200605][TracyChui] Implement get Serial Number start*/
4995#if defined(ENABLE_PRODINFO_ACCESS)
4996void CmdOemEnableAdb(const char *arg, void *data, unsigned size)
4997{
4998 dprintf(INFO, "Enabling Adb\n");
4999 prod.is_adb_enabled = 1;
5000 write_prod_info(&prod);
5001 fastboot_okay("");
5002}
5003#endif
5004/*[20200605][TracyChui] Implement get Serial Number end*/
5005
lijuanga25d8bb2015-09-16 13:11:52 +08005006void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
5007{
5008 char *p = NULL;
5009 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05305010 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08005011
5012 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05305013 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08005014 if (p) {
5015 if (!strncmp(p, "0", 1)) {
5016 device.charger_screen_enabled = 0;
5017 } else if (!strncmp(p, "1", 1)) {
5018 device.charger_screen_enabled = 1;
5019 }
5020 }
5021 }
5022
5023 /* update charger_screen_enabled value for getvar
5024 * command
5025 */
5026 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5027 device.charger_screen_enabled);
5028
5029 write_device_info(&device);
5030 fastboot_okay("");
5031}
5032
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305033void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
5034{
5035 dprintf(INFO, "Selecting display panel %s\n", arg);
5036 if (arg)
5037 strlcpy(device.display_panel, arg,
5038 sizeof(device.display_panel));
5039 write_device_info(&device);
5040 fastboot_okay("");
5041}
5042
Shashank Mittal162244e2011-08-08 19:01:25 -07005043void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
5044{
lijuang4ece1e72015-08-14 21:02:36 +08005045 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05305046}
5047
tracychui8fd71dc2020-06-11 10:17:13 +08005048/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start*/
5049#if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK)
5050void cmd_oem_unlock_skip_ui_check(const char *arg, void *data, unsigned sz)
5051{
5052 set_device_unlock_skip_ui_check(UNLOCK, TRUE);
5053}
5054#endif
5055/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */
5056
vijay kumarc65876c2015-04-24 13:29:16 +05305057void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
5058{
lijuang4ece1e72015-08-14 21:02:36 +08005059 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05305060 if(!is_allow_unlock) {
5061 fastboot_fail("oem unlock is not allowed");
5062 return;
5063 }
5064
lijuang4ece1e72015-08-14 21:02:36 +08005065 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05305066
lijuang4ece1e72015-08-14 21:02:36 +08005067 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05305068 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05305069 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05305070 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
5071 write_misc(0, &msg, sizeof(msg));
5072
5073 fastboot_okay("");
5074 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07005075 }
5076 fastboot_okay("");
5077}
5078
Channagoud Kadabiad259832015-05-29 11:14:17 -07005079static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
5080{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305081 int ret=1;
5082 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07005083
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305084 /*
5085 Authentication method not implemented.
5086
5087 OEM to implement, authentication system which on successful validataion,
5088 calls write_allow_oem_unlock() with is_allow_unlock.
5089 */
5090#if 0
5091 authentication_success = oem_specific_auth_mthd();
5092#endif
5093
5094 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07005095 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305096 is_allow_unlock = true;
5097 write_allow_oem_unlock(is_allow_unlock);
5098 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07005099 }
5100 return ret;
5101}
5102
Shashank Mittald3e54dd2014-08-28 15:24:02 -07005103void cmd_oem_lock(const char *arg, void *data, unsigned sz)
5104{
lijuang4ece1e72015-08-14 21:02:36 +08005105 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07005106}
5107
tracychui8fd71dc2020-06-11 10:17:13 +08005108/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start*/
5109#if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK)
5110void cmd_oem_lock_skip_ui_check(const char *arg, void *data, unsigned sz)
5111{
5112 set_device_unlock_skip_ui_check(UNLOCK, FALSE);
5113}
5114#endif
5115/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */
5116
Shashank Mittala0032282011-08-26 14:50:11 -07005117void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
5118{
lijuang511a2b52015-08-14 20:50:51 +08005119 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005120 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07005121 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005122 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
5123 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05305124#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305125 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05305126 {
5127 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
5128 (device.is_unlock_critical ? "true" : "false"));
5129 fastboot_info(response);
5130 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305131#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005132 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07005133 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305134 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
5135 fastboot_info(response);
tracychuid184b912020-06-05 17:31:38 +08005136/*[20200605][TracyChui] Implement get Serial Number start*/
5137#if defined(ENABLE_PRODINFO_ACCESS)
5138 snprintf(response, sizeof(response), "\tAdb enabled: %s", prod.is_adb_enabled ? "true" : "false");
5139 fastboot_info(response);
5140#endif
5141/*[20200605][TracyChui] Implement get Serial Number end*/
Shashank Mittala0032282011-08-26 14:50:11 -07005142 fastboot_okay("");
5143}
5144
lijuang511a2b52015-08-14 20:50:51 +08005145void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
5146{
5147 char response[MAX_RSP_SIZE];
5148 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
5149 fastboot_info(response);
5150 fastboot_okay("");
5151}
5152
5153void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
5154{
lijuang4ece1e72015-08-14 21:02:36 +08005155 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08005156}
5157
5158void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
5159{
lijuang4ece1e72015-08-14 21:02:36 +08005160 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08005161}
5162
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005163void cmd_preflash(const char *arg, void *data, unsigned sz)
5164{
5165 fastboot_okay("");
5166}
5167
Mao Flynn7b379f32015-04-20 00:28:30 +08005168static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305169
Mao Flynn7b379f32015-04-20 00:28:30 +08005170int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305171{
Mao Flynn7b379f32015-04-20 00:28:30 +08005172 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305173 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08005174 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305175 return -1;
5176 return 0;
5177}
5178
Mao Flynn7b379f32015-04-20 00:28:30 +08005179int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005180{
5181 struct ptentry *ptn;
5182 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08005183 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005184 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08005185
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305186 ptable = flash_get_ptable();
5187 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08005188 dprintf(CRITICAL, "ERROR: Partition table not found\n");
5189 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305190 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005191
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305192 ptn = ptable_find(ptable, "splash");
5193 if (ptn == NULL) {
5194 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005195 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305196 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005197 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305198 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005199 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305200 }
5201
Mao Flynn7b379f32015-04-20 00:28:30 +08005202 header = (struct logo_img_header *)logo_header;
5203 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305204 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005205 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305206 }
5207
5208 fb_display = fbcon_display();
5209 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05305210 if (header->type && (header->blocks != 0) &&
5211 (UINT_MAX >= header->blocks * 512) &&
5212 ((header->blocks * 512) <= (fb_display->width *
5213 fb_display->height * (fb_display->bpp / 8)))) {
5214 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08005215 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
5216
5217 /* if the logo is full-screen size, remove "fbcon_clear()" */
5218 if ((header->width != fb_display->width)
5219 || (header->height != fb_display->height))
5220 fbcon_clear();
5221
5222 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
5223 (uint32_t *)base,
5224 (header->blocks * 512))) {
5225 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5226 return -1;
5227 }
5228 fbcon_extract_to_screen(header, base);
5229 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005230 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005231
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07005232 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
5233 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005234 return -1;
5235 }
5236
Vineet Bajaj99291ed2014-09-09 12:29:46 +05305237 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305238 uint32_t fb_size = ROUNDUP(fb_display->width *
5239 fb_display->height *
5240 (fb_display->bpp / 8), 4096);
5241 uint32_t splash_size = ((((header->width * header->height *
5242 fb_display->bpp/8) + 511) >> 9) << 9);
5243
5244 if (splash_size > fb_size) {
5245 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5246 return -1;
5247 }
5248
Mao Flynn7b379f32015-04-20 00:28:30 +08005249 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
5250 (uint32_t *)base,
5251 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305252 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05305253 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005254 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005255 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305256 }
5257
Mao Flynn7b379f32015-04-20 00:28:30 +08005258 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305259}
5260
Mao Flynn7b379f32015-04-20 00:28:30 +08005261int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305262{
5263 int index = INVALID_PTN;
5264 unsigned long long ptn = 0;
5265 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08005266 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08005267 uint32_t blocksize, realsize, readsize;
5268 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305269
5270 index = partition_get_index("splash");
5271 if (index == 0) {
5272 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005273 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305274 }
5275
5276 ptn = partition_get_offset(index);
5277 if (ptn == 0) {
5278 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005279 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305280 }
5281
Mao Flynn1893a7f2015-06-03 12:03:36 +08005282 mmc_set_lun(partition_get_lun(index));
5283
5284 blocksize = mmc_get_device_blocksize();
5285 if (blocksize == 0) {
5286 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
5287 return -1;
5288 }
5289
5290 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07005291 if (!fb_display)
5292 {
5293 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
5294 return -1;
5295 }
5296
Mao Flynn1893a7f2015-06-03 12:03:36 +08005297 base = (uint8_t *) fb_display->base;
5298
jialongjhan1efba002020-05-22 18:25:56 +08005299 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 Start
5300 if (mmc_read(ptn , (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305301 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005302 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305303 }
jialongjhan1efba002020-05-22 18:25:56 +08005304 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 End
Mao Flynn1893a7f2015-06-03 12:03:36 +08005305 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08005306 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305307 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005308 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305309 }
5310
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305311 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05305312 if (header->type && (header->blocks != 0) &&
5313 (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
5314 ((header->blocks * 512) <= (fb_display->width *
5315 fb_display->height * (fb_display->bpp / 8)))) {
5316 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08005317 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08005318
Mao Flynn1893a7f2015-06-03 12:03:36 +08005319 realsize = header->blocks * 512;
5320 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
5321
5322 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08005323 if ((header->width != fb_display->width)
5324 || (header->height != fb_display->height))
5325 fbcon_clear();
5326
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305327 uint32_t fb_size = ROUNDUP(fb_display->width *
5328 fb_display->height *
5329 (fb_display->bpp / 8), 4096);
5330
5331 if (readsize > fb_size) {
5332 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5333 return -1;
5334 }
5335
jialongjhan1efba002020-05-22 18:25:56 +08005336 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 Start
5337 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08005338 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5339 return -1;
5340 }
jialongjhan1efba002020-05-22 18:25:56 +08005341 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 End
Mao Flynn7b379f32015-04-20 00:28:30 +08005342
Mao Flynn1893a7f2015-06-03 12:03:36 +08005343 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
5344 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305345
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07005346 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
5347 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08005348 return -1;
5349 }
5350
5351 realsize = header->width * header->height * fb_display->bpp / 8;
5352 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
5353
5354 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05305355 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08005356 fbcon_clear();
5357 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5358 return -1;
5359 }
5360 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05305361 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08005362 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
5363 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5364 return -1;
5365 }
5366 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
5367 }
5368 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305369 }
5370
Mao Flynn7b379f32015-04-20 00:28:30 +08005371 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305372}
5373
Mao Flynn7b379f32015-04-20 00:28:30 +08005374int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305375{
5376 if (target_is_emmc_boot()) {
5377 return splash_screen_mmc();
5378 } else {
5379 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005380 }
5381}
5382
Mayank Grover351a75e2017-05-30 20:06:08 +05305383void publish_getvar_multislot_vars()
5384{
5385 int i,count;
5386 static bool published = false;
5387 static char slot_count[MAX_RSP_SIZE];
5388 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
5389 static char active_slot_suffix[MAX_RSP_SIZE];
5390 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
5391 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
5392 const char *tmp;
5393 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305394 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05305395
5396 if (!published)
5397 {
5398 /* Update slot meta info */
5399 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
5400 partition_get_partition_count());
5401 for(i=0; i<count; i++)
5402 {
5403 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305404 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
5405 has_slot_pname[i]);
5406 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05305407 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
5408 }
5409
5410 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
5411 {
5412 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305413 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305414 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
5415 "slot-unbootable:%s", tmp);
5416 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
5417 "slot-active:%s", tmp);
5418 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
5419 "slot-success:%s", tmp);
5420 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
5421 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05305422 fastboot_publish(slot_info[i].slot_is_unbootable,
5423 slot_info[i].slot_is_unbootable_rsp);
5424 fastboot_publish(slot_info[i].slot_is_active,
5425 slot_info[i].slot_is_active_rsp);
5426 fastboot_publish(slot_info[i].slot_is_succesful,
5427 slot_info[i].slot_is_succesful_rsp);
5428 fastboot_publish(slot_info[i].slot_retry_count,
5429 slot_info[i].slot_retry_count_rsp);
5430 }
5431 fastboot_publish("current-slot", active_slot_suffix);
5432 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
5433 fastboot_publish("slot-count", slot_count);
5434 published = true;
5435 }
5436
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305437 active_slt = partition_find_active_slot();
5438 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305439 {
5440 tmp = SUFFIX_SLOT(active_slt);
5441 tmp++; // to remove "_" from slot_suffix.
5442 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
5443 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305444 else
5445 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
5446
Mayank Grover351a75e2017-05-30 20:06:08 +05305447 /* Update partition meta information */
5448 partition_fill_slot_meta(slot_info);
5449 return;
5450}
5451
lijuang4ece1e72015-08-14 21:02:36 +08005452void get_product_name(unsigned char *buf)
5453{
5454 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
5455 return;
5456}
5457
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305458#if PRODUCT_IOT
5459void get_bootloader_version_iot(unsigned char *buf)
5460{
5461 if (buf != NULL)
5462 {
5463 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
5464 strlcat(buf, "-", MAX_VERSION_LEN);
5465 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
5466 }
5467 return;
5468}
5469#endif
5470
lijuang4ece1e72015-08-14 21:02:36 +08005471void get_bootloader_version(unsigned char *buf)
5472{
jhchen7a504d12020-04-24 15:45:57 +08005473 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
lijuang4ece1e72015-08-14 21:02:36 +08005474 return;
5475}
5476
5477void get_baseband_version(unsigned char *buf)
5478{
jhchen7a504d12020-04-24 15:45:57 +08005479 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
lijuang4ece1e72015-08-14 21:02:36 +08005480 return;
5481}
5482
Monika Singh32a09f72018-03-14 13:08:29 +05305483bool is_device_locked_critical()
5484{
5485 return device.is_unlock_critical ? false:true;
5486}
5487
lijuang4ece1e72015-08-14 21:02:36 +08005488bool is_device_locked()
5489{
5490 return device.is_unlocked ? false:true;
5491}
5492
Monika Singh32a09f72018-03-14 13:08:29 +05305493bool is_verity_enforcing()
5494{
5495 return device.verity_mode ? true:false;
5496}
5497
Amol Jadi5edf3552013-07-23 14:15:34 -07005498/* register commands and variables for fastboot */
5499void aboot_fastboot_register_commands(void)
5500{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005501 int i;
lijuangf16461c2015-08-03 17:09:34 +08005502 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07005503
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005504 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08005505 /* By default the enabled list is empty. */
5506 {"", NULL},
5507 /* move commands enclosed within the below ifndef to here
5508 * if they need to be enabled in user build.
5509 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005510#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005511 /* Register the following commands only for non-user builds */
5512 {"flash:", cmd_flash},
5513 {"erase:", cmd_erase},
5514 {"boot", cmd_boot},
5515 {"continue", cmd_continue},
5516 {"reboot", cmd_reboot},
5517 {"reboot-bootloader", cmd_reboot_bootloader},
5518 {"oem unlock", cmd_oem_unlock},
5519 {"oem unlock-go", cmd_oem_unlock_go},
5520 {"oem lock", cmd_oem_lock},
tracychui8fd71dc2020-06-11 10:17:13 +08005521/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start */
5522#if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK)
5523 {"oem 8901_unlock", cmd_oem_unlock_skip_ui_check},
5524 {"oem 8901_lock", cmd_oem_lock_skip_ui_check},
5525#endif
5526/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */
lijuang511a2b52015-08-14 20:50:51 +08005527 {"flashing unlock", cmd_oem_unlock},
5528 {"flashing lock", cmd_oem_lock},
5529 {"flashing lock_critical", cmd_flashing_lock_critical},
5530 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5531 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5532 {"oem device-info", cmd_oem_devinfo},
5533 {"preflash", cmd_preflash},
5534 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5535 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005536 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005537 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305538 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305539#if DYNAMIC_PARTITION_SUPPORT
5540 {"reboot-fastboot",cmd_reboot_fastboot},
5541 {"reboot-recovery",cmd_reboot_recovery},
5542#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005543#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005544 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005545#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005546#endif
tracychuid184b912020-06-05 17:31:38 +08005547/*[20200605][TracyChui] Implement get Serial Number start*/
5548#if defined(ENABLE_PRODINFO_ACCESS)
5549 {"oem adb_enable", CmdOemEnableAdb},
5550#endif
5551/*[20200605][TracyChui] Implement get Serial Number end*/
lijuang511a2b52015-08-14 20:50:51 +08005552 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005553
5554 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5555 for (i = 1; i < fastboot_cmds_count; i++)
5556 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5557
Amol Jadi5edf3552013-07-23 14:15:34 -07005558 /* publish variables and their values */
5559 fastboot_publish("product", TARGET(BOARD));
5560 fastboot_publish("kernel", "lk");
5561 fastboot_publish("serialno", sn_buf);
5562
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305563 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5564 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5565 fastboot_publish("hw-revision", soc_version_str);
5566
Amol Jadi5edf3552013-07-23 14:15:34 -07005567 /*
5568 * partition info is supported only for emmc partitions
5569 * Calling this for NAND prints some error messages which
5570 * is harmless but misleading. Avoid calling this for NAND
5571 * devices.
5572 */
5573 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305574 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005575
Mayank Grover351a75e2017-05-30 20:06:08 +05305576 if (partition_multislot_is_supported())
5577 publish_getvar_multislot_vars();
5578
Amol Jadi5edf3552013-07-23 14:15:34 -07005579 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005580#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005581 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5582 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005583#else
5584 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5585 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5586#endif
5587
Amol Jadi5edf3552013-07-23 14:15:34 -07005588 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005589 /* Is the charger screen check enabled */
5590 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5591 device.charger_screen_enabled);
5592 fastboot_publish("charger-screen-enabled",
5593 (const char *) charger_screen_enabled);
tracychuid184b912020-06-05 17:31:38 +08005594/*[20200605][TracyChui] Implement get Serial Number start*/
5595#if defined(ENABLE_PRODINFO_ACCESS)
5596 read_prod_info(&prod);
5597 snprintf(AdbEnable, MAX_RSP_SIZE, "%d",
5598 prod.is_adb_enabled);
5599 fastboot_publish("adb-enabled",
5600 (const char *) AdbEnable);
5601#endif
5602/*[20200605][TracyChui] Implement get Serial Number end*/
lijuanga25d8bb2015-09-16 13:11:52 +08005603 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305604 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5605 device.display_panel);
5606 fastboot_publish("display-panel",
5607 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305608
5609 if (target_is_emmc_boot())
5610 {
5611 mmc_blocksize = mmc_get_device_blocksize();
5612 }
5613 else
5614 {
5615 mmc_blocksize = flash_block_size();
5616 }
5617 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5618 fastboot_publish("erase-block-size", (const char *) block_size_string);
5619 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305620#if PRODUCT_IOT
5621 get_bootloader_version_iot(&bootloader_version_string);
5622 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5623
5624 /* Version baseband is n/a for apq iot devices */
5625 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305626#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005627 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5628 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305629#endif
lijuangf16461c2015-08-03 17:09:34 +08005630 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305631 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005632 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5633 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5634 target_is_emmc_boot()? "eMMC":"UFS");
5635 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005636#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005637 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005638 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005639 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005640#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305641 if (target_dynamic_partition_supported())
5642 fastboot_publish("is-userspace", "no");
Amol Jadi5edf3552013-07-23 14:15:34 -07005643}
5644
Brian Swetland9c4c0752009-01-25 16:23:50 -08005645void aboot_init(const struct app_descriptor *app)
5646{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005647 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305648 int boot_err_type = 0;
5649 int boot_slot = INVALID;
jessicatseng67c4fd02020-05-26 13:17:56 +08005650//<2020/05/26-JessicaTseng, Add low battery icon
5651 int vbat = 0;
5652 char boot_vbat[MAX_RSP_SIZE];
5653//>2020/05/26-JessicaTseng
Chandan Uddarajubedca152010-06-02 23:05:15 -07005654
lijuang39831732016-01-08 17:49:02 +08005655 /* Initialise wdog to catch early lk crashes */
5656#if WDOG_SUPPORT
5657 msm_wdog_init();
5658#endif
5659
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005660 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005661 if (target_is_emmc_boot())
5662 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005663 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005664 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305665 mmc_blocksize = mmc_get_device_blocksize();
5666 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005667 }
5668 else
5669 {
5670 page_size = flash_page_size();
5671 page_mask = page_size - 1;
5672 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005673 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5674
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005675 read_device_info(&device);
tracychuid184b912020-06-05 17:31:38 +08005676/*[20200605][TracyChui] Implement get Serial Number start*/
5677#if defined(ENABLE_PRODINFO_ACCESS)
5678 read_prod_info(&prod);
5679#endif
5680/*[20200605][TracyChui] Implement get Serial Number end*/
vijay kumarc65876c2015-04-24 13:29:16 +05305681 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005682
Mayank Grover351a75e2017-05-30 20:06:08 +05305683 /* Detect multi-slot support */
5684 if (partition_multislot_is_supported())
5685 {
5686 boot_slot = partition_find_active_slot();
5687 if (boot_slot == INVALID)
5688 {
5689 boot_into_fastboot = true;
5690 dprintf(INFO, "Active Slot: (INVALID)\n");
5691 }
5692 else
5693 {
5694 /* Setting the state of system to boot active slot */
5695 partition_mark_active_slot(boot_slot);
5696 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5697 }
5698 }
5699
Greg Griscod6250552011-06-29 14:40:23 -07005700 target_serialno((unsigned char *) sn_buf);
tracychuid184b912020-06-05 17:31:38 +08005701/*[20200605][TracyChui] Implement get Serial Number start*/
5702#if defined(ENABLE_PRODINFO_ACCESS)
5703 dprintf(CRITICAL,"serial number: %s\n",sn_buf);
5704#else
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005705 dprintf(SPEW,"serial number: %s\n",sn_buf);
tracychuid184b912020-06-05 17:31:38 +08005706#endif
5707/*[20200605][TracyChui] Implement get Serial Number end*/
5708/*[20200605][TracyChui] Implement get Serial Number start */
5709#if defined(ENABLE_PRODINFO_ACCESS)
5710 read_prod_info(&prod);
5711 snprintf((char *)cust_sn_buf, PRODINFO_MAX_SSN_LEN + 1, "%s", prod.ssn);
5712 dprintf(CRITICAL,"customer serial number: %s\n", cust_sn_buf);
5713 snprintf((char *)factory_sn_buf, PRODINFO_MAX_ISN_LEN + 1, "%s", prod.isn);
5714 dprintf(CRITICAL,"factory serial number: %s\n", factory_sn_buf);
5715#endif
5716 /*[20200605][TracyChui] Implement get Serial Number end */
Dhaval Patel223ec952013-07-18 14:49:44 -07005717 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5718
Matthew Qindefd5562014-07-11 18:02:40 +08005719 /*
5720 * Check power off reason if user force reset,
5721 * if yes phone will do normal boot.
5722 */
5723 if (is_user_force_reset())
5724 goto normal_boot;
5725
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005726 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005727 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005728 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005729 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005730 reboot_device(EMERGENCY_DLOAD);
5731 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5732
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005733 boot_into_fastboot = true;
5734 }
5735 if (!boot_into_fastboot)
5736 {
5737 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5738 boot_into_recovery = 1;
5739 if (!boot_into_recovery &&
5740 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005741 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005742 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005743 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005744 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005745 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005746 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005747
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005748#if USE_PON_REBOOT_REG
5749 reboot_mode = check_hard_reboot_mode();
5750#else
Ajay Dudani77421292010-10-27 19:34:06 -07005751 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005752#endif
5753 if (reboot_mode == RECOVERY_MODE)
5754 {
Ajay Dudani77421292010-10-27 19:34:06 -07005755 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005756 }
5757 else if(reboot_mode == FASTBOOT_MODE)
5758 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005759 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005760 }
5761 else if(reboot_mode == ALARM_BOOT)
5762 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005763 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005764 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305765#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305766 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005767 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305768 if (reboot_mode == DM_VERITY_ENFORCING)
5769 {
5770 device.verity_mode = 1;
5771 write_device_info(&device);
5772 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005773#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305774 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005775#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305776 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005777#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305778 {
5779 device.verity_mode = 0;
5780 write_device_info(&device);
5781 }
5782 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5783 {
5784 if(send_delete_keys_to_tz())
5785 ASSERT(0);
5786 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005787 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305788#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005789
jialongjhan1efba002020-05-22 18:25:56 +08005790//[Arima][8901][JialongJhan][20200522]modify Display splash screen if enabled Start
5791 /* Display splash screen if enabled */
5792#if DISPLAY_SPLASH_SCREEN
5793#if NO_ALARM_DISPLAY
5794 if (!check_alarm_boot()) {
5795#endif
5796 dprintf(SPEW, "Display Init: Start\n");
5797#if DISPLAY_HDMI_PRIMARY
5798 if (!strlen(device.display_panel))
5799 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5800 sizeof(device.display_panel));
5801#endif
5802#if ENABLE_WBC
5803 /* Wait if the display shutdown is in progress */
5804 while(pm_app_display_shutdown_in_prgs());
5805 if (!pm_appsbl_display_init_done())
5806 target_display_init(device.display_panel);
5807 else
5808 display_image_on_screen();
5809#else
5810 target_display_init(device.display_panel);
5811#endif
5812 dprintf(SPEW, "Display Init: Done\n");
5813#if NO_ALARM_DISPLAY
5814 }
5815#endif
5816#endif
5817
5818//[Arima][8901][JialongJhan][20200522]modify Display splash screen if enabled End
5819
Matthew Qindefd5562014-07-11 18:02:40 +08005820normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005821 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005822 {
jessicatseng67c4fd02020-05-26 13:17:56 +08005823//<2020/05/26-JessicaTseng, Add low battery icon
5824 if(!target_pause_for_battery_charge())
5825 {
5826 vbat = target_get_battery_voltage();
5827 snprintf(boot_vbat, MAX_RSP_SIZE, "%d", vbat);
5828 dprintf(CRITICAL,"battery_voltage: %s\n", boot_vbat);
5829 if(vbat < 3500000)
5830 {
5831 display_lowbattery_image_on_screen();
5832 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 Start
5833 msm_display_flush();
5834 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 End
5835 dprintf(CRITICAL,"Low battery, cannot boot up...\n");
5836 mdelay(3000);
5837 shutdown_device();
5838 }
5839 }
5840//>2020/05/26-JessicaTseng
5841
Pavel Nedev5d91d412013-04-29 11:34:24 +03005842 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005843 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005844 if(emmc_recovery_init())
5845 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5846 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005847 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005848 if((device.is_unlocked) || (device.is_tampered))
5849 {
5850 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305851 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005852 #endif
5853 #if USE_PCOM_SECBOOT
5854 set_tamper_flag(device.is_tampered);
5855 #endif
5856 }
Shashank Mittala0032282011-08-26 14:50:11 -07005857 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005858
Mayank Grover351a75e2017-05-30 20:06:08 +05305859retry_boot:
5860 /* Trying to boot active partition */
5861 if (partition_multislot_is_supported())
5862 {
5863 boot_slot = partition_find_boot_slot();
5864 partition_mark_active_slot(boot_slot);
5865 if (boot_slot == INVALID)
5866 goto fastboot;
5867 }
5868
5869 boot_err_type = boot_linux_from_mmc();
5870 switch (boot_err_type)
5871 {
5872 case ERR_INVALID_PAGE_SIZE:
5873 case ERR_DT_PARSE:
5874 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305875 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305876 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305877 {
5878 /*
5879 * Deactivate current slot, as it failed to
5880 * boot, and retry next slot.
5881 */
5882 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305883 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305884 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305885 else
5886 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305887 default:
5888 break;
5889 /* going to fastboot menu */
5890 }
Shashank Mittala0032282011-08-26 14:50:11 -07005891 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005892 else
5893 {
5894 recovery_init();
5895 #if USE_PCOM_SECBOOT
5896 if((device.is_unlocked) || (device.is_tampered))
5897 set_tamper_flag(device.is_tampered);
5898 #endif
5899 boot_linux_from_flash();
5900 }
5901 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5902 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005903 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005904
Mayank Grover351a75e2017-05-30 20:06:08 +05305905fastboot:
jialongjhan1efba002020-05-22 18:25:56 +08005906
5907 //[Arima][8901][JialongJhan]Show Fastboot logo 1 second 20190627 Start
5908 mdelay(1000);
5909 //[Arima][8901][JialongJhan]Show Fastboot logo 1 second 20190627 End
5910
Amol Jadi5edf3552013-07-23 14:15:34 -07005911 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005912
Amol Jadi5edf3552013-07-23 14:15:34 -07005913 /* register aboot specific fastboot commands */
5914 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005915
Amol Jadi5edf3552013-07-23 14:15:34 -07005916 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005917 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005918
5919 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305920#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005921 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305922#else
5923 /* Add salt buffer offset at start of image address to copy VB salt */
5924 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5925 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5926#endif
lijuang4ece1e72015-08-14 21:02:36 +08005927#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005928 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005929#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005930}
5931
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005932uint32_t get_page_size()
5933{
5934 return page_size;
5935}
5936
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005937/*
5938 * Calculated and save hash (SHA256) for non-signed boot image.
5939 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005940 * @param image_addr - Boot image address
5941 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005942 *
5943 * @return int - 0 on success, negative value on failure.
5944 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005945static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005946{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005947 unsigned int digest[8];
5948#if IMAGE_VERIF_ALGO_SHA1
5949 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5950#else
5951 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5952#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005953
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005954 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005955 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005956
5957 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005958 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005959
5960 return 0;
5961}
5962
Mayank Grover351a75e2017-05-30 20:06:08 +05305963
Brian Swetland9c4c0752009-01-25 16:23:50 -08005964APP_START(aboot)
5965 .init = aboot_init,
5966APP_END