blob: 9a98663d35768cc34de6371b1ae7111fe87e109d [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
tedwu15b28c02020-07-03 18:02:48 +0800253//<2020/07/02-tedwu, Add RF variants in system properties
254#define RF_VARIANT_PIN_0 89
255#define RF_VARIANT_PIN_1 141
256
257static const char *rfvar_prefix = " androidboot.rfvariant=";
258static const char *rfvar_0 = "B13";
259static const char *rfvar_1 = "B28B";
260static const char *rfvar_2 = "B28A";
261static const char *rfvar_3 = "03";
262//>2020/07/02-tedwu, Add RF variants in system properties
263
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700264#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700265static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700266static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530267static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700268//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700269
270struct verified_boot_verity_mode vbvm[] =
271{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700272#if ENABLE_VB_ATTEST
273 {false, "eio"},
274#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700275 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700276#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700277 {true, "enforcing"},
278};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700279struct verified_boot_state_name vbsn[] =
280{
281 {GREEN, "green"},
282 {ORANGE, "orange"},
283 {YELLOW,"yellow"},
284 {RED,"red" },
285};
286#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700287/*As per spec delay wait time before shutdown in Red state*/
288#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700289static unsigned page_size = 0;
290static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530291static unsigned mmc_blocksize = 0;
292static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700293static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
294static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530295static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800296static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700297static bool devinfo_present = true;
tracychuid184b912020-06-05 17:31:38 +0800298/*[20200605][TracyChui] Implement get Serial Number start */
299#if defined(ENABLE_PRODINFO_ACCESS)
300static bool prodinfo_present = true;
301#endif
302/*[20200605][TracyChui] Implement get Serial Number end*/
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700303bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530304static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530305static char *vbcmdline;
306static bootinfo info = {0};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530307static void *recovery_dtbo_buf = NULL;
308static uint32_t recovery_dtbo_size = 0;
309
Shashank Mittalcd98d472011-08-02 14:29:24 -0700310/* Assuming unauthorized kernel image by default */
311static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530312static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
tracychuid184b912020-06-05 17:31:38 +0800313/*[20200605][TracyChui] Implement get Serial Number start*/
314#if defined(ENABLE_PRODINFO_ACCESS)
ymlu(180285)e5289bf2020-07-29 14:43:11 +0800315static prod_info prod = {PRODINFO_MAGIC, {0}, {0}, 0};
tracychuid184b912020-06-05 17:31:38 +0800316#endif
317/*[20200605][TracyChui] Implement get Serial Number end*/
Monika Singh292b3e92018-03-17 22:40:23 +0530318
jhchen7a504d12020-04-24 15:45:57 +0800319/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
320#ifdef ENABLE_FUSE_CHECK
321static const char *fuse_blown = " androidboot.oem.color=red";
322static const char *fuse_not_blown = " androidboot.oem.color=brown";
323#endif
324/*[Arima_8910][jhchen] 20181031 end*/
325
326/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
327#ifdef ENABLE_FUSE_CHECK
328 int is_fused = 0;
329#endif
330/*[Arima_8910][jhchen] 20181031 end*/
331
vijay kumarc65876c2015-04-24 13:29:16 +0530332static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700333
vijay kumarca2e6812015-07-08 20:28:25 +0530334static char frp_ptns[2][8] = {"config","frp"};
335
lijuang511a2b52015-08-14 20:50:51 +0800336static const char *critical_flash_allowed_ptn[] = {
337 "aboot",
338 "rpm",
339 "tz",
340 "sbl",
341 "sdi",
342 "sbl1",
343 "xbl",
344 "hyp",
345 "pmic",
346 "bootloader",
347 "devinfo",
348 "partition"};
349
Dima Zavin42168f22009-01-30 11:52:22 -0800350struct atag_ptbl_entry
351{
352 char name[16];
353 unsigned offset;
354 unsigned size;
355 unsigned flags;
356};
357
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700358/*
359 * Partition info, required to be published
360 * for fastboot
361 */
362struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530363 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700364 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
365 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
366 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
367 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
368};
369
370/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530371 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700372 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530373#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530374#define EXT_STR "ext4"
375#define F2FS_STR "f2fs"
376
377#define FS_SUPERBLOCK_OFFSET 0x400
378#define EXT_MAGIC 0xEF53
379#define EXT_MAGIC_OFFSET_SB 0x38
380#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
381#define F2FS_MAGIC_OFFSET_SB 0x0
382
383typedef enum fs_signature_type {
384 EXT_FS_SIGNATURE = 1,
385 EXT_F2FS_SIGNATURE = 2,
386 NO_FS = -1
387} fs_signature_type;
388
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530389struct getvar_partition_info part_info[NUM_PARTITIONS];
390struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700391{
Mayank Grover49920212018-02-09 18:15:55 +0530392 { "system" , "partition-size:", "partition-type:", "", "ext4" },
393 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
394 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
395 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700396};
397
398char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700399char charger_screen_enabled[MAX_RSP_SIZE];
tracychuid184b912020-06-05 17:31:38 +0800400/*[20200605][TracyChui] Implement get Serial Number start*/
401#if defined(ENABLE_PRODINFO_ACCESS)
402char cust_sn_buf[PRODINFO_MAX_SSN_LEN + 1];
403char factory_sn_buf[PRODINFO_MAX_SSN_LEN + 1];
404char AdbEnable[MAX_RSP_SIZE];
405#endif
406#if defined(ENABLE_PRODINFO_ACCESS)
407char sn_buf[PRODINFO_MAX_ISN_LEN + 1];
408#else
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800409char sn_buf[13];
tracychuid184b912020-06-05 17:31:38 +0800410#endif
411/*[20200605][TracyChui] Implement get Serial Number end*/
Dhaval Patel223ec952013-07-18 14:49:44 -0700412char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530413char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530414char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530415char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530416#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530417
418/* For IOT we are using custom version */
419#define PRODUCT_IOT_VERSION "IOT001"
420char bootloader_version_string[MAX_RSP_SIZE];
421#endif
lijuang102dfa92015-10-09 18:31:03 +0800422
423#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800424char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800425char battery_soc_ok [MAX_RSP_SIZE];
426#endif
427
lijuangf16461c2015-08-03 17:09:34 +0800428char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700429
Greg Griscod2471ef2011-07-14 13:00:42 -0700430extern int emmc_recovery_init(void);
431
Kinson Chik0b1c8162011-08-31 16:31:57 -0700432#if NO_KEYPAD_DRIVER
433extern int fastboot_trigger(void);
434#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700435
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530436static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700437{
438 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700439#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800440 if (is_arm64)
441 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
442 else
443 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700444 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
445 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700446#endif
447}
448
Dima Zavin42168f22009-01-30 11:52:22 -0800449static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
450{
451 struct atag_ptbl_entry atag_ptn;
452
453 memcpy(atag_ptn.name, ptn->name, 16);
454 atag_ptn.name[15] = '\0';
455 atag_ptn.offset = ptn->start;
456 atag_ptn.size = ptn->length;
457 atag_ptn.flags = ptn->flags;
458 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
459 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
460}
Mayank Grover9df84c02018-08-30 15:46:35 +0530461#ifdef VERIFIED_BOOT_2
462void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
463{
464 int index = 0;
465 char *vbm_img_buf = NULL;
466 unsigned long long ptn = 0;
467 unsigned long long ptn_size = 0;
468
469 /* Immediately return if dtbo is not supported */
470 index = partition_get_index("vbmeta");
471 ptn = partition_get_offset(index);
472 if(!ptn)
473 {
474 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
475 return;
476 }
477
478 ptn_size = partition_get_size(index);
479 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
480 {
481 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
482 return;
483 }
484
485 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
486 if (!vbm_img_buf)
487 {
488 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
489 return;
490 }
491
492 mmc_set_lun(partition_get_lun(index));
493 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
494 {
495 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
496 free(vbm_img_buf);
497 return;
498 }
499
500 *vbmeta_image_buf = vbm_img_buf;
501 *vbmeta_image_sz = (uint32_t)ptn_size;
502 return;
503}
504#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800505
lijuang102dfa92015-10-09 18:31:03 +0800506#if CHECK_BAT_VOLTAGE
507void update_battery_status(void)
508{
509 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
510 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
511}
512#endif
513
michaellincb058f12020-04-24 15:05:10 +0800514//<20200424-michaellin, Add PCBA stage to system properties
515uint32_t GetPcbaVariant(void)
516{
517 uint8_t GPIO_97=10;
518 uint8_t GPIO_98=10;
519 uint8_t GPIO_99=10;
520
521 uint8_t pcba_stage = 0;
522
523 // Config GPIO
524 gpio_tlmm_config(97, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
525 gpio_tlmm_config(98, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
526 gpio_tlmm_config(99, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
527
528 /* Wait for the gpio config to take effect - debounce time */
529 thread_sleep(10);
530
531 /* Get status of GPIO */
532 GPIO_97 = gpio_status(97);
533 GPIO_98 = gpio_status(98);
534 GPIO_99 = gpio_status(99);
535
536 pcba_stage = (GPIO_99 << 2) + (GPIO_98 << 1) + GPIO_97;
537 dprintf(ALWAYS, "pcba_stage status: %u\n", pcba_stage);
michaellin52b647a2020-05-05 14:58:47 +0800538 // GPIO_99 GPIO_98 GPIO_97
michaellincb058f12020-04-24 15:05:10 +0800539 // 0 0 0 EP0
540 // 0 0 1 EP1
541 // 0 1 0 EP2
michaellin52b647a2020-05-05 14:58:47 +0800542 // 0 1 1 FP(8901MB-007)/PP
543 // 1 0 0 MP(8901MB-008)
544 // 1 0 1 MP-8903MB_001
michaellincb058f12020-04-24 15:05:10 +0800545 return pcba_stage;
546}
547//>20200424-michaellin
548
tedwu15b28c02020-07-03 18:02:48 +0800549//<2020/07/02-tedwu, Add RF variant in system properties
550uint32_t get_rf_variant(void)
551{
552 static uint32_t rf_id = 0xFF;
553
554 if (0xFF == rf_id) {
555 rf_id = (gpio_status(RF_VARIANT_PIN_1) << 1) + gpio_status(RF_VARIANT_PIN_0);
556 }
557 dprintf(CRITICAL, "RF Variant ID: %d\n", rf_id);
558
559 return rf_id;
560}
561//>2020/07/02-tedwu, Add RF variant in system properties
562
Neeti Desaie245d492012-06-01 12:52:13 -0700563unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800564{
David Ng183a7422009-12-07 14:55:21 -0800565 int cmdline_len = 0;
566 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800567 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700568 int pause_at_bootup = 0;
tracychuid184b912020-06-05 17:31:38 +0800569/*[20200605][TracyChui] Implement get Serial Number start*/
570#if defined(ENABLE_PRODINFO_ACCESS)
571 int AdbEnable = 0;
572#endif
573/*[20200605][TracyChui] Implement get Serial Number end*/
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800574 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300575 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700576 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700577 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530578#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530579 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530580#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530581 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530582 int system_ptn_index = -1;
583 unsigned int lun = 0;
584 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530585#if VERITY_LE
586 int syspath_buflen = strlen(verity_dev)
587 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
588 + strlen(verity_params) + sizeof(int) + 2;
589#else
lijuang83ef4b22018-08-23 11:01:55 +0800590 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
591 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
592 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530593 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
594 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530595#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530596 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530597#if HIBERNATION_SUPPORT
598 int resume_buflen = strlen(resume) + sizeof(int) + 2;
599 char resume_buf[resume_buflen];
600 int swap_ptn_index = INVALID_PTN;
601#endif
602
Mayank Grover889be1b2017-09-12 20:12:23 +0530603#if VERIFIED_BOOT
604 uint32_t boot_state = RED;
605#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530606
tedwu15b28c02020-07-03 18:02:48 +0800607 //<2020/07/02-tedwu, Add RF variant in system properties
608 const char *pRFvar[4] = {rfvar_0, rfvar_1, rfvar_2, rfvar_3};
609 //>2020/07/02-tedwu, Add RF variant in system properties
610
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530611#if USE_LE_SYSTEMD
612 is_systemd_present=true;
613#endif
614
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700615#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530616 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530617 {
618 boot_state = boot_verify_get_state();
619 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530620#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700621
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200622#ifdef MDTP_SUPPORT
623 mdtp_activated(&is_mdtp_activated);
624#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800625
Brian Swetland9c4c0752009-01-25 16:23:50 -0800626 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800627 cmdline_len = strlen(cmdline);
628 have_cmdline = 1;
629 }
630 if (target_is_emmc_boot()) {
631 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700632 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530633 if (!boot_dev_buf) {
634 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
635 } else {
636 platform_boot_dev_cmdline(boot_dev_buf);
637#if USE_BOOTDEV_CMDLINE
638 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700639#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530640 if (target_dynamic_partition_supported()) {
641 cmdline_len += strlen(dynamic_bootdev_cmdline);
642 cmdline_len += strlen(boot_dev_buf);
643 }
644 }
David Ng183a7422009-12-07 14:55:21 -0800645 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800646
647 cmdline_len += strlen(usb_sn_cmdline);
648 cmdline_len += strlen(sn_buf);
tracychuid184b912020-06-05 17:31:38 +0800649/*[20200605][TracyChui] Implement get Serial Number start */
650#if defined(ENABLE_PRODINFO_ACCESS)
651 cmdline_len += strlen(cust_sn_cmdline);
652 cmdline_len += strlen(cust_sn_buf);
653 cmdline_len += strlen(factory_sn_cmdline);
654 cmdline_len += strlen(factory_sn_buf);
655#endif
656/*[20200605][TracyChui] Implement get Serial Number end */
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800657
tracychui3b4abb72020-06-15 10:50:49 +0800658
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700659#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530660 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700661 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530662 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
663 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
664 {
665 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
666 ASSERT(0);
667 }
668 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
669 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700670 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530671#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700672
Monika Singh292b3e92018-03-17 22:40:23 +0530673
674 if (vbcmdline != NULL) {
675 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
676 strlen(vbcmdline));
677 cmdline_len += strlen(vbcmdline);
678 }
679
Pavel Nedev5614d222013-06-17 18:01:02 +0300680 if (boot_into_recovery && gpt_exists)
681 cmdline_len += strlen(secondary_gpt_enable);
682
Monika Singh292b3e92018-03-17 22:40:23 +0530683#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200684 if(is_mdtp_activated)
685 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530686#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300687 if (boot_into_ffbm) {
688 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530689
690 if(is_systemd_present)
691 cmdline_len += strlen(systemd_ffbm_mode);
692
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700693 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800694 /* reduce kernel console messages to speed-up boot */
695 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800696 } else if (boot_reason_alarm) {
697 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800698 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800699 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700700 pause_at_bootup = 1;
701 cmdline_len += strlen(battchg_pause);
702 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800703
tracychuid184b912020-06-05 17:31:38 +0800704/*[20200605][TracyChui] Implement get Serial Number start*/
705#if defined(ENABLE_PRODINFO_ACCESS)
706 if (prod.is_adb_enabled) {
707 dprintf(CRITICAL, "Device will enable adb\n");
708
ymlu(180285)e5289bf2020-07-29 14:43:11 +0800709 prod.is_adb_enabled = 0;
tracychuid184b912020-06-05 17:31:38 +0800710 write_prod_info(&prod);
711
712 AdbEnable = 1;
713 cmdline_len += strlen(UsbAdbEnable);
714 }
715#endif
716/*[20200605][TracyChui] Implement get Serial Number end*/
717
Shashank Mittalcd98d472011-08-02 14:29:24 -0700718 if(target_use_signed_kernel() && auth_kernel_img) {
719 cmdline_len += strlen(auth_kernel);
720 }
721
Joonwoo Park61112782013-10-02 19:50:39 -0700722 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
723 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530724 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700725 have_target_boot_params = 1;
726 cmdline_len += strlen(target_boot_params);
727 }
728
Ajay Dudanid04110c2011-01-17 23:55:07 -0800729 /* Determine correct androidboot.baseband to use */
730 switch(target_baseband())
731 {
732 case BASEBAND_APQ:
733 cmdline_len += strlen(baseband_apq);
734 break;
735
736 case BASEBAND_MSM:
737 cmdline_len += strlen(baseband_msm);
738 break;
739
740 case BASEBAND_CSFB:
741 cmdline_len += strlen(baseband_csfb);
742 break;
743
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800744 case BASEBAND_SVLTE2A:
745 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800746 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700747
748 case BASEBAND_MDM:
749 cmdline_len += strlen(baseband_mdm);
750 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700751
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800752 case BASEBAND_MDM2:
753 cmdline_len += strlen(baseband_mdm2);
754 break;
755
Amol Jadi5c61a952012-05-04 17:05:35 -0700756 case BASEBAND_SGLTE:
757 cmdline_len += strlen(baseband_sglte);
758 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530759
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800760 case BASEBAND_SGLTE2:
761 cmdline_len += strlen(baseband_sglte2);
762 break;
763
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530764 case BASEBAND_DSDA:
765 cmdline_len += strlen(baseband_dsda);
766 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800767
768 case BASEBAND_DSDA2:
769 cmdline_len += strlen(baseband_dsda2);
770 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530771 case BASEBAND_APQ_NOWGR:
772 cmdline_len += strlen(baseband_apq_nowgr);
773 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800774 }
775
michaellincb058f12020-04-24 15:05:10 +0800776 //<20200424-michaellin, Add PCBA stage to system properties
777 switch(GetPcbaVariant())
778 {
779 case 0: // EP0
780 cmdline_len += strlen(PCBA_STAGE_0);
781 break;
782
783 case 1: // EP1
784 cmdline_len += strlen(PCBA_STAGE_1);
785 break;
786
787 case 2: // EP2
788 cmdline_len += strlen(PCBA_STAGE_2);
789 break;
790
michaellin52b647a2020-05-05 14:58:47 +0800791 case 3: // FP(8901MB-007)/PP
michaellincb058f12020-04-24 15:05:10 +0800792 cmdline_len += strlen(PCBA_STAGE_3);
793 break;
794
michaellin52b647a2020-05-05 14:58:47 +0800795 case 4: // MP(8901MB-008)
michaellincb058f12020-04-24 15:05:10 +0800796 cmdline_len += strlen(PCBA_STAGE_4);
tracychui3b4abb72020-06-15 10:50:49 +0800797 break;
michaellincb058f12020-04-24 15:05:10 +0800798
michaellin52b647a2020-05-05 14:58:47 +0800799 case 5: // MP-8903MB_001
800 cmdline_len += strlen(PCBA_STAGE_5);
tracychui3b4abb72020-06-15 10:50:49 +0800801 break;
michaellin52b647a2020-05-05 14:58:47 +0800802
michaellincb058f12020-04-24 15:05:10 +0800803 default:// Reserved
804 cmdline_len += strlen(PCBA_STAGE_F);
805 break;
806 }
807 //>20200424-michaellin
808
tedwu15b28c02020-07-03 18:02:48 +0800809 //<2020/07/02-tedwu, Add RF variant in system properties
810 cmdline_len += strlen(rfvar_prefix);
811 cmdline_len += strlen(pRFvar[get_rf_variant()]);
812 //>2020/07/02-tedwu, Add RF variant in system properties
813
tracychui3b4abb72020-06-15 10:50:49 +0800814 /* [TracyChui]Add memory_config property 20200615 start */
815 if(smem_get_ddr_size() == MEM_3GB )
816 {
817 cmdline_len += strlen(memory_config_3G);
818 }
819 else
820 {
821 cmdline_len += strlen(memory_config_4G);
822 }
823 /* [TracyChui]Add memory_config property 20200615 end */
824
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300825#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800826 if (cmdline) {
827 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530828 target_display_panel_node(display_panel_buf,
829 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800830 strlen(display_panel_buf)) {
831 cmdline_len += strlen(display_panel_buf);
832 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700833 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300834#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700835
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800836 if (target_warm_boot()) {
837 warm_boot = true;
838 cmdline_len += strlen(warmboot_cmdline);
839 }
840
Mayank Grover5384ed82018-05-09 12:09:24 +0530841 if (target_uses_system_as_root() ||
842 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530843 {
844 current_active_slot = partition_find_active_slot();
845 cmdline_len += (strlen(androidboot_slot_suffix)+
846 strlen(SUFFIX_SLOT(current_active_slot)));
847
Mayank Grover3804be72017-06-22 11:59:23 +0530848 system_ptn_index = partition_get_index("system");
849 if (platform_boot_dev_isemmc())
850 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530851#if VERITY_LE
852 /*
853 Condition 4: Verity and A/B both enabled
854 Eventual command line looks like:
855 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
856 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
857 */
858 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
859 verity_dev,
860 verity_system_part, suffix_slot[current_active_slot],
861 verity_params, system_ptn_index + 1);
862#else
863 /*
864 Condition 5: A/B enabled, but verity disabled
865 Eventual command line looks like:
866 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
867 ... root=/dev/mmcblk0p<NN> ...
868 */
869 snprintf(syspath_buf, syspath_buflen, " %s%d",
870 sys_path, system_ptn_index + 1);
871#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530872 }
873 else
874 {
875 lun = partition_get_lun(system_ptn_index);
876 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
877 lun_char_base + lun,
878 partition_get_index_in_lun("system", lun));
879 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530880
Monika Singh292b3e92018-03-17 22:40:23 +0530881#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530882 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530883#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530884 }
885
886 if (target_uses_system_as_root() ||
887 partition_multislot_is_supported())
888 {
889 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530890
891 /* For dynamic partition, support skip skip_initramfs */
892 if (!target_dynamic_partition_supported() &&
893 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530894 cmdline_len += strlen(skip_ramfs);
895 }
896
Mayank Grover466d6562018-05-10 14:52:20 +0530897#if HIBERNATION_SUPPORT
898 if (platform_boot_dev_isemmc())
899 {
900 swap_ptn_index = partition_get_index("swap");
901 if (swap_ptn_index != INVALID_PTN)
902 {
903 snprintf(resume_buf, resume_buflen,
904 " %s%d", resume,
905 (swap_ptn_index + 1));
906 cmdline_len += strlen(resume_buf);
907 }
908 else
909 {
910 dprintf(INFO, "WARNING: swap partition not found\n");
911 }
912 }
913#endif
914
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800915#if TARGET_CMDLINE_SUPPORT
916 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
917 int target_cmd_line_len;
918 ASSERT(target_cmdline_buf);
919 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
920 cmdline_len += target_cmd_line_len;
921#endif
922
lijuang83ef4b22018-08-23 11:01:55 +0800923#if !VERITY_LE
924 dtbo_idx = get_dtbo_idx ();
925 if (dtbo_idx != INVALID_PTN) {
926 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
927 android_boot_dtbo_idx, dtbo_idx);
928 cmdline_len += strlen (dtbo_idx_str);
929 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530930
931 dtb_idx = get_dtb_idx ();
932 if (dtb_idx != INVALID_PTN) {
933 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
934 android_boot_dtb_idx, dtb_idx);
935 cmdline_len += strlen (dtb_idx_str);
936 }
lijuang83ef4b22018-08-23 11:01:55 +0800937#endif
938
jhchen7a504d12020-04-24 15:45:57 +0800939/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
940#ifdef ENABLE_FUSE_CHECK
941 {
942 int hw_key_status = readl(0xA01D0);
943 is_fused = (hw_key_status & 0x2) >> 1;
944 dprintf(CRITICAL, "hw_key_status = 0x%x, is_fused=%d\n", hw_key_status, is_fused);
945 if (is_fused) {
946 cmdline_len += strlen(fuse_blown);
947 } else {
948 cmdline_len += strlen(fuse_not_blown);
949 }
950 }
951#endif
952/*[Arima_8910][jhchen] 20181031 end*/
953
David Ng183a7422009-12-07 14:55:21 -0800954 if (cmdline_len > 0) {
955 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800956 unsigned char *dst;
957
958 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
959 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530960 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800961 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700962
Amol Jadi168b7712012-03-06 16:15:00 -0800963 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800964 if (have_cmdline) {
965 src = cmdline;
966 while ((*dst++ = *src++));
967 }
968 if (target_is_emmc_boot()) {
969 src = emmc_cmdline;
970 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700971 have_cmdline = 1;
972 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800973#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700974 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530975 if (have_cmdline &&
976 boot_dev_buf) {
977 --dst;
978 while ((*dst++ = *src++));
979 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700980#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530981 /* Dynamic partition append boot_devices */
982 if (target_dynamic_partition_supported() &&
983 boot_dev_buf) {
984 src = dynamic_bootdev_cmdline;
985 if (have_cmdline) --dst;
986 while ((*dst++ = *src++));
987 src = boot_dev_buf;
988 if (have_cmdline) --dst;
989 while ((*dst++ = *src++));
990 }
David Ngf773dde2010-07-26 19:55:08 -0700991 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800992
michaellincb058f12020-04-24 15:05:10 +0800993 //<20200424-michaellin, Add PCBA stage to system properties
994 switch(GetPcbaVariant())
995 {
996 case 0:
997 src = PCBA_STAGE_0;
998 if (have_cmdline) --dst;
999 while ((*dst++ = *src++));
1000 break;
1001 case 1:
1002 src = PCBA_STAGE_1;
1003 if (have_cmdline) --dst;
1004 while ((*dst++ = *src++));
1005 break;
1006 case 2:
1007 src = PCBA_STAGE_2;
1008 if (have_cmdline) --dst;
1009 while ((*dst++ = *src++));
1010 break;
1011 case 3:
1012 src = PCBA_STAGE_3;
1013 if (have_cmdline) --dst;
1014 while ((*dst++ = *src++));
1015 break;
1016 case 4:
1017 src = PCBA_STAGE_4;
1018 if (have_cmdline) --dst;
1019 while ((*dst++ = *src++));
1020 break;
michaellin52b647a2020-05-05 14:58:47 +08001021 case 5:
1022 src = PCBA_STAGE_5;
1023 if (have_cmdline) --dst;
1024 while ((*dst++ = *src++));
1025 break;
michaellincb058f12020-04-24 15:05:10 +08001026 default:
1027 src = PCBA_STAGE_F;
1028 if (have_cmdline) --dst;
1029 while ((*dst++ = *src++));
1030 break;
1031 }
1032 //>20200424-michaellin
1033
tedwu15b28c02020-07-03 18:02:48 +08001034 //<2020/07/02-tedwu, Add RF variant in system properties
1035 src = rfvar_prefix;
1036 if (have_cmdline) --dst;
1037 while ((*dst++ = *src++));
1038 src = pRFvar[get_rf_variant()];
1039 if (have_cmdline) --dst;
1040 while ((*dst++ = *src++));
1041 //>2020/07/02-tedwu, Add RF variant in system properties
1042
tracychui3b4abb72020-06-15 10:50:49 +08001043 /* [TracyChui]Add memory_config property 20200615 start */
1044 if(smem_get_ddr_size() == MEM_3GB )
1045 {
1046 src = memory_config_3G;
1047 if (have_cmdline) --dst;
1048 while ((*dst++ = *src++));
1049 }
1050 else
1051 {
1052 src = memory_config_4G;
1053 if (have_cmdline) --dst;
1054 while ((*dst++ = *src++));
1055 }
1056 /* [TracyChui]Add memory_config property 20200615 end */
1057
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -07001058#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05301059 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -07001060 {
Mayank Grover889be1b2017-09-12 20:12:23 +05301061 src = verified_state;
1062 if(have_cmdline) --dst;
1063 have_cmdline = 1;
1064 while ((*dst++ = *src++));
1065 src = vbsn[boot_state].name;
1066 if(have_cmdline) --dst;
1067 while ((*dst++ = *src++));
1068
1069 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
1070 {
1071 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
1072 ASSERT(0);
1073 }
1074 src = verity_mode;
1075 if(have_cmdline) --dst;
1076 while ((*dst++ = *src++));
1077 src = vbvm[device.verity_mode].name;
1078 if(have_cmdline) -- dst;
1079 while ((*dst++ = *src++));
1080 src = keymaster_v1;
1081 if(have_cmdline) --dst;
1082 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -07001083 }
Parth Dixitddbc7352015-10-18 03:13:31 +05301084#endif
Monika Singh292b3e92018-03-17 22:40:23 +05301085
1086 if (vbcmdline != NULL) {
1087 src = vbcmdline;
1088 if (have_cmdline) --dst;
1089 while ((*dst++ = *src++));
1090 }
1091
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001092 src = usb_sn_cmdline;
1093 if (have_cmdline) --dst;
1094 have_cmdline = 1;
1095 while ((*dst++ = *src++));
1096 src = sn_buf;
1097 if (have_cmdline) --dst;
1098 have_cmdline = 1;
1099 while ((*dst++ = *src++));
tracychuid184b912020-06-05 17:31:38 +08001100
1101 /* [20200605][TracyChui] Implement get Serial Number */
1102 #if defined(ENABLE_PRODINFO_ACCESS)
1103 src = cust_sn_cmdline;
1104 if (have_cmdline) --dst;
1105 have_cmdline = 1;
1106 while ((*dst++ = *src++));
1107 src = cust_sn_buf;
1108 if (have_cmdline) --dst;
1109 have_cmdline = 1;
1110 while ((*dst++ = *src++));
1111 src = factory_sn_cmdline;
1112 if (have_cmdline) --dst;
1113 have_cmdline = 1;
1114 while ((*dst++ = *src++));
1115 src = factory_sn_buf;
1116 if (have_cmdline) --dst;
1117 have_cmdline = 1;
1118 while ((*dst++ = *src++));
1119 #endif
1120 /*[20200605][TracyChui] Implement get Serial Number end */
Hanumant Singh8e1ac232014-01-29 13:41:51 -08001121 if (warm_boot) {
1122 if (have_cmdline) --dst;
1123 src = warmboot_cmdline;
1124 while ((*dst++ = *src++));
1125 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001126
Pavel Nedev5614d222013-06-17 18:01:02 +03001127 if (boot_into_recovery && gpt_exists) {
1128 src = secondary_gpt_enable;
1129 if (have_cmdline) --dst;
1130 while ((*dst++ = *src++));
1131 }
Monika Singh292b3e92018-03-17 22:40:23 +05301132#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +02001133 if (is_mdtp_activated) {
1134 src = mdtp_activated_flag;
1135 if (have_cmdline) --dst;
1136 while ((*dst++ = *src++));
1137 }
Monika Singh292b3e92018-03-17 22:40:23 +05301138#endif
Pavel Nedev328ac822013-04-05 15:25:11 +03001139 if (boot_into_ffbm) {
1140 src = androidboot_mode;
1141 if (have_cmdline) --dst;
1142 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001143 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +03001144 if (have_cmdline) --dst;
1145 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +05301146
1147 if(is_systemd_present) {
1148 src = systemd_ffbm_mode;
1149 if (have_cmdline) --dst;
1150 while ((*dst++ = *src++));
1151 }
1152
Pavel Nedev898298c2013-02-27 12:36:09 -08001153 src = loglevel;
1154 if (have_cmdline) --dst;
1155 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +08001156 } else if (boot_reason_alarm) {
1157 src = alarmboot_cmdline;
1158 if (have_cmdline) --dst;
1159 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +03001160 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -07001161 src = battchg_pause;
1162 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -08001163 while ((*dst++ = *src++));
1164 }
Ajay Dudanid04110c2011-01-17 23:55:07 -08001165
tracychuid184b912020-06-05 17:31:38 +08001166/*[20200605][TracyChui] Implement get Serial Number start*/
1167#if defined(ENABLE_PRODINFO_ACCESS)
1168 if (AdbEnable) {
1169 src = UsbAdbEnable;
1170 if (have_cmdline) --dst;
1171 while ((*dst++ = *src++));
1172 }
1173#endif
1174/*[20200605][TracyChui] Implement get Serial Number end*/
1175
Shashank Mittalcd98d472011-08-02 14:29:24 -07001176 if(target_use_signed_kernel() && auth_kernel_img) {
1177 src = auth_kernel;
1178 if (have_cmdline) --dst;
1179 while ((*dst++ = *src++));
1180 }
1181
Ajay Dudanid04110c2011-01-17 23:55:07 -08001182 switch(target_baseband())
1183 {
1184 case BASEBAND_APQ:
1185 src = baseband_apq;
1186 if (have_cmdline) --dst;
1187 while ((*dst++ = *src++));
1188 break;
1189
1190 case BASEBAND_MSM:
1191 src = baseband_msm;
1192 if (have_cmdline) --dst;
1193 while ((*dst++ = *src++));
1194 break;
1195
1196 case BASEBAND_CSFB:
1197 src = baseband_csfb;
1198 if (have_cmdline) --dst;
1199 while ((*dst++ = *src++));
1200 break;
1201
Ajay Dudani6cff85e2011-02-04 16:02:16 -08001202 case BASEBAND_SVLTE2A:
1203 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001204 if (have_cmdline) --dst;
1205 while ((*dst++ = *src++));
1206 break;
Ajay Dudani403bc492011-09-30 16:17:21 -07001207
1208 case BASEBAND_MDM:
1209 src = baseband_mdm;
1210 if (have_cmdline) --dst;
1211 while ((*dst++ = *src++));
1212 break;
Amol Jadi5c61a952012-05-04 17:05:35 -07001213
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -08001214 case BASEBAND_MDM2:
1215 src = baseband_mdm2;
1216 if (have_cmdline) --dst;
1217 while ((*dst++ = *src++));
1218 break;
1219
Amol Jadi5c61a952012-05-04 17:05:35 -07001220 case BASEBAND_SGLTE:
1221 src = baseband_sglte;
1222 if (have_cmdline) --dst;
1223 while ((*dst++ = *src++));
1224 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301225
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -08001226 case BASEBAND_SGLTE2:
1227 src = baseband_sglte2;
1228 if (have_cmdline) --dst;
1229 while ((*dst++ = *src++));
1230 break;
1231
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301232 case BASEBAND_DSDA:
1233 src = baseband_dsda;
1234 if (have_cmdline) --dst;
1235 while ((*dst++ = *src++));
1236 break;
Amol Jadi2a15a272013-01-22 12:03:36 -08001237
1238 case BASEBAND_DSDA2:
1239 src = baseband_dsda2;
1240 if (have_cmdline) --dst;
1241 while ((*dst++ = *src++));
1242 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +05301243 case BASEBAND_APQ_NOWGR:
1244 src = baseband_apq_nowgr;
1245 if (have_cmdline) --dst;
1246 while ((*dst++ = *src++));
1247 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001248 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001249
1250 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -07001251 src = display_panel_buf;
1252 if (have_cmdline) --dst;
1253 while ((*dst++ = *src++));
1254 }
Joonwoo Park61112782013-10-02 19:50:39 -07001255
1256 if (have_target_boot_params) {
1257 if (have_cmdline) --dst;
1258 src = target_boot_params;
1259 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +05301260 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -07001261 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001262
Mayank Grover351a75e2017-05-30 20:06:08 +05301263 if (partition_multislot_is_supported() && have_cmdline)
1264 {
1265 src = androidboot_slot_suffix;
1266 --dst;
1267 while ((*dst++ = *src++));
1268 --dst;
1269 src = SUFFIX_SLOT(current_active_slot);
1270 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301271 }
Mayank Grover351a75e2017-05-30 20:06:08 +05301272
Mayank Grover351a75e2017-05-30 20:06:08 +05301273
Mayank Grover5384ed82018-05-09 12:09:24 +05301274 /*
1275 * System-As-Root behaviour, system.img should contain both
1276 * system content and ramdisk content, and should be mounted at
1277 * root(a/b).
1278 * Apending skip_ramfs for non a/b builds which use, system as root.
1279 */
1280 if ((target_uses_system_as_root() ||
1281 partition_multislot_is_supported()) &&
1282 have_cmdline)
1283 {
Mayank Groverb716edf2019-05-08 16:06:55 +05301284 if (!target_dynamic_partition_supported() &&
1285 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +05301286 {
1287 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +05301288 --dst;
1289 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301290 }
1291
1292 src = sys_path_cmdline;
1293 --dst;
1294 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +05301295
Monika Singh292b3e92018-03-17 22:40:23 +05301296#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +05301297 src = syspath_buf;
1298 --dst;
1299 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +05301300#endif
Mayank Grover351a75e2017-05-30 20:06:08 +05301301 }
1302
Mayank Grover466d6562018-05-10 14:52:20 +05301303#if HIBERNATION_SUPPORT
1304 if (swap_ptn_index != INVALID_PTN)
1305 {
1306 src = resume_buf;
1307 --dst;
1308 while ((*dst++ = *src++));
1309 }
1310#endif
1311
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001312#if TARGET_CMDLINE_SUPPORT
1313 if (target_cmdline_buf && target_cmd_line_len)
1314 {
1315 if (have_cmdline) --dst;
1316 src = target_cmdline_buf;
1317 while((*dst++ = *src++));
1318 free(target_cmdline_buf);
1319 }
1320#endif
lijuang83ef4b22018-08-23 11:01:55 +08001321
1322#if !VERITY_LE
1323 if (dtbo_idx != INVALID_PTN) {
1324 src = dtbo_idx_str;
1325 --dst;
1326 while ((*dst++ = *src++));
1327 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301328
1329 if (dtb_idx != INVALID_PTN) {
1330 src = dtb_idx_str;
1331 --dst;
1332 while ((*dst++ = *src++));
1333 }
lijuang83ef4b22018-08-23 11:01:55 +08001334#endif
jhchen7a504d12020-04-24 15:45:57 +08001335
1336/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
1337#ifdef ENABLE_FUSE_CHECK
1338 if (is_fused) {
1339 src = fuse_blown;
1340 if (have_cmdline) --dst;
1341 while ((*dst++ = *src++));
1342 } else {
1343 src = fuse_not_blown;
1344 if (have_cmdline) --dst;
1345 while ((*dst++ = *src++));
1346 }
1347#endif
1348/*[Arima_8910][jhchen] 20181031 end*/
Neeti Desaie245d492012-06-01 12:52:13 -07001349 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001350
1351
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001352 if (boot_dev_buf)
1353 free(boot_dev_buf);
1354
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001355 if (cmdline_final)
1356 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1357 else
1358 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001359 return cmdline_final;
1360}
1361
1362unsigned *atag_core(unsigned *ptr)
1363{
1364 /* CORE */
1365 *ptr++ = 2;
1366 *ptr++ = 0x54410001;
1367
1368 return ptr;
1369
1370}
1371
1372unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1373 unsigned ramdisk_size)
1374{
1375 if (ramdisk_size) {
1376 *ptr++ = 4;
1377 *ptr++ = 0x54420005;
1378 *ptr++ = (unsigned)ramdisk;
1379 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001380 }
1381
Neeti Desaie245d492012-06-01 12:52:13 -07001382 return ptr;
1383}
1384
1385unsigned *atag_ptable(unsigned **ptr_addr)
1386{
1387 int i;
1388 struct ptable *ptable;
1389
1390 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001391 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1392 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001393 *(*ptr_addr)++ = 0x4d534d70;
1394 for (i = 0; i < ptable->count; ++i)
1395 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1396 }
1397
1398 return (*ptr_addr);
1399}
1400
1401unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1402{
1403 int cmdline_length = 0;
1404 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001405 char *dest;
1406
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001407 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001408 n = (cmdline_length + 4) & (~3);
1409
1410 *ptr++ = (n / 4) + 2;
1411 *ptr++ = 0x54410009;
1412 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001413 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001414 ptr += (n / 4);
1415
1416 return ptr;
1417}
1418
1419unsigned *atag_end(unsigned *ptr)
1420{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001421 /* END */
1422 *ptr++ = 0;
1423 *ptr++ = 0;
1424
Neeti Desaie245d492012-06-01 12:52:13 -07001425 return ptr;
1426}
1427
1428void generate_atags(unsigned *ptr, const char *cmdline,
1429 void *ramdisk, unsigned ramdisk_size)
1430{
vijay kumar21a37452015-12-29 16:23:21 +05301431 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001432 ptr = atag_core(ptr);
1433 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1434 ptr = target_atag_mem(ptr);
1435
1436 /* Skip NAND partition ATAGS for eMMC boot */
1437 if (!target_is_emmc_boot()){
1438 ptr = atag_ptable(&ptr);
1439 }
1440
vijay kumar21a37452015-12-29 16:23:21 +05301441 /*
1442 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1443 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1444 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301445 if (!cmdline)
1446 return;
1447
vijay kumar21a37452015-12-29 16:23:21 +05301448 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1449 ptr = atag_cmdline(ptr, cmdline);
1450 ptr = atag_end(ptr);
1451 }
1452 else {
1453 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1454 ASSERT(0);
1455 }
Neeti Desaie245d492012-06-01 12:52:13 -07001456}
1457
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001458typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001459void boot_linux(void *kernel, unsigned *tags,
1460 const char *cmdline, unsigned machtype,
1461 void *ramdisk, unsigned ramdisk_size)
1462{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001463 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001464#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001465 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001466#endif
1467
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001468 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001469 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301470 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001471
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301472 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001473
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001474 final_cmdline = update_cmdline((const char*)cmdline);
1475
Neeti Desai17379b82012-06-04 18:42:53 -07001476#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001477 dprintf(INFO, "Updating device tree: start\n");
1478
Neeti Desai17379b82012-06-04 18:42:53 -07001479 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301480 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001481 if(ret)
1482 {
1483 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1484 ASSERT(0);
1485 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001486 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001487#else
Neeti Desaie245d492012-06-01 12:52:13 -07001488 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001489 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001490#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001491
Monika Singh0ec6db82019-07-18 16:57:58 +05301492#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301493 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301494 {
1495 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001496#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001497#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301498 display_bootverify_menu(DISPLAY_MENU_EIO);
1499 wait_for_users_action();
1500 if(!pwr_key_is_pressed)
1501 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001502#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301503 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001504#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301505 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001506#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301507 dprintf(CRITICAL,
1508 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1509 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001510#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301511 }
lijuangbdd9bb42016-03-01 18:22:17 +08001512 }
lijuangbdd9bb42016-03-01 18:22:17 +08001513#endif
1514
1515#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001516 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001517 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001518 {
1519 dprintf(INFO, "Failed to write protect dev info\n");
1520 ASSERT(0);
1521 }
1522#endif
1523
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001524 /* Turn off splash screen if enabled */
1525#if DISPLAY_SPLASH_SCREEN
1526 target_display_shutdown();
1527#endif
1528
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001529 /* Perform target specific cleanup */
1530 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301531 free_verified_boot_resource(&info);
1532 if (final_cmdline)
1533 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001534
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001535 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301536 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001537
1538 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001539
Amol Jadi4421e652011-06-16 15:00:48 -07001540 /* do any platform specific cleanup before kernel entry */
1541 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001542
Brian Swetland9c4c0752009-01-25 16:23:50 -08001543 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001544
Amol Jadi504f9fe2012-08-16 13:56:48 -07001545#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001546 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001547#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001548 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001549
1550 if (IS_ARM64(kptr))
1551 /* Jump to a 64bit kernel */
1552 scm_elexec_call((paddr_t)kernel, tags_phys);
1553 else
1554 /* Jump to a 32bit kernel */
1555 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001556}
1557
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001558/* Function to check if the memory address range falls within the aboot
1559 * boundaries.
1560 * start: Start of the memory region
1561 * size: Size of the memory region
1562 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301563int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001564{
1565 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001566 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001567 return -1;
1568
1569 /* Check for memory overlap. */
1570 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1571 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001572 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001573 return 0;
1574 else
1575 return -1;
1576}
1577
Mayank Grovere559cec2017-10-17 15:12:03 +05301578/* Function to check if the memory address range falls beyond ddr region.
1579 * start: Start of the memory region
1580 * size: Size of the memory region
1581 */
1582int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1583{
1584 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1585 uint64_t ddr_size = smem_get_ddr_size();
1586 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1587 uintptr_t pa_start_addr = PA(start);
1588
1589 /* Check for boundary conditions. */
1590 if ((UINT_MAX - start) < size)
1591 return -1;
1592
1593 /* Check if memory range is beyond the ddr range. */
1594 if (pa_start_addr < ddr_pa_start_addr ||
1595 pa_start_addr >= (ddr_pa_end_addr) ||
1596 (pa_start_addr + size) > ddr_pa_end_addr)
1597 return -1;
1598 else
1599 return 0;
1600}
1601
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001602BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001603
Mayank Groverdd080e82018-09-04 20:12:13 +05301604int getimage(void **image_buffer, uint32_t *imgsize,
1605 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301606{
Mayank Groverdd080e82018-09-04 20:12:13 +05301607 uint32_t loadedindex;
1608 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301609 imgname == NULL) {
1610 dprintf(CRITICAL, "getimage: invalid parameters\n");
1611 return -1;
1612 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301613 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1614 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301615 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301616 *image_buffer = info.images[loadedindex].image_buffer;
1617 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301618 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1619 info.images[loadedindex].name,
1620 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301621 return 0;
1622 }
1623 }
1624 return -1;
1625}
1626
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001627static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1628{
1629 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001630
1631#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001632#if IMAGE_VERIF_ALGO_SHA1
1633 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1634#else
1635 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1636#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001637#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001638
1639 /* Assume device is rooted at this time. */
1640 device.is_tampered = 1;
1641
1642 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1643
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001644#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301645 uint32_t bootstate;
1646 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301647 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301648 {
1649 ret = boot_verify_image((unsigned char *)bootimg_addr,
1650 bootimg_size, "/recovery", &bootstate);
1651 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001652 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301653 {
1654 ret = boot_verify_image((unsigned char *)bootimg_addr,
1655 bootimg_size, "/boot", &bootstate);
1656 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001657 boot_verify_print_state();
1658#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001659 ret = image_verify((unsigned char *)bootimg_addr,
1660 (unsigned char *)(bootimg_addr + bootimg_size),
1661 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001662 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001663#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001664 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1665
1666 if (ret)
1667 {
1668 /* Authorized kernel */
1669 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001670 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001671 }
1672
Amit Blay4aa292f2015-04-28 21:55:59 +03001673#ifdef MDTP_SUPPORT
1674 {
1675 /* Verify MDTP lock.
1676 * For boot & recovery partitions, use aboot's verification result.
1677 */
1678 mdtp_ext_partition_verification_t ext_partition;
1679 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1680 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1681 ext_partition.page_size = 0; /* Not needed since already validated */
1682 ext_partition.image_addr = 0; /* Not needed since already validated */
1683 ext_partition.image_size = 0; /* Not needed since already validated */
1684 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1685 mdtp_fwlock_verify_lock(&ext_partition);
1686 }
1687#endif /* MDTP_SUPPORT */
1688
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001689#if USE_PCOM_SECBOOT
1690 set_tamper_flag(device.is_tampered);
1691#endif
1692
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001693#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001694 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001695 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001696 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001697#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001698 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001699#if ENABLE_VB_ATTEST
1700 mdelay(DELAY_WAIT);
1701 shutdown_device();
1702#else
lijuanga40d6302015-07-20 20:10:13 +08001703 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001704#endif
lijuanga40d6302015-07-20 20:10:13 +08001705#else
1706 dprintf(CRITICAL,
1707 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1708 mdelay(5000);
1709#endif
1710
1711 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001712 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001713#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001714 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001715 wait_for_users_action();
1716#else
1717 dprintf(CRITICAL,
1718 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1719 mdelay(5000);
1720#endif
1721 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001722 default:
lijuanga40d6302015-07-20 20:10:13 +08001723 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001724 }
1725#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001726#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301727 if(device.is_tampered)
1728 {
1729 write_device_info_mmc(&device);
1730 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301731 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301732 #endif
1733 #ifdef ASSERT_ON_TAMPER
1734 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1735 ASSERT(0);
1736 #endif
1737 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001738#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001739}
1740
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301741static bool check_format_bit()
1742{
1743 bool ret = false;
1744 int index;
1745 uint64_t offset;
1746 struct boot_selection_info *in = NULL;
1747 char *buf = NULL;
1748
1749 index = partition_get_index("bootselect");
1750 if (index == INVALID_PTN)
1751 {
1752 dprintf(INFO, "Unable to locate /bootselect partition\n");
1753 return ret;
1754 }
1755 offset = partition_get_offset(index);
1756 if(!offset)
1757 {
1758 dprintf(INFO, "partition /bootselect doesn't exist\n");
1759 return ret;
1760 }
1761 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301762 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301763 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301764 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301765 {
1766 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1767 free(buf);
1768 return ret;
1769 }
1770 in = (struct boot_selection_info *) buf;
1771 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1772 (in->version == BOOTSELECT_VERSION)) {
1773 if ((in->state_info & BOOTSELECT_FORMAT) &&
1774 !(in->state_info & BOOTSELECT_FACTORY))
1775 ret = true;
1776 } else {
1777 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1778 in->signature, in->version);
1779 ASSERT(0);
1780 }
1781 free(buf);
1782 return ret;
1783}
1784
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001785void boot_verifier_init()
1786{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001787 uint32_t boot_state;
1788 /* Check if device unlock */
1789 if(device.is_unlocked)
1790 {
1791 boot_verify_send_event(DEV_UNLOCK);
1792 boot_verify_print_state();
1793 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1794 return;
1795 }
1796 else
1797 {
1798 boot_verify_send_event(BOOT_INIT);
1799 }
1800
1801 /* Initialize keystore */
1802 boot_state = boot_verify_keystore_init();
1803 if(boot_state == YELLOW)
1804 {
1805 boot_verify_print_state();
1806 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1807 }
1808}
1809
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301810/* Function to return recovery appended dtbo buffer info */
1811void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1812{
1813 *dtbo_size = recovery_dtbo_size;
1814 *dtbo_buf = recovery_dtbo_buf;
1815 return;
1816}
1817
Shashank Mittal23b8f422010-04-16 19:27:21 -07001818int boot_linux_from_mmc(void)
1819{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301820 boot_img_hdr *hdr = (void*) buf;
1821 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001822 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001823 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001824 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001825 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001826
Shashank Mittalcd98d472011-08-02 14:29:24 -07001827 unsigned char *image_addr = 0;
1828 unsigned kernel_actual;
1829 unsigned ramdisk_actual;
1830 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001831 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301832 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001833
Matthew Qin271927e2015-03-31 22:07:07 -04001834 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301835 unsigned dtb_image_size = 0;
1836 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001837 unsigned int out_len = 0;
1838 unsigned int out_avai_len = 0;
1839 unsigned char *out_addr = NULL;
1840 uint32_t dtb_offset = 0;
1841 unsigned char *kernel_start_addr = NULL;
1842 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001843 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301844 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001845 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301846#if VERIFIED_BOOT_2
1847 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301848 void *dtbo_image_buf = NULL;
1849 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301850 void *vbmeta_image_buf = NULL;
1851 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301852#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301853 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001854#if DEVICE_TREE
1855 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001856 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001857 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001858 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001859 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001860 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001861#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001862 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301863 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001864
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301865 if (check_format_bit())
1866 boot_into_recovery = 1;
1867
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001868 if (!boot_into_recovery) {
1869 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1870 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1871 if (rcode <= 0) {
1872 boot_into_ffbm = false;
1873 if (rcode < 0)
1874 dprintf(CRITICAL,"failed to get ffbm cookie");
1875 } else
1876 boot_into_ffbm = true;
1877 } else
1878 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301879 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001880 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1881 dprintf(INFO, "Unified boot method!\n");
1882 hdr = uhdr;
1883 goto unified_boot;
1884 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301885
1886 /* For a/b recovery image code is on boot partition.
1887 If we support multislot, always use boot partition. */
1888 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301889 ((!partition_multislot_is_supported()) ||
1890 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301891 ptn_name = "recovery";
1892 else
1893 ptn_name = "boot";
1894
1895 index = partition_get_index(ptn_name);
1896 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301897 image_size = partition_get_size(index);
1898 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301899 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1900 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001901 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301902
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001903 /* Set Lun for boot & recovery partitions */
1904 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001905
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301906 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001907 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1908 return -1;
1909 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001910
1911 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001912 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301913 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001914 }
1915
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001916 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301917
1918 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1919 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1920 return -1;
1921 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001922 page_size = hdr->page_size;
1923 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001924 }
1925
Matthew Qin49e51fa2015-02-09 10:40:45 +08001926 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1927 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301928 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001929
Matthew Qin49e51fa2015-02-09 10:40:45 +08001930 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301931#if VERIFIED_BOOT_2
1932 /* Create hole in start of image for VB salt to copy */
1933 image_addr += SALT_BUFF_OFFSET;
1934#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301935 memcpy(image_addr, (void *)buf, page_size);
1936
1937 /* ensure commandline is terminated */
1938 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001939
1940#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301941#ifndef OSVERSION_IN_BOOTIMAGE
1942 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301943#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301944 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301945#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301946
Parth Dixit4097b622016-03-15 14:42:27 +05301947 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301948 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 +05301949 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1950 return -1;
1951 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301952 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001953#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301954 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 +05301955 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1956 return -1;
1957 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301958 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001959#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301960 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001961
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301962#ifdef OSVERSION_IN_BOOTIMAGE
1963 /* If header version is ONE and booting into recovery,
1964 dtbo is appended with recovery image.
1965 Doing following:
1966 * Validating the recovery offset and size.
1967 * Extracting recovery dtbo to be used as dtbo.
1968 */
1969 if (boot_into_recovery &&
1970 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1971 {
1972 struct boot_img_hdr_v1 *hdr1 =
1973 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301974 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301975
Mayank Grovera1a83c72018-09-24 11:23:15 +05301976 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301977 if ((hdr1->header_size !=
1978 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1979 {
1980 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1981 return -1;
1982 }
1983
Mayank Grovera1a83c72018-09-24 11:23:15 +05301984 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1985 {
1986 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301987 MAX_SUPPORTED_DTBO_IMG_BUF);
1988 return -1;
1989 }
1990
Mayank Grovera1a83c72018-09-24 11:23:15 +05301991 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301992 {
1993 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1994 return -1;
1995 }
1996
Mayank Grovera1a83c72018-09-24 11:23:15 +05301997 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1998 dprintf(CRITICAL,
1999 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
2000 return -1;
2001 }
2002
2003 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302004 {
2005 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
2006 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
2007 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
2008 return -1;
2009 }
2010
2011 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05302012 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302013 imagesize_actual += recovery_dtbo_size;
2014
2015 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
2016 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
2017 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
2018
2019 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05302020
2021 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2022 struct boot_img_hdr_v1 *hdr1 =
2023 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
2024 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
2025 (image_addr + sizeof(boot_img_hdr) +
2026 BOOT_IMAGE_HEADER_V2_OFFSET);
2027 unsigned int recovery_dtbo_actual = 0;
2028
lijuang0b17ba22019-09-04 14:20:47 +08002029 recovery_dtbo_actual =
2030 ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
2031 imagesize_actual += recovery_dtbo_actual;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302032
2033 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
2034
2035
2036 dtb_image_offset = page_size + patched_kernel_hdr_size +
2037 kernel_actual + ramdisk_actual + second_actual +
2038 recovery_dtbo_actual;
2039
2040 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
2041 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
2042 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302043#endif
2044
Parth Dixit1375d9e2019-07-08 20:56:13 +05302045
Saranya Chidurab1aaf232018-11-19 15:47:50 +05302046 /* Validate the boot/recovery image size is within the bounds of partition size */
2047 if (imagesize_actual > image_size) {
2048 dprintf(CRITICAL, "Image size is greater than partition size.\n");
2049 return -1;
2050 }
2051
Matthew Qin49e51fa2015-02-09 10:40:45 +08002052#if VERIFIED_BOOT
2053 boot_verifier_init();
2054#endif
2055
Mayank Grover1ceaee22019-04-01 17:54:32 +05302056#if VERIFIED_BOOT_2
2057 /* read full partition if device is unlocked */
2058 if (device.is_unlocked)
2059 imagesize_actual = image_size;
2060#endif
2061
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05302062 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002063 {
2064 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
2065 return -1;
2066 }
2067
Matthew Qinbb7923d2015-02-09 10:56:09 +08002068 /*
2069 * Update loading flow of bootimage to support compressed/uncompressed
2070 * bootimage on both 64bit and 32bit platform.
2071 * 1. Load bootimage from emmc partition onto DDR.
2072 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
2073 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
2074 * platform accordingly.
2075 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
2076 */
Mayank Grover351a75e2017-05-30 20:06:08 +05302077 if (partition_multislot_is_supported())
2078 {
2079 current_active_slot = partition_find_active_slot();
2080 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
2081 imagesize_actual, SUFFIX_SLOT(current_active_slot));
2082 }
2083 else
2084 {
2085 dprintf(INFO, "Loading (%s) image (%d): start\n",
2086 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2087 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002088 bs_set_timestamp(BS_KERNEL_LOAD_START);
2089
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05302090 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
2091 {
2092 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
2093 return -1;
2094 }
Kishor PK9e91b592017-05-24 12:14:55 +05302095 offset = page_size;
2096 /* Read image without signature and header*/
2097 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002098 {
2099 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2100 return -1;
2101 }
2102
Mayank Grover351a75e2017-05-30 20:06:08 +05302103 if (partition_multislot_is_supported())
2104 {
2105 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
2106 imagesize_actual, SUFFIX_SLOT(current_active_slot));
2107 }
2108 else
2109 {
2110 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07002111 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2112
Mayank Grover351a75e2017-05-30 20:06:08 +05302113 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002114 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2115
2116 /* Authenticate Kernel */
2117 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
2118 (int) target_use_signed_kernel(),
2119 device.is_unlocked,
2120 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05302121#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05302122 /* if device is unlocked skip reading signature, as full partition is read */
2123 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05302124 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05302125 offset = imagesize_actual;
2126 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
2127 {
2128 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
2129 return -1;
2130 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002131
Mayank Grover1ceaee22019-04-01 17:54:32 +05302132 /* Read signature */
2133 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
2134 {
2135 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
2136 return -1;
2137 }
Monika Singh292b3e92018-03-17 22:40:23 +05302138 }
2139
Mayank Grover027a9412018-09-04 15:12:05 +05302140 /* load and validate dtbo partition */
2141 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
2142
Mayank Grover9df84c02018-08-30 15:46:35 +05302143 /* load vbmeta partition */
2144 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
2145
Monika Singh292b3e92018-03-17 22:40:23 +05302146 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05302147
2148 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05302149 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05302150 info.images[IMG_BOOT].imgsize = imagesize_actual;
2151 info.images[IMG_BOOT].name = ptn_name;
2152 ++info.num_loaded_images;
2153
2154 /* Pass loaded dtbo image */
2155 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05302156 info.images[IMG_DTBO].image_buffer =
2157 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05302158 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
2159 info.images[IMG_DTBO].name = "dtbo";
2160 ++info.num_loaded_images;
2161 }
2162
Mayank Grover9df84c02018-08-30 15:46:35 +05302163 /* Pass loaded vbmeta image */
2164 if (vbmeta_image_buf != NULL) {
2165 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
2166 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
2167 info.images[IMG_VBMETA].name = "vbmeta";
2168 ++info.num_loaded_images;
2169 }
2170
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302171 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05302172 info.multi_slot_boot = partition_multislot_is_supported();
2173 info.bootreason_alarm = boot_reason_alarm;
2174 info.bootinto_recovery = boot_into_recovery;
2175 status = load_image_and_auth(&info);
2176 if(status)
2177 return -1;
2178
2179 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05302180
2181 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05302182 if (vbmeta_image_buf != NULL) {
2183 free(vbmeta_image_buf);
2184 --info.num_loaded_images;
2185 }
Monika Singh292b3e92018-03-17 22:40:23 +05302186#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002187 /* Change the condition a little bit to include the test framework support.
2188 * We would never reach this point if device is in fastboot mode, even if we did
2189 * that means we are in test mode, so execute kernel authentication part for the
2190 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302191 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08002192 {
2193 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05302194 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002195 {
2196 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
2197 return -1;
2198 }
2199
2200 /* Read signature */
2201 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
2202 {
2203 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
2204 return -1;
2205 }
2206
2207 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002208 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302209 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002210 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08002211 } else {
2212 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
2213 #ifdef TZ_SAVE_KERNEL_HASH
2214 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
2215 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03002216
2217#ifdef MDTP_SUPPORT
2218 {
2219 /* Verify MDTP lock.
2220 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
2221 * since verification was skipped in aboot. The signature is not part of the loaded image.
2222 */
2223 mdtp_ext_partition_verification_t ext_partition;
2224 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
2225 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
2226 ext_partition.page_size = page_size;
2227 ext_partition.image_addr = (uint32)image_addr;
2228 ext_partition.image_size = imagesize_actual;
2229 ext_partition.sig_avail = FALSE;
2230 mdtp_fwlock_verify_lock(&ext_partition);
2231 }
2232#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002233 }
Monika Singh292b3e92018-03-17 22:40:23 +05302234#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002235
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07002236#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08002237 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02002238 {
2239#if FBCON_DISPLAY_MSG
2240 display_bootverify_menu(DISPLAY_MENU_ORANGE);
2241 wait_for_users_action();
2242#else
2243 dprintf(CRITICAL,
2244 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
2245 mdelay(5000);
2246#endif
2247 }
2248#endif
2249
2250#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302251 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302252 {
2253 /* set boot and system versions. */
2254 set_os_version((unsigned char *)image_addr);
2255 // send root of trust
2256 if(!send_rot_command((uint32_t)device.is_unlocked))
2257 ASSERT(0);
2258 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302259#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002260 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002261 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2262 * If not, continue booting.
2263 */
2264 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
2265 {
2266 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
2267 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05302268#if VERIFIED_BOOT_2
2269 if (dtbo_image_sz)
2270 out_avai_len -= DTBO_IMG_BUF;
2271#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04002272 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002273 rc = decompress((unsigned char *)(image_addr + page_size),
2274 hdr->kernel_size, out_addr, out_avai_len,
2275 &dtb_offset, &out_len);
2276 if (rc)
2277 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002278 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002279 ASSERT(0);
2280 }
2281
Matthew Qin0b15b322015-05-19 05:20:54 -04002282 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002283 kptr = (struct kernel64_hdr *)out_addr;
2284 kernel_start_addr = out_addr;
2285 kernel_size = out_len;
2286 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07002287 dprintf(INFO, "Uncpmpressed kernel in use\n");
2288 if (!strncmp((char*)(image_addr + page_size),
2289 PATCHED_KERNEL_MAGIC,
2290 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
2291 dprintf(INFO, "Patched kernel detected\n");
2292 kptr = (struct kernel64_hdr *)(image_addr + page_size +
2293 PATCHED_KERNEL_HEADER_SIZE);
2294 //The size of the kernel is stored at start of kernel image + 16
2295 //The dtb would start just after the kernel
2296 dtb_offset = *((uint32_t*)((unsigned char*)
2297 (image_addr + page_size +
2298 sizeof(PATCHED_KERNEL_MAGIC) -
2299 1)));
2300 //The actual kernel starts after the 20 byte header.
2301 kernel_start_addr = (unsigned char*)(image_addr +
2302 page_size + PATCHED_KERNEL_HEADER_SIZE);
2303 kernel_size = hdr->kernel_size;
2304 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
2305 } else {
2306 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
2307 kptr = (struct kernel64_hdr *)(image_addr + page_size);
2308 kernel_start_addr = (unsigned char *)(image_addr + page_size);
2309 kernel_size = hdr->kernel_size;
2310 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08002311 }
2312
2313 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002314 * Update the kernel/ramdisk/tags address if the boot image header
2315 * has default values, these default values come from mkbootimg when
2316 * the boot image is flashed using fastboot flash:raw
2317 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002318 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002319
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002320 /* Get virtual addresses since the hdr saves physical addresses. */
2321 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2322 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2323 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002324
Matthew Qinbb7923d2015-02-09 10:56:09 +08002325 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002326 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002327 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302328 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2329 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2330 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002331 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302332 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002333 return -1;
2334 }
2335
2336#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302337 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2338 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002339 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302340 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002341 return -1;
2342 }
2343#endif
2344
Matthew Qin49e51fa2015-02-09 10:40:45 +08002345 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002346 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002347 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002348
Matthew Qin49e51fa2015-02-09 10:40:45 +08002349 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302350 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002351 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2352 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002353
Matthew Qin49e51fa2015-02-09 10:40:45 +08002354 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2355 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2356 return -1;
2357 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002358
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302359 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2360 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302361 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302362 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2363 return -1;
2364 }
2365
Matthew Qin49e51fa2015-02-09 10:40:45 +08002366 /* Find index of device tree within device tree table */
2367 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2368 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2369 return -1;
2370 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002371
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302372 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2373 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2374 return -1;
2375 }
2376
2377 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302378 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302379 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2380 return -1;
2381 }
2382
Matthew Qin271927e2015-03-31 22:07:07 -04002383 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2384 {
2385 unsigned int compressed_size = 0;
2386 out_addr += out_len;
2387 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002388 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002389 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2390 dt_entry.size, out_addr, out_avai_len,
2391 &compressed_size, &dtb_size);
2392 if (rc)
2393 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002394 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002395 ASSERT(0);
2396 }
2397
Matthew Qin0b15b322015-05-19 05:20:54 -04002398 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002399 best_match_dt_addr = out_addr;
2400 } else {
2401 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2402 dtb_size = dt_entry.size;
2403 }
2404
Matthew Qin49e51fa2015-02-09 10:40:45 +08002405 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302406 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2407 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002408 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302409 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002410 return -1;
2411 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002412
Matthew Qin271927e2015-03-31 22:07:07 -04002413 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002414 } else {
2415 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302416 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2417 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002418 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302419 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002420 return -1;
2421 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002422 /*
2423 * If appended dev tree is found, update the atags with
2424 * memory address to the DTB appended location on RAM.
2425 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302426 *
2427 * Make sure everything from scratch address is read before next step!
2428 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002429 */
2430 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302431 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2432
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302433#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302434 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2435
2436 image_buf = (void*)(image_addr);
2437 dtb_offset = dtb_image_offset;
2438 dtb_image_size = imagesize_actual;
2439 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302440#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302441
2442 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002443 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002444 if (!dtb) {
2445 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002446 return -1;
2447 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002448 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002449 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002450
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002451 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2452 target_load_ssd_keystore();
2453
Shashank Mittal23b8f422010-04-16 19:27:21 -07002454unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002455
Dima Zavin77e41f32013-03-06 16:10:43 -08002456 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002457 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002458 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2459
2460 return 0;
2461}
2462
Dima Zavin214cc642009-01-26 11:16:21 -08002463int boot_linux_from_flash(void)
2464{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302465 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002466 struct ptentry *ptn;
2467 struct ptable *ptable;
2468 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002469
Shashank Mittalcd98d472011-08-02 14:29:24 -07002470 unsigned char *image_addr = 0;
2471 unsigned kernel_actual;
2472 unsigned ramdisk_actual;
2473 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302474 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002475
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002476#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302477 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002478 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302479 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002480 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302481 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302482 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302483 unsigned int dtb_size = 0;
2484 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002485#endif
2486
David Ng183a7422009-12-07 14:55:21 -08002487 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302488 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002489 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2490 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2491 return -1;
2492 }
2493 goto continue_boot;
2494 }
2495
Dima Zavin214cc642009-01-26 11:16:21 -08002496 ptable = flash_get_ptable();
2497 if (ptable == NULL) {
2498 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2499 return -1;
2500 }
2501
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002502 if(!boot_into_recovery)
2503 {
2504 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002505
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002506 if (ptn == NULL) {
2507 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2508 return -1;
2509 }
2510 }
2511 else
2512 {
2513 ptn = ptable_find(ptable, "recovery");
2514 if (ptn == NULL) {
2515 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2516 return -1;
2517 }
Dima Zavin214cc642009-01-26 11:16:21 -08002518 }
2519
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302520 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002521 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002522 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2523 return -1;
2524 }
Dima Zavin214cc642009-01-26 11:16:21 -08002525
2526 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002527 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002528 return -1;
2529 }
2530
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002531 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002532 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 -08002533 return -1;
2534 }
2535
Kishor PK9e91b592017-05-24 12:14:55 +05302536 image_addr = (unsigned char *)target_get_scratch_address();
2537 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302538
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002539 /*
2540 * Update the kernel/ramdisk/tags address if the boot image header
2541 * has default values, these default values come from mkbootimg when
2542 * the boot image is flashed using fastboot flash:raw
2543 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002544 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002545
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002546 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002547 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2548 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2549 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2550
2551 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2552 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302553 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002554
Kishor PK9e91b592017-05-24 12:14:55 +05302555 /* ensure commandline is terminated */
2556 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2557
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002558 /* Check if the addresses in the header are valid. */
2559 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302560 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2561 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2562 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002563 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302564 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002565 return -1;
2566 }
2567
2568#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302569 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302570 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2571 return -1;
2572 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302573 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302574
Mayank Grovere559cec2017-10-17 15:12:03 +05302575 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2576 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302577 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302578 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302579 return -1;
2580 }
2581#else
2582
2583#ifndef OSVERSION_IN_BOOTIMAGE
2584 dt_size = hdr->dt_size;
2585#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302586 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302587 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 +05302588 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2589 return -1;
2590 }
2591
Kishor PKd8ddcad2017-07-27 13:53:57 +05302592 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302593
Mayank Grovere559cec2017-10-17 15:12:03 +05302594 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2595 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302596 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302597 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302598 return -1;
2599 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002600#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002601
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302602 /* Read full boot.img from flash */
2603 dprintf(INFO, "Loading (%s) image (%d): start\n",
2604 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2605 bs_set_timestamp(BS_KERNEL_LOAD_START);
2606
2607 if (UINT_MAX - page_size < imagesize_actual)
2608 {
2609 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2610 return -1;
2611 }
2612
2613 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2614 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2615 {
2616 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2617 return -1;
2618 }
2619
2620 offset = page_size;
2621 /* Read image without signature and header */
2622 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2623 {
2624 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2625 return -1;
2626 }
2627
2628 dprintf(INFO, "Loading (%s) image (%d): done\n",
2629 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2630 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2631
Shashank Mittalcd98d472011-08-02 14:29:24 -07002632 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002633 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002634 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002635 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302636
Shashank Mittalcd98d472011-08-02 14:29:24 -07002637 /* Read signature */
2638 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2639 {
2640 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002641 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002642 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002643
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302644 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302645 }
2646 offset = page_size;
2647 if(hdr->second_size != 0) {
2648 if (UINT_MAX - offset < second_actual)
2649 {
2650 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2651 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302652 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302653 offset += second_actual;
2654 /* Second image loading not implemented. */
2655 ASSERT(0);
2656 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302657 /* Move kernel and ramdisk to correct address */
2658 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2659 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2660
2661#if DEVICE_TREE
2662 if(dt_size != 0) {
2663
2664 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2665
2666 table = (struct dt_table*) dt_table_offset;
2667
2668 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2669 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2670 return -1;
2671 }
2672
2673 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2674 goes beyound hdr->dt_size*/
2675 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2676 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2677 return -1;
2678 }
2679
2680 /* Find index of device tree within device tree table */
2681 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2682 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2683 return -1;
2684 }
2685
2686 /* Validate and Read device device tree in the "tags_add */
2687 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2688 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2689 {
2690 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2691 return -1;
2692 }
2693
2694 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2695 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2696 return -1;
2697 }
2698
2699 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2700 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2701 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2702 return -1;
2703 }
2704
2705 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2706 dtb_size = dt_entry.size;
2707 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302708
2709 } else {
2710 /* Validate the tags_addr */
2711 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2712 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2713 {
2714 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2715 return -1;
2716 }
2717 /*
2718 * If appended dev tree is found, update the atags with
2719 * memory address to the DTB appended location on RAM.
2720 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302721 *
2722 * Make sure everything from scratch address is read before next step!
2723 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302724 */
2725 void *dtb = NULL;
2726 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2727 if (!dtb) {
2728 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2729 return -1;
2730 }
2731 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302732#endif
2733 if(target_use_signed_kernel() && (!device.is_unlocked))
2734 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002735 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002736 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002737 {
2738 write_device_info_flash(&device);
2739 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302740#if USE_PCOM_SECBOOT
2741 set_tamper_flag(device.is_tampered);
2742#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002743 }
David Ng183a7422009-12-07 14:55:21 -08002744continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002745
Dima Zavin214cc642009-01-26 11:16:21 -08002746 /* TODO: create/pass atags to kernel */
2747
Ajay Dudanie28a6072011-07-01 13:59:46 -07002748 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002749 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002750 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2751
2752 return 0;
2753}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002754
Shashank Mittal162244e2011-08-08 19:01:25 -07002755void write_device_info_mmc(device_info *dev)
2756{
Shashank Mittal162244e2011-08-08 19:01:25 -07002757 unsigned long long ptn = 0;
2758 unsigned long long size;
2759 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002760 uint8_t lun = 0;
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 (devinfo_present)
2765 index = partition_get_index("devinfo");
2766 else
2767 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002768
Shashank Mittal162244e2011-08-08 19:01:25 -07002769 ptn = partition_get_offset(index);
2770 if(ptn == 0)
2771 {
2772 return;
2773 }
2774
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002775 lun = partition_get_lun(index);
2776 mmc_set_lun(lun);
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_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002790 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302791 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002792 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002793 {
2794 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002795 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002796 }
2797}
2798
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002799void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002800{
Shashank Mittal162244e2011-08-08 19:01:25 -07002801 unsigned long long ptn = 0;
2802 unsigned long long size;
2803 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002804 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302805 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002806
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002807 if ((index = partition_get_index("devinfo")) < 0)
2808 {
2809 devinfo_present = false;
2810 index = partition_get_index("aboot");
2811 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002812
Shashank Mittal162244e2011-08-08 19:01:25 -07002813 ptn = partition_get_offset(index);
2814 if(ptn == 0)
2815 {
2816 return;
2817 }
2818
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002819 mmc_set_lun(partition_get_lun(index));
2820
Shashank Mittal162244e2011-08-08 19:01:25 -07002821 size = partition_get_size(index);
2822
Mayank Groverddd348d2018-01-23 14:07:09 +05302823 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2824 mmc_blocksize_mask);
2825 if (device_info_sz == UINT_MAX)
2826 {
2827 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2828 return;
2829 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002830
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002831 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302832 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002833 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302834 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002835 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002836 {
2837 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002838 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002839 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002840}
2841
tracychuid184b912020-06-05 17:31:38 +08002842/*[20200605][TracyChui] Implement get Serial Number start*/
2843#if defined(ENABLE_PRODINFO_ACCESS)
2844void write_prod_info_mmc(prod_info *dev)
2845{
2846 unsigned long long ptn = 0;
2847 unsigned long long size;
2848 int index = INVALID_PTN;
2849 uint32_t blocksize;
2850 uint8_t lun = 0;
2851 uint32_t ret = 0;
2852
2853 if (prodinfo_present)
2854 index = partition_get_index("prodinfo");
2855 else
2856 index = partition_get_index("aboot");
2857
2858 ptn = partition_get_offset(index);
2859 if(ptn == 0)
2860 {
2861 return;
2862 }
2863
2864 lun = partition_get_lun(index);
2865 mmc_set_lun(lun);
2866
2867 size = partition_get_size(index);
2868
2869 blocksize = mmc_get_device_blocksize();
2870
2871 if (prodinfo_present)
2872 ret = mmc_write(ptn, blocksize, (void *)prodinfo_buf);
2873 else
2874 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)prodinfo_buf);
2875 if (ret)
2876 {
2877 dprintf(CRITICAL, "ERROR: Cannot write prod info\n");
2878 ASSERT(0);
2879 }
2880}
2881
2882void read_prod_info_mmc(struct prod_info *info)
2883{
2884 unsigned long long ptn = 0;
2885 unsigned long long size;
2886 int index = INVALID_PTN;
2887 uint32_t blocksize;
2888 uint32_t ret = 0;
2889
2890 if ((index = partition_get_index("prodinfo")) < 0)
2891 {
2892 prodinfo_present = false;
2893 index = partition_get_index("aboot");
2894 }
2895
2896 ptn = partition_get_offset(index);
2897 if(ptn == 0)
2898 {
2899 return;
2900 }
2901
2902 mmc_set_lun(partition_get_lun(index));
2903
2904 size = partition_get_size(index);
2905
2906 blocksize = mmc_get_device_blocksize();
2907
2908 if (prodinfo_present)
2909 ret = mmc_read(ptn, (void *)prodinfo_buf, blocksize);
2910 else
2911 ret = mmc_read((ptn + size - blocksize), (void *)prodinfo_buf, blocksize);
2912 if (ret)
2913 {
2914 dprintf(CRITICAL, "ERROR: Cannot read prod info\n");
2915 ASSERT(0);
2916 }
2917}
2918#endif
2919/*[20200605][TracyChui] Implement get Serial Number end*/
2920
Shashank Mittal162244e2011-08-08 19:01:25 -07002921void write_device_info_flash(device_info *dev)
2922{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002923 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002924 struct ptentry *ptn;
2925 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002926 if(info == NULL)
2927 {
2928 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2929 ASSERT(0);
2930 }
2931 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002932 ptable = flash_get_ptable();
2933 if (ptable == NULL)
2934 {
2935 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2936 return;
2937 }
2938
2939 ptn = ptable_find(ptable, "devinfo");
2940 if (ptn == NULL)
2941 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002942 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002943 return;
2944 }
2945
Mayank Groverdedbc892017-10-24 13:41:34 +05302946 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002947 memcpy(info, dev, sizeof(device_info));
2948
2949 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2950 {
2951 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2952 return;
2953 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002954 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002955}
2956
vijay kumarc65876c2015-04-24 13:29:16 +05302957static int read_allow_oem_unlock(device_info *dev)
2958{
vijay kumarc65876c2015-04-24 13:29:16 +05302959 unsigned offset;
2960 int index;
2961 unsigned long long ptn;
2962 unsigned long long ptn_size;
2963 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002964 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302965
vijay kumarca2e6812015-07-08 20:28:25 +05302966 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302967 if (index == INVALID_PTN)
2968 {
vijay kumarca2e6812015-07-08 20:28:25 +05302969 index = partition_get_index(frp_ptns[1]);
2970 if (index == INVALID_PTN)
2971 {
2972 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2973 return -1;
2974 }
vijay kumarc65876c2015-04-24 13:29:16 +05302975 }
2976
2977 ptn = partition_get_offset(index);
2978 ptn_size = partition_get_size(index);
2979 offset = ptn_size - blocksize;
2980
Mayank Grover48860402016-11-29 12:34:53 +05302981 /* Set Lun for partition */
2982 mmc_set_lun(partition_get_lun(index));
2983
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002984 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302985 {
2986 dprintf(CRITICAL, "Reading MMC failed\n");
2987 return -1;
2988 }
2989
2990 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2991 is_allow_unlock = buf[blocksize-1] & 0x01;
2992 return 0;
2993}
2994
2995static int write_allow_oem_unlock(bool allow_unlock)
2996{
vijay kumarc65876c2015-04-24 13:29:16 +05302997 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302998 int index;
2999 unsigned long long ptn;
3000 unsigned long long ptn_size;
3001 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07003002 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05303003
vijay kumarca2e6812015-07-08 20:28:25 +05303004 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05303005 if (index == INVALID_PTN)
3006 {
vijay kumarca2e6812015-07-08 20:28:25 +05303007 index = partition_get_index(frp_ptns[1]);
3008 if (index == INVALID_PTN)
3009 {
3010 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
3011 return -1;
3012 }
vijay kumarc65876c2015-04-24 13:29:16 +05303013 }
3014
3015 ptn = partition_get_offset(index);
3016 ptn_size = partition_get_size(index);
3017 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05303018 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05303019
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07003020 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05303021 {
3022 dprintf(CRITICAL, "Reading MMC failed\n");
3023 return -1;
3024 }
3025
3026 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
3027 buf[blocksize-1] = allow_unlock;
3028 if (mmc_write(ptn + offset, blocksize, buf))
3029 {
3030 dprintf(CRITICAL, "Writing MMC failed\n");
3031 return -1;
3032 }
3033
3034 return 0;
3035}
3036
Shashank Mittal162244e2011-08-08 19:01:25 -07003037void read_device_info_flash(device_info *dev)
3038{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003039 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07003040 struct ptentry *ptn;
3041 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003042 if(info == NULL)
3043 {
3044 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
3045 ASSERT(0);
3046 }
3047 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07003048 ptable = flash_get_ptable();
3049 if (ptable == NULL)
3050 {
3051 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3052 return;
3053 }
3054
3055 ptn = ptable_find(ptable, "devinfo");
3056 if (ptn == NULL)
3057 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07003058 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07003059 return;
3060 }
3061
3062 if (flash_read(ptn, 0, (void *)info_buf, page_size))
3063 {
3064 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
3065 return;
3066 }
3067
3068 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
3069 {
Shashank Mittal162244e2011-08-08 19:01:25 -07003070 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
3071 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07003072 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07003073 write_device_info_flash(info);
3074 }
3075 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003076 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003077}
3078
3079void write_device_info(device_info *dev)
3080{
3081 if(target_is_emmc_boot())
3082 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003083 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3084 if(info == NULL)
3085 {
3086 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
3087 ASSERT(0);
3088 }
3089 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003090 memcpy(info, dev, sizeof(struct device_info));
3091
3092#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05303093 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303094 is_secure_boot_enable()) {
3095 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
3096 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07003097 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07003098 else
3099 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003100#else
3101 write_device_info_mmc(info);
3102#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003103 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003104 }
3105 else
3106 {
3107 write_device_info_flash(dev);
3108 }
3109}
3110
Monika Singh94316462018-03-15 18:39:01 +05303111int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
3112{
3113 if (!devinfo_present) {
3114 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3115 return -EINVAL;
3116 }
3117 if (loc >= ARRAY_SIZE(device.rollback_index)) {
3118 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
3119 __func__, loc, ARRAY_SIZE(device.rollback_index));
3120 ASSERT(0);
3121 }
3122
3123 *roll_back_index = device.rollback_index[loc];
3124 return 0;
3125}
3126
3127int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
3128{
3129 if (!devinfo_present) {
3130 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3131 return -EINVAL;
3132 }
3133 if (loc >= ARRAY_SIZE(device.rollback_index)) {
3134 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
3135 __func__, loc, ARRAY_SIZE(device.rollback_index));
3136 ASSERT(0);
3137 }
3138
3139 device.rollback_index[loc] = roll_back_index;
3140 write_device_info(&device);
3141 return 0;
3142}
3143
Monika Singhb0fad822018-03-15 18:50:55 +05303144int store_userkey(uint8_t *user_key, uint32_t user_key_size)
3145{
3146 if (!devinfo_present) {
3147 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3148 return -EINVAL;
3149 }
3150
3151 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
3152 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
3153 return -ENODEV;
3154 }
3155
3156 memcpy(device.user_public_key, user_key, user_key_size);
3157 device.user_public_key_length = user_key_size;
3158 write_device_info(&device);
3159 return 0;
3160}
3161
3162int erase_userkey()
3163{
3164 if (!devinfo_present) {
3165 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3166 return -EINVAL;
3167 }
3168 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
3169 device.user_public_key_length = 0;
3170 write_device_info(&device);
3171 return 0;
3172}
3173
3174int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
3175{
3176 if (!devinfo_present) {
3177 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3178 return -EINVAL;
3179 }
3180 *user_key = device.user_public_key;
3181 *user_key_size = device.user_public_key_length;
3182 return 0;
3183}
3184
Shashank Mittal162244e2011-08-08 19:01:25 -07003185void read_device_info(device_info *dev)
3186{
3187 if(target_is_emmc_boot())
3188 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003189 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3190 if(info == NULL)
3191 {
3192 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
3193 ASSERT(0);
3194 }
3195 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003196
3197#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05303198 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303199 is_secure_boot_enable()) {
3200 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
3201 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07003202 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07003203 else
3204 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003205#else
3206 read_device_info_mmc(info);
3207#endif
3208
3209 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
3210 {
3211 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08003212 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07003213 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05303214#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303215 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303216 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05303217#endif
lijuang511a2b52015-08-14 20:50:51 +08003218 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07003219 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05303220#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303221 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303222 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05303223#endif
lijuang511a2b52015-08-14 20:50:51 +08003224 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003225 info->is_tampered = 0;
3226 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05303227#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303228 if (VB_M <= target_get_vb_version())
Monika Singh1c71a1c2019-12-03 12:12:48 +05303229 {
Mayank Grover889be1b2017-09-12 20:12:23 +05303230 info->verity_mode = 1; //enforcing by default
Monika Singh1c71a1c2019-12-03 12:12:48 +05303231 info->user_public_key_length = 0;
3232 memset(info->rollback_index, 0, sizeof(info->rollback_index));
3233 memset(info->user_public_key, 0, sizeof(info->user_public_key));
3234 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303235#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003236 write_device_info(info);
3237 }
jessicatseng1b9da342020-04-22 11:13:20 +08003238
3239//<2020/04/22-JessicaTseng, Enable off-charging
3240 info->charger_screen_enabled = 1;
3241 write_device_info(info);
3242//>2020/04/22-JessicaTseng
3243
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003244 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003245 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003246 }
3247 else
3248 {
3249 read_device_info_flash(dev);
3250 }
3251}
tracychuid184b912020-06-05 17:31:38 +08003252/*[20200605][TracyChui] Implement get Serial Number start*/
3253#if defined(ENABLE_PRODINFO_ACCESS)
3254void write_prod_info(prod_info *dev)
3255{
3256 if(target_is_emmc_boot())
3257 {
3258 struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3259 if(info == NULL)
3260 {
3261 dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n");
3262 ASSERT(0);
3263 }
3264 prodinfo_buf = info;
3265 memcpy(info, dev, sizeof(struct prod_info));
3266
3267 write_prod_info_mmc(info);
3268 free(info);
3269 }
3270}
3271
3272void read_prod_info(prod_info *dev)
3273{
3274 if(target_is_emmc_boot())
3275 {
3276 struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3277 if(info == NULL)
3278 {
3279 dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n");
3280 ASSERT(0);
3281 }
3282 prodinfo_buf = info;
3283
3284 read_prod_info_mmc(info);
3285
3286 if (memcmp(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE))
3287 {
3288 memcpy(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE);
3289 memcpy(info->isn, "No_Serial_Number", PRODINFO_MAX_ISN_LEN);
3290 memcpy(info->ssn, "No_Custer_Serial_Number", PRODINFO_MAX_SSN_LEN);
3291 info->is_adb_enabled = 0;
3292 write_prod_info(info);
3293 }
3294 memcpy(dev, info, sizeof(prod_info));
3295 free(info);
3296 }
3297}
3298#endif
3299/*[20200605][TracyChui] Implement get Serial Number end*/
Shashank Mittal162244e2011-08-08 19:01:25 -07003300
3301void reset_device_info()
3302{
3303 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003304 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07003305 write_device_info(&device);
3306}
3307
3308void set_device_root()
3309{
3310 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003311 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07003312 write_device_info(&device);
3313}
3314
lijuang4ece1e72015-08-14 21:02:36 +08003315/* set device unlock value
3316 * Must check FRP before call this function
3317 * Need to wipe data when unlock status changed
3318 * type 0: oem unlock
3319 * type 1: unlock critical
3320 * status 0: unlock as false
3321 * status 1: lock as true
3322 */
3323void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08003324{
lijuang4ece1e72015-08-14 21:02:36 +08003325 if (type == UNLOCK)
3326 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05303327#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303328 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303329 type == UNLOCK_CRITICAL)
3330 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05303331#endif
lijuang4ece1e72015-08-14 21:02:36 +08003332 write_device_info(&device);
3333}
3334
3335static void set_device_unlock(int type, bool status)
3336{
3337 int is_unlocked = -1;
3338 char response[MAX_RSP_SIZE];
3339
3340 /* check device unlock status if it is as expected */
3341 if (type == UNLOCK)
3342 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05303343#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303344 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303345 type == UNLOCK_CRITICAL)
3346 {
3347 is_unlocked = device.is_unlock_critical;
3348 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303349#endif
lijuang4ece1e72015-08-14 21:02:36 +08003350 if (is_unlocked == status) {
3351 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
3352 fastboot_info(response);
3353 fastboot_okay("");
3354 return;
3355 }
3356
3357 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08003358 if (status && !is_allow_unlock) {
3359 fastboot_fail("oem unlock is not allowed");
3360 return;
3361 }
lijuang21f12f52015-08-22 16:22:19 +08003362
ericlin8ef2adf2020-04-29 14:14:24 +08003363 //<2018/01/09-EricLin, To remove display_unlock_menu for quiet mode.
3364 if(target_build_variant_user()){
lijuang4ece1e72015-08-14 21:02:36 +08003365#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08003366 display_unlock_menu(type, status);
3367 fastboot_okay("");
3368 return;
lijuang4ece1e72015-08-14 21:02:36 +08003369#else
lijuang07c5d6e2018-04-23 18:32:13 +08003370 if (status && type == UNLOCK) {
3371 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
3372 return;
lijuang21f12f52015-08-22 16:22:19 +08003373 }
lijuang07c5d6e2018-04-23 18:32:13 +08003374#endif
ericlin8ef2adf2020-04-29 14:14:24 +08003375 }
3376 //<2018/01/09-EricLin
lijuang4ece1e72015-08-14 21:02:36 +08003377
3378 set_device_unlock_value(type, status);
3379
3380 /* wipe data */
3381 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303382 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08003383 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3384 write_misc(0, &msg, sizeof(msg));
3385
3386 fastboot_okay("");
ericlin8ef2adf2020-04-29 14:14:24 +08003387 //<2018/10/16-EricLin, To remove reset after oem lock/unlock.
3388 //reboot_device(RECOVERY_MODE);
3389 //<2018/10/16-EricLin
lijuang21f12f52015-08-22 16:22:19 +08003390}
3391
tracychui8fd71dc2020-06-11 10:17:13 +08003392/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start*/
3393#if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK)
3394static void set_device_unlock_skip_ui_check(int type, bool status)
3395{
3396 int is_unlocked = -1;
3397 char response[MAX_RSP_SIZE];
3398
3399 /* check device unlock status if it is as expected */
3400 if (type == UNLOCK)
3401 is_unlocked = device.is_unlocked;
3402#if VERIFIED_BOOT || VERIFIED_BOOT_2
3403 if(VB_M <= target_get_vb_version() &&
3404 type == UNLOCK_CRITICAL)
3405 {
3406 is_unlocked = device.is_unlock_critical;
3407 }
3408#endif
3409 if (is_unlocked == status) {
3410 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
3411 fastboot_info(response);
3412 fastboot_okay("");
3413 return;
3414 }
3415
3416 set_device_unlock_value(type, status);
3417 fastboot_okay("");
3418}
3419#endif
tracychui3b4abb72020-06-15 10:50:49 +08003420/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */
tracychui8fd71dc2020-06-11 10:17:13 +08003421
lijuang511a2b52015-08-14 20:50:51 +08003422static bool critical_flash_allowed(const char * entry)
3423{
3424 uint32_t i = 0;
3425 if (entry == NULL)
3426 return false;
3427
3428 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
3429 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
3430 return true;
3431 }
3432 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04003433}
3434
3435#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07003436int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
3437{
3438 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003439 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05303440 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07003441 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05303442 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003443 unsigned int compressed_size = 0;
3444 unsigned int dtb_size = 0;
3445 unsigned int out_avai_len = 0;
3446 unsigned char *out_addr = NULL;
3447 unsigned char *best_match_dt_addr = NULL;
3448 int rc;
3449
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303450 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07003451
Parth Dixit4097b622016-03-15 14:42:27 +05303452#ifndef OSVERSION_IN_BOOTIMAGE
3453 dt_size = hdr->dt_size;
3454#endif
3455
3456 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003457 /* add kernel offset */
3458 dt_image_offset += page_size;
3459 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3460 dt_image_offset += n;
3461
3462 /* add ramdisk offset */
3463 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
3464 dt_image_offset += n;
3465
3466 /* add second offset */
3467 if(hdr->second_size != 0) {
3468 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
3469 dt_image_offset += n;
3470 }
3471
3472 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07003473 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07003474
Deepa Dinamani19648b42013-09-05 17:05:55 -07003475 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003476 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
3477 return -1;
3478 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303479
3480 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
3481 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05303482 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303483 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
3484 return -1;
3485 }
3486
Joel Kingaa335dc2013-06-03 16:11:08 -07003487 /* Find index of device tree within device tree table */
3488 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07003489 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
3490 return -1;
3491 }
3492
Matthew Qin271927e2015-03-31 22:07:07 -04003493 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
3494 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
3495 {
3496 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
3497 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04003498 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003499 rc = decompress(best_match_dt_addr,
3500 dt_entry.size, out_addr, out_avai_len,
3501 &compressed_size, &dtb_size);
3502 if (rc)
3503 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003504 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003505 ASSERT(0);
3506 }
3507
Matthew Qin0b15b322015-05-19 05:20:54 -04003508 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003509 best_match_dt_addr = out_addr;
3510 } else {
3511 dtb_size = dt_entry.size;
3512 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003513 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303514 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3515 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003516 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303517 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003518 return -1;
3519 }
3520
Amol Jadicb524072012-08-09 16:40:18 -07003521 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003522 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003523 } else
3524 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003525
3526 /* Everything looks fine. Return success. */
3527 return 0;
3528}
3529#endif
3530
Brian Swetland9c4c0752009-01-25 16:23:50 -08003531void cmd_boot(const char *arg, void *data, unsigned sz)
3532{
3533 unsigned kernel_actual;
3534 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303535 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003536 uint32_t image_actual;
3537 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303538 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003539 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003540 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003541 int ret = 0;
3542 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003543 unsigned int out_len = 0;
3544 unsigned int out_avai_len = 0;
3545 unsigned char *out_addr = NULL;
3546 uint32_t dtb_offset = 0;
3547 unsigned char *kernel_start_addr = NULL;
3548 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003549 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303550#if VERIFIED_BOOT_2
3551 void *dtbo_image_buf = NULL;
3552 uint32_t dtbo_image_sz = 0;
3553 void *vbmeta_image_buf = NULL;
3554 uint32_t vbmeta_image_sz = 0;
3555#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303556#if !VERIFIED_BOOT_2
3557 uint32_t sig_actual = 0;
3558 uint32_t sig_size = 0;
3559#ifdef MDTP_SUPPORT
3560 static bool is_mdtp_activated = 0;
3561#endif /* MDTP_SUPPORT */
3562#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003563
lijuang2008ff22016-03-07 17:56:27 +08003564#if FBCON_DISPLAY_MSG
3565 /* Exit keys' detection thread firstly */
3566 exit_menu_keys_detection();
3567#endif
3568
Monika Singh292b3e92018-03-17 22:40:23 +05303569#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003570 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003571 {
3572 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003573 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003574 }
3575#endif
3576
Brian Swetland9c4c0752009-01-25 16:23:50 -08003577 if (sz < sizeof(hdr)) {
3578 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003579 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003580 }
3581
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303582 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003583
3584 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003585 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003586
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003587 if(target_is_emmc_boot() && hdr->page_size) {
3588 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003589 page_mask = page_size - 1;
3590 }
3591
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003592 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3593 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303594 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003595#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303596#ifndef OSVERSION_IN_BOOTIMAGE
3597 dt_size = hdr->dt_size;
3598#endif
3599 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003600#endif
3601
3602 image_actual = ADD_OF(page_size, kernel_actual);
3603 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303604 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003605 image_actual = ADD_OF(image_actual, dt_actual);
3606
Kishor PK5134b332017-05-09 17:50:08 +05303607 /* Checking to prevent oob access in read_der_message_length */
3608 if (image_actual > sz) {
3609 fastboot_fail("bootimage header fields are invalid");
3610 goto boot_failed;
3611 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303612
Monika Singh292b3e92018-03-17 22:40:23 +05303613#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303614 /* Pass size of boot partition, as imgsize, to avoid
3615 read fewer bytes error */
3616 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303617
3618 /* load and validate dtbo partition */
3619 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3620
3621 /* load vbmeta partition */
3622 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3623
Monika Singh292b3e92018-03-17 22:40:23 +05303624 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303625
3626 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3627 info.images[IMG_BOOT].imgsize = image_actual;
3628 info.images[IMG_BOOT].name = "boot";
3629 ++info.num_loaded_images;
3630
3631 /* Pass loaded dtbo image */
3632 if (dtbo_image_buf != NULL) {
3633 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3634 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3635 info.images[IMG_DTBO].name = "dtbo";
3636 ++info.num_loaded_images;
3637 }
3638
3639 /* Pass loaded vbmeta image */
3640 if (vbmeta_image_buf != NULL) {
3641 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3642 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3643 info.images[IMG_VBMETA].name = "vbmeta";
3644 ++info.num_loaded_images;
3645 }
3646
Monika Singh292b3e92018-03-17 22:40:23 +05303647 info.multi_slot_boot = partition_multislot_is_supported();
3648 if (load_image_and_auth(&info))
3649 goto boot_failed;
3650 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303651
3652 /* Free the buffer allocated to vbmeta post verification */
3653 if (vbmeta_image_buf != NULL) {
3654 free(vbmeta_image_buf);
3655 --info.num_loaded_images;
3656 }
Monika Singh292b3e92018-03-17 22:40:23 +05303657#else
Kishor PK5134b332017-05-09 17:50:08 +05303658 sig_size = sz - image_actual;
3659
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303660 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303661 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303662 /* Calculate the signature length from boot image */
3663 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303664 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303665 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003666
Monika Singh292b3e92018-03-17 22:40:23 +05303667 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303668 fastboot_fail("bootimage header fields are invalid");
3669 goto boot_failed;
3670 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003671 }
3672
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003673 // Initialize boot state before trying to verify boot.img
3674#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003675 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303676#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003677 /* Handle overflow if the input image size is greater than
3678 * boot image buffer can hold
3679 */
Monika Singh292b3e92018-03-17 22:40:23 +05303680 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003681 {
3682 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003683 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003684 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003685
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003686 /* Verify the boot image
3687 * device & page_size are initialized in aboot_init
3688 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003689 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003690 /* Pass size excluding signature size, otherwise we would try to
3691 * access signature beyond its length
3692 */
Monika Singh292b3e92018-03-17 22:40:23 +05303693 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003694 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003695#ifdef MDTP_SUPPORT
3696 else
3697 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003698 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003699 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003700
3701 if (!is_mdtp_activated) {
3702 ext_partition.partition = MDTP_PARTITION_NONE;
3703 mdtp_fwlock_verify_lock(&ext_partition);
3704 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003705 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003706
Amir Kotzer7c768c02016-04-13 09:08:05 +03003707 /* If mdtp state cannot be validate, block fastboot boot*/
3708 if(mdtp_activated(&is_mdtp_activated)){
3709 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3710 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3711 goto boot_failed;
3712 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003713 if(is_mdtp_activated){
3714 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003715 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003716 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003717#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303718#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003719
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003720#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303721 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303722 {
3723 /* set boot and system versions. */
3724 set_os_version((unsigned char *)data);
3725 // send root of trust
3726 if(!send_rot_command((uint32_t)device.is_unlocked))
3727 ASSERT(0);
3728 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303729#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003730 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003731 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3732 * If not, continue booting.
3733 */
3734 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3735 {
3736 out_addr = (unsigned char *)target_get_scratch_address();
3737 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3738 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303739#if VERIFIED_BOOT_2
3740 if (dtbo_image_sz)
3741 out_avai_len -= DTBO_IMG_BUF;
3742#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003743 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003744 ret = decompress((unsigned char *)(ptr + page_size),
3745 hdr->kernel_size, out_addr, out_avai_len,
3746 &dtb_offset, &out_len);
3747 if (ret)
3748 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003749 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003750 ASSERT(0);
3751 }
3752
Matthew Qin0b15b322015-05-19 05:20:54 -04003753 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003754 kptr = (struct kernel64_hdr *)out_addr;
3755 kernel_start_addr = out_addr;
3756 kernel_size = out_len;
3757 } else {
3758 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3759 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3760 kernel_size = hdr->kernel_size;
3761 }
3762
3763 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003764 * Update the kernel/ramdisk/tags address if the boot image header
3765 * has default values, these default values come from mkbootimg when
3766 * the boot image is flashed using fastboot flash:raw
3767 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003768 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003769
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003770 /* Get virtual addresses since the hdr saves physical addresses. */
3771 hdr->kernel_addr = VA(hdr->kernel_addr);
3772 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3773 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003774
Matthew Qinbb7923d2015-02-09 10:56:09 +08003775 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003776 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003777 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303778 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3779 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3780 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003781 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303782 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003783 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003784 }
3785
Amol Jadicb524072012-08-09 16:40:18 -07003786#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003787 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003788 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003789 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003790
3791 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003792#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303793 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3794 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003795 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303796 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003797 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003798 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003799#endif
3800
3801 /* Load ramdisk & kernel */
3802 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003803 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003804
3805#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303806 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3807 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003808 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303809 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003810 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003811 }
3812
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003813 /*
3814 * If dtb is not found look for appended DTB in the kernel.
3815 * If appended dev tree is found, update the atags with
3816 * memory address to the DTB appended location on RAM.
3817 * Else update with the atags address in the kernel header
3818 */
3819 if (!dtb_copied) {
3820 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003821 dtb = dev_tree_appended((void*)(ptr + page_size),
3822 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003823 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003824 if (!dtb) {
3825 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003826 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003827 }
Amol Jadicb524072012-08-09 16:40:18 -07003828 }
3829#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003830
3831 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003832 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003833
Dima Zavin77e41f32013-03-06 16:10:43 -08003834 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003835 (const char*) hdr->cmdline, board_machtype(),
3836 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003837
3838 /* fastboot already stop, it's no need to show fastboot menu */
3839 return;
3840boot_failed:
3841#if FBCON_DISPLAY_MSG
3842 /* revert to fastboot menu if boot failed */
3843 display_fastboot_menu();
3844#endif
3845 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003846}
3847
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003848void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003849{
3850 struct ptentry *ptn;
3851 struct ptable *ptable;
3852
3853 ptable = flash_get_ptable();
3854 if (ptable == NULL) {
3855 fastboot_fail("partition table doesn't exist");
3856 return;
3857 }
3858
3859 ptn = ptable_find(ptable, arg);
3860 if (ptn == NULL) {
3861 fastboot_fail("unknown partition name");
3862 return;
3863 }
3864
Monika Singh292b3e92018-03-17 22:40:23 +05303865 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3866 dprintf(INFO, "erasing avb_custom_key\n");
3867 if (erase_userkey()) {
3868 fastboot_fail("Erasing avb_custom_key failed");
3869 } else {
3870 fastboot_okay("");
3871 }
3872 return;
3873 }
3874
Dima Zavin214cc642009-01-26 11:16:21 -08003875 if (flash_erase(ptn)) {
3876 fastboot_fail("failed to erase partition");
3877 return;
3878 }
3879 fastboot_okay("");
3880}
3881
Bikas Gurungd48bd242010-09-04 19:54:32 -07003882
3883void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3884{
3885 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003886 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003887 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003888 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303889 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003890
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003891#if VERIFIED_BOOT
3892 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3893 {
3894 if(!device.is_unlocked)
3895 {
3896 fastboot_fail("unlock device to erase keystore");
3897 return;
3898 }
3899 }
3900#endif
3901
Kinson Chikf1a43512011-07-14 11:28:39 -07003902 index = partition_get_index(arg);
3903 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003904 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003905
Monika Singhc4778b72018-05-16 11:16:42 +05303906 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3907 dprintf(INFO, "erasing avb_custom_key\n");
3908 if (erase_userkey()) {
3909 fastboot_fail("Erasing avb_custom_key failed");
3910 } else {
3911 fastboot_okay("");
3912 }
3913 return;
3914 }
3915
Kinson Chikf1a43512011-07-14 11:28:39 -07003916 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003917 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003918 return;
3919 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003920
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003921 lun = partition_get_lun(index);
3922 mmc_set_lun(lun);
3923
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003924 if (platform_boot_dev_isemmc())
3925 {
3926 if (mmc_erase_card(ptn, size)) {
3927 fastboot_fail("failed to erase partition\n");
3928 return;
3929 }
3930 } else {
3931 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3932 size = partition_get_size(index);
3933 if (size > DEFAULT_ERASE_SIZE)
3934 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003935
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003936 /* Simple inefficient version of erase. Just writing
3937 0 in first several blocks */
3938 if (mmc_write(ptn , size, (unsigned int *)out)) {
3939 fastboot_fail("failed to erase partition");
3940 return;
3941 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303942 /*Erase FDE metadata at the userdata footer*/
3943 if(!(strncmp(arg, "userdata", 8)))
3944 {
3945 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3946 memset((void *)footer, 0, FOOTER_SIZE);
3947
3948 size = partition_get_size(index);
3949
3950 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3951 fastboot_fail("failed to erase userdata footer");
3952 free(footer);
3953 return;
3954 }
3955 free(footer);
3956 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003957 }
Monika Singh292b3e92018-03-17 22:40:23 +05303958#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303959 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303960 !(strncmp(arg, "userdata", 8)) &&
3961 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003962 ASSERT(0);
3963#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003964 fastboot_okay("");
3965}
3966
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003967void cmd_erase(const char *arg, void *data, unsigned sz)
3968{
Monika Singh292b3e92018-03-17 22:40:23 +05303969#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003970 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003971 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003972 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003973 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003974 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003975 return;
3976 }
3977 }
3978#endif
3979
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003980 if(target_is_emmc_boot())
3981 cmd_erase_mmc(arg, data, sz);
3982 else
3983 cmd_erase_nand(arg, data, sz);
3984}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003985
Mayank Grover11ff9692018-01-11 11:54:49 +05303986/* Get the size from partiton name */
3987static void get_partition_size(const char *arg, char *response)
3988{
3989 uint64_t ptn = 0;
3990 uint64_t size;
3991 int index = INVALID_PTN;
3992
3993 index = partition_get_index(arg);
3994
3995 if (index == INVALID_PTN)
3996 {
3997 dprintf(CRITICAL, "Invalid partition index\n");
3998 return;
3999 }
4000
4001 ptn = partition_get_offset(index);
4002
4003 if(!ptn)
4004 {
4005 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
4006 return;
4007 }
4008
4009 size = partition_get_size(index);
4010
4011 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
4012 return;
4013}
4014
Mayank Grover52cd10a2018-03-15 12:57:54 +05304015/* Function to check partition type of a partition*/
4016static fs_signature_type
4017check_partition_fs_signature(const char *arg)
4018{
4019 fs_signature_type ret = NO_FS;
4020 int index;
4021 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05304022 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
4023 uint32_t sb_blk_offset = 0;
4024 char *sb_buffer = buffer;
4025
Mayank Grover52cd10a2018-03-15 12:57:54 +05304026 if (!sb_buffer)
4027 {
4028 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
4029 goto out;
4030 }
4031
4032 /* Read super block */
4033 if ((index = partition_get_index(arg)) < 0)
4034 {
4035 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
4036 goto out;
4037 }
4038 ptn = partition_get_offset(index);
4039 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05304040 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
4041
4042 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05304043 (void *)sb_buffer, mmc_blocksize))
4044 {
4045 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
4046 goto out;
4047 }
4048
Mayank Grover399826a2018-08-27 12:15:15 +05304049 if (sb_blk_offset == 0)
4050 sb_buffer += FS_SUPERBLOCK_OFFSET;
4051
4052 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05304053 {
4054 dprintf(SPEW, "%s() Found EXT FS\n", arg);
4055 ret = EXT_FS_SIGNATURE;
4056 }
Mayank Grover399826a2018-08-27 12:15:15 +05304057 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05304058 {
4059 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
4060 ret = EXT_F2FS_SIGNATURE;
4061 }
4062 else
4063 {
4064 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
4065 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
4066 ret = NO_FS;
4067 }
4068
4069out:
Mayank Grover399826a2018-08-27 12:15:15 +05304070 if(buffer)
4071 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304072 return ret;
4073}
4074
Mayank Groverd38fe012018-03-13 15:33:16 +05304075/* Function to get partition type */
4076static void get_partition_type(const char *arg, char *response)
4077{
4078 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05304079 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05304080
4081 if (arg == NULL ||
4082 response == NULL)
4083 {
4084 dprintf(CRITICAL, "Invalid input parameter\n");
4085 return;
4086 }
4087
4088 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05304089 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05304090
4091 /* Mark partiton type for known paritions only */
4092 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
4093 {
4094 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
4095 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05304096 /* Check partition for FS signature */
4097 fs_signature = check_partition_fs_signature(arg);
4098 switch (fs_signature)
4099 {
4100 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05304101 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304102 break;
4103 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05304104 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304105 break;
4106 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05304107 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304108 }
Mayank Groverd38fe012018-03-13 15:33:16 +05304109 }
4110 }
4111 return;
4112}
4113
Mayank Grover11ff9692018-01-11 11:54:49 +05304114/*
4115 * Publish the partition type & size info
4116 * fastboot getvar will publish the required information.
4117 * fastboot getvar partition_size:<partition_name>: partition size in hex
4118 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
4119 */
4120static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
4121{
Mayank Groverd38fe012018-03-13 15:33:16 +05304122 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05304123 static bool published = false;
4124 struct partition_entry *ptn_entry =
4125 partition_get_partition_entries();
4126 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
4127
4128 for (i = 0; i < num_parts; i++) {
4129 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
4130 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
4131 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
4132
Mayank Groverd38fe012018-03-13 15:33:16 +05304133 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05304134 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05304135 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4136 {
4137 dprintf(CRITICAL, "partition size name truncated\n");
4138 return;
4139 }
4140 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4141 {
4142 dprintf(CRITICAL, "partition type name truncated\n");
4143 return;
4144 }
4145
4146 if (!published)
4147 {
4148 /* publish partition size & type info */
4149 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
4150 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
4151 }
4152 }
4153 if (!published)
4154 published = true;
4155}
4156
4157
Ajay Dudani5c761132011-04-07 20:19:04 -07004158void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07004159{
4160 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07004161 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07004162 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004163 char *token = NULL;
4164 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004165 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004166 uint8_t lun = 0;
4167 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05304168 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07004169
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004170 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004171 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004172 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004173 if(token)
4174 {
4175 lun = atoi(token);
4176 mmc_set_lun(lun);
4177 lun_set = true;
4178 }
4179
Mao Jinlong226f33a2014-07-04 17:24:10 +08004180 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07004181 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07004182 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
4183 {
4184 if (!aboot_frp_unlock(pname, data, sz))
4185 {
4186 fastboot_info("FRP unlock successful");
4187 fastboot_okay("");
4188 }
4189 else
4190 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
4191
4192 return;
4193 }
4194
Mao Jinlong226f33a2014-07-04 17:24:10 +08004195 if (!strcmp(pname, "partition"))
4196 {
4197 dprintf(INFO, "Attempt to write partition image.\n");
4198 if (write_partition(sz, (unsigned char *) data)) {
4199 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07004200 return;
4201 }
Mayank Grover11ff9692018-01-11 11:54:49 +05304202 /* Re-publish partition table */
4203 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05304204
4205 /* Rescan partition table to ensure we have multislot support*/
4206 if (partition_scan_for_multislot())
4207 {
4208 current_active_slot = partition_find_active_slot();
4209 dprintf(INFO, "Multislot supported: Slot %s active",
4210 (SUFFIX_SLOT(current_active_slot)));
4211 }
4212 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07004213 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08004214 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004215 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004216#if VERIFIED_BOOT
4217 if(!strcmp(pname, KEYSTORE_PTN_NAME))
4218 {
4219 if(!device.is_unlocked)
4220 {
4221 fastboot_fail("unlock device to flash keystore");
4222 return;
4223 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05304224 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004225 {
4226 fastboot_fail("image is not a keystore file");
4227 return;
4228 }
4229 }
4230#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08004231 index = partition_get_index(pname);
4232 ptn = partition_get_offset(index);
4233 if(ptn == 0) {
4234 fastboot_fail("partition table doesn't exist");
4235 return;
4236 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004237
Mayank Grover351a75e2017-05-30 20:06:08 +05304238 if (!strncmp(pname, "boot", strlen("boot"))
4239 || !strcmp(pname, "recovery"))
4240 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08004241 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
4242 fastboot_fail("image is not a boot image");
4243 return;
4244 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304245
4246 /* Reset multislot_partition attributes in case of flashing boot */
4247 if (partition_multislot_is_supported())
4248 {
4249 partition_reset_attributes(index);
4250 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08004251 }
4252
4253 if(!lun_set)
4254 {
4255 lun = partition_get_lun(index);
4256 mmc_set_lun(lun);
4257 }
4258
4259 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304260 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08004261 fastboot_fail("size too large");
4262 return;
4263 }
4264 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
4265 fastboot_fail("flash write failure");
4266 return;
4267 }
Greg Grisco6e754772011-06-23 12:19:39 -07004268 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07004269 }
4270 fastboot_okay("");
4271 return;
4272}
4273
Ajay Dudanide984792015-03-02 09:57:41 -08004274void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
4275{
4276 int i, images;
4277 meta_header_t *meta_header;
4278 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05304279 /*End of the image address*/
4280 uintptr_t data_end;
4281
4282 if( (UINT_MAX - sz) > (uintptr_t)data )
4283 data_end = (uintptr_t)data + sz;
4284 else
4285 {
4286 fastboot_fail("Cannot flash: image header corrupt");
4287 return;
4288 }
4289
4290 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
4291 {
4292 fastboot_fail("Cannot flash: image header corrupt");
4293 return;
4294 }
Ajay Dudanide984792015-03-02 09:57:41 -08004295
lijuang8ee21882016-04-19 16:57:11 +08004296 /* If device is locked:
4297 * Forbid to flash image to avoid the device to bypass the image
4298 * which with "any" name other than bootloader. Because it maybe
4299 * a meta package of all partitions.
4300 */
Monika Singh292b3e92018-03-17 22:40:23 +05304301#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08004302 if (target_build_variant_user()) {
4303 if (!device.is_unlocked) {
4304 fastboot_fail("Device is locked, meta image flashing is not allowed");
4305 return;
4306 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304307
Mayank Grover912eaa62017-10-26 12:08:53 +05304308 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304309 !device.is_unlock_critical)
4310 {
lijuang8ee21882016-04-19 16:57:11 +08004311 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
4312 return;
4313 }
lijuang8ee21882016-04-19 16:57:11 +08004314 }
4315#endif
4316
Ajay Dudanide984792015-03-02 09:57:41 -08004317 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05304318 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
4319 {
4320 fastboot_fail("Cannot flash: image header corrupt");
4321 return;
4322 }
Ajay Dudanide984792015-03-02 09:57:41 -08004323 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
4324
4325 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
4326
4327 for (i=0; i<images; i++) {
4328
4329 if((img_header_entry[i].ptn_name == NULL) ||
4330 (img_header_entry[i].start_offset == 0) ||
4331 (img_header_entry[i].size == 0))
4332 break;
Kishor PK49831502017-04-21 17:55:43 +05304333 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
4334 fastboot_fail("Integer overflow detected in start_offset of img");
4335 break;
4336 }
4337 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
4338 fastboot_fail("Integer overflow detected in size of img");
4339 break;
4340 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05304341 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
4342 + img_header_entry[i].size) )
4343 {
4344 fastboot_fail("Cannot flash: image size mismatch");
4345 break;
4346 }
4347
Ajay Dudanide984792015-03-02 09:57:41 -08004348 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
4349 (void *) data + img_header_entry[i].start_offset,
4350 img_header_entry[i].size);
4351 }
4352
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004353 if (!strncmp(arg, "bootloader", strlen("bootloader")))
4354 {
4355 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
4356 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
4357 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
4358 }
4359 else
4360 {
4361 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
4362 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
4363 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
4364 }
4365
4366 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08004367 fastboot_okay("");
4368 return;
4369}
4370
Ajay Dudani5c761132011-04-07 20:19:04 -07004371void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
4372{
4373 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04004374 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004375 uint32_t *fill_buf = NULL;
4376 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05304377 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004378 sparse_header_t *sparse_header;
4379 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07004380 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004381 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304382 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07004383 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05304384 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004385 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05304386 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05304387 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004388
Kinson Chikf1a43512011-07-14 11:28:39 -07004389 index = partition_get_index(arg);
4390 ptn = partition_get_offset(index);
4391 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07004392 fastboot_fail("partition table doesn't exist");
4393 return;
4394 }
4395
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304396 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304397
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004398 lun = partition_get_lun(index);
4399 mmc_set_lun(lun);
4400
vijay kumar800255e2015-04-24 20:26:19 +05304401 if (sz < sizeof(sparse_header_t)) {
4402 fastboot_fail("size too low");
4403 return;
4404 }
4405
Ajay Dudani5c761132011-04-07 20:19:04 -07004406 /* Read and skip over sparse image header */
4407 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05304408
Mayank Grover48bd9bb2017-07-19 12:04:16 +05304409 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
4410 fastboot_fail("Invalid block size\n");
4411 return;
4412 }
4413
vijay kumar1321f342015-03-27 12:13:42 +05304414 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08004415 fastboot_fail("size too large");
4416 return;
4417 }
4418
vijay kumarde4fcf62015-04-23 13:05:49 +05304419 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05304420
Parth Dixit64b1a482016-03-07 16:31:26 +05304421 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304422 fastboot_fail("buffer overreads occured due to invalid sparse header");
4423 return;
4424 }
4425
vijay kumarde4fcf62015-04-23 13:05:49 +05304426 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004427 {
vijay kumarde4fcf62015-04-23 13:05:49 +05304428 fastboot_fail("sparse header size mismatch");
4429 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004430 }
4431
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004432 dprintf (SPEW, "=== Sparse Image Header ===\n");
4433 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
4434 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
4435 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
4436 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
4437 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
4438 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
4439 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
4440 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07004441
4442 /* Start processing chunks */
4443 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
4444 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304445 /* Make sure the total image size does not exceed the partition size */
4446 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
4447 fastboot_fail("size too large");
4448 return;
4449 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004450 /* Read and skip over chunk header */
4451 chunk_header = (chunk_header_t *) data;
4452 data += sizeof(chunk_header_t);
4453
Parth Dixit64b1a482016-03-07 16:31:26 +05304454 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304455 fastboot_fail("buffer overreads occured due to invalid sparse header");
4456 return;
4457 }
4458
Ajay Dudani5c761132011-04-07 20:19:04 -07004459 dprintf (SPEW, "=== Chunk Header ===\n");
4460 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
4461 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
4462 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
4463
vijay kumar800255e2015-04-24 20:26:19 +05304464 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004465 {
vijay kumar800255e2015-04-24 20:26:19 +05304466 fastboot_fail("chunk header size mismatch");
4467 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004468 }
4469
wufeng.jiang813dc352015-06-02 23:02:46 -04004470 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
4471
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304472 /* Make sure that the chunk size calculated from sparse image does not
4473 * exceed partition size
4474 */
4475 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
4476 {
4477 fastboot_fail("Chunk data size exceeds partition size");
4478 return;
4479 }
4480
Ajay Dudani5c761132011-04-07 20:19:04 -07004481 switch (chunk_header->chunk_type)
4482 {
4483 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04004484 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07004485 chunk_data_sz))
4486 {
4487 fastboot_fail("Bogus chunk size for chunk type Raw");
4488 return;
4489 }
4490
Parth Dixit64b1a482016-03-07 16:31:26 +05304491 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304492 fastboot_fail("buffer overreads occured due to invalid sparse header");
4493 return;
4494 }
4495
wufeng.jiang813dc352015-06-02 23:02:46 -04004496 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
4497 otherwise it will return in the line above
4498 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07004499 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04004500 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07004501 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07004502 {
4503 fastboot_fail("flash write failure");
4504 return;
4505 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304506 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4507 fastboot_fail("Bogus size for RAW chunk type");
4508 return;
4509 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004510 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004511 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004512 break;
4513
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004514 case CHUNK_TYPE_FILL:
4515 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4516 sizeof(uint32_t)))
4517 {
4518 fastboot_fail("Bogus chunk size for chunk type FILL");
4519 return;
4520 }
4521
Mayank Grover4f50bba2017-07-19 18:17:08 +05304522 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4523 /* Integer overflow detected */
4524 if (blk_sz_actual < sparse_header->blk_sz)
4525 {
4526 fastboot_fail("Invalid block size");
4527 return;
4528 }
4529
4530 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004531 if (!fill_buf)
4532 {
4533 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4534 return;
4535 }
4536
Parth Dixit64b1a482016-03-07 16:31:26 +05304537 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304538 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304539 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304540 return;
4541 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004542 fill_val = *(uint32_t *)data;
4543 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004544
4545 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4546 {
4547 fill_buf[i] = fill_val;
4548 }
4549
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304550 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4551 {
4552 fastboot_fail("bogus size for chunk FILL type");
4553 free(fill_buf);
4554 return;
4555 }
4556
wufeng.jiang813dc352015-06-02 23:02:46 -04004557 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004558 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304559 /* Make sure that the data written to partition does not exceed partition size */
4560 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4561 {
4562 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304563 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304564 return;
4565 }
4566
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004567 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4568 sparse_header->blk_sz,
4569 fill_buf))
4570 {
4571 fastboot_fail("flash write failure");
4572 free(fill_buf);
4573 return;
4574 }
4575
4576 total_blocks++;
4577 }
4578
4579 free(fill_buf);
4580 break;
4581
Ajay Dudani5c761132011-04-07 20:19:04 -07004582 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304583 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4584 fastboot_fail("bogus size for chunk DONT CARE type");
4585 return;
4586 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004587 total_blocks += chunk_header->chunk_sz;
4588 break;
4589
Ajay Dudani5c761132011-04-07 20:19:04 -07004590 case CHUNK_TYPE_CRC:
4591 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4592 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004593 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004594 return;
4595 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304596 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4597 fastboot_fail("bogus size for chunk CRC type");
4598 return;
4599 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004600 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304601 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304602 fastboot_fail("integer overflow occured");
4603 return;
4604 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004605 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304606 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304607 fastboot_fail("buffer overreads occured due to invalid sparse header");
4608 return;
4609 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004610 break;
4611
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004612 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004613 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004614 fastboot_fail("Unknown chunk type");
4615 return;
4616 }
4617 }
4618
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004619 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4620 total_blocks, sparse_header->total_blks);
4621
4622 if(total_blocks != sparse_header->total_blks)
4623 {
4624 fastboot_fail("sparse image write failure");
4625 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004626
4627 fastboot_okay("");
4628 return;
4629}
4630
4631void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4632{
4633 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004634 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004635
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004636#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004637 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4638 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004639 int ret=0;
4640 uint32 major_version=0;
4641 uint32 minor_version=0;
4642
4643 ret = scm_svc_version(&major_version,&minor_version);
4644 if(!ret)
4645 {
4646 if(major_version >= 2)
4647 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004648 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004649 {
4650 ret = encrypt_scm((uint32 **) &data, &sz);
4651 if (ret != 0) {
4652 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4653 return;
4654 }
4655
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004656 /* Protect only for SSD */
4657 if (!strcmp(arg, "ssd")) {
4658 ret = scm_protect_keystore((uint32 *) data, sz);
4659 if (ret != 0) {
4660 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4661 return;
4662 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004663 }
4664 }
4665 else
4666 {
4667 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4668 if(ret != 0)
4669 {
4670 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4671 return;
4672 }
4673 }
4674 }
4675 else
4676 {
4677 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4678 magic_number[1] == DECRYPT_MAGIC_1)
4679 {
4680 ret = decrypt_scm((uint32 **) &data, &sz);
4681 if (ret != 0) {
4682 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4683 return;
4684 }
4685 }
4686 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4687 magic_number[1] == ENCRYPT_MAGIC_1)
4688 {
4689 ret = encrypt_scm((uint32 **) &data, &sz);
4690 if (ret != 0) {
4691 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4692 return;
4693 }
4694 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004695 }
4696 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004697 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004698 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004699 dprintf(CRITICAL,"INVALID SVC Version\n");
4700 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004701 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004702#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004703
Monika Singh292b3e92018-03-17 22:40:23 +05304704#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004705 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004706 {
lijuang511a2b52015-08-14 20:50:51 +08004707 /* if device is locked:
4708 * common partition will not allow to be flashed
4709 * critical partition will allow to flash image.
4710 */
4711 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4712 fastboot_fail("Partition flashing is not allowed");
4713 return;
4714 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304715
lijuang511a2b52015-08-14 20:50:51 +08004716 /* if device critical is locked:
4717 * common partition will allow to be flashed
4718 * critical partition will not allow to flash image.
4719 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304720 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304721 !device.is_unlock_critical &&
4722 critical_flash_allowed(arg)) {
4723 fastboot_fail("Critical partition flashing is not allowed");
4724 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004725 }
4726 }
4727#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304728 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4729 dprintf(INFO, "flashing avb_custom_key\n");
4730 if (store_userkey(data, sz)) {
4731 fastboot_fail("Flashing avb_custom_key failed");
4732 } else {
4733 fastboot_okay("");
4734 }
4735 return;
4736 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004737
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004738 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004739 meta_header = (meta_header_t *) data;
4740 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004741 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004742 else if (meta_header->magic == META_HEADER_MAGIC)
4743 cmd_flash_meta_img(arg, data, sz);
4744 else
4745 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004746
4747#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304748 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304749 (!strncmp(arg, "system", 6)) &&
4750 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004751 // reset dm_verity mode to enforcing
4752 device.verity_mode = 1;
4753 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304754#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004755
Ajay Dudani5c761132011-04-07 20:19:04 -07004756 return;
4757}
4758
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004759void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4760{
4761 struct ptentry *sys_ptn;
4762 struct ptable *ptable;
4763
4764 ptable = flash_get_ptable();
4765 if (ptable == NULL) {
4766 fastboot_fail("partition table doesn't exist");
4767 return;
4768 }
4769
4770 sys_ptn = ptable_find(ptable, "system");
4771 if (sys_ptn == NULL) {
4772 fastboot_fail("system partition not found");
4773 return;
4774 }
4775
4776 sz = ROUND_TO_PAGE(sz, page_mask);
4777 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4778 fastboot_fail("update_ubi_vol failed");
4779 else
4780 fastboot_okay("");
4781}
4782
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004783void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004784{
4785 struct ptentry *ptn;
4786 struct ptable *ptable;
4787 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304788 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304789 unsigned bytes_to_round_page = 0;
4790 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004791
Kishor PK38ed93d2017-04-25 14:19:26 +05304792 if((uintptr_t)data > (UINT_MAX - sz)) {
4793 fastboot_fail("Cannot flash: image header corrupt");
4794 return;
4795 }
4796
Dima Zavin214cc642009-01-26 11:16:21 -08004797 ptable = flash_get_ptable();
4798 if (ptable == NULL) {
4799 fastboot_fail("partition table doesn't exist");
4800 return;
4801 }
4802
4803 ptn = ptable_find(ptable, arg);
4804 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004805 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4806 arg);
4807 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004808 return;
4809 }
4810
Monika Singh292b3e92018-03-17 22:40:23 +05304811 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4812 dprintf(INFO, "flashing avb_custom_key\n");
4813 if (store_userkey(data, sz)) {
4814 fastboot_fail("Flashing avb_custom_key failed");
4815 } else {
4816 fastboot_okay("");
4817 }
4818 return;
4819 }
4820
Dima Zavin214cc642009-01-26 11:16:21 -08004821 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304822 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4823 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4824 } else {
4825 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004826 return;
4827 }
4828 }
4829
Amol Jadi5c61a952012-05-04 17:05:35 -07004830 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004831 || !strcmp(ptn->name, "userdata")
4832 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004833 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004834 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004835 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304836 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304837 rounded_size = ROUNDUP(sz, page_size);
4838 bytes_to_round_page = rounded_size - sz;
4839 if (bytes_to_round_page) {
4840 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4841 fastboot_fail("Integer overflow detected");
4842 return;
4843 }
4844 if (((uintptr_t)data + sz + bytes_to_round_page) >
4845 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4846 fastboot_fail("Buffer size is not aligned to page_size");
4847 return;
4848 }
4849 else {
4850 memset(data + sz, 0, bytes_to_round_page);
4851 sz = rounded_size;
4852 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304853 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304854 }
4855
Kishor PK38ed93d2017-04-25 14:19:26 +05304856 /*Checking partition_size for the possible integer overflow */
4857 partition_size = validate_partition_size(ptn);
4858
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304859 if (sz > partition_size) {
4860 fastboot_fail("Image size too large");
4861 return;
4862 }
4863
Dima Zavin214cc642009-01-26 11:16:21 -08004864 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304865 if ((sz > UBI_EC_HDR_SIZE) &&
4866 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004867 if (flash_ubi_img(ptn, data, sz)) {
4868 fastboot_fail("flash write failure");
4869 return;
4870 }
4871 } else {
4872 if (flash_write(ptn, extra, data, sz)) {
4873 fastboot_fail("flash write failure");
4874 return;
4875 }
Dima Zavin214cc642009-01-26 11:16:21 -08004876 }
4877 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4878 fastboot_okay("");
4879}
4880
Kishor PK38ed93d2017-04-25 14:19:26 +05304881
4882static inline uint64_t validate_partition_size(struct ptentry *ptn)
4883{
4884 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4885 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4886 return ptn->length * flash_num_pages_per_blk() * page_size;
4887 }
4888 }
4889 return 0;
4890}
4891
4892
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004893void cmd_flash(const char *arg, void *data, unsigned sz)
4894{
4895 if(target_is_emmc_boot())
4896 cmd_flash_mmc(arg, data, sz);
4897 else
4898 cmd_flash_nand(arg, data, sz);
4899}
4900
Dima Zavin214cc642009-01-26 11:16:21 -08004901void cmd_continue(const char *arg, void *data, unsigned sz)
4902{
4903 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004904 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004905
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004906 if (target_is_emmc_boot())
4907 {
lijuanga40d6302015-07-20 20:10:13 +08004908#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004909 /* Exit keys' detection thread firstly */
4910 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004911#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004912 boot_linux_from_mmc();
4913 }
4914 else
4915 {
4916 boot_linux_from_flash();
4917 }
Dima Zavin214cc642009-01-26 11:16:21 -08004918}
4919
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004920void cmd_reboot(const char *arg, void *data, unsigned sz)
4921{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004922 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004923 fastboot_okay("");
4924 reboot_device(0);
4925}
4926
Mayank Grover351a75e2017-05-30 20:06:08 +05304927void cmd_set_active(const char *arg, void *data, unsigned sz)
4928{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304929 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304930 unsigned i,current_active_slot;
4931 const char *current_slot_suffix;
4932
4933 if (!partition_multislot_is_supported())
4934 {
4935 fastboot_fail("Command not supported");
4936 return;
4937 }
4938
4939 if (arg)
4940 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304941 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304942 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304943 {
4944 current_active_slot = partition_find_active_slot();
4945
4946 /* Check if trying to make curent slot active */
4947 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304948 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4949 (char *)suffix_delimiter, &sp);
4950
Mayank Grover5eb5f692017-07-19 20:16:04 +05304951 if (current_slot_suffix &&
4952 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304953 {
4954 fastboot_okay("Slot already set active");
4955 return;
4956 }
4957 else
4958 {
4959 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4960 {
4961 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304962 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4963 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304964 if (current_slot_suffix &&
4965 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304966 {
lijuang8b03e5f2019-07-12 18:16:19 +08004967 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304968 publish_getvar_multislot_vars();
4969 fastboot_okay("");
4970 return;
4971 }
4972 }
4973 }
4974 }
4975 }
4976 fastboot_fail("Invalid slot suffix.");
4977 return;
4978}
4979
Mayank Grover98c4c742019-04-25 17:21:37 +05304980#if DYNAMIC_PARTITION_SUPPORT
4981void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4982{
4983 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4984 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4985 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4986 fastboot_fail("Failed to update recovery command");
4987 return;
4988 }
4989 fastboot_okay("");
4990 reboot_device(REBOOT_MODE_UNKNOWN);
4991
4992 //shouldn't come here.
4993 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4994 return;
4995}
4996
4997void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4998{
4999 dprintf(INFO, "rebooting the device - recovery\n");
5000 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
5001 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
5002 fastboot_fail("Failed to update recovery command");
5003 return;
5004 }
5005 fastboot_okay("");
5006 reboot_device(REBOOT_MODE_UNKNOWN);
5007
5008 //shouldn't come here.
5009 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
5010 return;
5011}
5012#endif
5013
Chandan Uddaraju94183c02010-01-15 15:13:59 -08005014void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
5015{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005016 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08005017 fastboot_okay("");
5018 reboot_device(FASTBOOT_MODE);
5019}
5020
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005021void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
5022{
5023 dprintf(INFO, "Enabling charger screen check\n");
5024 device.charger_screen_enabled = 1;
5025 write_device_info(&device);
5026 fastboot_okay("");
5027}
5028
5029void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
5030{
5031 dprintf(INFO, "Disabling charger screen check\n");
5032 device.charger_screen_enabled = 0;
5033 write_device_info(&device);
5034 fastboot_okay("");
5035}
5036
tracychuid184b912020-06-05 17:31:38 +08005037/*[20200605][TracyChui] Implement get Serial Number start*/
5038#if defined(ENABLE_PRODINFO_ACCESS)
5039void CmdOemEnableAdb(const char *arg, void *data, unsigned size)
5040{
5041 dprintf(INFO, "Enabling Adb\n");
5042 prod.is_adb_enabled = 1;
5043 write_prod_info(&prod);
5044 fastboot_okay("");
5045}
5046#endif
5047/*[20200605][TracyChui] Implement get Serial Number end*/
5048
lijuanga25d8bb2015-09-16 13:11:52 +08005049void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
5050{
5051 char *p = NULL;
5052 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05305053 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08005054
5055 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05305056 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08005057 if (p) {
5058 if (!strncmp(p, "0", 1)) {
5059 device.charger_screen_enabled = 0;
5060 } else if (!strncmp(p, "1", 1)) {
5061 device.charger_screen_enabled = 1;
5062 }
5063 }
5064 }
5065
5066 /* update charger_screen_enabled value for getvar
5067 * command
5068 */
5069 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5070 device.charger_screen_enabled);
5071
5072 write_device_info(&device);
5073 fastboot_okay("");
5074}
5075
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305076void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
5077{
5078 dprintf(INFO, "Selecting display panel %s\n", arg);
5079 if (arg)
5080 strlcpy(device.display_panel, arg,
5081 sizeof(device.display_panel));
5082 write_device_info(&device);
5083 fastboot_okay("");
5084}
5085
Shashank Mittal162244e2011-08-08 19:01:25 -07005086void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
5087{
lijuang4ece1e72015-08-14 21:02:36 +08005088 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05305089}
5090
tracychui8fd71dc2020-06-11 10:17:13 +08005091/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start*/
5092#if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK)
5093void cmd_oem_unlock_skip_ui_check(const char *arg, void *data, unsigned sz)
5094{
5095 set_device_unlock_skip_ui_check(UNLOCK, TRUE);
5096}
5097#endif
5098/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */
5099
vijay kumarc65876c2015-04-24 13:29:16 +05305100void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
5101{
lijuang4ece1e72015-08-14 21:02:36 +08005102 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05305103 if(!is_allow_unlock) {
5104 fastboot_fail("oem unlock is not allowed");
5105 return;
5106 }
5107
lijuang4ece1e72015-08-14 21:02:36 +08005108 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05305109
lijuang4ece1e72015-08-14 21:02:36 +08005110 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05305111 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05305112 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05305113 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
5114 write_misc(0, &msg, sizeof(msg));
5115
5116 fastboot_okay("");
5117 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07005118 }
5119 fastboot_okay("");
5120}
5121
Channagoud Kadabiad259832015-05-29 11:14:17 -07005122static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
5123{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305124 int ret=1;
5125 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07005126
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305127 /*
5128 Authentication method not implemented.
5129
5130 OEM to implement, authentication system which on successful validataion,
5131 calls write_allow_oem_unlock() with is_allow_unlock.
5132 */
5133#if 0
5134 authentication_success = oem_specific_auth_mthd();
5135#endif
5136
5137 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07005138 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305139 is_allow_unlock = true;
5140 write_allow_oem_unlock(is_allow_unlock);
5141 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07005142 }
5143 return ret;
5144}
5145
Shashank Mittald3e54dd2014-08-28 15:24:02 -07005146void cmd_oem_lock(const char *arg, void *data, unsigned sz)
5147{
lijuang4ece1e72015-08-14 21:02:36 +08005148 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07005149}
5150
tracychui8fd71dc2020-06-11 10:17:13 +08005151/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start*/
5152#if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK)
5153void cmd_oem_lock_skip_ui_check(const char *arg, void *data, unsigned sz)
5154{
5155 set_device_unlock_skip_ui_check(UNLOCK, FALSE);
5156}
5157#endif
5158/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */
5159
Shashank Mittala0032282011-08-26 14:50:11 -07005160void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
5161{
lijuang511a2b52015-08-14 20:50:51 +08005162 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005163 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07005164 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005165 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
5166 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05305167#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305168 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05305169 {
5170 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
5171 (device.is_unlock_critical ? "true" : "false"));
5172 fastboot_info(response);
5173 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305174#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005175 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07005176 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305177 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
5178 fastboot_info(response);
tracychuid184b912020-06-05 17:31:38 +08005179/*[20200605][TracyChui] Implement get Serial Number start*/
5180#if defined(ENABLE_PRODINFO_ACCESS)
5181 snprintf(response, sizeof(response), "\tAdb enabled: %s", prod.is_adb_enabled ? "true" : "false");
5182 fastboot_info(response);
5183#endif
5184/*[20200605][TracyChui] Implement get Serial Number end*/
Shashank Mittala0032282011-08-26 14:50:11 -07005185 fastboot_okay("");
5186}
5187
lijuang511a2b52015-08-14 20:50:51 +08005188void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
5189{
5190 char response[MAX_RSP_SIZE];
5191 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
5192 fastboot_info(response);
5193 fastboot_okay("");
5194}
5195
5196void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
5197{
lijuang4ece1e72015-08-14 21:02:36 +08005198 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08005199}
5200
5201void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
5202{
lijuang4ece1e72015-08-14 21:02:36 +08005203 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08005204}
5205
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005206void cmd_preflash(const char *arg, void *data, unsigned sz)
5207{
5208 fastboot_okay("");
5209}
5210
Mao Flynn7b379f32015-04-20 00:28:30 +08005211static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305212
Mao Flynn7b379f32015-04-20 00:28:30 +08005213int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305214{
Mao Flynn7b379f32015-04-20 00:28:30 +08005215 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305216 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08005217 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305218 return -1;
5219 return 0;
5220}
5221
Mao Flynn7b379f32015-04-20 00:28:30 +08005222int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005223{
5224 struct ptentry *ptn;
5225 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08005226 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005227 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08005228
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305229 ptable = flash_get_ptable();
5230 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08005231 dprintf(CRITICAL, "ERROR: Partition table not found\n");
5232 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305233 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005234
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305235 ptn = ptable_find(ptable, "splash");
5236 if (ptn == NULL) {
5237 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005238 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305239 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005240 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305241 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005242 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305243 }
5244
Mao Flynn7b379f32015-04-20 00:28:30 +08005245 header = (struct logo_img_header *)logo_header;
5246 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305247 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005248 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305249 }
5250
5251 fb_display = fbcon_display();
5252 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05305253 if (header->type && (header->blocks != 0) &&
5254 (UINT_MAX >= header->blocks * 512) &&
5255 ((header->blocks * 512) <= (fb_display->width *
5256 fb_display->height * (fb_display->bpp / 8)))) {
5257 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08005258 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
5259
5260 /* if the logo is full-screen size, remove "fbcon_clear()" */
5261 if ((header->width != fb_display->width)
5262 || (header->height != fb_display->height))
5263 fbcon_clear();
5264
5265 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
5266 (uint32_t *)base,
5267 (header->blocks * 512))) {
5268 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5269 return -1;
5270 }
5271 fbcon_extract_to_screen(header, base);
5272 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005273 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005274
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07005275 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
5276 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005277 return -1;
5278 }
5279
Vineet Bajaj99291ed2014-09-09 12:29:46 +05305280 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305281 uint32_t fb_size = ROUNDUP(fb_display->width *
5282 fb_display->height *
5283 (fb_display->bpp / 8), 4096);
5284 uint32_t splash_size = ((((header->width * header->height *
5285 fb_display->bpp/8) + 511) >> 9) << 9);
5286
5287 if (splash_size > fb_size) {
5288 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5289 return -1;
5290 }
5291
Mao Flynn7b379f32015-04-20 00:28:30 +08005292 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
5293 (uint32_t *)base,
5294 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305295 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05305296 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005297 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005298 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305299 }
5300
Mao Flynn7b379f32015-04-20 00:28:30 +08005301 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305302}
5303
Mao Flynn7b379f32015-04-20 00:28:30 +08005304int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305305{
5306 int index = INVALID_PTN;
5307 unsigned long long ptn = 0;
5308 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08005309 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08005310 uint32_t blocksize, realsize, readsize;
5311 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305312
5313 index = partition_get_index("splash");
5314 if (index == 0) {
5315 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005316 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305317 }
5318
5319 ptn = partition_get_offset(index);
5320 if (ptn == 0) {
5321 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005322 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305323 }
5324
Mao Flynn1893a7f2015-06-03 12:03:36 +08005325 mmc_set_lun(partition_get_lun(index));
5326
5327 blocksize = mmc_get_device_blocksize();
5328 if (blocksize == 0) {
5329 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
5330 return -1;
5331 }
5332
5333 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07005334 if (!fb_display)
5335 {
5336 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
5337 return -1;
5338 }
5339
Mao Flynn1893a7f2015-06-03 12:03:36 +08005340 base = (uint8_t *) fb_display->base;
5341
jialongjhan1efba002020-05-22 18:25:56 +08005342 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 Start
5343 if (mmc_read(ptn , (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305344 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005345 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305346 }
jialongjhan1efba002020-05-22 18:25:56 +08005347 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 End
Mao Flynn1893a7f2015-06-03 12:03:36 +08005348 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08005349 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305350 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005351 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305352 }
5353
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305354 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05305355 if (header->type && (header->blocks != 0) &&
5356 (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
5357 ((header->blocks * 512) <= (fb_display->width *
5358 fb_display->height * (fb_display->bpp / 8)))) {
5359 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08005360 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08005361
Mao Flynn1893a7f2015-06-03 12:03:36 +08005362 realsize = header->blocks * 512;
5363 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
5364
5365 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08005366 if ((header->width != fb_display->width)
5367 || (header->height != fb_display->height))
5368 fbcon_clear();
5369
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305370 uint32_t fb_size = ROUNDUP(fb_display->width *
5371 fb_display->height *
5372 (fb_display->bpp / 8), 4096);
5373
5374 if (readsize > fb_size) {
5375 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5376 return -1;
5377 }
5378
jialongjhan1efba002020-05-22 18:25:56 +08005379 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 Start
5380 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08005381 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5382 return -1;
5383 }
jialongjhan1efba002020-05-22 18:25:56 +08005384 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 End
Mao Flynn7b379f32015-04-20 00:28:30 +08005385
Mao Flynn1893a7f2015-06-03 12:03:36 +08005386 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
5387 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305388
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07005389 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
5390 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08005391 return -1;
5392 }
5393
5394 realsize = header->width * header->height * fb_display->bpp / 8;
5395 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
5396
5397 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05305398 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08005399 fbcon_clear();
5400 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5401 return -1;
5402 }
5403 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05305404 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08005405 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
5406 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5407 return -1;
5408 }
5409 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
5410 }
5411 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305412 }
5413
Mao Flynn7b379f32015-04-20 00:28:30 +08005414 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305415}
5416
Mao Flynn7b379f32015-04-20 00:28:30 +08005417int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305418{
5419 if (target_is_emmc_boot()) {
5420 return splash_screen_mmc();
5421 } else {
5422 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005423 }
5424}
5425
Mayank Grover351a75e2017-05-30 20:06:08 +05305426void publish_getvar_multislot_vars()
5427{
5428 int i,count;
5429 static bool published = false;
5430 static char slot_count[MAX_RSP_SIZE];
5431 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
5432 static char active_slot_suffix[MAX_RSP_SIZE];
5433 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
5434 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
5435 const char *tmp;
5436 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305437 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05305438
5439 if (!published)
5440 {
5441 /* Update slot meta info */
5442 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
5443 partition_get_partition_count());
5444 for(i=0; i<count; i++)
5445 {
5446 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305447 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
5448 has_slot_pname[i]);
5449 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05305450 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
5451 }
5452
5453 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
5454 {
5455 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305456 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305457 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
5458 "slot-unbootable:%s", tmp);
5459 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
5460 "slot-active:%s", tmp);
5461 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
5462 "slot-success:%s", tmp);
5463 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
5464 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05305465 fastboot_publish(slot_info[i].slot_is_unbootable,
5466 slot_info[i].slot_is_unbootable_rsp);
5467 fastboot_publish(slot_info[i].slot_is_active,
5468 slot_info[i].slot_is_active_rsp);
5469 fastboot_publish(slot_info[i].slot_is_succesful,
5470 slot_info[i].slot_is_succesful_rsp);
5471 fastboot_publish(slot_info[i].slot_retry_count,
5472 slot_info[i].slot_retry_count_rsp);
5473 }
5474 fastboot_publish("current-slot", active_slot_suffix);
5475 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
5476 fastboot_publish("slot-count", slot_count);
5477 published = true;
5478 }
5479
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305480 active_slt = partition_find_active_slot();
5481 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305482 {
5483 tmp = SUFFIX_SLOT(active_slt);
5484 tmp++; // to remove "_" from slot_suffix.
5485 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
5486 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305487 else
5488 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
5489
Mayank Grover351a75e2017-05-30 20:06:08 +05305490 /* Update partition meta information */
5491 partition_fill_slot_meta(slot_info);
5492 return;
5493}
5494
lijuang4ece1e72015-08-14 21:02:36 +08005495void get_product_name(unsigned char *buf)
5496{
5497 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
5498 return;
5499}
5500
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305501#if PRODUCT_IOT
5502void get_bootloader_version_iot(unsigned char *buf)
5503{
5504 if (buf != NULL)
5505 {
5506 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
5507 strlcat(buf, "-", MAX_VERSION_LEN);
5508 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
5509 }
5510 return;
5511}
5512#endif
5513
lijuang4ece1e72015-08-14 21:02:36 +08005514void get_bootloader_version(unsigned char *buf)
5515{
jhchen7a504d12020-04-24 15:45:57 +08005516 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
lijuang4ece1e72015-08-14 21:02:36 +08005517 return;
5518}
5519
5520void get_baseband_version(unsigned char *buf)
5521{
jhchen7a504d12020-04-24 15:45:57 +08005522 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
lijuang4ece1e72015-08-14 21:02:36 +08005523 return;
5524}
5525
Monika Singh32a09f72018-03-14 13:08:29 +05305526bool is_device_locked_critical()
5527{
5528 return device.is_unlock_critical ? false:true;
5529}
5530
lijuang4ece1e72015-08-14 21:02:36 +08005531bool is_device_locked()
5532{
5533 return device.is_unlocked ? false:true;
5534}
5535
Monika Singh32a09f72018-03-14 13:08:29 +05305536bool is_verity_enforcing()
5537{
5538 return device.verity_mode ? true:false;
5539}
5540
Amol Jadi5edf3552013-07-23 14:15:34 -07005541/* register commands and variables for fastboot */
5542void aboot_fastboot_register_commands(void)
5543{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005544 int i;
lijuangf16461c2015-08-03 17:09:34 +08005545 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07005546
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005547 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08005548 /* By default the enabled list is empty. */
5549 {"", NULL},
5550 /* move commands enclosed within the below ifndef to here
5551 * if they need to be enabled in user build.
5552 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005553#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005554 /* Register the following commands only for non-user builds */
5555 {"flash:", cmd_flash},
5556 {"erase:", cmd_erase},
5557 {"boot", cmd_boot},
5558 {"continue", cmd_continue},
5559 {"reboot", cmd_reboot},
5560 {"reboot-bootloader", cmd_reboot_bootloader},
5561 {"oem unlock", cmd_oem_unlock},
5562 {"oem unlock-go", cmd_oem_unlock_go},
5563 {"oem lock", cmd_oem_lock},
tracychui8fd71dc2020-06-11 10:17:13 +08005564/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start */
5565#if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK)
5566 {"oem 8901_unlock", cmd_oem_unlock_skip_ui_check},
5567 {"oem 8901_lock", cmd_oem_lock_skip_ui_check},
5568#endif
5569/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */
lijuang511a2b52015-08-14 20:50:51 +08005570 {"flashing unlock", cmd_oem_unlock},
5571 {"flashing lock", cmd_oem_lock},
5572 {"flashing lock_critical", cmd_flashing_lock_critical},
5573 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5574 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5575 {"oem device-info", cmd_oem_devinfo},
5576 {"preflash", cmd_preflash},
5577 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5578 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005579 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005580 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305581 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305582#if DYNAMIC_PARTITION_SUPPORT
5583 {"reboot-fastboot",cmd_reboot_fastboot},
5584 {"reboot-recovery",cmd_reboot_recovery},
5585#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005586#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005587 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005588#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005589#endif
tracychuid184b912020-06-05 17:31:38 +08005590/*[20200605][TracyChui] Implement get Serial Number start*/
5591#if defined(ENABLE_PRODINFO_ACCESS)
5592 {"oem adb_enable", CmdOemEnableAdb},
5593#endif
5594/*[20200605][TracyChui] Implement get Serial Number end*/
lijuang511a2b52015-08-14 20:50:51 +08005595 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005596
5597 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5598 for (i = 1; i < fastboot_cmds_count; i++)
5599 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5600
Amol Jadi5edf3552013-07-23 14:15:34 -07005601 /* publish variables and their values */
5602 fastboot_publish("product", TARGET(BOARD));
5603 fastboot_publish("kernel", "lk");
5604 fastboot_publish("serialno", sn_buf);
5605
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305606 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5607 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5608 fastboot_publish("hw-revision", soc_version_str);
5609
Amol Jadi5edf3552013-07-23 14:15:34 -07005610 /*
5611 * partition info is supported only for emmc partitions
5612 * Calling this for NAND prints some error messages which
5613 * is harmless but misleading. Avoid calling this for NAND
5614 * devices.
5615 */
5616 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305617 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005618
Mayank Grover351a75e2017-05-30 20:06:08 +05305619 if (partition_multislot_is_supported())
5620 publish_getvar_multislot_vars();
5621
Amol Jadi5edf3552013-07-23 14:15:34 -07005622 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005623#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005624 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5625 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005626#else
5627 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5628 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5629#endif
5630
Amol Jadi5edf3552013-07-23 14:15:34 -07005631 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005632 /* Is the charger screen check enabled */
5633 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5634 device.charger_screen_enabled);
5635 fastboot_publish("charger-screen-enabled",
5636 (const char *) charger_screen_enabled);
tracychuid184b912020-06-05 17:31:38 +08005637/*[20200605][TracyChui] Implement get Serial Number start*/
5638#if defined(ENABLE_PRODINFO_ACCESS)
5639 read_prod_info(&prod);
5640 snprintf(AdbEnable, MAX_RSP_SIZE, "%d",
5641 prod.is_adb_enabled);
5642 fastboot_publish("adb-enabled",
5643 (const char *) AdbEnable);
5644#endif
5645/*[20200605][TracyChui] Implement get Serial Number end*/
lijuanga25d8bb2015-09-16 13:11:52 +08005646 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305647 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5648 device.display_panel);
5649 fastboot_publish("display-panel",
5650 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305651
5652 if (target_is_emmc_boot())
5653 {
5654 mmc_blocksize = mmc_get_device_blocksize();
5655 }
5656 else
5657 {
5658 mmc_blocksize = flash_block_size();
5659 }
5660 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5661 fastboot_publish("erase-block-size", (const char *) block_size_string);
5662 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305663#if PRODUCT_IOT
5664 get_bootloader_version_iot(&bootloader_version_string);
5665 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5666
5667 /* Version baseband is n/a for apq iot devices */
5668 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305669#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005670 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5671 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305672#endif
lijuangf16461c2015-08-03 17:09:34 +08005673 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305674 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005675 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5676 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5677 target_is_emmc_boot()? "eMMC":"UFS");
5678 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005679#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005680 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005681 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005682 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005683#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305684 if (target_dynamic_partition_supported())
5685 fastboot_publish("is-userspace", "no");
Amol Jadi5edf3552013-07-23 14:15:34 -07005686}
5687
Brian Swetland9c4c0752009-01-25 16:23:50 -08005688void aboot_init(const struct app_descriptor *app)
5689{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005690 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305691 int boot_err_type = 0;
5692 int boot_slot = INVALID;
jessicatseng67c4fd02020-05-26 13:17:56 +08005693//<2020/05/26-JessicaTseng, Add low battery icon
5694 int vbat = 0;
5695 char boot_vbat[MAX_RSP_SIZE];
5696//>2020/05/26-JessicaTseng
Chandan Uddarajubedca152010-06-02 23:05:15 -07005697
lijuang39831732016-01-08 17:49:02 +08005698 /* Initialise wdog to catch early lk crashes */
5699#if WDOG_SUPPORT
5700 msm_wdog_init();
5701#endif
5702
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005703 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005704 if (target_is_emmc_boot())
5705 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005706 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005707 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305708 mmc_blocksize = mmc_get_device_blocksize();
5709 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005710 }
5711 else
5712 {
5713 page_size = flash_page_size();
5714 page_mask = page_size - 1;
5715 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005716 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5717
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005718 read_device_info(&device);
tracychuid184b912020-06-05 17:31:38 +08005719/*[20200605][TracyChui] Implement get Serial Number start*/
5720#if defined(ENABLE_PRODINFO_ACCESS)
5721 read_prod_info(&prod);
5722#endif
5723/*[20200605][TracyChui] Implement get Serial Number end*/
vijay kumarc65876c2015-04-24 13:29:16 +05305724 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005725
Mayank Grover351a75e2017-05-30 20:06:08 +05305726 /* Detect multi-slot support */
5727 if (partition_multislot_is_supported())
5728 {
5729 boot_slot = partition_find_active_slot();
5730 if (boot_slot == INVALID)
5731 {
5732 boot_into_fastboot = true;
5733 dprintf(INFO, "Active Slot: (INVALID)\n");
5734 }
5735 else
5736 {
5737 /* Setting the state of system to boot active slot */
5738 partition_mark_active_slot(boot_slot);
5739 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5740 }
5741 }
5742
Greg Griscod6250552011-06-29 14:40:23 -07005743 target_serialno((unsigned char *) sn_buf);
tracychuid184b912020-06-05 17:31:38 +08005744/*[20200605][TracyChui] Implement get Serial Number start*/
5745#if defined(ENABLE_PRODINFO_ACCESS)
5746 dprintf(CRITICAL,"serial number: %s\n",sn_buf);
5747#else
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005748 dprintf(SPEW,"serial number: %s\n",sn_buf);
tracychuid184b912020-06-05 17:31:38 +08005749#endif
5750/*[20200605][TracyChui] Implement get Serial Number end*/
5751/*[20200605][TracyChui] Implement get Serial Number start */
5752#if defined(ENABLE_PRODINFO_ACCESS)
5753 read_prod_info(&prod);
5754 snprintf((char *)cust_sn_buf, PRODINFO_MAX_SSN_LEN + 1, "%s", prod.ssn);
5755 dprintf(CRITICAL,"customer serial number: %s\n", cust_sn_buf);
5756 snprintf((char *)factory_sn_buf, PRODINFO_MAX_ISN_LEN + 1, "%s", prod.isn);
5757 dprintf(CRITICAL,"factory serial number: %s\n", factory_sn_buf);
5758#endif
5759 /*[20200605][TracyChui] Implement get Serial Number end */
Dhaval Patel223ec952013-07-18 14:49:44 -07005760 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5761
Matthew Qindefd5562014-07-11 18:02:40 +08005762 /*
5763 * Check power off reason if user force reset,
5764 * if yes phone will do normal boot.
5765 */
5766 if (is_user_force_reset())
5767 goto normal_boot;
5768
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005769 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005770 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005771 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005772 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005773 reboot_device(EMERGENCY_DLOAD);
5774 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5775
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005776 boot_into_fastboot = true;
5777 }
5778 if (!boot_into_fastboot)
5779 {
5780 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5781 boot_into_recovery = 1;
5782 if (!boot_into_recovery &&
5783 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005784 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005785 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005786 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005787 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005788 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005789 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005790
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005791#if USE_PON_REBOOT_REG
5792 reboot_mode = check_hard_reboot_mode();
5793#else
Ajay Dudani77421292010-10-27 19:34:06 -07005794 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005795#endif
5796 if (reboot_mode == RECOVERY_MODE)
5797 {
Ajay Dudani77421292010-10-27 19:34:06 -07005798 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005799 }
5800 else if(reboot_mode == FASTBOOT_MODE)
5801 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005802 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005803 }
5804 else if(reboot_mode == ALARM_BOOT)
5805 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005806 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005807 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305808#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305809 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005810 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305811 if (reboot_mode == DM_VERITY_ENFORCING)
5812 {
5813 device.verity_mode = 1;
5814 write_device_info(&device);
5815 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005816#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305817 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005818#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305819 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005820#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305821 {
5822 device.verity_mode = 0;
5823 write_device_info(&device);
5824 }
5825 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5826 {
5827 if(send_delete_keys_to_tz())
5828 ASSERT(0);
5829 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005830 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305831#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005832
jialongjhan1efba002020-05-22 18:25:56 +08005833//[Arima][8901][JialongJhan][20200522]modify Display splash screen if enabled Start
5834 /* Display splash screen if enabled */
5835#if DISPLAY_SPLASH_SCREEN
5836#if NO_ALARM_DISPLAY
5837 if (!check_alarm_boot()) {
5838#endif
5839 dprintf(SPEW, "Display Init: Start\n");
5840#if DISPLAY_HDMI_PRIMARY
5841 if (!strlen(device.display_panel))
5842 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5843 sizeof(device.display_panel));
5844#endif
5845#if ENABLE_WBC
5846 /* Wait if the display shutdown is in progress */
5847 while(pm_app_display_shutdown_in_prgs());
5848 if (!pm_appsbl_display_init_done())
5849 target_display_init(device.display_panel);
5850 else
5851 display_image_on_screen();
5852#else
5853 target_display_init(device.display_panel);
5854#endif
5855 dprintf(SPEW, "Display Init: Done\n");
5856#if NO_ALARM_DISPLAY
5857 }
5858#endif
5859#endif
5860
5861//[Arima][8901][JialongJhan][20200522]modify Display splash screen if enabled End
5862
Matthew Qindefd5562014-07-11 18:02:40 +08005863normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005864 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005865 {
jessicatseng67c4fd02020-05-26 13:17:56 +08005866//<2020/05/26-JessicaTseng, Add low battery icon
5867 if(!target_pause_for_battery_charge())
5868 {
5869 vbat = target_get_battery_voltage();
5870 snprintf(boot_vbat, MAX_RSP_SIZE, "%d", vbat);
5871 dprintf(CRITICAL,"battery_voltage: %s\n", boot_vbat);
5872 if(vbat < 3500000)
5873 {
5874 display_lowbattery_image_on_screen();
5875 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 Start
5876 msm_display_flush();
5877 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 End
5878 dprintf(CRITICAL,"Low battery, cannot boot up...\n");
5879 mdelay(3000);
5880 shutdown_device();
5881 }
5882 }
5883//>2020/05/26-JessicaTseng
5884
Pavel Nedev5d91d412013-04-29 11:34:24 +03005885 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005886 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005887 if(emmc_recovery_init())
5888 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5889 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005890 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005891 if((device.is_unlocked) || (device.is_tampered))
5892 {
5893 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305894 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005895 #endif
5896 #if USE_PCOM_SECBOOT
5897 set_tamper_flag(device.is_tampered);
5898 #endif
5899 }
Shashank Mittala0032282011-08-26 14:50:11 -07005900 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005901
Mayank Grover351a75e2017-05-30 20:06:08 +05305902retry_boot:
5903 /* Trying to boot active partition */
5904 if (partition_multislot_is_supported())
5905 {
5906 boot_slot = partition_find_boot_slot();
5907 partition_mark_active_slot(boot_slot);
5908 if (boot_slot == INVALID)
5909 goto fastboot;
5910 }
5911
5912 boot_err_type = boot_linux_from_mmc();
5913 switch (boot_err_type)
5914 {
5915 case ERR_INVALID_PAGE_SIZE:
5916 case ERR_DT_PARSE:
5917 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305918 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305919 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305920 {
5921 /*
5922 * Deactivate current slot, as it failed to
5923 * boot, and retry next slot.
5924 */
5925 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305926 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305927 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305928 else
5929 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305930 default:
5931 break;
5932 /* going to fastboot menu */
5933 }
Shashank Mittala0032282011-08-26 14:50:11 -07005934 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005935 else
5936 {
5937 recovery_init();
5938 #if USE_PCOM_SECBOOT
5939 if((device.is_unlocked) || (device.is_tampered))
5940 set_tamper_flag(device.is_tampered);
5941 #endif
5942 boot_linux_from_flash();
5943 }
5944 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5945 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005946 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005947
Mayank Grover351a75e2017-05-30 20:06:08 +05305948fastboot:
jialongjhan1efba002020-05-22 18:25:56 +08005949
5950 //[Arima][8901][JialongJhan]Show Fastboot logo 1 second 20190627 Start
5951 mdelay(1000);
5952 //[Arima][8901][JialongJhan]Show Fastboot logo 1 second 20190627 End
5953
Amol Jadi5edf3552013-07-23 14:15:34 -07005954 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005955
Amol Jadi5edf3552013-07-23 14:15:34 -07005956 /* register aboot specific fastboot commands */
5957 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005958
Amol Jadi5edf3552013-07-23 14:15:34 -07005959 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005960 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005961
5962 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305963#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005964 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305965#else
5966 /* Add salt buffer offset at start of image address to copy VB salt */
5967 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5968 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5969#endif
lijuang4ece1e72015-08-14 21:02:36 +08005970#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005971 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005972#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005973}
5974
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005975uint32_t get_page_size()
5976{
5977 return page_size;
5978}
5979
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005980/*
5981 * Calculated and save hash (SHA256) for non-signed boot image.
5982 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005983 * @param image_addr - Boot image address
5984 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005985 *
5986 * @return int - 0 on success, negative value on failure.
5987 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005988static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005989{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005990 unsigned int digest[8];
5991#if IMAGE_VERIF_ALGO_SHA1
5992 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5993#else
5994 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5995#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005996
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005997 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005998 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005999
6000 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07006001 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03006002
6003 return 0;
6004}
6005
Mayank Grover351a75e2017-05-30 20:06:08 +05306006
Brian Swetland9c4c0752009-01-25 16:23:50 -08006007APP_START(aboot)
6008 .init = aboot_init,
6009APP_END